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