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