summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: e77379a65e528114d49381c9d8c2e1286bc1e59e (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. # scan changed and new files
  280. my @changed;
  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. debug(sprintf(gettext("scanning %s"), $file));
  288. $pagemtime{$page}=$mtime;
  289. push @changed, $file;
  290. scan($file);
  291. }
  292. }
  293. calculate_backlinks();
  294. # render changed and new pages
  295. foreach my $file (@changed) {
  296. debug(sprintf(gettext("rendering %s"), $file));
  297. render($file);
  298. $rendered{$file}=1;
  299. }
  300. # rebuild pages that link to added or removed pages
  301. if (@add || @del) {
  302. foreach my $f (@add, @del) {
  303. my $p=pagename($f);
  304. foreach my $page (keys %{$backlinks{$p}}) {
  305. my $file=$pagesources{$page};
  306. next if $rendered{$file};
  307. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  308. render($file);
  309. $rendered{$file}=1;
  310. }
  311. }
  312. }
  313. if (%rendered || @del) {
  314. # rebuild dependant pages
  315. foreach my $f (@files) {
  316. next if $rendered{$f};
  317. my $p=pagename($f);
  318. if (exists $depends{$p}) {
  319. foreach my $file (keys %rendered, @del) {
  320. next if $f eq $file;
  321. my $page=pagename($file);
  322. if (pagespec_match($page, $depends{$p}, $p)) {
  323. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  324. render($f);
  325. $rendered{$f}=1;
  326. last;
  327. }
  328. }
  329. }
  330. }
  331. # handle backlinks; if a page has added/removed links,
  332. # update the pages it links to
  333. my %linkchanged;
  334. foreach my $file (keys %rendered, @del) {
  335. my $page=pagename($file);
  336. if (exists $links{$page}) {
  337. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  338. if (length $link &&
  339. (! exists $oldlinks{$page} ||
  340. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  341. $linkchanged{$link}=1;
  342. }
  343. }
  344. }
  345. if (exists $oldlinks{$page}) {
  346. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  347. if (length $link &&
  348. (! exists $links{$page} ||
  349. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  350. $linkchanged{$link}=1;
  351. }
  352. }
  353. }
  354. }
  355. foreach my $link (keys %linkchanged) {
  356. my $linkfile=$pagesources{$link};
  357. if (defined $linkfile) {
  358. next if $rendered{$linkfile};
  359. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  360. render($linkfile);
  361. $rendered{$linkfile}=1;
  362. }
  363. }
  364. }
  365. # remove no longer rendered files
  366. foreach my $src (keys %rendered) {
  367. my $page=pagename($src);
  368. foreach my $file (@{$oldrenderedfiles{$page}}) {
  369. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  370. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  371. prune($config{destdir}."/".$file);
  372. }
  373. }
  374. }
  375. if (@del) {
  376. run_hooks(delete => sub { shift->(@del) });
  377. }
  378. if (%rendered) {
  379. run_hooks(change => sub { shift->(keys %rendered) });
  380. }
  381. } #}}}
  382. sub commandline_render () { #{{{
  383. loadplugins();
  384. checkconfig();
  385. lockwiki();
  386. loadindex();
  387. unlockwiki();
  388. my $srcfile=possibly_foolish_untaint($config{render});
  389. my $file=$srcfile;
  390. $file=~s/\Q$config{srcdir}\E\/?//;
  391. my $type=pagetype($file);
  392. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  393. my $content=readfile($srcfile);
  394. my $page=pagename($file);
  395. $pagesources{$page}=$file;
  396. $content=filter($page, $content);
  397. $content=preprocess($page, $page, $content);
  398. $content=linkify($page, $page, $content);
  399. $content=htmlize($page, $type, $content);
  400. print genpage($page, $content, mtime($srcfile));
  401. exit 0;
  402. } #}}}
  403. 1