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