summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: e5a1679f8fbf9df3e91c54cb5f973ac3dca2d414 (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 filter ($$) {
  213. my $page=shift;
  214. my $content=shift;
  215. if (exists $hooks{filter}) {
  216. foreach my $id (keys %{$hooks{filter}}) {
  217. $content=$hooks{filter}{$id}{call}->(
  218. page => $page,
  219. content => $content
  220. );
  221. }
  222. }
  223. return $content;
  224. }
  225. sub render ($) { #{{{
  226. my $file=shift;
  227. my $type=pagetype($file);
  228. my $srcfile=srcfile($file);
  229. if (defined $type) {
  230. my $content=readfile($srcfile);
  231. my $page=pagename($file);
  232. delete $depends{$page};
  233. $content=filter($page, $content);
  234. $links{$page}=[findlinks($page, $content)];
  235. $content=linkify($page, $page, $content);
  236. $content=preprocess($page, $content);
  237. $content=htmlize($type, $content);
  238. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  239. writefile(htmlpage($page), $config{destdir},
  240. genpage($page, $content, mtime($srcfile)));
  241. $oldpagemtime{$page}=time;
  242. $renderedfiles{$page}=htmlpage($page);
  243. }
  244. else {
  245. my $content=readfile($srcfile, 1);
  246. $links{$file}=[];
  247. delete $depends{$file};
  248. check_overwrite("$config{destdir}/$file", $file);
  249. writefile($file, $config{destdir}, $content, 1);
  250. $oldpagemtime{$file}=time;
  251. $renderedfiles{$file}=$file;
  252. }
  253. } #}}}
  254. sub prune ($) { #{{{
  255. my $file=shift;
  256. unlink($file);
  257. my $dir=dirname($file);
  258. while (rmdir($dir)) {
  259. $dir=dirname($dir);
  260. }
  261. } #}}}
  262. sub refresh () { #{{{
  263. # find existing pages
  264. my %exists;
  265. my @files;
  266. eval q{use File::Find};
  267. find({
  268. no_chdir => 1,
  269. wanted => sub {
  270. $_=decode_utf8($_);
  271. if (/$config{wiki_file_prune_regexp}/) {
  272. $File::Find::prune=1;
  273. }
  274. elsif (! -d $_ && ! -l $_) {
  275. my ($f)=/$config{wiki_file_regexp}/; # untaint
  276. if (! defined $f) {
  277. warn("skipping bad filename $_\n");
  278. }
  279. else {
  280. $f=~s/^\Q$config{srcdir}\E\/?//;
  281. push @files, $f;
  282. $exists{pagename($f)}=1;
  283. }
  284. }
  285. },
  286. }, $config{srcdir});
  287. find({
  288. no_chdir => 1,
  289. wanted => sub {
  290. $_=decode_utf8($_);
  291. if (/$config{wiki_file_prune_regexp}/) {
  292. $File::Find::prune=1;
  293. }
  294. elsif (! -d $_ && ! -l $_) {
  295. my ($f)=/$config{wiki_file_regexp}/; # untaint
  296. if (! defined $f) {
  297. warn("skipping bad filename $_\n");
  298. }
  299. else {
  300. # Don't add files that are in the
  301. # srcdir.
  302. $f=~s/^\Q$config{underlaydir}\E\/?//;
  303. if (! -e "$config{srcdir}/$f" &&
  304. ! -l "$config{srcdir}/$f") {
  305. push @files, $f;
  306. $exists{pagename($f)}=1;
  307. }
  308. }
  309. }
  310. },
  311. }, $config{underlaydir});
  312. my %rendered;
  313. # check for added or removed pages
  314. my @add;
  315. foreach my $file (@files) {
  316. my $page=pagename($file);
  317. if (! $oldpagemtime{$page}) {
  318. debug("new page $page") unless exists $pagectime{$page};
  319. push @add, $file;
  320. $links{$page}=[];
  321. $pagesources{$page}=$file;
  322. if ($config{getctime} && -e "$config{srcdir}/$file") {
  323. $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
  324. }
  325. elsif (! exists $pagectime{$page}) {
  326. $pagectime{$page}=mtime(srcfile($file));
  327. }
  328. }
  329. }
  330. my @del;
  331. foreach my $page (keys %oldpagemtime) {
  332. if (! $exists{$page}) {
  333. debug("removing old page $page");
  334. push @del, $pagesources{$page};
  335. prune($config{destdir}."/".$renderedfiles{$page});
  336. delete $renderedfiles{$page};
  337. $oldpagemtime{$page}=0;
  338. delete $pagesources{$page};
  339. }
  340. }
  341. # render any updated files
  342. foreach my $file (@files) {
  343. my $page=pagename($file);
  344. if (! exists $oldpagemtime{$page} ||
  345. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  346. debug("rendering $file");
  347. render($file);
  348. $rendered{$file}=1;
  349. }
  350. }
  351. # if any files were added or removed, check to see if each page
  352. # needs an update due to linking to them or inlining them.
  353. # TODO: inefficient; pages may get rendered above and again here;
  354. # problem is the bestlink may have changed and we won't know until
  355. # now
  356. if (@add || @del) {
  357. FILE: foreach my $file (@files) {
  358. my $page=pagename($file);
  359. foreach my $f (@add, @del) {
  360. my $p=pagename($f);
  361. foreach my $link (@{$links{$page}}) {
  362. if (bestlink($page, $link) eq $p) {
  363. debug("rendering $file, which links to $p");
  364. render($file);
  365. $rendered{$file}=1;
  366. next FILE;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. # Handle backlinks; if a page has added/removed links, update the
  373. # pages it links to. Also handles rebuilding dependant pages.
  374. # TODO: inefficient; pages may get rendered above and again here;
  375. # problem is the backlinks could be wrong in the first pass render
  376. # above
  377. if (%rendered || @del) {
  378. foreach my $f (@files) {
  379. my $p=pagename($f);
  380. if (exists $depends{$p}) {
  381. foreach my $file (keys %rendered, @del) {
  382. next if $f eq $file;
  383. my $page=pagename($file);
  384. if (globlist_match($page, $depends{$p})) {
  385. debug("rendering $f, which depends on $page");
  386. render($f);
  387. $rendered{$f}=1;
  388. last;
  389. }
  390. }
  391. }
  392. }
  393. my %linkchanged;
  394. foreach my $file (keys %rendered, @del) {
  395. my $page=pagename($file);
  396. if (exists $links{$page}) {
  397. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  398. if (length $link &&
  399. (! exists $oldlinks{$page} ||
  400. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  401. $linkchanged{$link}=1;
  402. }
  403. }
  404. }
  405. if (exists $oldlinks{$page}) {
  406. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  407. if (length $link &&
  408. (! exists $links{$page} ||
  409. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  410. $linkchanged{$link}=1;
  411. }
  412. }
  413. }
  414. }
  415. foreach my $link (keys %linkchanged) {
  416. my $linkfile=$pagesources{$link};
  417. if (defined $linkfile) {
  418. debug("rendering $linkfile, to update its backlinks");
  419. render($linkfile);
  420. $rendered{$linkfile}=1;
  421. }
  422. }
  423. }
  424. if (@del && exists $hooks{delete}) {
  425. foreach my $id (keys %{$hooks{delete}}) {
  426. $hooks{delete}{$id}{call}->(@del);
  427. }
  428. }
  429. if (%rendered && exists $hooks{change}) {
  430. foreach my $id (keys %{$hooks{change}}) {
  431. $hooks{change}{$id}{call}->(keys %rendered);
  432. }
  433. }
  434. } #}}}
  435. 1