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