summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 911e9c273f978d49486c176dc37c71c01ed64c6b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. my %backlinks;
  8. my $backlinks_calculated=0;
  9. sub calculate_backlinks () { #{{{
  10. return if $backlinks_calculated;
  11. %backlinks=();
  12. foreach my $page (keys %links) {
  13. foreach my $link (@{$links{$page}}) {
  14. my $bestlink=bestlink($page, $link);
  15. if (length $bestlink && $bestlink ne $page) {
  16. $backlinks{$bestlink}{$page}=1;
  17. }
  18. }
  19. }
  20. $backlinks_calculated=1;
  21. } #}}}
  22. sub backlinks ($) { #{{{
  23. my $page=shift;
  24. calculate_backlinks();
  25. my @links;
  26. foreach my $p (keys %{$backlinks{$page}}) {
  27. my $href=urlto($p, $page);
  28. # Trim common dir prefixes from both pages.
  29. my $p_trimmed=$p;
  30. my $page_trimmed=$page;
  31. my $dir;
  32. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  33. defined $dir &&
  34. $p_trimmed=~s/^\Q$dir\E// &&
  35. $page_trimmed=~s/^\Q$dir\E//;
  36. push @links, { url => $href, page => pagetitle($p_trimmed) };
  37. }
  38. return @links;
  39. } #}}}
  40. sub parentlinks ($) { #{{{
  41. my $page=shift;
  42. my @ret;
  43. my $pagelink="";
  44. my $path="";
  45. my $title=$config{wikiname};
  46. return if $page eq 'index'; # toplevel
  47. foreach my $dir (split("/", $page)) {
  48. push @ret, { url => urlto($path, $page), page => $title };
  49. $path.="/".$dir;
  50. $title=pagetitle($dir);
  51. }
  52. return @ret;
  53. } #}}}
  54. sub genpage ($$$) { #{{{
  55. my $page=shift;
  56. my $content=shift;
  57. my $mtime=shift;
  58. my $template=template("page.tmpl", blind_cache => 1);
  59. my $actions=0;
  60. if (length $config{cgiurl}) {
  61. $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
  62. $template->param(prefsurl => cgiurl(do => "prefs"));
  63. if ($config{rcs}) {
  64. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  65. }
  66. $actions++;
  67. }
  68. if (length $config{historyurl}) {
  69. my $u=$config{historyurl};
  70. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  71. $template->param(historyurl => $u);
  72. $actions++;
  73. }
  74. if ($config{discussion}) {
  75. my $discussionlink=gettext("discussion");
  76. if ($page !~ /.*\/\Q$discussionlink\E$/ &&
  77. (length $config{cgiurl} ||
  78. exists $links{$page."/".$discussionlink})) {
  79. $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
  80. $actions++;
  81. }
  82. }
  83. if ($actions) {
  84. $template->param(have_actions => 1);
  85. }
  86. my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
  87. my ($backlinks, $more_backlinks);
  88. if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
  89. $backlinks=\@backlinks;
  90. $more_backlinks=[];
  91. }
  92. else {
  93. $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
  94. $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
  95. }
  96. $template->param(
  97. title => $page eq 'index'
  98. ? $config{wikiname}
  99. : pagetitle(basename($page)),
  100. wikiname => $config{wikiname},
  101. parentlinks => [parentlinks($page)],
  102. content => $content,
  103. backlinks => $backlinks,
  104. more_backlinks => $more_backlinks,
  105. mtime => displaytime($mtime),
  106. baseurl => baseurl($page),
  107. );
  108. run_hooks(pagetemplate => sub {
  109. shift->(page => $page, destpage => $page, template => $template);
  110. });
  111. $content=$template->output;
  112. run_hooks(format => sub {
  113. $content=shift->(
  114. page => $page,
  115. content => $content,
  116. );
  117. });
  118. return $content;
  119. } #}}}
  120. sub mtime ($) { #{{{
  121. my $file=shift;
  122. return (stat($file))[9];
  123. } #}}}
  124. sub scan ($) { #{{{
  125. my $file=shift;
  126. my $type=pagetype($file);
  127. if (defined $type) {
  128. my $srcfile=srcfile($file);
  129. my $content=readfile($srcfile);
  130. my $page=pagename($file);
  131. will_render($page, htmlpage($page), 1);
  132. # Always needs to be done, since filters might add links
  133. # to the content.
  134. $content=filter($page, $page, $content);
  135. my @links;
  136. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  137. push @links, linkpage($2);
  138. }
  139. if ($config{discussion}) {
  140. # Discussion links are a special case since they're
  141. # not in the text of the page, but on its template.
  142. push @links, $page."/".gettext("discussion");
  143. }
  144. $links{$page}=\@links;
  145. # Preprocess in scan-only mode.
  146. preprocess($page, $page, $content, 1);
  147. }
  148. else {
  149. will_render($file, $file, 1);
  150. }
  151. } #}}}
  152. sub render ($) { #{{{
  153. my $file=shift;
  154. my $type=pagetype($file);
  155. my $srcfile=srcfile($file);
  156. if (defined $type) {
  157. my $page=pagename($file);
  158. delete $depends{$page};
  159. will_render($page, htmlpage($page), 1);
  160. my $content=htmlize($page, $type,
  161. linkify($page, $page,
  162. preprocess($page, $page,
  163. filter($page, $page,
  164. readfile($srcfile)))));
  165. writefile(htmlpage($page), $config{destdir},
  166. genpage($page, $content, mtime($srcfile)));
  167. }
  168. else {
  169. my $srcfd=readfile($srcfile, 1, 1);
  170. delete $depends{$file};
  171. will_render($file, $file, 1);
  172. writefile($file, $config{destdir}, undef, 1, sub {
  173. my $destfd=shift;
  174. my $cleanup=shift;
  175. my $blksize = 16384;
  176. my ($len, $buf, $written);
  177. while ($len = sysread $srcfd, $buf, $blksize) {
  178. if (! defined $len) {
  179. next if $! =~ /^Interrupted/;
  180. error("failed to read $srcfile: $!", $cleanup);
  181. }
  182. my $offset = 0;
  183. while ($len) {
  184. defined($written = syswrite $destfd, $buf, $len, $offset)
  185. or error("failed to write $file: $!", $cleanup);
  186. $len -= $written;
  187. $offset += $written;
  188. }
  189. }
  190. });
  191. }
  192. } #}}}
  193. sub prune ($) { #{{{
  194. my $file=shift;
  195. unlink($file);
  196. my $dir=dirname($file);
  197. while (rmdir($dir)) {
  198. $dir=dirname($dir);
  199. }
  200. } #}}}
  201. sub refresh () { #{{{
  202. # find existing pages
  203. my %exists;
  204. my @files;
  205. eval q{use File::Find};
  206. error($@) if $@;
  207. find({
  208. no_chdir => 1,
  209. wanted => sub {
  210. $_=decode_utf8($_);
  211. if (file_pruned($_, $config{srcdir})) {
  212. $File::Find::prune=1;
  213. }
  214. elsif (! -d $_ && ! -l $_) {
  215. my ($f)=/$config{wiki_file_regexp}/; # untaint
  216. if (! defined $f) {
  217. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  218. }
  219. else {
  220. $f=~s/^\Q$config{srcdir}\E\/?//;
  221. push @files, $f;
  222. $exists{pagename($f)}=1;
  223. }
  224. }
  225. },
  226. }, $config{srcdir});
  227. find({
  228. no_chdir => 1,
  229. wanted => sub {
  230. $_=decode_utf8($_);
  231. if (file_pruned($_, $config{underlaydir})) {
  232. $File::Find::prune=1;
  233. }
  234. elsif (! -d $_ && ! -l $_) {
  235. my ($f)=/$config{wiki_file_regexp}/; # untaint
  236. if (! defined $f) {
  237. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  238. }
  239. else {
  240. # Don't add pages that are in the
  241. # srcdir.
  242. $f=~s/^\Q$config{underlaydir}\E\/?//;
  243. if (! -e "$config{srcdir}/$f" &&
  244. ! -l "$config{srcdir}/$f") {
  245. my $page=pagename($f);
  246. if (! $exists{$page}) {
  247. push @files, $f;
  248. $exists{$page}=1;
  249. }
  250. }
  251. }
  252. }
  253. },
  254. }, $config{underlaydir});
  255. my %rendered;
  256. # check for added or removed pages
  257. my @add;
  258. foreach my $file (@files) {
  259. my $page=pagename($file);
  260. $pagesources{$page}=$file;
  261. if (! $pagemtime{$page}) {
  262. push @add, $file;
  263. $pagecase{lc $page}=$page;
  264. if ($config{getctime} && -e "$config{srcdir}/$file") {
  265. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  266. }
  267. elsif (! exists $pagectime{$page}) {
  268. $pagectime{$page}=mtime(srcfile($file));
  269. }
  270. }
  271. }
  272. my @del;
  273. foreach my $page (keys %pagemtime) {
  274. if (! $exists{$page}) {
  275. debug(sprintf(gettext("removing old page %s"), $page));
  276. push @del, $pagesources{$page};
  277. $links{$page}=[];
  278. $renderedfiles{$page}=[];
  279. $pagemtime{$page}=0;
  280. prune($config{destdir}."/".$_)
  281. foreach @{$oldrenderedfiles{$page}};
  282. delete $pagesources{$page};
  283. foreach (keys %destsources) {
  284. if ($destsources{$_} eq $page) {
  285. delete $destsources{$_};
  286. }
  287. }
  288. }
  289. }
  290. # find changed and new files
  291. my @needsbuild;
  292. foreach my $file (@files) {
  293. my $page=pagename($file);
  294. my $mtime=mtime(srcfile($file));
  295. if (! exists $pagemtime{$page} ||
  296. $mtime > $pagemtime{$page} ||
  297. $forcerebuild{$page}) {
  298. $pagemtime{$page}=$mtime;
  299. push @needsbuild, $file;
  300. }
  301. }
  302. run_hooks(needsbuild => sub { shift->(\@needsbuild) });
  303. # scan and rendder files
  304. foreach my $file (@needsbuild) {
  305. debug(sprintf(gettext("scanning %s"), $file));
  306. scan($file);
  307. }
  308. calculate_backlinks();
  309. foreach my $file (@needsbuild) {
  310. debug(sprintf(gettext("rendering %s"), $file));
  311. render($file);
  312. $rendered{$file}=1;
  313. }
  314. # rebuild pages that link to added or removed pages
  315. if (@add || @del) {
  316. foreach my $f (@add, @del) {
  317. my $p=pagename($f);
  318. foreach my $page (keys %{$backlinks{$p}}) {
  319. my $file=$pagesources{$page};
  320. next if $rendered{$file};
  321. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  322. render($file);
  323. $rendered{$file}=1;
  324. }
  325. }
  326. }
  327. if (%rendered || @del) {
  328. # rebuild dependant pages
  329. foreach my $f (@files) {
  330. next if $rendered{$f};
  331. my $p=pagename($f);
  332. if (exists $depends{$p}) {
  333. foreach my $file (keys %rendered, @del) {
  334. next if $f eq $file;
  335. my $page=pagename($file);
  336. if (pagespec_match($page, $depends{$p}, location => $p)) {
  337. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  338. render($f);
  339. $rendered{$f}=1;
  340. last;
  341. }
  342. }
  343. }
  344. }
  345. # handle backlinks; if a page has added/removed links,
  346. # update the pages it links to
  347. my %linkchanged;
  348. foreach my $file (keys %rendered, @del) {
  349. my $page=pagename($file);
  350. if (exists $links{$page}) {
  351. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  352. if (length $link &&
  353. (! exists $oldlinks{$page} ||
  354. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  355. $linkchanged{$link}=1;
  356. }
  357. }
  358. }
  359. if (exists $oldlinks{$page}) {
  360. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  361. if (length $link &&
  362. (! exists $links{$page} ||
  363. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  364. $linkchanged{$link}=1;
  365. }
  366. }
  367. }
  368. }
  369. foreach my $link (keys %linkchanged) {
  370. my $linkfile=$pagesources{$link};
  371. if (defined $linkfile) {
  372. next if $rendered{$linkfile};
  373. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  374. render($linkfile);
  375. $rendered{$linkfile}=1;
  376. }
  377. }
  378. }
  379. # remove no longer rendered files
  380. foreach my $src (keys %rendered) {
  381. my $page=pagename($src);
  382. foreach my $file (@{$oldrenderedfiles{$page}}) {
  383. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  384. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  385. prune($config{destdir}."/".$file);
  386. }
  387. }
  388. }
  389. if (@del) {
  390. run_hooks(delete => sub { shift->(@del) });
  391. }
  392. if (%rendered) {
  393. run_hooks(change => sub { shift->(keys %rendered) });
  394. }
  395. } #}}}
  396. sub commandline_render () { #{{{
  397. loadplugins();
  398. checkconfig();
  399. lockwiki();
  400. loadindex();
  401. unlockwiki();
  402. my $srcfile=possibly_foolish_untaint($config{render});
  403. my $file=$srcfile;
  404. $file=~s/\Q$config{srcdir}\E\/?//;
  405. my $type=pagetype($file);
  406. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  407. my $content=readfile($srcfile);
  408. my $page=pagename($file);
  409. $pagesources{$page}=$file;
  410. $content=filter($page, $page, $content);
  411. $content=preprocess($page, $page, $content);
  412. $content=linkify($page, $page, $content);
  413. $content=htmlize($page, $type, $content);
  414. print genpage($page, $content, mtime($srcfile));
  415. exit 0;
  416. } #}}}
  417. 1