summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 6d5ea9ee587ebe470d6ee2b55d681f659da73572 (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;
  9. my $page=shift;
  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. if (exists $hooks{sanitize}) {
  27. foreach my $id (keys %{$hooks{sanitize}}) {
  28. $content=$hooks{sanitize}{$id}{call}->($content);
  29. }
  30. }
  31. return $content;
  32. } #}}}
  33. sub backlinks ($) { #{{{
  34. my $page=shift;
  35. my @links;
  36. foreach my $p (keys %links) {
  37. next if bestlink($page, $p) eq $page;
  38. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  39. my $href=abs2rel(htmlpage($p), dirname($page));
  40. # Trim common dir prefixes from both pages.
  41. my $p_trimmed=$p;
  42. my $page_trimmed=$page;
  43. my $dir;
  44. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  45. defined $dir &&
  46. $p_trimmed=~s/^\Q$dir\E// &&
  47. $page_trimmed=~s/^\Q$dir\E//;
  48. push @links, { url => $href, page => $p_trimmed };
  49. }
  50. }
  51. return sort { $a->{page} cmp $b->{page} } @links;
  52. } #}}}
  53. sub parentlinks ($) { #{{{
  54. my $page=shift;
  55. my @ret;
  56. my $pagelink="";
  57. my $path="";
  58. my $skip=1;
  59. return if $page eq 'index'; # toplevel
  60. foreach my $dir (reverse split("/", $page)) {
  61. if (! $skip) {
  62. $path.="../";
  63. unshift @ret, { url => "$path$dir.html", page => $dir };
  64. }
  65. else {
  66. $skip=0;
  67. }
  68. }
  69. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  70. return @ret;
  71. } #}}}
  72. sub preprocess ($$;$) { #{{{
  73. my $page=shift;
  74. my $content=shift;
  75. my $onlystrip=shift || 0; # strip directives without processing
  76. my $handle=sub {
  77. my $escape=shift;
  78. my $command=shift;
  79. my $params=shift;
  80. if (length $escape) {
  81. return "[[$command $params]]";
  82. }
  83. elsif ($onlystrip) {
  84. return "";
  85. }
  86. elsif (exists $hooks{preprocess}{$command}) {
  87. # Note: preserve order of params, some plugins may
  88. # consider it significant.
  89. my @params;
  90. while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
  91. push @params, $1, $2;
  92. }
  93. return $hooks{preprocess}{$command}{call}->(@params, page => $page);
  94. }
  95. else {
  96. return "[[$command not processed]]";
  97. }
  98. };
  99. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  100. return $content;
  101. } #}}}
  102. sub add_depends ($$) { #{{{
  103. my $page=shift;
  104. my $globlist=shift;
  105. if (! exists $depends{$page}) {
  106. $depends{$page}=$globlist;
  107. }
  108. else {
  109. $depends{$page}=globlist_merge($depends{$page}, $globlist);
  110. }
  111. } # }}}
  112. sub globlist_merge ($$) { #{{{
  113. my $a=shift;
  114. my $b=shift;
  115. my $ret="";
  116. # Only add negated globs if they are not matched by the other globlist.
  117. foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
  118. (map { [ $b, $_ ] } split(" ", $a))) {
  119. if ($i->[1]=~/^!(.*)/) {
  120. if (! globlist_match($1, $i->[0])) {
  121. $ret.=" ".$i->[1];
  122. }
  123. }
  124. else {
  125. $ret.=" ".$i->[1];
  126. }
  127. }
  128. return $ret;
  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. styleurl => styleurl($page),
  167. );
  168. if (exists $hooks{pagetemplate}) {
  169. foreach my $id (keys %{$hooks{pagetemplate}}) {
  170. $hooks{pagetemplate}{$id}{call}->($page, $template);
  171. }
  172. }
  173. return $template->output;
  174. } #}}}
  175. sub check_overwrite ($$) { #{{{
  176. # Important security check. Make sure to call this before saving
  177. # any files to the source directory.
  178. my $dest=shift;
  179. my $src=shift;
  180. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  181. error("$dest already exists and was not rendered from $src before");
  182. }
  183. } #}}}
  184. sub displaytime ($) { #{{{
  185. my $time=shift;
  186. eval q{use POSIX};
  187. # strftime doesn't know about encodings, so make sure
  188. # its output is properly treated as utf8
  189. return decode_utf8(POSIX::strftime(
  190. $config{timeformat}, localtime($time)));
  191. } #}}}
  192. sub mtime ($) { #{{{
  193. my $file=shift;
  194. return (stat($file))[9];
  195. } #}}}
  196. sub findlinks ($$) { #{{{
  197. my $page=shift;
  198. my $content=shift;
  199. my @links;
  200. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  201. push @links, titlepage($2);
  202. }
  203. if ($config{discussion}) {
  204. # Discussion links are a special case since they're not in the
  205. # text of the page, but on its template.
  206. return @links, "$page/discussion";
  207. }
  208. else {
  209. return @links;
  210. }
  211. } #}}}
  212. sub render ($) { #{{{
  213. my $file=shift;
  214. my $type=pagetype($file);
  215. my $srcfile=srcfile($file);
  216. if (defined $type) {
  217. my $content=readfile($srcfile);
  218. my $page=pagename($file);
  219. delete $depends{$page};
  220. if (exists $hooks{filter}) {
  221. foreach my $id (keys %{$hooks{filter}}) {
  222. $content=$hooks{filter}{$id}{call}->(
  223. page => $page,
  224. content => $content
  225. );
  226. }
  227. }
  228. $links{$page}=[findlinks($page, $content)];
  229. $content=linkify($page, $page, $content);
  230. $content=preprocess($page, $content);
  231. $content=htmlize($type, $content);
  232. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  233. writefile(htmlpage($page), $config{destdir},
  234. genpage($page, $content, mtime($srcfile)));
  235. $oldpagemtime{$page}=time;
  236. $renderedfiles{$page}=htmlpage($page);
  237. }
  238. else {
  239. my $content=readfile($srcfile, 1);
  240. $links{$file}=[];
  241. delete $depends{$file};
  242. check_overwrite("$config{destdir}/$file", $file);
  243. writefile($file, $config{destdir}, $content, 1);
  244. $oldpagemtime{$file}=time;
  245. $renderedfiles{$file}=$file;
  246. }
  247. } #}}}
  248. sub prune ($) { #{{{
  249. my $file=shift;
  250. unlink($file);
  251. my $dir=dirname($file);
  252. while (rmdir($dir)) {
  253. $dir=dirname($dir);
  254. }
  255. } #}}}
  256. sub refresh () { #{{{
  257. # find existing pages
  258. my %exists;
  259. my @files;
  260. eval q{use File::Find};
  261. find({
  262. no_chdir => 1,
  263. wanted => sub {
  264. $_=decode_utf8($_);
  265. if (/$config{wiki_file_prune_regexp}/) {
  266. $File::Find::prune=1;
  267. }
  268. elsif (! -d $_ && ! -l $_) {
  269. my ($f)=/$config{wiki_file_regexp}/; # untaint
  270. if (! defined $f) {
  271. warn("skipping bad filename $_\n");
  272. }
  273. else {
  274. $f=~s/^\Q$config{srcdir}\E\/?//;
  275. push @files, $f;
  276. $exists{pagename($f)}=1;
  277. }
  278. }
  279. },
  280. }, $config{srcdir});
  281. find({
  282. no_chdir => 1,
  283. wanted => sub {
  284. $_=decode_utf8($_);
  285. if (/$config{wiki_file_prune_regexp}/) {
  286. $File::Find::prune=1;
  287. }
  288. elsif (! -d $_ && ! -l $_) {
  289. my ($f)=/$config{wiki_file_regexp}/; # untaint
  290. if (! defined $f) {
  291. warn("skipping bad filename $_\n");
  292. }
  293. else {
  294. # Don't add files that are in the
  295. # srcdir.
  296. $f=~s/^\Q$config{underlaydir}\E\/?//;
  297. if (! -e "$config{srcdir}/$f" &&
  298. ! -l "$config{srcdir}/$f") {
  299. push @files, $f;
  300. $exists{pagename($f)}=1;
  301. }
  302. }
  303. }
  304. },
  305. }, $config{underlaydir});
  306. my %rendered;
  307. # check for added or removed pages
  308. my @add;
  309. foreach my $file (@files) {
  310. my $page=pagename($file);
  311. if (! $oldpagemtime{$page}) {
  312. debug("new page $page") unless exists $pagectime{$page};
  313. push @add, $file;
  314. $links{$page}=[];
  315. $pagesources{$page}=$file;
  316. if ($config{getctime} && -e "$config{srcdir}/$file") {
  317. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  318. }
  319. elsif (! exists $pagectime{$page}) {
  320. $pagectime{$page}=mtime(srcfile($file));
  321. }
  322. }
  323. }
  324. my @del;
  325. foreach my $page (keys %oldpagemtime) {
  326. if (! $exists{$page}) {
  327. debug("removing old page $page");
  328. push @del, $pagesources{$page};
  329. prune($config{destdir}."/".$renderedfiles{$page});
  330. delete $renderedfiles{$page};
  331. $oldpagemtime{$page}=0;
  332. delete $pagesources{$page};
  333. }
  334. }
  335. # render any updated files
  336. foreach my $file (@files) {
  337. my $page=pagename($file);
  338. if (! exists $oldpagemtime{$page} ||
  339. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  340. debug("rendering $file");
  341. render($file);
  342. $rendered{$file}=1;
  343. }
  344. }
  345. # if any files were added or removed, check to see if each page
  346. # needs an update due to linking to them or inlining them.
  347. # TODO: inefficient; pages may get rendered above and again here;
  348. # problem is the bestlink may have changed and we won't know until
  349. # now
  350. if (@add || @del) {
  351. FILE: foreach my $file (@files) {
  352. my $page=pagename($file);
  353. foreach my $f (@add, @del) {
  354. my $p=pagename($f);
  355. foreach my $link (@{$links{$page}}) {
  356. if (bestlink($page, $link) eq $p) {
  357. debug("rendering $file, which links to $p");
  358. render($file);
  359. $rendered{$file}=1;
  360. next FILE;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. # Handle backlinks; if a page has added/removed links, update the
  367. # pages it links to. Also handles rebuilding dependant pages.
  368. # TODO: inefficient; pages may get rendered above and again here;
  369. # problem is the backlinks could be wrong in the first pass render
  370. # above
  371. if (%rendered || @del) {
  372. foreach my $f (@files) {
  373. my $p=pagename($f);
  374. if (exists $depends{$p}) {
  375. foreach my $file (keys %rendered, @del) {
  376. next if $f eq $file;
  377. my $page=pagename($file);
  378. if (globlist_match($page, $depends{$p})) {
  379. debug("rendering $f, which depends on $page");
  380. render($f);
  381. $rendered{$f}=1;
  382. last;
  383. }
  384. }
  385. }
  386. }
  387. my %linkchanged;
  388. foreach my $file (keys %rendered, @del) {
  389. my $page=pagename($file);
  390. if (exists $links{$page}) {
  391. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  392. if (length $link &&
  393. (! exists $oldlinks{$page} ||
  394. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  395. $linkchanged{$link}=1;
  396. }
  397. }
  398. }
  399. if (exists $oldlinks{$page}) {
  400. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  401. if (length $link &&
  402. (! exists $links{$page} ||
  403. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  404. $linkchanged{$link}=1;
  405. }
  406. }
  407. }
  408. }
  409. foreach my $link (keys %linkchanged) {
  410. my $linkfile=$pagesources{$link};
  411. if (defined $linkfile) {
  412. debug("rendering $linkfile, to update its backlinks");
  413. render($linkfile);
  414. $rendered{$linkfile}=1;
  415. }
  416. }
  417. }
  418. if (@del && exists $hooks{delete}) {
  419. foreach my $id (keys %{$hooks{delete}}) {
  420. $hooks{delete}{$id}{call}->(@del);
  421. }
  422. }
  423. if (%rendered && exists $hooks{change}) {
  424. foreach my $id (keys %{$hooks{change}}) {
  425. $hooks{change}{$id}{call}->(keys %rendered);
  426. }
  427. }
  428. } #}}}
  429. 1