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