summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: d0d28e80276db70650a196323fea9054d79967cc (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. sub linkify ($$) { #{{{
  7. my $content=shift;
  8. my $page=shift;
  9. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  10. $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
  11. : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
  12. }eg;
  13. return $content;
  14. } #}}}
  15. my $_scrubber;
  16. sub scrubber { #{{{
  17. return $_scrubber if defined $_scrubber;
  18. eval q{use HTML::Scrubber};
  19. # Lists based on http://feedparser.org/docs/html-sanitization.html
  20. $_scrubber = HTML::Scrubber->new(
  21. allow => [qw{
  22. a abbr acronym address area b big blockquote br
  23. button caption center cite code col colgroup dd del
  24. dfn dir div dl dt em fieldset font form h1 h2 h3 h4
  25. h5 h6 hr i img input ins kbd label legend li map
  26. menu ol optgroup option p pre q s samp select small
  27. span strike strong sub sup table tbody td textarea
  28. tfoot th thead tr tt u ul var
  29. }],
  30. default => [undef, { map { $_ => 1 } qw{
  31. abbr accept accept-charset accesskey action
  32. align alt axis border cellpadding cellspacing
  33. char charoff charset checked cite class
  34. clear cols colspan color compact coords
  35. datetime dir disabled enctype for frame
  36. headers height href hreflang hspace id ismap
  37. label lang longdesc maxlength media method
  38. multiple name nohref noshade nowrap prompt
  39. readonly rel rev rows rowspan rules scope
  40. selected shape size span src start summary
  41. tabindex target title type usemap valign
  42. value vspace width
  43. }}],
  44. );
  45. return $_scrubber;
  46. } # }}}
  47. sub htmlize ($$) { #{{{
  48. my $type=shift;
  49. my $content=shift;
  50. if (! $INC{"/usr/bin/markdown"}) {
  51. no warnings 'once';
  52. $blosxom::version="is a proper perl module too much to ask?";
  53. use warnings 'all';
  54. do "/usr/bin/markdown";
  55. }
  56. if ($type eq '.mdwn') {
  57. $content=Markdown::Markdown($content);
  58. }
  59. else {
  60. error("htmlization of $type not supported");
  61. }
  62. if ($config{sanitize}) {
  63. $content=scrubber()->scrub($content);
  64. }
  65. return $content;
  66. } #}}}
  67. sub backlinks ($) { #{{{
  68. my $page=shift;
  69. my @links;
  70. foreach my $p (keys %links) {
  71. next if bestlink($page, $p) eq $page;
  72. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  73. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  74. # Trim common dir prefixes from both pages.
  75. my $p_trimmed=$p;
  76. my $page_trimmed=$page;
  77. my $dir;
  78. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  79. defined $dir &&
  80. $p_trimmed=~s/^\Q$dir\E// &&
  81. $page_trimmed=~s/^\Q$dir\E//;
  82. push @links, { url => $href, page => $p_trimmed };
  83. }
  84. }
  85. return sort { $a->{page} cmp $b->{page} } @links;
  86. } #}}}
  87. sub parentlinks ($) { #{{{
  88. my $page=shift;
  89. my @ret;
  90. my $pagelink="";
  91. my $path="";
  92. my $skip=1;
  93. foreach my $dir (reverse split("/", $page)) {
  94. if (! $skip) {
  95. $path.="../";
  96. unshift @ret, { url => "$path$dir.html", page => $dir };
  97. }
  98. else {
  99. $skip=0;
  100. }
  101. }
  102. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  103. return @ret;
  104. } #}}}
  105. sub rsspage ($) { #{{{
  106. my $page=shift;
  107. return $page.".rss";
  108. } #}}}
  109. sub preprocess ($$) { #{{{
  110. my $page=shift;
  111. my $content=shift;
  112. my %commands=(inline => \&preprocess_inline);
  113. my $handle=sub {
  114. my $escape=shift;
  115. my $command=shift;
  116. my $params=shift;
  117. if (length $escape) {
  118. "[[$command $params]]";
  119. }
  120. elsif (exists $commands{$command}) {
  121. my %params;
  122. while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
  123. $params{$1}=$2;
  124. }
  125. $commands{$command}->($page, %params);
  126. }
  127. else {
  128. "[[bad directive $command]]";
  129. }
  130. };
  131. $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
  132. return $content;
  133. } #}}}
  134. sub blog_list ($$) { #{{{
  135. my $globlist=shift;
  136. my $maxitems=shift;
  137. my @list;
  138. foreach my $page (keys %pagesources) {
  139. if (globlist_match($page, $globlist)) {
  140. push @list, $page;
  141. }
  142. }
  143. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  144. return @list if ! $maxitems || @list <= $maxitems;
  145. return @list[0..$maxitems - 1];
  146. } #}}}
  147. sub get_inline_content ($$) { #{{{
  148. my $parentpage=shift;
  149. my $page=shift;
  150. my $file=$pagesources{$page};
  151. my $type=pagetype($file);
  152. if ($type ne 'unknown') {
  153. return htmlize($type, linkify(readfile(srcfile($file)), $parentpage));
  154. }
  155. else {
  156. return "";
  157. }
  158. } #}}}
  159. sub preprocess_inline ($@) { #{{{
  160. my $parentpage=shift;
  161. my %params=@_;
  162. if (! exists $params{pages}) {
  163. return "";
  164. }
  165. if (! exists $params{archive}) {
  166. $params{archive}="no";
  167. }
  168. if (! exists $params{show} && $params{archive} eq "no") {
  169. $params{show}=10;
  170. }
  171. $inlinepages{$parentpage}=$params{pages};
  172. my $ret="";
  173. if (exists $params{rootpage}) {
  174. my $formtemplate=HTML::Template->new(blind_cache => 1,
  175. filename => "$config{templatedir}/blogpost.tmpl");
  176. $formtemplate->param(cgiurl => $config{cgiurl});
  177. $formtemplate->param(rootpage => $params{rootpage});
  178. my $form=$formtemplate->output;
  179. $ret.=$form;
  180. }
  181. my $template=HTML::Template->new(blind_cache => 1,
  182. filename => (($params{archive} eq "no")
  183. ? "$config{templatedir}/inlinepage.tmpl"
  184. : "$config{templatedir}/inlinepagetitle.tmpl"));
  185. my @pages;
  186. foreach my $page (blog_list($params{pages}, $params{show})) {
  187. next if $page eq $parentpage;
  188. push @pages, $page;
  189. $template->param(pagelink => htmllink($parentpage, $page));
  190. $template->param(content => get_inline_content($parentpage, $page))
  191. if $params{archive} eq "no";
  192. $template->param(ctime => scalar(gmtime($pagectime{$page})));
  193. $ret.=$template->output;
  194. }
  195. # TODO: should really add this to renderedfiles and call
  196. # check_overwrite, but currently renderedfiles
  197. # only supports listing one file per page.
  198. if ($config{rss}) {
  199. writefile(rsspage($parentpage), $config{destdir},
  200. genrss($parentpage, @pages));
  201. }
  202. return $ret;
  203. } #}}}
  204. sub genpage ($$$) { #{{{
  205. my $content=shift;
  206. my $page=shift;
  207. my $mtime=shift;
  208. my $title=pagetitle(basename($page));
  209. my $template=HTML::Template->new(blind_cache => 1,
  210. filename => "$config{templatedir}/page.tmpl");
  211. if (length $config{cgiurl}) {
  212. $template->param(editurl => cgiurl(do => "edit", page => $page));
  213. $template->param(prefsurl => cgiurl(do => "prefs"));
  214. if ($config{rcs}) {
  215. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  216. }
  217. }
  218. if (length $config{historyurl}) {
  219. my $u=$config{historyurl};
  220. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  221. $template->param(historyurl => $u);
  222. }
  223. if ($config{hyperestraier}) {
  224. $template->param(hyperestraierurl => cgiurl());
  225. }
  226. if ($config{rss} && $inlinepages{$page}) {
  227. $template->param(rssurl => rsspage(basename($page)));
  228. }
  229. $template->param(
  230. title => $title,
  231. wikiname => $config{wikiname},
  232. parentlinks => [parentlinks($page)],
  233. content => $content,
  234. backlinks => [backlinks($page)],
  235. discussionlink => htmllink($page, "Discussion", 1, 1),
  236. mtime => scalar(gmtime($mtime)),
  237. styleurl => styleurl($page),
  238. );
  239. return $template->output;
  240. } #}}}
  241. sub date_822 ($) { #{{{
  242. my $time=shift;
  243. eval q{use POSIX};
  244. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  245. } #}}}
  246. sub absolute_urls ($$) { #{{{
  247. # sucky sub because rss sucks
  248. my $content=shift;
  249. my $url=shift;
  250. $url=~s/[^\/]+$//;
  251. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  252. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  253. return $content;
  254. } #}}}
  255. sub genrss ($@) { #{{{
  256. my $page=shift;
  257. my @pages=@_;
  258. my $url="$config{url}/".htmlpage($page);
  259. my $template=HTML::Template->new(blind_cache => 1,
  260. filename => "$config{templatedir}/rsspage.tmpl");
  261. my @items;
  262. foreach my $p (@pages) {
  263. push @items, {
  264. itemtitle => pagetitle(basename($p)),
  265. itemurl => "$config{url}/$renderedfiles{$p}",
  266. itempubdate => date_822($pagectime{$p}),
  267. itemcontent => absolute_urls(get_inline_content($page, $p), $url),
  268. } if exists $renderedfiles{$p};
  269. }
  270. $template->param(
  271. title => $config{wikiname},
  272. pageurl => $url,
  273. items => \@items,
  274. );
  275. return $template->output;
  276. } #}}}
  277. sub check_overwrite ($$) { #{{{
  278. # Important security check. Make sure to call this before saving
  279. # any files to the source directory.
  280. my $dest=shift;
  281. my $src=shift;
  282. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  283. error("$dest already exists and was rendered from ".
  284. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  285. %renderedfiles)).
  286. ", before, so not rendering from $src");
  287. }
  288. } #}}}
  289. sub mtime ($) { #{{{
  290. my $file=shift;
  291. return (stat($file))[9];
  292. } #}}}
  293. sub findlinks ($$) { #{{{
  294. my $content=shift;
  295. my $page=shift;
  296. my @links;
  297. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  298. push @links, titlepage($2);
  299. }
  300. # Discussion links are a special case since they're not in the text
  301. # of the page, but on its template.
  302. return @links, "$page/discussion";
  303. } #}}}
  304. sub render ($) { #{{{
  305. my $file=shift;
  306. my $type=pagetype($file);
  307. my $srcfile=srcfile($file);
  308. if ($type ne 'unknown') {
  309. my $content=readfile($srcfile);
  310. my $page=pagename($file);
  311. $links{$page}=[findlinks($content, $page)];
  312. delete $inlinepages{$page};
  313. $content=linkify($content, $page);
  314. $content=preprocess($page, $content);
  315. $content=htmlize($type, $content);
  316. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  317. writefile(htmlpage($page), $config{destdir},
  318. genpage($content, $page, mtime($srcfile)));
  319. $oldpagemtime{$page}=time;
  320. $renderedfiles{$page}=htmlpage($page);
  321. }
  322. else {
  323. my $content=readfile($srcfile, 1);
  324. $links{$file}=[];
  325. check_overwrite("$config{destdir}/$file", $file);
  326. writefile($file, $config{destdir}, $content, 1);
  327. $oldpagemtime{$file}=time;
  328. $renderedfiles{$file}=$file;
  329. }
  330. } #}}}
  331. sub prune ($) { #{{{
  332. my $file=shift;
  333. unlink($file);
  334. my $dir=dirname($file);
  335. while (rmdir($dir)) {
  336. $dir=dirname($dir);
  337. }
  338. } #}}}
  339. sub estcfg () { #{{{
  340. my $estdir="$config{wikistatedir}/hyperestraier";
  341. my $cgi=basename($config{cgiurl});
  342. $cgi=~s/\..*$//;
  343. open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
  344. error("write $estdir/$cgi.tmpl: $!");
  345. print TEMPLATE misctemplate("search",
  346. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
  347. close TEMPLATE;
  348. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  349. error("write $estdir/$cgi.conf: $!");
  350. my $template=HTML::Template->new(
  351. filename => "$config{templatedir}/estseek.conf"
  352. );
  353. eval q{use Cwd 'abs_path'};
  354. $template->param(
  355. index => $estdir,
  356. tmplfile => "$estdir/$cgi.tmpl",
  357. destdir => abs_path($config{destdir}),
  358. url => $config{url},
  359. );
  360. print TEMPLATE $template->output;
  361. close TEMPLATE;
  362. $cgi="$estdir/".basename($config{cgiurl});
  363. unlink($cgi);
  364. symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
  365. error("symlink $cgi: $!");
  366. } # }}}
  367. sub estcmd ($;@) { #{{{
  368. my @params=split(' ', shift);
  369. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  370. if (@_) {
  371. push @params, "-";
  372. }
  373. my $pid=open(CHILD, "|-");
  374. if ($pid) {
  375. # parent
  376. foreach (@_) {
  377. print CHILD "$_\n";
  378. }
  379. close(CHILD) || error("estcmd @params exited nonzero: $?");
  380. }
  381. else {
  382. # child
  383. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  384. exec("estcmd", @params) || error("can't run estcmd");
  385. }
  386. } #}}}
  387. sub refresh () { #{{{
  388. # find existing pages
  389. my %exists;
  390. my @files;
  391. eval q{use File::Find};
  392. find({
  393. no_chdir => 1,
  394. wanted => sub {
  395. if (/$config{wiki_file_prune_regexp}/) {
  396. $File::Find::prune=1;
  397. }
  398. elsif (! -d $_ && ! -l $_) {
  399. my ($f)=/$config{wiki_file_regexp}/; # untaint
  400. if (! defined $f) {
  401. warn("skipping bad filename $_\n");
  402. }
  403. else {
  404. $f=~s/^\Q$config{srcdir}\E\/?//;
  405. push @files, $f;
  406. $exists{pagename($f)}=1;
  407. }
  408. }
  409. },
  410. }, $config{srcdir});
  411. find({
  412. no_chdir => 1,
  413. wanted => sub {
  414. if (/$config{wiki_file_prune_regexp}/) {
  415. $File::Find::prune=1;
  416. }
  417. elsif (! -d $_ && ! -l $_) {
  418. my ($f)=/$config{wiki_file_regexp}/; # untaint
  419. if (! defined $f) {
  420. warn("skipping bad filename $_\n");
  421. }
  422. else {
  423. # Don't add files that are in the
  424. # srcdir.
  425. $f=~s/^\Q$config{underlaydir}\E\/?//;
  426. if (! -e "$config{srcdir}/$f" &&
  427. ! -l "$config{srcdir}/$f") {
  428. push @files, $f;
  429. $exists{pagename($f)}=1;
  430. }
  431. }
  432. }
  433. },
  434. }, $config{underlaydir});
  435. my %rendered;
  436. # check for added or removed pages
  437. my @add;
  438. foreach my $file (@files) {
  439. my $page=pagename($file);
  440. if (! $oldpagemtime{$page}) {
  441. debug("new page $page") unless exists $pagectime{$page};
  442. push @add, $file;
  443. $links{$page}=[];
  444. $pagesources{$page}=$file;
  445. $pagectime{$page}=mtime(srcfile($file))
  446. unless exists $pagectime{$page};
  447. }
  448. }
  449. my @del;
  450. foreach my $page (keys %oldpagemtime) {
  451. if (! $exists{$page}) {
  452. debug("removing old page $page");
  453. push @del, $pagesources{$page};
  454. prune($config{destdir}."/".$renderedfiles{$page});
  455. delete $renderedfiles{$page};
  456. $oldpagemtime{$page}=0;
  457. delete $pagesources{$page};
  458. }
  459. }
  460. # render any updated files
  461. foreach my $file (@files) {
  462. my $page=pagename($file);
  463. if (! exists $oldpagemtime{$page} ||
  464. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  465. debug("rendering changed file $file");
  466. render($file);
  467. $rendered{$file}=1;
  468. }
  469. }
  470. # if any files were added or removed, check to see if each page
  471. # needs an update due to linking to them or inlining them.
  472. # TODO: inefficient; pages may get rendered above and again here;
  473. # problem is the bestlink may have changed and we won't know until
  474. # now
  475. if (@add || @del) {
  476. FILE: foreach my $file (@files) {
  477. my $page=pagename($file);
  478. foreach my $f (@add, @del) {
  479. my $p=pagename($f);
  480. foreach my $link (@{$links{$page}}) {
  481. if (bestlink($page, $link) eq $p) {
  482. debug("rendering $file, which links to $p");
  483. render($file);
  484. $rendered{$file}=1;
  485. next FILE;
  486. }
  487. }
  488. }
  489. }
  490. }
  491. # Handle backlinks; if a page has added/removed links, update the
  492. # pages it links to. Also handle inlining here.
  493. # TODO: inefficient; pages may get rendered above and again here;
  494. # problem is the backlinks could be wrong in the first pass render
  495. # above
  496. if (%rendered || @del) {
  497. foreach my $f (@files) {
  498. my $p=pagename($f);
  499. if (exists $inlinepages{$p}) {
  500. foreach my $file (keys %rendered, @del) {
  501. my $page=pagename($file);
  502. if (globlist_match($page, $inlinepages{$p})) {
  503. debug("rendering $f, which inlines $page");
  504. render($f);
  505. $rendered{$f}=1;
  506. last;
  507. }
  508. }
  509. }
  510. }
  511. my %linkchanged;
  512. foreach my $file (keys %rendered, @del) {
  513. my $page=pagename($file);
  514. if (exists $links{$page}) {
  515. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  516. if (length $link &&
  517. ! exists $oldlinks{$page} ||
  518. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  519. $linkchanged{$link}=1;
  520. }
  521. }
  522. }
  523. if (exists $oldlinks{$page}) {
  524. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  525. if (length $link &&
  526. ! exists $links{$page} ||
  527. ! grep { $_ eq $link } @{$links{$page}}) {
  528. $linkchanged{$link}=1;
  529. }
  530. }
  531. }
  532. }
  533. foreach my $link (keys %linkchanged) {
  534. my $linkfile=$pagesources{$link};
  535. if (defined $linkfile) {
  536. debug("rendering $linkfile, to update its backlinks");
  537. render($linkfile);
  538. $rendered{$linkfile}=1;
  539. }
  540. }
  541. }
  542. if ($config{hyperestraier} && (%rendered || @del)) {
  543. debug("updating hyperestraier search index");
  544. if (%rendered) {
  545. estcmd("gather -cm -bc -cl -sd",
  546. map { $config{destdir}."/".$renderedfiles{pagename($_)} }
  547. keys %rendered);
  548. }
  549. if (@del) {
  550. estcmd("purge -cl");
  551. }
  552. debug("generating hyperestraier cgi config");
  553. estcfg();
  554. }
  555. } #}}}
  556. 1