summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 6f685f3b8f65ae0d3904e3b939df12fe933a4532 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use IkiWiki;
  7. sub linkify ($$) { #{{{
  8. my $content=shift;
  9. my $page=shift;
  10. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  11. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
  12. : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
  13. }eg;
  14. return $content;
  15. } #}}}
  16. my $_scrubber;
  17. sub scrubber { #{{{
  18. return $_scrubber if defined $_scrubber;
  19. eval q{use HTML::Scrubber};
  20. # Lists based on http://feedparser.org/docs/html-sanitization.html
  21. $_scrubber = HTML::Scrubber->new(
  22. allow => [qw{
  23. a abbr acronym address area b big blockquote br
  24. button caption center cite code col colgroup dd del
  25. dfn dir div dl dt em fieldset font form h1 h2 h3 h4
  26. h5 h6 hr i img input ins kbd label legend li map
  27. menu ol optgroup option p pre q s samp select small
  28. span strike strong sub sup table tbody td textarea
  29. tfoot th thead tr tt u ul var
  30. }],
  31. default => [undef, { map { $_ => 1 } qw{
  32. abbr accept accept-charset accesskey action
  33. align alt axis border cellpadding cellspacing
  34. char charoff charset checked cite class
  35. clear cols colspan color compact coords
  36. datetime dir disabled enctype for frame
  37. headers height href hreflang hspace id ismap
  38. label lang longdesc maxlength media method
  39. multiple name nohref noshade nowrap prompt
  40. readonly rel rev rows rowspan rules scope
  41. selected shape size span src start summary
  42. tabindex target title type usemap valign
  43. value vspace width
  44. }}],
  45. );
  46. return $_scrubber;
  47. } # }}}
  48. sub htmlize ($$) { #{{{
  49. my $type=shift;
  50. my $content=shift;
  51. if (! $INC{"/usr/bin/markdown"}) {
  52. no warnings 'once';
  53. $blosxom::version="is a proper perl module too much to ask?";
  54. use warnings 'all';
  55. do "/usr/bin/markdown";
  56. }
  57. if ($type eq '.mdwn') {
  58. $content=Markdown::Markdown($content);
  59. }
  60. else {
  61. error("htmlization of $type not supported");
  62. }
  63. if ($config{sanitize}) {
  64. $content=scrubber()->scrub($content);
  65. }
  66. return $content;
  67. } #}}}
  68. sub backlinks ($) { #{{{
  69. my $page=shift;
  70. my @links;
  71. foreach my $p (keys %links) {
  72. next if bestlink($page, $p) eq $page;
  73. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  74. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  75. # Trim common dir prefixes from both pages.
  76. my $p_trimmed=$p;
  77. my $page_trimmed=$page;
  78. my $dir;
  79. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  80. defined $dir &&
  81. $p_trimmed=~s/^\Q$dir\E// &&
  82. $page_trimmed=~s/^\Q$dir\E//;
  83. push @links, { url => $href, page => $p_trimmed };
  84. }
  85. }
  86. return sort { $a->{page} cmp $b->{page} } @links;
  87. } #}}}
  88. sub parentlinks ($) { #{{{
  89. my $page=shift;
  90. my @ret;
  91. my $pagelink="";
  92. my $path="";
  93. my $skip=1;
  94. foreach my $dir (reverse split("/", $page)) {
  95. if (! $skip) {
  96. $path.="../";
  97. unshift @ret, { url => "$path$dir.html", page => $dir };
  98. }
  99. else {
  100. $skip=0;
  101. }
  102. }
  103. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  104. return @ret;
  105. } #}}}
  106. sub preprocess ($$) { #{{{
  107. my $page=shift;
  108. my $content=shift;
  109. my $handle=sub {
  110. my $escape=shift;
  111. my $command=shift;
  112. my $params=shift;
  113. if (length $escape) {
  114. return "[[$command $params]]";
  115. }
  116. elsif (exists $hooks{preprocess}{$command}) {
  117. my %params;
  118. while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
  119. $params{$1}=$2;
  120. }
  121. return $hooks{preprocess}{$command}{call}->(page => $page, %params);
  122. }
  123. else {
  124. return "[[$command not processed]]";
  125. }
  126. };
  127. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  128. return $content;
  129. } #}}}
  130. sub add_depends ($$) { #{{{
  131. my $page=shift;
  132. my $globlist=shift;
  133. if (! exists $depends{$page}) {
  134. $depends{$page}=$globlist;
  135. }
  136. else {
  137. $depends{$page}=globlist_merge($depends{$page}, $globlist);
  138. }
  139. } # }}}
  140. sub globlist_merge ($$) { #{{{
  141. my $a=shift;
  142. my $b=shift;
  143. my $ret="";
  144. # Only add negated globs if they are not matched by the other globlist.
  145. foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
  146. (map { [ $b, $_ ] } split(" ", $a))) {
  147. if ($i->[1]=~/^!(.*)/) {
  148. if (! globlist_match($1, $i->[0])) {
  149. $ret.=" ".$i->[1];
  150. }
  151. }
  152. else {
  153. $ret.=" ".$i->[1];
  154. }
  155. }
  156. return $ret;
  157. } #}}}
  158. sub genpage ($$$) { #{{{
  159. my $content=shift;
  160. my $page=shift;
  161. my $mtime=shift;
  162. my $title=pagetitle(basename($page));
  163. my $template=HTML::Template->new(blind_cache => 1,
  164. filename => "$config{templatedir}/page.tmpl");
  165. if (length $config{cgiurl}) {
  166. $template->param(editurl => cgiurl(do => "edit", page => $page));
  167. $template->param(prefsurl => cgiurl(do => "prefs"));
  168. if ($config{rcs}) {
  169. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  170. }
  171. }
  172. if (length $config{historyurl}) {
  173. my $u=$config{historyurl};
  174. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  175. $template->param(historyurl => $u);
  176. }
  177. $template->param(headercontent => $config{headercontent});
  178. $template->param(
  179. title => $title,
  180. wikiname => $config{wikiname},
  181. parentlinks => [parentlinks($page)],
  182. content => $content,
  183. backlinks => [backlinks($page)],
  184. discussionlink => htmllink($page, "Discussion", 1, 1),
  185. mtime => scalar(gmtime($mtime)),
  186. styleurl => styleurl($page),
  187. );
  188. return $template->output;
  189. } #}}}
  190. sub check_overwrite ($$) { #{{{
  191. # Important security check. Make sure to call this before saving
  192. # any files to the source directory.
  193. my $dest=shift;
  194. my $src=shift;
  195. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  196. error("$dest already exists and was rendered from ".
  197. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  198. %renderedfiles)).
  199. ", before, so not rendering from $src");
  200. }
  201. } #}}}
  202. sub mtime ($) { #{{{
  203. my $file=shift;
  204. return (stat($file))[9];
  205. } #}}}
  206. sub findlinks ($$) { #{{{
  207. my $content=shift;
  208. my $page=shift;
  209. my @links;
  210. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  211. push @links, titlepage($2);
  212. }
  213. # Discussion links are a special case since they're not in the text
  214. # of the page, but on its template.
  215. return @links, "$page/discussion";
  216. } #}}}
  217. sub render ($) { #{{{
  218. my $file=shift;
  219. my $type=pagetype($file);
  220. my $srcfile=srcfile($file);
  221. if ($type ne 'unknown') {
  222. my $content=readfile($srcfile);
  223. my $page=pagename($file);
  224. delete $depends{$page};
  225. if (exists $hooks{filter}) {
  226. foreach my $id (keys %{$hooks{filter}}) {
  227. $content=$hooks{filter}{$id}{call}->(
  228. page => $page,
  229. content => $content
  230. );
  231. }
  232. }
  233. $links{$page}=[findlinks($content, $page)];
  234. $content=linkify($content, $page);
  235. $content=preprocess($page, $content);
  236. $content=htmlize($type, $content);
  237. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  238. writefile(htmlpage($page), $config{destdir},
  239. genpage($content, $page, mtime($srcfile)));
  240. $oldpagemtime{$page}=time;
  241. $renderedfiles{$page}=htmlpage($page);
  242. }
  243. else {
  244. my $content=readfile($srcfile, 1);
  245. $links{$file}=[];
  246. delete $depends{$file};
  247. check_overwrite("$config{destdir}/$file", $file);
  248. writefile($file, $config{destdir}, $content, 1);
  249. $oldpagemtime{$file}=time;
  250. $renderedfiles{$file}=$file;
  251. }
  252. } #}}}
  253. sub prune ($) { #{{{
  254. my $file=shift;
  255. unlink($file);
  256. my $dir=dirname($file);
  257. while (rmdir($dir)) {
  258. $dir=dirname($dir);
  259. }
  260. } #}}}
  261. sub refresh () { #{{{
  262. # find existing pages
  263. my %exists;
  264. my @files;
  265. eval q{use File::Find};
  266. find({
  267. no_chdir => 1,
  268. wanted => sub {
  269. if (/$config{wiki_file_prune_regexp}/) {
  270. $File::Find::prune=1;
  271. }
  272. elsif (! -d $_ && ! -l $_) {
  273. my ($f)=/$config{wiki_file_regexp}/; # untaint
  274. if (! defined $f) {
  275. warn("skipping bad filename $_\n");
  276. }
  277. else {
  278. $f=~s/^\Q$config{srcdir}\E\/?//;
  279. push @files, $f;
  280. $exists{pagename($f)}=1;
  281. }
  282. }
  283. },
  284. }, $config{srcdir});
  285. find({
  286. no_chdir => 1,
  287. wanted => sub {
  288. if (/$config{wiki_file_prune_regexp}/) {
  289. $File::Find::prune=1;
  290. }
  291. elsif (! -d $_ && ! -l $_) {
  292. my ($f)=/$config{wiki_file_regexp}/; # untaint
  293. if (! defined $f) {
  294. warn("skipping bad filename $_\n");
  295. }
  296. else {
  297. # Don't add files that are in the
  298. # srcdir.
  299. $f=~s/^\Q$config{underlaydir}\E\/?//;
  300. if (! -e "$config{srcdir}/$f" &&
  301. ! -l "$config{srcdir}/$f") {
  302. push @files, $f;
  303. $exists{pagename($f)}=1;
  304. }
  305. }
  306. }
  307. },
  308. }, $config{underlaydir});
  309. my %rendered;
  310. # check for added or removed pages
  311. my @add;
  312. foreach my $file (@files) {
  313. my $page=pagename($file);
  314. if (! $oldpagemtime{$page}) {
  315. debug("new page $page") unless exists $pagectime{$page};
  316. push @add, $file;
  317. $links{$page}=[];
  318. $pagesources{$page}=$file;
  319. $pagectime{$page}=mtime(srcfile($file))
  320. unless exists $pagectime{$page};
  321. }
  322. }
  323. my @del;
  324. foreach my $page (keys %oldpagemtime) {
  325. if (! $exists{$page}) {
  326. debug("removing old page $page");
  327. push @del, $pagesources{$page};
  328. prune($config{destdir}."/".$renderedfiles{$page});
  329. delete $renderedfiles{$page};
  330. $oldpagemtime{$page}=0;
  331. delete $pagesources{$page};
  332. }
  333. }
  334. # render any updated files
  335. foreach my $file (@files) {
  336. my $page=pagename($file);
  337. if (! exists $oldpagemtime{$page} ||
  338. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  339. debug("rendering changed file $file");
  340. render($file);
  341. $rendered{$file}=1;
  342. }
  343. }
  344. # if any files were added or removed, check to see if each page
  345. # needs an update due to linking to them or inlining them.
  346. # TODO: inefficient; pages may get rendered above and again here;
  347. # problem is the bestlink may have changed and we won't know until
  348. # now
  349. if (@add || @del) {
  350. FILE: foreach my $file (@files) {
  351. my $page=pagename($file);
  352. foreach my $f (@add, @del) {
  353. my $p=pagename($f);
  354. foreach my $link (@{$links{$page}}) {
  355. if (bestlink($page, $link) eq $p) {
  356. debug("rendering $file, which links to $p");
  357. render($file);
  358. $rendered{$file}=1;
  359. next FILE;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. # Handle backlinks; if a page has added/removed links, update the
  366. # pages it links to. Also handles rebuilding dependat pages.
  367. # TODO: inefficient; pages may get rendered above and again here;
  368. # problem is the backlinks could be wrong in the first pass render
  369. # above
  370. if (%rendered || @del) {
  371. foreach my $f (@files) {
  372. my $p=pagename($f);
  373. if (exists $depends{$p}) {
  374. foreach my $file (keys %rendered, @del) {
  375. next if $f eq $file;
  376. my $page=pagename($file);
  377. if (globlist_match($page, $depends{$p})) {
  378. debug("rendering $f, which depends on $page");
  379. render($f);
  380. $rendered{$f}=1;
  381. last;
  382. }
  383. }
  384. }
  385. }
  386. my %linkchanged;
  387. foreach my $file (keys %rendered, @del) {
  388. my $page=pagename($file);
  389. if (exists $links{$page}) {
  390. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  391. if (length $link &&
  392. (! exists $oldlinks{$page} ||
  393. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  394. $linkchanged{$link}=1;
  395. }
  396. }
  397. }
  398. if (exists $oldlinks{$page}) {
  399. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  400. if (length $link &&
  401. (! exists $links{$page} ||
  402. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  403. $linkchanged{$link}=1;
  404. }
  405. }
  406. }
  407. }
  408. foreach my $link (keys %linkchanged) {
  409. my $linkfile=$pagesources{$link};
  410. if (defined $linkfile) {
  411. debug("rendering $linkfile, to update its backlinks");
  412. render($linkfile);
  413. $rendered{$linkfile}=1;
  414. }
  415. }
  416. }
  417. if (@del && exists $hooks{delete}) {
  418. foreach my $id (keys %{$hooks{delete}}) {
  419. $hooks{delete}{$id}{call}->(@del);
  420. }
  421. }
  422. if (%rendered && exists $hooks{render}) {
  423. foreach my $id (keys %{$hooks{render}}) {
  424. $hooks{render}{$id}{call}->(keys %rendered);
  425. }
  426. }
  427. } #}}}
  428. 1