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