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