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