summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: deec539ae4b43fed8b3d4161baee55604dfdcc7b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. sub backlinks ($) { #{{{
  8. my $page=shift;
  9. my @links;
  10. foreach my $p (keys %links) {
  11. next if bestlink($page, $p) eq $page;
  12. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  13. my $href=abs2rel(htmlpage($p), dirname($page));
  14. # Trim common dir prefixes from both pages.
  15. my $p_trimmed=$p;
  16. my $page_trimmed=$page;
  17. my $dir;
  18. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  19. defined $dir &&
  20. $p_trimmed=~s/^\Q$dir\E// &&
  21. $page_trimmed=~s/^\Q$dir\E//;
  22. push @links, { url => $href, page => pagetitle($p_trimmed) };
  23. }
  24. }
  25. return sort { $a->{page} cmp $b->{page} } @links;
  26. } #}}}
  27. sub parentlinks ($) { #{{{
  28. my $page=shift;
  29. my @ret;
  30. my $pagelink="";
  31. my $path="";
  32. my $skip=1;
  33. return if $page eq 'index'; # toplevel
  34. foreach my $dir (reverse split("/", $page)) {
  35. if (! $skip) {
  36. $path.="../";
  37. unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
  38. }
  39. else {
  40. $skip=0;
  41. }
  42. }
  43. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  44. return @ret;
  45. } #}}}
  46. sub genpage ($$$) { #{{{
  47. my $page=shift;
  48. my $content=shift;
  49. my $mtime=shift;
  50. my $template=template("page.tmpl", blind_cache => 1);
  51. my $actions=0;
  52. if (length $config{cgiurl}) {
  53. $template->param(editurl => cgiurl(do => "edit", page => $page));
  54. $template->param(prefsurl => cgiurl(do => "prefs"));
  55. if ($config{rcs}) {
  56. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  57. }
  58. $actions++;
  59. }
  60. if (length $config{historyurl}) {
  61. my $u=$config{historyurl};
  62. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  63. $template->param(historyurl => $u);
  64. $actions++;
  65. }
  66. if ($config{discussion}) {
  67. $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
  68. $actions++;
  69. }
  70. if ($actions) {
  71. $template->param(have_actions => 1);
  72. }
  73. $template->param(
  74. title => $page eq 'index'
  75. ? $config{wikiname}
  76. : pagetitle(basename($page)),
  77. wikiname => $config{wikiname},
  78. parentlinks => [parentlinks($page)],
  79. content => $content,
  80. backlinks => [backlinks($page)],
  81. mtime => displaytime($mtime),
  82. baseurl => baseurl($page),
  83. );
  84. run_hooks(pagetemplate => sub {
  85. shift->(page => $page, destpage => $page, template => $template);
  86. });
  87. $content=$template->output;
  88. run_hooks(format => sub {
  89. $content=shift->(
  90. page => $page,
  91. content => $content,
  92. );
  93. });
  94. return $content;
  95. } #}}}
  96. sub mtime ($) { #{{{
  97. my $file=shift;
  98. return (stat($file))[9];
  99. } #}}}
  100. sub findlinks ($$) { #{{{
  101. my $page=shift;
  102. my $content=shift;
  103. my @links;
  104. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  105. push @links, titlepage($2);
  106. }
  107. if ($config{discussion}) {
  108. # Discussion links are a special case since they're not in the
  109. # text of the page, but on its template.
  110. return @links, "$page/discussion";
  111. }
  112. else {
  113. return @links;
  114. }
  115. } #}}}
  116. sub render ($) { #{{{
  117. my $file=shift;
  118. my $type=pagetype($file);
  119. my $srcfile=srcfile($file);
  120. if (defined $type) {
  121. my $content=readfile($srcfile);
  122. my $page=pagename($file);
  123. delete $depends{$page};
  124. will_render($page, htmlpage($page), 1);
  125. $content=filter($page, $content);
  126. $links{$page}=[findlinks($page, $content)];
  127. $content=preprocess($page, $page, $content);
  128. $content=linkify($page, $page, $content);
  129. $content=htmlize($page, $type, $content);
  130. writefile(htmlpage($page), $config{destdir},
  131. genpage($page, $content, mtime($srcfile)));
  132. $oldpagemtime{$page}=time;
  133. }
  134. else {
  135. my $content=readfile($srcfile, 1);
  136. $links{$file}=[];
  137. delete $depends{$file};
  138. will_render($file, $file, 1);
  139. writefile($file, $config{destdir}, $content, 1);
  140. $oldpagemtime{$file}=time;
  141. }
  142. } #}}}
  143. sub prune ($) { #{{{
  144. my $file=shift;
  145. unlink($file);
  146. my $dir=dirname($file);
  147. while (rmdir($dir)) {
  148. $dir=dirname($dir);
  149. }
  150. } #}}}
  151. sub refresh () { #{{{
  152. # find existing pages
  153. my %exists;
  154. my @files;
  155. eval q{use File::Find};
  156. find({
  157. no_chdir => 1,
  158. wanted => sub {
  159. $_=decode_utf8($_);
  160. if (/$config{wiki_file_prune_regexp}/) {
  161. $File::Find::prune=1;
  162. }
  163. elsif (! -d $_ && ! -l $_) {
  164. my ($f)=/$config{wiki_file_regexp}/; # untaint
  165. if (! defined $f) {
  166. warn("skipping bad filename $_\n");
  167. }
  168. else {
  169. $f=~s/^\Q$config{srcdir}\E\/?//;
  170. push @files, $f;
  171. $exists{pagename($f)}=1;
  172. }
  173. }
  174. },
  175. }, $config{srcdir});
  176. find({
  177. no_chdir => 1,
  178. wanted => sub {
  179. $_=decode_utf8($_);
  180. if (/$config{wiki_file_prune_regexp}/) {
  181. $File::Find::prune=1;
  182. }
  183. elsif (! -d $_ && ! -l $_) {
  184. my ($f)=/$config{wiki_file_regexp}/; # untaint
  185. if (! defined $f) {
  186. warn("skipping bad filename $_\n");
  187. }
  188. else {
  189. # Don't add files that are in the
  190. # srcdir.
  191. $f=~s/^\Q$config{underlaydir}\E\/?//;
  192. if (! -e "$config{srcdir}/$f" &&
  193. ! -l "$config{srcdir}/$f") {
  194. push @files, $f;
  195. $exists{pagename($f)}=1;
  196. }
  197. }
  198. }
  199. },
  200. }, $config{underlaydir});
  201. my %rendered;
  202. # check for added or removed pages
  203. my @add;
  204. foreach my $file (@files) {
  205. my $page=pagename($file);
  206. if (! $oldpagemtime{$page}) {
  207. debug("new page $page") unless exists $pagectime{$page};
  208. push @add, $file;
  209. $links{$page}=[];
  210. $pagecase{lc $page}=$page;
  211. $pagesources{$page}=$file;
  212. if ($config{getctime} && -e "$config{srcdir}/$file") {
  213. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  214. }
  215. elsif (! exists $pagectime{$page}) {
  216. $pagectime{$page}=mtime(srcfile($file));
  217. }
  218. }
  219. }
  220. my @del;
  221. foreach my $page (keys %oldpagemtime) {
  222. if (! $exists{$page}) {
  223. debug("removing old page $page");
  224. push @del, $pagesources{$page};
  225. $renderedfiles{$page}=[];
  226. $oldpagemtime{$page}=0;
  227. prune($config{destdir}."/".$_)
  228. foreach @{$oldrenderedfiles{$page}};
  229. delete $pagesources{$page};
  230. }
  231. }
  232. # render any updated files
  233. foreach my $file (@files) {
  234. my $page=pagename($file);
  235. if (! exists $oldpagemtime{$page} ||
  236. mtime(srcfile($file)) > $oldpagemtime{$page} ||
  237. $forcerebuild{$page}) {
  238. debug("rendering $file");
  239. render($file);
  240. $rendered{$file}=1;
  241. }
  242. }
  243. # if any files were added or removed, check to see if each page
  244. # needs an update due to linking to them or inlining them.
  245. # TODO: inefficient; pages may get rendered above and again here;
  246. # problem is the bestlink may have changed and we won't know until
  247. # now
  248. if (@add || @del) {
  249. FILE: foreach my $file (@files) {
  250. my $page=pagename($file);
  251. foreach my $f (@add, @del) {
  252. my $p=pagename($f);
  253. foreach my $link (@{$links{$page}}) {
  254. if (bestlink($page, $link) eq $p) {
  255. debug("rendering $file, which links to $p");
  256. render($file);
  257. $rendered{$file}=1;
  258. next FILE;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. # Handle backlinks; if a page has added/removed links, update the
  265. # pages it links to. Also handles rebuilding dependant pages.
  266. # TODO: inefficient; pages may get rendered above and again here;
  267. # problem is the backlinks could be wrong in the first pass render
  268. # above
  269. if (%rendered || @del) {
  270. foreach my $f (@files) {
  271. my $p=pagename($f);
  272. if (exists $depends{$p}) {
  273. foreach my $file (keys %rendered, @del) {
  274. next if $f eq $file;
  275. my $page=pagename($file);
  276. if (pagespec_match($page, $depends{$p})) {
  277. debug("rendering $f, which depends on $page");
  278. render($f);
  279. $rendered{$f}=1;
  280. last;
  281. }
  282. }
  283. }
  284. }
  285. my %linkchanged;
  286. foreach my $file (keys %rendered, @del) {
  287. my $page=pagename($file);
  288. if (exists $links{$page}) {
  289. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  290. if (length $link &&
  291. (! exists $oldlinks{$page} ||
  292. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  293. $linkchanged{$link}=1;
  294. }
  295. }
  296. }
  297. if (exists $oldlinks{$page}) {
  298. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  299. if (length $link &&
  300. (! exists $links{$page} ||
  301. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  302. $linkchanged{$link}=1;
  303. }
  304. }
  305. }
  306. }
  307. foreach my $link (keys %linkchanged) {
  308. my $linkfile=$pagesources{$link};
  309. if (defined $linkfile) {
  310. debug("rendering $linkfile, to update its backlinks");
  311. render($linkfile);
  312. $rendered{$linkfile}=1;
  313. }
  314. }
  315. }
  316. # Remove no longer rendered files.
  317. foreach my $src (keys %rendered) {
  318. my $page=pagename($src);
  319. foreach my $file (@{$oldrenderedfiles{$page}}) {
  320. if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
  321. debug("removing $file, no longer rendered by $page");
  322. prune($config{destdir}."/".$file);
  323. }
  324. }
  325. }
  326. if (@del) {
  327. run_hooks(delete => sub { shift->(@del) });
  328. }
  329. if (%rendered) {
  330. run_hooks(change => sub { shift->(keys %rendered) });
  331. }
  332. } #}}}
  333. sub commandline_render () { #{{{
  334. loadplugins();
  335. checkconfig();
  336. lockwiki();
  337. loadindex();
  338. unlockwiki();
  339. my $srcfile=possibly_foolish_untaint($config{render});
  340. my $file=$srcfile;
  341. $file=~s/\Q$config{srcdir}\E\/?//;
  342. my $type=pagetype($file);
  343. die "ikiwiki: cannot render $srcfile\n" unless defined $type;
  344. my $content=readfile($srcfile);
  345. my $page=pagename($file);
  346. $pagesources{$page}=$file;
  347. $content=filter($page, $content);
  348. $content=preprocess($page, $page, $content);
  349. $content=linkify($page, $page, $content);
  350. $content=htmlize($page, $type, $content);
  351. print genpage($page, $content, mtime($srcfile));
  352. exit 0;
  353. } #}}}
  354. 1