summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 3d827d341e45f0c654d4c12aaf0d546ebdb6b8b3 (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{rss} && $inlinepages{$page}) {
  181. $template->param(rssurl => rsspage(basename($page)));
  182. }
  183. $template->param(
  184. title => $title,
  185. wikiname => $config{wikiname},
  186. parentlinks => [parentlinks($page)],
  187. content => $content,
  188. backlinks => [backlinks($page)],
  189. discussionlink => htmllink($page, "Discussion", 1, 1),
  190. mtime => scalar(gmtime($mtime)),
  191. styleurl => styleurl($page),
  192. );
  193. return $template->output;
  194. } #}}}
  195. sub date_822 ($) { #{{{
  196. my $time=shift;
  197. eval q{use POSIX};
  198. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  199. } #}}}
  200. sub absolute_urls ($$) { #{{{
  201. # sucky sub because rss sucks
  202. my $content=shift;
  203. my $url=shift;
  204. $url=~s/[^\/]+$//;
  205. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  206. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  207. return $content;
  208. } #}}}
  209. sub genrss ($$$) { #{{{
  210. my $content=shift;
  211. my $page=shift;
  212. my $mtime=shift;
  213. my $url="$config{url}/".htmlpage($page);
  214. my $template=HTML::Template->new(blind_cache => 1,
  215. filename => "$config{templatedir}/rsspage.tmpl");
  216. my @items;
  217. my $isblog=0;
  218. my $gen_blog=sub {
  219. my $parentpage=shift;
  220. my %params=@_;
  221. if (! exists $params{show}) {
  222. $params{show}=10;
  223. }
  224. if (! exists $params{pages}) {
  225. return "";
  226. }
  227. $isblog=1;
  228. foreach my $page (blog_list($params{pages}, $params{show})) {
  229. next if $page eq $parentpage;
  230. push @items, {
  231. itemtitle => pagetitle(basename($page)),
  232. itemurl => "$config{url}/$renderedfiles{$page}",
  233. itempubdate => date_822($pagectime{$page}),
  234. itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
  235. } if exists $renderedfiles{$page};
  236. }
  237. return "";
  238. };
  239. $content = postprocess($page, $content, inline => $gen_blog);
  240. $template->param(
  241. title => $config{wikiname},
  242. pageurl => $url,
  243. items => \@items,
  244. );
  245. return $template->output;
  246. } #}}}
  247. sub check_overwrite ($$) { #{{{
  248. # Important security check. Make sure to call this before saving
  249. # any files to the source directory.
  250. my $dest=shift;
  251. my $src=shift;
  252. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  253. error("$dest already exists and was rendered from ".
  254. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  255. %renderedfiles)).
  256. ", before, so not rendering from $src");
  257. }
  258. } #}}}
  259. sub mtime ($) { #{{{
  260. my $file=shift;
  261. return (stat($file))[9];
  262. } #}}}
  263. sub findlinks ($$) { #{{{
  264. my $content=shift;
  265. my $page=shift;
  266. my @links;
  267. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  268. push @links, titlepage($2);
  269. }
  270. # Discussion links are a special case since they're not in the text
  271. # of the page, but on its template.
  272. return @links, "$page/discussion";
  273. } #}}}
  274. sub render ($) { #{{{
  275. my $file=shift;
  276. my $type=pagetype($file);
  277. my $srcfile=srcfile($file);
  278. my $content=readfile($srcfile);
  279. if ($type ne 'unknown') {
  280. my $page=pagename($file);
  281. $links{$page}=[findlinks($content, $page)];
  282. delete $inlinepages{$page};
  283. $content=linkify($content, $page);
  284. $content=htmlize($type, $content);
  285. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  286. writefile("$config{destdir}/".htmlpage($page),
  287. genpage($content, $page, mtime($srcfile)));
  288. $oldpagemtime{$page}=time;
  289. $renderedfiles{$page}=htmlpage($page);
  290. # TODO: should really add this to renderedfiles and call
  291. # check_overwrite, as above, but currently renderedfiles
  292. # only supports listing one file per page.
  293. if ($config{rss} && exists $inlinepages{$page}) {
  294. writefile("$config{destdir}/".rsspage($page),
  295. genrss($content, $page, mtime($srcfile)));
  296. }
  297. }
  298. else {
  299. $links{$file}=[];
  300. check_overwrite("$config{destdir}/$file", $file);
  301. writefile("$config{destdir}/$file", $content);
  302. $oldpagemtime{$file}=time;
  303. $renderedfiles{$file}=$file;
  304. }
  305. } #}}}
  306. sub prune ($) { #{{{
  307. my $file=shift;
  308. unlink($file);
  309. my $dir=dirname($file);
  310. while (rmdir($dir)) {
  311. $dir=dirname($dir);
  312. }
  313. } #}}}
  314. sub refresh () { #{{{
  315. # find existing pages
  316. my %exists;
  317. my @files;
  318. eval q{use File::Find};
  319. find({
  320. no_chdir => 1,
  321. wanted => sub {
  322. if (/$config{wiki_file_prune_regexp}/) {
  323. $File::Find::prune=1;
  324. }
  325. elsif (! -d $_ && ! -l $_) {
  326. my ($f)=/$config{wiki_file_regexp}/; # untaint
  327. if (! defined $f) {
  328. warn("skipping bad filename $_\n");
  329. }
  330. else {
  331. $f=~s/^\Q$config{srcdir}\E\/?//;
  332. push @files, $f;
  333. $exists{pagename($f)}=1;
  334. }
  335. }
  336. },
  337. }, $config{srcdir});
  338. find({
  339. no_chdir => 1,
  340. wanted => sub {
  341. if (/$config{wiki_file_prune_regexp}/) {
  342. $File::Find::prune=1;
  343. }
  344. elsif (! -d $_ && ! -l $_) {
  345. my ($f)=/$config{wiki_file_regexp}/; # untaint
  346. if (! defined $f) {
  347. warn("skipping bad filename $_\n");
  348. }
  349. else {
  350. # Don't add files that are in the
  351. # srcdir.
  352. $f=~s/^\Q$config{underlaydir}\E\/?//;
  353. if (! -e "$config{srcdir}/$f" &&
  354. ! -l "$config{srcdir}/$f") {
  355. push @files, $f;
  356. $exists{pagename($f)}=1;
  357. }
  358. }
  359. }
  360. },
  361. }, $config{underlaydir});
  362. my %rendered;
  363. # check for added or removed pages
  364. my @add;
  365. foreach my $file (@files) {
  366. my $page=pagename($file);
  367. if (! $oldpagemtime{$page}) {
  368. debug("new page $page") unless exists $pagectime{$page};
  369. push @add, $file;
  370. $links{$page}=[];
  371. $pagesources{$page}=$file;
  372. $pagectime{$page}=mtime(srcfile($file))
  373. unless exists $pagectime{$page};
  374. }
  375. }
  376. my @del;
  377. foreach my $page (keys %oldpagemtime) {
  378. if (! $exists{$page}) {
  379. debug("removing old page $page");
  380. push @del, $pagesources{$page};
  381. prune($config{destdir}."/".$renderedfiles{$page});
  382. delete $renderedfiles{$page};
  383. $oldpagemtime{$page}=0;
  384. delete $pagesources{$page};
  385. }
  386. }
  387. # render any updated files
  388. foreach my $file (@files) {
  389. my $page=pagename($file);
  390. if (! exists $oldpagemtime{$page} ||
  391. mtime(srcfile($file)) > $oldpagemtime{$page}) {
  392. debug("rendering changed file $file");
  393. render($file);
  394. $rendered{$file}=1;
  395. }
  396. }
  397. # if any files were added or removed, check to see if each page
  398. # needs an update due to linking to them or inlining them.
  399. # TODO: inefficient; pages may get rendered above and again here;
  400. # problem is the bestlink may have changed and we won't know until
  401. # now
  402. if (@add || @del) {
  403. FILE: foreach my $file (@files) {
  404. my $page=pagename($file);
  405. foreach my $f (@add, @del) {
  406. my $p=pagename($f);
  407. foreach my $link (@{$links{$page}}) {
  408. if (bestlink($page, $link) eq $p) {
  409. debug("rendering $file, which links to $p");
  410. render($file);
  411. $rendered{$file}=1;
  412. next FILE;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. # Handle backlinks; if a page has added/removed links, update the
  419. # pages it links to. Also handle inlining here.
  420. # TODO: inefficient; pages may get rendered above and again here;
  421. # problem is the backlinks could be wrong in the first pass render
  422. # above
  423. if (%rendered || @del) {
  424. foreach my $f (@files) {
  425. my $p=pagename($f);
  426. if (exists $inlinepages{$p}) {
  427. foreach my $file (keys %rendered, @del) {
  428. my $page=pagename($file);
  429. if (globlist_match($page, $inlinepages{$p})) {
  430. debug("rendering $f, which inlines $page");
  431. render($f);
  432. last;
  433. }
  434. }
  435. }
  436. }
  437. my %linkchanged;
  438. foreach my $file (keys %rendered, @del) {
  439. my $page=pagename($file);
  440. if (exists $links{$page}) {
  441. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  442. if (length $link &&
  443. ! exists $oldlinks{$page} ||
  444. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  445. $linkchanged{$link}=1;
  446. }
  447. }
  448. }
  449. if (exists $oldlinks{$page}) {
  450. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  451. if (length $link &&
  452. ! exists $links{$page} ||
  453. ! grep { $_ eq $link } @{$links{$page}}) {
  454. $linkchanged{$link}=1;
  455. }
  456. }
  457. }
  458. }
  459. foreach my $link (keys %linkchanged) {
  460. my $linkfile=$pagesources{$link};
  461. if (defined $linkfile) {
  462. debug("rendering $linkfile, to update its backlinks");
  463. render($linkfile);
  464. }
  465. }
  466. }
  467. } #}}}
  468. 1