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