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