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