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