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