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