summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 7d2e8c4ee981750ea99b5a56de4c580a539f0404 (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}) {
  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. } #}}}zo
  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. $inlinepages{$parentpage}=$params{pages};
  219. $isblog=1;
  220. foreach my $page (blog_list($params{pages}, $params{show})) {
  221. next if $page eq $parentpage;
  222. push @items, {
  223. itemtitle => pagetitle(basename($page)),
  224. itemurl => "$config{url}/$renderedfiles{$page}",
  225. itempubdate => date_822($pagectime{$page}),
  226. itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
  227. } if exists $renderedfiles{$page};
  228. }
  229. return "";
  230. };
  231. $content = postprocess($page, $content, inline => $gen_blog);
  232. # Regular page gets a feed that is updated every time the
  233. # page is changed, so the mtime is encoded in the guid.
  234. push @items, {
  235. itemtitle => pagetitle(basename($page)),
  236. itemguid => "$url?mtime=$mtime",
  237. itemurl => $url,
  238. itempubdate => date_822($mtime),
  239. itemcontent => absolute_urls($content, $url),
  240. } unless $isblog;
  241. $template->param(
  242. title => $config{wikiname},
  243. pageurl => $url,
  244. items => \@items,
  245. );
  246. return $template->output;
  247. } #}}}
  248. sub check_overwrite ($$) { #{{{
  249. # Important security check. Make sure to call this before saving
  250. # any files to the source directory.
  251. my $dest=shift;
  252. my $src=shift;
  253. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  254. error("$dest already exists and was rendered from ".
  255. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  256. %renderedfiles)).
  257. ", before, so not rendering from $src");
  258. }
  259. } #}}}
  260. sub mtime ($) { #{{{
  261. my $page=shift;
  262. return (stat($page))[9];
  263. } #}}}
  264. sub findlinks ($$) { #{{{
  265. my $content=shift;
  266. my $page=shift;
  267. my @links;
  268. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  269. push @links, lc($1);
  270. }
  271. # Discussion links are a special case since they're not in the text
  272. # of the page, but on its template.
  273. return @links, "$page/discussion";
  274. } #}}}
  275. sub render ($) { #{{{
  276. my $file=shift;
  277. my $type=pagetype($file);
  278. my $content=readfile("$config{srcdir}/$file");
  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("$config{srcdir}/$file")));
  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}) {
  294. writefile("$config{destdir}/".rsspage($page),
  295. genrss($content, $page, mtime("$config{srcdir}/$file")));
  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. no warnings 'once';
  324. $File::Find::prune=1;
  325. use warnings "all";
  326. }
  327. elsif (! -d $_ && ! -l $_) {
  328. my ($f)=/$config{wiki_file_regexp}/; # untaint
  329. if (! defined $f) {
  330. warn("skipping bad filename $_\n");
  331. }
  332. else {
  333. $f=~s/^\Q$config{srcdir}\E\/?//;
  334. push @files, $f;
  335. $exists{pagename($f)}=1;
  336. }
  337. }
  338. },
  339. }, $config{srcdir});
  340. my %rendered;
  341. # check for added or removed pages
  342. my @add;
  343. foreach my $file (@files) {
  344. my $page=pagename($file);
  345. if (! $oldpagemtime{$page}) {
  346. debug("new page $page") unless exists $pagectime{$page};
  347. push @add, $file;
  348. $links{$page}=[];
  349. $pagesources{$page}=$file;
  350. $pagectime{$page}=time unless exists $pagectime{$page};
  351. }
  352. }
  353. my @del;
  354. foreach my $page (keys %oldpagemtime) {
  355. if (! $exists{$page}) {
  356. debug("removing old page $page");
  357. push @del, $pagesources{$page};
  358. prune($config{destdir}."/".$renderedfiles{$page});
  359. delete $renderedfiles{$page};
  360. $oldpagemtime{$page}=0;
  361. delete $pagesources{$page};
  362. }
  363. }
  364. # render any updated files
  365. foreach my $file (@files) {
  366. my $page=pagename($file);
  367. if (! exists $oldpagemtime{$page} ||
  368. mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
  369. debug("rendering changed file $file");
  370. render($file);
  371. $rendered{$file}=1;
  372. }
  373. }
  374. # if any files were added or removed, check to see if each page
  375. # needs an update due to linking to them or inlining them.
  376. # TODO: inefficient; pages may get rendered above and again here;
  377. # problem is the bestlink may have changed and we won't know until
  378. # now
  379. if (@add || @del) {
  380. FILE: foreach my $file (@files) {
  381. my $page=pagename($file);
  382. foreach my $f (@add, @del) {
  383. my $p=pagename($f);
  384. foreach my $link (@{$links{$page}}) {
  385. if (bestlink($page, $link) eq $p) {
  386. debug("rendering $file, which links to $p");
  387. render($file);
  388. $rendered{$file}=1;
  389. next FILE;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. # Handle backlinks; if a page has added/removed links, update the
  396. # pages it links to. Also handle inlining here.
  397. # TODO: inefficient; pages may get rendered above and again here;
  398. # problem is the backlinks could be wrong in the first pass render
  399. # above
  400. if (%rendered || @del) {
  401. my %linkchanged;
  402. foreach my $file (keys %rendered, @del) {
  403. my $page=pagename($file);
  404. foreach my $f (@files) {
  405. my $p=pagename($f);
  406. if (exists $inlinepages{$p} &&
  407. globlist_match($page, $inlinepages{$p})) {
  408. debug("rendering $f, which inlines $page");
  409. render($f);
  410. next;
  411. }
  412. }
  413. if (exists $links{$page}) {
  414. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  415. if (length $link &&
  416. ! exists $oldlinks{$page} ||
  417. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  418. $linkchanged{$link}=1;
  419. }
  420. }
  421. }
  422. if (exists $oldlinks{$page}) {
  423. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  424. if (length $link &&
  425. ! exists $links{$page} ||
  426. ! grep { $_ eq $link } @{$links{$page}}) {
  427. $linkchanged{$link}=1;
  428. }
  429. }
  430. }
  431. }
  432. foreach my $link (keys %linkchanged) {
  433. my $linkfile=$pagesources{$link};
  434. if (defined $linkfile) {
  435. debug("rendering $linkfile, to update its backlinks");
  436. render($linkfile);
  437. }
  438. }
  439. }
  440. } #}}}
  441. 1