summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: be6e6d1cbe8b5e09b0c5e626bb6d8c44023e7519 (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 $plugins{preprocess}{$command}) {
  117. my %params;
  118. while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
  119. $params{$1}=$2;
  120. }
  121. return $plugins{preprocess}{$command}->(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. if ($config{hyperestraier}) {
  178. $template->param(hyperestraierurl => cgiurl());
  179. }
  180. $template->param(
  181. title => $title,
  182. wikiname => $config{wikiname},
  183. parentlinks => [parentlinks($page)],
  184. content => $content,
  185. backlinks => [backlinks($page)],
  186. discussionlink => htmllink($page, "Discussion", 1, 1),
  187. mtime => scalar(gmtime($mtime)),
  188. styleurl => styleurl($page),
  189. );
  190. return $template->output;
  191. } #}}}
  192. sub check_overwrite ($$) { #{{{
  193. # Important security check. Make sure to call this before saving
  194. # any files to the source directory.
  195. my $dest=shift;
  196. my $src=shift;
  197. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  198. error("$dest already exists and was rendered from ".
  199. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  200. %renderedfiles)).
  201. ", before, so not rendering from $src");
  202. }
  203. } #}}}
  204. sub mtime ($) { #{{{
  205. my $file=shift;
  206. return (stat($file))[9];
  207. } #}}}
  208. sub findlinks ($$) { #{{{
  209. my $content=shift;
  210. my $page=shift;
  211. my @links;
  212. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  213. push @links, titlepage($2);
  214. }
  215. # Discussion links are a special case since they're not in the text
  216. # of the page, but on its template.
  217. return @links, "$page/discussion";
  218. } #}}}
  219. sub render ($) { #{{{
  220. my $file=shift;
  221. my $type=pagetype($file);
  222. my $srcfile=srcfile($file);
  223. if ($type ne 'unknown') {
  224. my $content=readfile($srcfile);
  225. my $page=pagename($file);
  226. $links{$page}=[findlinks($content, $page)];
  227. delete $depends{$page};
  228. $content=linkify($content, $page);
  229. $content=preprocess($page, $content);
  230. $content=htmlize($type, $content);
  231. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  232. writefile(htmlpage($page), $config{destdir},
  233. genpage($content, $page, mtime($srcfile)));
  234. $oldpagemtime{$page}=time;
  235. $renderedfiles{$page}=htmlpage($page);
  236. }
  237. else {
  238. my $content=readfile($srcfile, 1);
  239. $links{$file}=[];
  240. delete $depends{$file};
  241. check_overwrite("$config{destdir}/$file", $file);
  242. writefile($file, $config{destdir}, $content, 1);
  243. $oldpagemtime{$file}=time;
  244. $renderedfiles{$file}=$file;
  245. }
  246. } #}}}
  247. sub prune ($) { #{{{
  248. my $file=shift;
  249. unlink($file);
  250. my $dir=dirname($file);
  251. while (rmdir($dir)) {
  252. $dir=dirname($dir);
  253. }
  254. } #}}}
  255. sub estcfg () { #{{{
  256. my $estdir="$config{wikistatedir}/hyperestraier";
  257. my $cgi=basename($config{cgiurl});
  258. $cgi=~s/\..*$//;
  259. open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
  260. error("write $estdir/$cgi.tmpl: $!");
  261. print TEMPLATE misctemplate("search",
  262. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
  263. close TEMPLATE;
  264. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  265. error("write $estdir/$cgi.conf: $!");
  266. my $template=HTML::Template->new(
  267. filename => "$config{templatedir}/estseek.conf"
  268. );
  269. eval q{use Cwd 'abs_path'};
  270. $template->param(
  271. index => $estdir,
  272. tmplfile => "$estdir/$cgi.tmpl",
  273. destdir => abs_path($config{destdir}),
  274. url => $config{url},
  275. );
  276. print TEMPLATE $template->output;
  277. close TEMPLATE;
  278. $cgi="$estdir/".basename($config{cgiurl});
  279. unlink($cgi);
  280. symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
  281. error("symlink $cgi: $!");
  282. } # }}}
  283. sub estcmd ($;@) { #{{{
  284. my @params=split(' ', shift);
  285. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  286. if (@_) {
  287. push @params, "-";
  288. }
  289. my $pid=open(CHILD, "|-");
  290. if ($pid) {
  291. # parent
  292. foreach (@_) {
  293. print CHILD "$_\n";
  294. }
  295. close(CHILD) || error("estcmd @params exited nonzero: $?");
  296. }
  297. else {
  298. # child
  299. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  300. exec("estcmd", @params) || error("can't run estcmd");
  301. }
  302. } #}}}
  303. sub refresh () { #{{{
  304. # find existing pages
  305. my %exists;
  306. my @files;
  307. eval q{use File::Find};
  308. find({
  309. no_chdir => 1,
  310. wanted => sub {
  311. if (/$config{wiki_file_prune_regexp}/) {
  312. $File::Find::prune=1;
  313. }
  314. elsif (! -d $_ && ! -l $_) {
  315. my ($f)=/$config{wiki_file_regexp}/; # untaint
  316. if (! defined $f) {
  317. warn("skipping bad filename $_\n");
  318. }
  319. else {
  320. $f=~s/^\Q$config{srcdir}\E\/?//;
  321. push @files, $f;
  322. $exists{pagename($f)}=1;
  323. }
  324. }
  325. },
  326. }, $config{srcdir});
  327. find({
  328. no_chdir => 1,
  329. wanted => sub {
  330. if (/$config{wiki_file_prune_regexp}/) {
  331. $File::Find::prune=1;
  332. }
  333. elsif (! -d $_ && ! -l $_) {
  334. my ($f)=/$config{wiki_file_regexp}/; # untaint
  335. if (! defined $f) {
  336. warn("skipping bad filename $_\n");
  337. }
  338. else {
  339. # Don't add files that are in the
  340. # srcdir.
  341. $f=~s/^\Q$config{underlaydir}\E\/?//;
  342. if (! -e "$config{srcdir}/$f" &&
  343. ! -l "$config{srcdir}/$f") {
  344. push @files, $f;
  345. $exists{pagename($f)}=1;
  346. }
  347. }
  348. }
  349. },
  350. }, $config{underlaydir});
  351. my %rendered;
  352. # check for added or removed pages
  353. my @add;
  354. foreach my $file (@files) {
  355. my $page=pagename($file);
  356. if (! $oldpagemtime{$page}) {
  357. debug("new page $page") unless exists $pagectime{$page};
  358. push @add, $file;
  359. $links{$page}=[];
  360. $pagesources{$page}=$file;
  361. $pagectime{$page}=mtime(srcfile($file))
  362. unless exists $pagectime{$page};
  363. }
  364. }
  365. my @del;
  366. foreach my $page (keys %oldpagemtime) {
  367. if (! $exists{$page}) {
  368. debug("removing old page $page");
  369. push @del, $pagesources{$page};
  370. prune($config{destdir}."/".$renderedfiles{$page});
  371. delete $renderedfiles{$page};
  372. $oldpagemtime{$page}=0;
  373. delete $pagesources{$page};
  374. }
  375. }
  376. # render any updated files
  377. foreach my $file (@files) {
  378. my $page=pagename($file);
  379. if (! exists $oldpagemtime{$page} ||
  380. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  381. debug("rendering changed file $file");
  382. render($file);
  383. $rendered{$file}=1;
  384. }
  385. }
  386. # if any files were added or removed, check to see if each page
  387. # needs an update due to linking to them or inlining them.
  388. # TODO: inefficient; pages may get rendered above and again here;
  389. # problem is the bestlink may have changed and we won't know until
  390. # now
  391. if (@add || @del) {
  392. FILE: foreach my $file (@files) {
  393. my $page=pagename($file);
  394. foreach my $f (@add, @del) {
  395. my $p=pagename($f);
  396. foreach my $link (@{$links{$page}}) {
  397. if (bestlink($page, $link) eq $p) {
  398. debug("rendering $file, which links to $p");
  399. render($file);
  400. $rendered{$file}=1;
  401. next FILE;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. # Handle backlinks; if a page has added/removed links, update the
  408. # pages it links to. Also handles rebuilding dependat pages.
  409. # TODO: inefficient; pages may get rendered above and again here;
  410. # problem is the backlinks could be wrong in the first pass render
  411. # above
  412. if (%rendered || @del) {
  413. foreach my $f (@files) {
  414. my $p=pagename($f);
  415. if (exists $depends{$p}) {
  416. foreach my $file (keys %rendered, @del) {
  417. next if $f eq $file;
  418. my $page=pagename($file);
  419. if (globlist_match($page, $depends{$p})) {
  420. debug("rendering $f, which depends on $page");
  421. render($f);
  422. $rendered{$f}=1;
  423. last;
  424. }
  425. }
  426. }
  427. }
  428. my %linkchanged;
  429. foreach my $file (keys %rendered, @del) {
  430. my $page=pagename($file);
  431. if (exists $links{$page}) {
  432. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  433. if (length $link &&
  434. (! exists $oldlinks{$page} ||
  435. ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
  436. $linkchanged{$link}=1;
  437. }
  438. }
  439. }
  440. if (exists $oldlinks{$page}) {
  441. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  442. if (length $link &&
  443. (! exists $links{$page} ||
  444. ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
  445. $linkchanged{$link}=1;
  446. }
  447. }
  448. }
  449. }
  450. foreach my $link (keys %linkchanged) {
  451. my $linkfile=$pagesources{$link};
  452. if (defined $linkfile) {
  453. debug("rendering $linkfile, to update its backlinks");
  454. render($linkfile);
  455. $rendered{$linkfile}=1;
  456. }
  457. }
  458. }
  459. if ($config{hyperestraier} && (%rendered || @del)) {
  460. debug("updating hyperestraier search index");
  461. if (%rendered) {
  462. estcmd("gather -cm -bc -cl -sd",
  463. map { $config{destdir}."/".$renderedfiles{pagename($_)} }
  464. keys %rendered);
  465. }
  466. if (@del) {
  467. estcmd("purge -cl");
  468. }
  469. debug("generating hyperestraier cgi config");
  470. estcfg();
  471. }
  472. } #}}}
  473. 1