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