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