summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: dcd107b815172d86031d7cce3fc15a9d16544099 (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 $content=readfile($srcfile, 1);
  166. delete $depends{$file};
  167. will_render($file, $file, 1);
  168. writefile($file, $config{destdir}, $content, 1);
  169. $oldpagemtime{$file}=time;
  170. }
  171. } #}}}
  172. sub prune ($) { #{{{
  173. my $file=shift;
  174. unlink($file);
  175. my $dir=dirname($file);
  176. while (rmdir($dir)) {
  177. $dir=dirname($dir);
  178. }
  179. } #}}}
  180. sub refresh () { #{{{
  181. # find existing pages
  182. my %exists;
  183. my @files;
  184. eval q{use File::Find};
  185. error($@) if $@;
  186. find({
  187. no_chdir => 1,
  188. wanted => sub {
  189. $_=decode_utf8($_);
  190. if (file_pruned($_, $config{srcdir})) {
  191. $File::Find::prune=1;
  192. }
  193. elsif (! -d $_ && ! -l $_) {
  194. my ($f)=/$config{wiki_file_regexp}/; # untaint
  195. if (! defined $f) {
  196. warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
  197. }
  198. else {
  199. $f=~s/^\Q$config{srcdir}\E\/?//;
  200. push @files, $f;
  201. $exists{pagename($f)}=1;
  202. }
  203. }
  204. },
  205. }, $config{srcdir});
  206. find({
  207. no_chdir => 1,
  208. wanted => sub {
  209. $_=decode_utf8($_);
  210. if (file_pruned($_, $config{underlaydir})) {
  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. # Don't add pages that are in the
  220. # srcdir.
  221. $f=~s/^\Q$config{underlaydir}\E\/?//;
  222. if (! -e "$config{srcdir}/$f" &&
  223. ! -l "$config{srcdir}/$f") {
  224. my $page=pagename($f);
  225. if (! $exists{$page}) {
  226. push @files, $f;
  227. $exists{$page}=1;
  228. }
  229. }
  230. }
  231. }
  232. },
  233. }, $config{underlaydir});
  234. my %rendered;
  235. # check for added or removed pages
  236. my @add;
  237. foreach my $file (@files) {
  238. my $page=pagename($file);
  239. $pagesources{$page}=$file;
  240. if (! $oldpagemtime{$page}) {
  241. push @add, $file;
  242. $pagecase{lc $page}=$page;
  243. if ($config{getctime} && -e "$config{srcdir}/$file") {
  244. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  245. }
  246. elsif (! exists $pagectime{$page}) {
  247. $pagectime{$page}=mtime(srcfile($file));
  248. }
  249. }
  250. }
  251. my @del;
  252. foreach my $page (keys %oldpagemtime) {
  253. if (! $exists{$page}) {
  254. debug(sprintf(gettext("removing old page %s"), $page));
  255. push @del, $pagesources{$page};
  256. $links{$page}=[];
  257. $renderedfiles{$page}=[];
  258. $oldpagemtime{$page}=0;
  259. prune($config{destdir}."/".$_)
  260. foreach @{$oldrenderedfiles{$page}};
  261. delete $pagesources{$page};
  262. }
  263. }
  264. # scan changed and new files
  265. my @changed;
  266. foreach my $file (@files) {
  267. my $page=pagename($file);
  268. if (! exists $oldpagemtime{$page} ||
  269. mtime(srcfile($file)) > $oldpagemtime{$page} ||
  270. $forcerebuild{$page}) {
  271. debug(sprintf(gettext("scanning %s"), $file));
  272. push @changed, $file;
  273. scan($file);
  274. }
  275. }
  276. calculate_backlinks();
  277. # render changed and new pages
  278. foreach my $file (@changed) {
  279. debug(sprintf(gettext("rendering %s"), $file));
  280. render($file);
  281. $rendered{$file}=1;
  282. }
  283. # rebuild pages that link to added or removed pages
  284. if (@add || @del) {
  285. foreach my $f (@add, @del) {
  286. my $p=pagename($f);
  287. foreach my $page (keys %{$backlinks{$p}}) {
  288. my $file=$pagesources{$page};
  289. next if $rendered{$file};
  290. debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
  291. render($file);
  292. $rendered{$file}=1;
  293. }
  294. }
  295. }
  296. if (%rendered || @del) {
  297. # rebuild dependant pages
  298. foreach my $f (@files) {
  299. next if $rendered{$f};
  300. my $p=pagename($f);
  301. if (exists $depends{$p}) {
  302. foreach my $file (keys %rendered, @del) {
  303. next if $f eq $file;
  304. my $page=pagename($file);
  305. if (pagespec_match($page, $depends{$p})) {
  306. debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
  307. render($f);
  308. $rendered{$f}=1;
  309. last;
  310. }
  311. }
  312. }
  313. }
  314. # handle backlinks; if a page has added/removed links,
  315. # update the pages it links to
  316. my %linkchanged;
  317. foreach my $file (keys %rendered, @del) {
  318. my $page=pagename($file);
  319. if (exists $links{$page}) {
  320. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  321. if (length $link &&
  322. (! exists $oldlinks{$page} ||
  323. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  324. $linkchanged{$link}=1;
  325. }
  326. }
  327. }
  328. if (exists $oldlinks{$page}) {
  329. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  330. if (length $link &&
  331. (! exists $links{$page} ||
  332. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  333. $linkchanged{$link}=1;
  334. }
  335. }
  336. }
  337. }
  338. foreach my $link (keys %linkchanged) {
  339. my $linkfile=$pagesources{$link};
  340. if (defined $linkfile) {
  341. next if $rendered{$linkfile};
  342. debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
  343. render($linkfile);
  344. $rendered{$linkfile}=1;
  345. }
  346. }
  347. }
  348. # remove no longer rendered files
  349. foreach my $src (keys %rendered) {
  350. my $page=pagename($src);
  351. foreach my $file (@{$oldrenderedfiles{$page}}) {
  352. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  353. debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
  354. prune($config{destdir}."/".$file);
  355. }
  356. }
  357. }
  358. if (@del) {
  359. run_hooks(delete => sub { shift->(@del) });
  360. }
  361. if (%rendered) {
  362. run_hooks(change => sub { shift->(keys %rendered) });
  363. }
  364. } #}}}
  365. sub commandline_render () { #{{{
  366. loadplugins();
  367. checkconfig();
  368. lockwiki();
  369. loadindex();
  370. unlockwiki();
  371. my $srcfile=possibly_foolish_untaint($config{render});
  372. my $file=$srcfile;
  373. $file=~s/\Q$config{srcdir}\E\/?//;
  374. my $type=pagetype($file);
  375. die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
  376. my $content=readfile($srcfile);
  377. my $page=pagename($file);
  378. $pagesources{$page}=$file;
  379. $content=filter($page, $content);
  380. $content=preprocess($page, $page, $content);
  381. $content=linkify($page, $page, $content);
  382. $content=htmlize($page, $type, $content);
  383. print genpage($page, $content, mtime($srcfile));
  384. exit 0;
  385. } #}}}
  386. 1