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