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