summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: c16877b3deb9117573962d7a061843ecf26e5067 (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 check_overwrite ($$) { #{{{
  97. # Important security check. Make sure to call this before saving
  98. # any files to the source directory.
  99. my $dest=shift;
  100. my $src=shift;
  101. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  102. error("$dest already exists and was not rendered from $src before");
  103. }
  104. } #}}}
  105. sub mtime ($) { #{{{
  106. my $file=shift;
  107. return (stat($file))[9];
  108. } #}}}
  109. sub findlinks ($$) { #{{{
  110. my $page=shift;
  111. my $content=shift;
  112. my @links;
  113. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  114. push @links, titlepage($2);
  115. }
  116. if ($config{discussion}) {
  117. # Discussion links are a special case since they're not in the
  118. # text of the page, but on its template.
  119. return @links, "$page/discussion";
  120. }
  121. else {
  122. return @links;
  123. }
  124. } #}}}
  125. sub render ($) { #{{{
  126. my $file=shift;
  127. my $type=pagetype($file);
  128. my $srcfile=srcfile($file);
  129. if (defined $type) {
  130. my $content=readfile($srcfile);
  131. my $page=pagename($file);
  132. delete $depends{$page};
  133. $content=filter($page, $content);
  134. $links{$page}=[findlinks($page, $content)];
  135. $content=preprocess($page, $page, $content);
  136. $content=linkify($page, $page, $content);
  137. $content=htmlize($page, $type, $content);
  138. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  139. writefile(htmlpage($page), $config{destdir},
  140. genpage($page, $content, mtime($srcfile)));
  141. $oldpagemtime{$page}=time;
  142. $renderedfiles{$page}=htmlpage($page);
  143. }
  144. else {
  145. my $content=readfile($srcfile, 1);
  146. $links{$file}=[];
  147. delete $depends{$file};
  148. check_overwrite("$config{destdir}/$file", $file);
  149. writefile($file, $config{destdir}, $content, 1);
  150. $oldpagemtime{$file}=time;
  151. $renderedfiles{$file}=$file;
  152. }
  153. } #}}}
  154. sub prune ($) { #{{{
  155. my $file=shift;
  156. unlink($file);
  157. my $dir=dirname($file);
  158. while (rmdir($dir)) {
  159. $dir=dirname($dir);
  160. }
  161. } #}}}
  162. sub refresh () { #{{{
  163. # find existing pages
  164. my %exists;
  165. my @files;
  166. eval q{use File::Find};
  167. find({
  168. no_chdir => 1,
  169. wanted => sub {
  170. $_=decode_utf8($_);
  171. if (/$config{wiki_file_prune_regexp}/) {
  172. $File::Find::prune=1;
  173. }
  174. elsif (! -d $_ && ! -l $_) {
  175. my ($f)=/$config{wiki_file_regexp}/; # untaint
  176. if (! defined $f) {
  177. warn("skipping bad filename $_\n");
  178. }
  179. else {
  180. $f=~s/^\Q$config{srcdir}\E\/?//;
  181. push @files, $f;
  182. $exists{pagename($f)}=1;
  183. }
  184. }
  185. },
  186. }, $config{srcdir});
  187. find({
  188. no_chdir => 1,
  189. wanted => sub {
  190. $_=decode_utf8($_);
  191. if (/$config{wiki_file_prune_regexp}/) {
  192. $File::Find::prune=1;
  193. }
  194. elsif (! -d $_ && ! -l $_) {
  195. my ($f)=/$config{wiki_file_regexp}/; # untaint
  196. if (! defined $f) {
  197. warn("skipping bad filename $_\n");
  198. }
  199. else {
  200. # Don't add files that are in the
  201. # srcdir.
  202. $f=~s/^\Q$config{underlaydir}\E\/?//;
  203. if (! -e "$config{srcdir}/$f" &&
  204. ! -l "$config{srcdir}/$f") {
  205. push @files, $f;
  206. $exists{pagename($f)}=1;
  207. }
  208. }
  209. }
  210. },
  211. }, $config{underlaydir});
  212. my %rendered;
  213. # check for added or removed pages
  214. my @add;
  215. foreach my $file (@files) {
  216. my $page=pagename($file);
  217. if (! $oldpagemtime{$page}) {
  218. debug("new page $page") unless exists $pagectime{$page};
  219. push @add, $file;
  220. $links{$page}=[];
  221. $pagecase{lc $page}=$page;
  222. $pagesources{$page}=$file;
  223. if ($config{getctime} && -e "$config{srcdir}/$file") {
  224. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  225. }
  226. elsif (! exists $pagectime{$page}) {
  227. $pagectime{$page}=mtime(srcfile($file));
  228. }
  229. }
  230. }
  231. my @del;
  232. foreach my $page (keys %oldpagemtime) {
  233. if (! $exists{$page}) {
  234. debug("removing old page $page");
  235. push @del, $pagesources{$page};
  236. prune($config{destdir}."/".$renderedfiles{$page});
  237. delete $renderedfiles{$page};
  238. $oldpagemtime{$page}=0;
  239. delete $pagesources{$page};
  240. }
  241. }
  242. # render any updated files
  243. foreach my $file (@files) {
  244. my $page=pagename($file);
  245. if (! exists $oldpagemtime{$page} ||
  246. mtime(srcfile($file)) > $oldpagemtime{$page} ||
  247. $forcerebuild{$page}) {
  248. debug("rendering $file");
  249. render($file);
  250. $rendered{$file}=1;
  251. }
  252. }
  253. # if any files were added or removed, check to see if each page
  254. # needs an update due to linking to them or inlining them.
  255. # TODO: inefficient; pages may get rendered above and again here;
  256. # problem is the bestlink may have changed and we won't know until
  257. # now
  258. if (@add || @del) {
  259. FILE: foreach my $file (@files) {
  260. my $page=pagename($file);
  261. foreach my $f (@add, @del) {
  262. my $p=pagename($f);
  263. foreach my $link (@{$links{$page}}) {
  264. if (bestlink($page, $link) eq $p) {
  265. debug("rendering $file, which links to $p");
  266. render($file);
  267. $rendered{$file}=1;
  268. next FILE;
  269. }
  270. }
  271. }
  272. }
  273. }
  274. # Handle backlinks; if a page has added/removed links, update the
  275. # pages it links to. Also handles rebuilding dependant pages.
  276. # TODO: inefficient; pages may get rendered above and again here;
  277. # problem is the backlinks could be wrong in the first pass render
  278. # above
  279. if (%rendered || @del) {
  280. foreach my $f (@files) {
  281. my $p=pagename($f);
  282. if (exists $depends{$p}) {
  283. foreach my $file (keys %rendered, @del) {
  284. next if $f eq $file;
  285. my $page=pagename($file);
  286. if (pagespec_match($page, $depends{$p})) {
  287. debug("rendering $f, which depends on $page");
  288. render($f);
  289. $rendered{$f}=1;
  290. last;
  291. }
  292. }
  293. }
  294. }
  295. my %linkchanged;
  296. foreach my $file (keys %rendered, @del) {
  297. my $page=pagename($file);
  298. if (exists $links{$page}) {
  299. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  300. if (length $link &&
  301. (! exists $oldlinks{$page} ||
  302. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  303. $linkchanged{$link}=1;
  304. }
  305. }
  306. }
  307. if (exists $oldlinks{$page}) {
  308. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  309. if (length $link &&
  310. (! exists $links{$page} ||
  311. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  312. $linkchanged{$link}=1;
  313. }
  314. }
  315. }
  316. }
  317. foreach my $link (keys %linkchanged) {
  318. my $linkfile=$pagesources{$link};
  319. if (defined $linkfile) {
  320. debug("rendering $linkfile, to update its backlinks");
  321. render($linkfile);
  322. $rendered{$linkfile}=1;
  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. 1