summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: f9da33e300e6837cf9e1224b332dbb4e0383b22c (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. return "[[$command $params]]";
  119. }
  120. elsif (exists $commands{$command}) {
  121. my %params;
  122. while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
  123. $params{$1}=$2;
  124. }
  125. return $commands{$command}->($page, %params);
  126. }
  127. else {
  128. return "[[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. if (! exists $depends{$parentpage}) {
  172. $depends{$parentpage}=$params{pages};
  173. }
  174. else {
  175. $depends{$parentpage}.=" ".$params{pages};
  176. }
  177. my $ret="";
  178. if (exists $params{rootpage}) {
  179. # Add a blog post form, with a rss link button.
  180. my $formtemplate=HTML::Template->new(blind_cache => 1,
  181. filename => "$config{templatedir}/blogpost.tmpl");
  182. $formtemplate->param(cgiurl => $config{cgiurl});
  183. $formtemplate->param(rootpage => $params{rootpage});
  184. if ($config{rss}) {
  185. $formtemplate->param(rssurl => rsspage(basename($parentpage)));
  186. }
  187. $ret.=$formtemplate->output;
  188. }
  189. elsif ($config{rss}) {
  190. # Add a rss link button.
  191. my $linktemplate=HTML::Template->new(blind_cache => 1,
  192. filename => "$config{templatedir}/rsslink.tmpl");
  193. $linktemplate->param(rssurl => rsspage(basename($parentpage)));
  194. $ret.=$linktemplate->output;
  195. }
  196. my $template=HTML::Template->new(blind_cache => 1,
  197. filename => (($params{archive} eq "no")
  198. ? "$config{templatedir}/inlinepage.tmpl"
  199. : "$config{templatedir}/inlinepagetitle.tmpl"));
  200. my @pages;
  201. foreach my $page (blog_list($params{pages}, $params{show})) {
  202. next if $page eq $parentpage;
  203. push @pages, $page;
  204. $template->param(pagelink => htmllink($parentpage, $page));
  205. $template->param(content => get_inline_content($parentpage, $page))
  206. if $params{archive} eq "no";
  207. $template->param(ctime => scalar(gmtime($pagectime{$page})));
  208. $ret.=$template->output;
  209. }
  210. # TODO: should really add this to renderedfiles and call
  211. # check_overwrite, but currently renderedfiles
  212. # only supports listing one file per page.
  213. if ($config{rss}) {
  214. writefile(rsspage($parentpage), $config{destdir},
  215. genrss($parentpage, @pages));
  216. }
  217. return $ret;
  218. } #}}}
  219. sub genpage ($$$) { #{{{
  220. my $content=shift;
  221. my $page=shift;
  222. my $mtime=shift;
  223. my $title=pagetitle(basename($page));
  224. my $template=HTML::Template->new(blind_cache => 1,
  225. filename => "$config{templatedir}/page.tmpl");
  226. if (length $config{cgiurl}) {
  227. $template->param(editurl => cgiurl(do => "edit", page => $page));
  228. $template->param(prefsurl => cgiurl(do => "prefs"));
  229. if ($config{rcs}) {
  230. $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
  231. }
  232. }
  233. if (length $config{historyurl}) {
  234. my $u=$config{historyurl};
  235. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  236. $template->param(historyurl => $u);
  237. }
  238. if ($config{hyperestraier}) {
  239. $template->param(hyperestraierurl => cgiurl());
  240. }
  241. $template->param(
  242. title => $title,
  243. wikiname => $config{wikiname},
  244. parentlinks => [parentlinks($page)],
  245. content => $content,
  246. backlinks => [backlinks($page)],
  247. discussionlink => htmllink($page, "Discussion", 1, 1),
  248. mtime => scalar(gmtime($mtime)),
  249. styleurl => styleurl($page),
  250. );
  251. return $template->output;
  252. } #}}}
  253. sub date_822 ($) { #{{{
  254. my $time=shift;
  255. eval q{use POSIX};
  256. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  257. } #}}}
  258. sub absolute_urls ($$) { #{{{
  259. # sucky sub because rss sucks
  260. my $content=shift;
  261. my $url=shift;
  262. $url=~s/[^\/]+$//;
  263. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  264. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  265. return $content;
  266. } #}}}
  267. sub genrss ($@) { #{{{
  268. my $page=shift;
  269. my @pages=@_;
  270. my $url="$config{url}/".htmlpage($page);
  271. my $template=HTML::Template->new(blind_cache => 1,
  272. filename => "$config{templatedir}/rsspage.tmpl");
  273. my @items;
  274. foreach my $p (@pages) {
  275. push @items, {
  276. itemtitle => pagetitle(basename($p)),
  277. itemurl => "$config{url}/$renderedfiles{$p}",
  278. itempubdate => date_822($pagectime{$p}),
  279. itemcontent => absolute_urls(get_inline_content($page, $p), $url),
  280. } if exists $renderedfiles{$p};
  281. }
  282. $template->param(
  283. title => $config{wikiname},
  284. pageurl => $url,
  285. items => \@items,
  286. );
  287. return $template->output;
  288. } #}}}
  289. sub check_overwrite ($$) { #{{{
  290. # Important security check. Make sure to call this before saving
  291. # any files to the source directory.
  292. my $dest=shift;
  293. my $src=shift;
  294. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  295. error("$dest already exists and was rendered from ".
  296. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  297. %renderedfiles)).
  298. ", before, so not rendering from $src");
  299. }
  300. } #}}}
  301. sub mtime ($) { #{{{
  302. my $file=shift;
  303. return (stat($file))[9];
  304. } #}}}
  305. sub findlinks ($$) { #{{{
  306. my $content=shift;
  307. my $page=shift;
  308. my @links;
  309. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  310. push @links, titlepage($2);
  311. }
  312. # Discussion links are a special case since they're not in the text
  313. # of the page, but on its template.
  314. return @links, "$page/discussion";
  315. } #}}}
  316. sub render ($) { #{{{
  317. my $file=shift;
  318. my $type=pagetype($file);
  319. my $srcfile=srcfile($file);
  320. if ($type ne 'unknown') {
  321. my $content=readfile($srcfile);
  322. my $page=pagename($file);
  323. $links{$page}=[findlinks($content, $page)];
  324. delete $depends{$page};
  325. $content=linkify($content, $page);
  326. $content=preprocess($page, $content);
  327. $content=htmlize($type, $content);
  328. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  329. writefile(htmlpage($page), $config{destdir},
  330. genpage($content, $page, mtime($srcfile)));
  331. $oldpagemtime{$page}=time;
  332. $renderedfiles{$page}=htmlpage($page);
  333. }
  334. else {
  335. my $content=readfile($srcfile, 1);
  336. $links{$file}=[];
  337. check_overwrite("$config{destdir}/$file", $file);
  338. writefile($file, $config{destdir}, $content, 1);
  339. $oldpagemtime{$file}=time;
  340. $renderedfiles{$file}=$file;
  341. }
  342. } #}}}
  343. sub prune ($) { #{{{
  344. my $file=shift;
  345. unlink($file);
  346. my $dir=dirname($file);
  347. while (rmdir($dir)) {
  348. $dir=dirname($dir);
  349. }
  350. } #}}}
  351. sub estcfg () { #{{{
  352. my $estdir="$config{wikistatedir}/hyperestraier";
  353. my $cgi=basename($config{cgiurl});
  354. $cgi=~s/\..*$//;
  355. open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
  356. error("write $estdir/$cgi.tmpl: $!");
  357. print TEMPLATE misctemplate("search",
  358. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
  359. close TEMPLATE;
  360. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  361. error("write $estdir/$cgi.conf: $!");
  362. my $template=HTML::Template->new(
  363. filename => "$config{templatedir}/estseek.conf"
  364. );
  365. eval q{use Cwd 'abs_path'};
  366. $template->param(
  367. index => $estdir,
  368. tmplfile => "$estdir/$cgi.tmpl",
  369. destdir => abs_path($config{destdir}),
  370. url => $config{url},
  371. );
  372. print TEMPLATE $template->output;
  373. close TEMPLATE;
  374. $cgi="$estdir/".basename($config{cgiurl});
  375. unlink($cgi);
  376. symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
  377. error("symlink $cgi: $!");
  378. } # }}}
  379. sub estcmd ($;@) { #{{{
  380. my @params=split(' ', shift);
  381. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  382. if (@_) {
  383. push @params, "-";
  384. }
  385. my $pid=open(CHILD, "|-");
  386. if ($pid) {
  387. # parent
  388. foreach (@_) {
  389. print CHILD "$_\n";
  390. }
  391. close(CHILD) || error("estcmd @params exited nonzero: $?");
  392. }
  393. else {
  394. # child
  395. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  396. exec("estcmd", @params) || error("can't run estcmd");
  397. }
  398. } #}}}
  399. sub refresh () { #{{{
  400. # find existing pages
  401. my %exists;
  402. my @files;
  403. eval q{use File::Find};
  404. find({
  405. no_chdir => 1,
  406. wanted => sub {
  407. if (/$config{wiki_file_prune_regexp}/) {
  408. $File::Find::prune=1;
  409. }
  410. elsif (! -d $_ && ! -l $_) {
  411. my ($f)=/$config{wiki_file_regexp}/; # untaint
  412. if (! defined $f) {
  413. warn("skipping bad filename $_\n");
  414. }
  415. else {
  416. $f=~s/^\Q$config{srcdir}\E\/?//;
  417. push @files, $f;
  418. $exists{pagename($f)}=1;
  419. }
  420. }
  421. },
  422. }, $config{srcdir});
  423. find({
  424. no_chdir => 1,
  425. wanted => sub {
  426. if (/$config{wiki_file_prune_regexp}/) {
  427. $File::Find::prune=1;
  428. }
  429. elsif (! -d $_ && ! -l $_) {
  430. my ($f)=/$config{wiki_file_regexp}/; # untaint
  431. if (! defined $f) {
  432. warn("skipping bad filename $_\n");
  433. }
  434. else {
  435. # Don't add files that are in the
  436. # srcdir.
  437. $f=~s/^\Q$config{underlaydir}\E\/?//;
  438. if (! -e "$config{srcdir}/$f" &&
  439. ! -l "$config{srcdir}/$f") {
  440. push @files, $f;
  441. $exists{pagename($f)}=1;
  442. }
  443. }
  444. }
  445. },
  446. }, $config{underlaydir});
  447. my %rendered;
  448. # check for added or removed pages
  449. my @add;
  450. foreach my $file (@files) {
  451. my $page=pagename($file);
  452. if (! $oldpagemtime{$page}) {
  453. debug("new page $page") unless exists $pagectime{$page};
  454. push @add, $file;
  455. $links{$page}=[];
  456. $pagesources{$page}=$file;
  457. $pagectime{$page}=mtime(srcfile($file))
  458. unless exists $pagectime{$page};
  459. }
  460. }
  461. my @del;
  462. foreach my $page (keys %oldpagemtime) {
  463. if (! $exists{$page}) {
  464. debug("removing old page $page");
  465. push @del, $pagesources{$page};
  466. prune($config{destdir}."/".$renderedfiles{$page});
  467. delete $renderedfiles{$page};
  468. $oldpagemtime{$page}=0;
  469. delete $pagesources{$page};
  470. }
  471. }
  472. # render any updated files
  473. foreach my $file (@files) {
  474. my $page=pagename($file);
  475. if (! exists $oldpagemtime{$page} ||
  476. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  477. debug("rendering changed file $file");
  478. render($file);
  479. $rendered{$file}=1;
  480. }
  481. }
  482. # if any files were added or removed, check to see if each page
  483. # needs an update due to linking to them or inlining them.
  484. # TODO: inefficient; pages may get rendered above and again here;
  485. # problem is the bestlink may have changed and we won't know until
  486. # now
  487. if (@add || @del) {
  488. FILE: foreach my $file (@files) {
  489. my $page=pagename($file);
  490. foreach my $f (@add, @del) {
  491. my $p=pagename($f);
  492. foreach my $link (@{$links{$page}}) {
  493. if (bestlink($page, $link) eq $p) {
  494. debug("rendering $file, which links to $p");
  495. render($file);
  496. $rendered{$file}=1;
  497. next FILE;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. # Handle backlinks; if a page has added/removed links, update the
  504. # pages it links to. Also handles rebuilding dependat pages.
  505. # TODO: inefficient; pages may get rendered above and again here;
  506. # problem is the backlinks could be wrong in the first pass render
  507. # above
  508. if (%rendered || @del) {
  509. foreach my $f (@files) {
  510. my $p=pagename($f);
  511. if (exists $depends{$p}) {
  512. foreach my $file (keys %rendered, @del) {
  513. my $page=pagename($file);
  514. if (globlist_match($page, $depends{$p})) {
  515. debug("rendering $f, which depends on $page");
  516. render($f);
  517. $rendered{$f}=1;
  518. last;
  519. }
  520. }
  521. }
  522. }
  523. my %linkchanged;
  524. foreach my $file (keys %rendered, @del) {
  525. my $page=pagename($file);
  526. if (exists $links{$page}) {
  527. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  528. if (length $link &&
  529. ! exists $oldlinks{$page} ||
  530. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  531. $linkchanged{$link}=1;
  532. }
  533. }
  534. }
  535. if (exists $oldlinks{$page}) {
  536. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  537. if (length $link &&
  538. ! exists $links{$page} ||
  539. ! grep { $_ eq $link } @{$links{$page}}) {
  540. $linkchanged{$link}=1;
  541. }
  542. }
  543. }
  544. }
  545. foreach my $link (keys %linkchanged) {
  546. my $linkfile=$pagesources{$link};
  547. if (defined $linkfile) {
  548. debug("rendering $linkfile, to update its backlinks");
  549. render($linkfile);
  550. $rendered{$linkfile}=1;
  551. }
  552. }
  553. }
  554. if ($config{hyperestraier} && (%rendered || @del)) {
  555. debug("updating hyperestraier search index");
  556. if (%rendered) {
  557. estcmd("gather -cm -bc -cl -sd",
  558. map { $config{destdir}."/".$renderedfiles{pagename($_)} }
  559. keys %rendered);
  560. }
  561. if (@del) {
  562. estcmd("purge -cl");
  563. }
  564. debug("generating hyperestraier cgi config");
  565. estcfg();
  566. }
  567. } #}}}
  568. 1