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