summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 4e2caa6cef31435bc026bf70f9b7573d406e46c1 (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. $1 ? "[[$2]]" : htmllink($page, $2)
  10. }eg;
  11. return $content;
  12. } #}}}
  13. sub htmlize ($$) { #{{{
  14. my $type=shift;
  15. my $content=shift;
  16. if (! $INC{"/usr/bin/markdown"}) {
  17. no warnings 'once';
  18. $blosxom::version="is a proper perl module too much to ask?";
  19. use warnings 'all';
  20. do "/usr/bin/markdown";
  21. }
  22. if ($type eq '.mdwn') {
  23. return Markdown::Markdown($content);
  24. }
  25. else {
  26. error("htmlization of $type not supported");
  27. }
  28. } #}}}
  29. sub backlinks ($) { #{{{
  30. my $page=shift;
  31. my @links;
  32. foreach my $p (keys %links) {
  33. next if bestlink($page, $p) eq $page;
  34. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  35. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  36. # Trim common dir prefixes from both pages.
  37. my $p_trimmed=$p;
  38. my $page_trimmed=$page;
  39. my $dir;
  40. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  41. defined $dir &&
  42. $p_trimmed=~s/^\Q$dir\E// &&
  43. $page_trimmed=~s/^\Q$dir\E//;
  44. push @links, { url => $href, page => $p_trimmed };
  45. }
  46. }
  47. return sort { $a->{page} cmp $b->{page} } @links;
  48. } #}}}
  49. sub parentlinks ($) { #{{{
  50. my $page=shift;
  51. my @ret;
  52. my $pagelink="";
  53. my $path="";
  54. my $skip=1;
  55. foreach my $dir (reverse split("/", $page)) {
  56. if (! $skip) {
  57. $path.="../";
  58. unshift @ret, { url => "$path$dir.html", page => $dir };
  59. }
  60. else {
  61. $skip=0;
  62. }
  63. }
  64. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  65. return @ret;
  66. } #}}}
  67. sub rsspage ($) { #{{{
  68. my $page=shift;
  69. return $page.".rss";
  70. } #}}}
  71. sub postprocess { #{{{
  72. # Takes content to postprocess followed by a list of postprocessor
  73. # commands and subroutine references to run for the commands.
  74. my $page=shift;
  75. my $content=shift;
  76. my %commands=@_;
  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("$config{srcdir}/$file"), $parentpage));
  118. }
  119. else {
  120. return "";
  121. }
  122. } #}}}
  123. sub postprocess_html_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. foreach my $page (blog_list($params{pages}, $params{show})) {
  150. next if $page eq $parentpage;
  151. $template->param(pagelink => htmllink($parentpage, $page));
  152. $template->param(content => get_inline_content($parentpage, $page))
  153. if $params{archive} eq "no";
  154. $template->param(ctime => scalar(gmtime($pagectime{$page})));
  155. $ret.=$template->output;
  156. }
  157. return $ret;
  158. } #}}}
  159. sub genpage ($$$) { #{{{
  160. my $content=shift;
  161. my $page=shift;
  162. my $mtime=shift;
  163. $content = postprocess($page, $content, inline => \&postprocess_html_inline);
  164. my $title=pagetitle(basename($page));
  165. my $template=HTML::Template->new(blind_cache => 1,
  166. filename => "$config{templatedir}/page.tmpl");
  167. if (length $config{cgiurl}) {
  168. $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
  169. $template->param(prefsurl => "$config{cgiurl}?do=prefs");
  170. if ($config{rcs}) {
  171. $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
  172. }
  173. }
  174. if (length $config{historyurl}) {
  175. my $u=$config{historyurl};
  176. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  177. $template->param(historyurl => $u);
  178. }
  179. if ($config{rss} && $inlinepages{$page}) {
  180. $template->param(rssurl => rsspage($page));
  181. }
  182. $template->param(
  183. title => $title,
  184. wikiname => $config{wikiname},
  185. parentlinks => [parentlinks($page)],
  186. content => $content,
  187. backlinks => [backlinks($page)],
  188. discussionlink => htmllink($page, "Discussion", 1, 1),
  189. mtime => scalar(gmtime($mtime)),
  190. );
  191. return $template->output;
  192. } #}}}
  193. sub date_822 ($) { #{{{
  194. my $time=shift;
  195. eval q{use POSIX};
  196. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  197. } #}}}
  198. sub absolute_urls ($$) { #{{{
  199. # sucky sub because rss sucks
  200. my $content=shift;
  201. my $url=shift;
  202. $url=~s/[^\/]+$//;
  203. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  204. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  205. return $content;
  206. } #}}}
  207. sub genrss ($$$) { #{{{
  208. my $content=shift;
  209. my $page=shift;
  210. my $mtime=shift;
  211. my $url="$config{url}/".htmlpage($page);
  212. my $template=HTML::Template->new(blind_cache => 1,
  213. filename => "$config{templatedir}/rsspage.tmpl");
  214. my @items;
  215. my $isblog=0;
  216. my $gen_blog=sub {
  217. my $parentpage=shift;
  218. my %params=@_;
  219. return "" if exists $params{archive} && $params{archive} eq 'yes';
  220. if (! exists $params{show}) {
  221. $params{show}=10;
  222. }
  223. if (! exists $params{pages}) {
  224. return "";
  225. }
  226. $isblog=1;
  227. foreach my $page (blog_list($params{pages}, $params{show})) {
  228. next if $page eq $parentpage;
  229. push @items, {
  230. itemtitle => pagetitle(basename($page)),
  231. itemurl => "$config{url}/$renderedfiles{$page}",
  232. itempubdate => date_822($pagectime{$page}),
  233. itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
  234. } if exists $renderedfiles{$page};
  235. }
  236. return "";
  237. };
  238. $content = postprocess($page, $content, inline => $gen_blog);
  239. $template->param(
  240. title => $config{wikiname},
  241. pageurl => $url,
  242. items => \@items,
  243. );
  244. return $template->output;
  245. } #}}}
  246. sub check_overwrite ($$) { #{{{
  247. # Important security check. Make sure to call this before saving
  248. # any files to the source directory.
  249. my $dest=shift;
  250. my $src=shift;
  251. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  252. error("$dest already exists and was rendered from ".
  253. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  254. %renderedfiles)).
  255. ", before, so not rendering from $src");
  256. }
  257. } #}}}
  258. sub mtime ($) { #{{{
  259. my $page=shift;
  260. return (stat($page))[9];
  261. } #}}}
  262. sub findlinks ($$) { #{{{
  263. my $content=shift;
  264. my $page=shift;
  265. my @links;
  266. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  267. push @links, lc($1);
  268. }
  269. # Discussion links are a special case since they're not in the text
  270. # of the page, but on its template.
  271. return @links, "$page/discussion";
  272. } #}}}
  273. sub render ($) { #{{{
  274. my $file=shift;
  275. my $type=pagetype($file);
  276. my $content=readfile("$config{srcdir}/$file");
  277. if ($type ne 'unknown') {
  278. my $page=pagename($file);
  279. $links{$page}=[findlinks($content, $page)];
  280. delete $inlinepages{$page};
  281. $content=linkify($content, $page);
  282. $content=htmlize($type, $content);
  283. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  284. writefile("$config{destdir}/".htmlpage($page),
  285. genpage($content, $page, mtime("$config{srcdir}/$file")));
  286. $oldpagemtime{$page}=time;
  287. $renderedfiles{$page}=htmlpage($page);
  288. # TODO: should really add this to renderedfiles and call
  289. # check_overwrite, as above, but currently renderedfiles
  290. # only supports listing one file per page.
  291. if ($config{rss} && exists $inlinepages{$page}) {
  292. writefile("$config{destdir}/".rsspage($page),
  293. genrss($content, $page, mtime("$config{srcdir}/$file")));
  294. }
  295. }
  296. else {
  297. $links{$file}=[];
  298. check_overwrite("$config{destdir}/$file", $file);
  299. writefile("$config{destdir}/$file", $content);
  300. $oldpagemtime{$file}=time;
  301. $renderedfiles{$file}=$file;
  302. }
  303. } #}}}
  304. sub prune ($) { #{{{
  305. my $file=shift;
  306. unlink($file);
  307. my $dir=dirname($file);
  308. while (rmdir($dir)) {
  309. $dir=dirname($dir);
  310. }
  311. } #}}}
  312. sub refresh () { #{{{
  313. # find existing pages
  314. my %exists;
  315. my @files;
  316. eval q{use File::Find};
  317. find({
  318. no_chdir => 1,
  319. wanted => sub {
  320. if (/$config{wiki_file_prune_regexp}/) {
  321. no warnings 'once';
  322. $File::Find::prune=1;
  323. use warnings "all";
  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. my %rendered;
  339. # check for added or removed pages
  340. my @add;
  341. foreach my $file (@files) {
  342. my $page=pagename($file);
  343. if (! $oldpagemtime{$page}) {
  344. debug("new page $page") unless exists $pagectime{$page};
  345. push @add, $file;
  346. $links{$page}=[];
  347. $pagesources{$page}=$file;
  348. $pagectime{$page}=time unless exists $pagectime{$page};
  349. }
  350. }
  351. my @del;
  352. foreach my $page (keys %oldpagemtime) {
  353. if (! $exists{$page}) {
  354. debug("removing old page $page");
  355. push @del, $pagesources{$page};
  356. prune($config{destdir}."/".$renderedfiles{$page});
  357. delete $renderedfiles{$page};
  358. $oldpagemtime{$page}=0;
  359. delete $pagesources{$page};
  360. }
  361. }
  362. # render any updated files
  363. foreach my $file (@files) {
  364. my $page=pagename($file);
  365. if (! exists $oldpagemtime{$page} ||
  366. mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
  367. debug("rendering changed file $file");
  368. render($file);
  369. $rendered{$file}=1;
  370. }
  371. }
  372. # if any files were added or removed, check to see if each page
  373. # needs an update due to linking to them or inlining them.
  374. # TODO: inefficient; pages may get rendered above and again here;
  375. # problem is the bestlink may have changed and we won't know until
  376. # now
  377. if (@add || @del) {
  378. FILE: foreach my $file (@files) {
  379. my $page=pagename($file);
  380. foreach my $f (@add, @del) {
  381. my $p=pagename($f);
  382. foreach my $link (@{$links{$page}}) {
  383. if (bestlink($page, $link) eq $p) {
  384. debug("rendering $file, which links to $p");
  385. render($file);
  386. $rendered{$file}=1;
  387. next FILE;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. # Handle backlinks; if a page has added/removed links, update the
  394. # pages it links to. Also handle inlining here.
  395. # TODO: inefficient; pages may get rendered above and again here;
  396. # problem is the backlinks could be wrong in the first pass render
  397. # above
  398. if (%rendered || @del) {
  399. foreach my $f (@files) {
  400. my $p=pagename($f);
  401. if (exists $inlinepages{$p}) {
  402. foreach my $file (keys %rendered, @del) {
  403. my $page=pagename($file);
  404. if (globlist_match($page, $inlinepages{$p})) {
  405. debug("rendering $f, which inlines $page");
  406. render($f);
  407. last;
  408. }
  409. }
  410. }
  411. }
  412. my %linkchanged;
  413. foreach my $file (keys %rendered, @del) {
  414. my $page=pagename($file);
  415. if (exists $links{$page}) {
  416. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  417. if (length $link &&
  418. ! exists $oldlinks{$page} ||
  419. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  420. $linkchanged{$link}=1;
  421. }
  422. }
  423. }
  424. if (exists $oldlinks{$page}) {
  425. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  426. if (length $link &&
  427. ! exists $links{$page} ||
  428. ! grep { $_ eq $link } @{$links{$page}}) {
  429. $linkchanged{$link}=1;
  430. }
  431. }
  432. }
  433. }
  434. foreach my $link (keys %linkchanged) {
  435. my $linkfile=$pagesources{$link};
  436. if (defined $linkfile) {
  437. debug("rendering $linkfile, to update its backlinks");
  438. render($linkfile);
  439. }
  440. }
  441. }
  442. } #}}}
  443. 1