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