summaryrefslogtreecommitdiff
path: root/IkiWiki/Render.pm
blob: 1ac85be44dad722e1588000fde059d420a433659 (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 genpage ($$$) { #{{{
  72. my $content=shift;
  73. my $page=shift;
  74. my $mtime=shift;
  75. my $title=pagetitle(basename($page));
  76. my $template=HTML::Template->new(blind_cache => 1,
  77. filename => "$config{templatedir}/page.tmpl");
  78. if (length $config{cgiurl}) {
  79. $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
  80. $template->param(prefsurl => "$config{cgiurl}?do=prefs");
  81. if ($config{rcs}) {
  82. $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
  83. }
  84. }
  85. if (length $config{historyurl}) {
  86. my $u=$config{historyurl};
  87. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  88. $template->param(historyurl => $u);
  89. }
  90. if ($config{rss}) {
  91. $template->param(rssurl => rsspage($page));
  92. }
  93. $template->param(
  94. title => $title,
  95. wikiname => $config{wikiname},
  96. parentlinks => [parentlinks($page)],
  97. content => $content,
  98. backlinks => [backlinks($page)],
  99. discussionlink => htmllink($page, "Discussion", 1, 1),
  100. mtime => scalar(gmtime($mtime)),
  101. );
  102. return $template->output;
  103. } #}}}
  104. sub date_822 ($) { #{{{
  105. my $time=shift;
  106. eval q{use POSIX};
  107. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  108. } #}}}
  109. sub absolute_urls ($$) { #{{{
  110. my $content=shift;
  111. my $url=shift;
  112. $url=~s/[^\/]+$//;
  113. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  114. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  115. return $content;
  116. } #}}}
  117. sub genrss ($$$) { #{{{
  118. my $content=shift;
  119. my $page=shift;
  120. my $mtime=shift;
  121. my $url="$config{url}/".htmlpage($page);
  122. my $template=HTML::Template->new(blind_cache => 1,
  123. filename => "$config{templatedir}/rsspage.tmpl");
  124. # Regular page gets a feed that is updated every time the
  125. # page is changed, so the mtime is encoded in the guid.
  126. my @items=(
  127. {
  128. itemtitle => pagetitle(basename($page)),
  129. itemguid => "$url?mtime=$mtime",
  130. itemurl => $url,
  131. itempubdate => date_822($mtime),
  132. itemcontent => absolute_urls($content, $url), # rss sucks
  133. },
  134. );
  135. $template->param(
  136. title => $config{wikiname},
  137. pageurl => $url,
  138. items => \@items,
  139. );
  140. return $template->output;
  141. } #}}}
  142. sub check_overwrite ($$) { #{{{
  143. # Important security check. Make sure to call this before saving
  144. # any files to the source directory.
  145. my $dest=shift;
  146. my $src=shift;
  147. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  148. error("$dest already exists and was rendered from ".
  149. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  150. %renderedfiles)).
  151. ", before, so not rendering from $src");
  152. }
  153. } #}}}
  154. sub mtime ($) { #{{{
  155. my $page=shift;
  156. return (stat($page))[9];
  157. } #}}}
  158. sub findlinks ($$) { #{{{
  159. my $content=shift;
  160. my $page=shift;
  161. my @links;
  162. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  163. push @links, lc($1);
  164. }
  165. # Discussion links are a special case since they're not in the text
  166. # of the page, but on its template.
  167. return @links, "$page/discussion";
  168. } #}}}
  169. sub render ($) { #{{{
  170. my $file=shift;
  171. my $type=pagetype($file);
  172. my $content=readfile("$config{srcdir}/$file");
  173. if ($type ne 'unknown') {
  174. my $page=pagename($file);
  175. $links{$page}=[findlinks($content, $page)];
  176. $content=linkify($content, $page);
  177. $content=htmlize($type, $content);
  178. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  179. writefile("$config{destdir}/".htmlpage($page),
  180. genpage($content, $page, mtime("$config{srcdir}/$file")));
  181. $oldpagemtime{$page}=time;
  182. $renderedfiles{$page}=htmlpage($page);
  183. # TODO: should really add this to renderedfiles and call
  184. # check_overwrite, as above, but currently renderedfiles
  185. # only supports listing one file per page.
  186. if ($config{rss}) {
  187. writefile("$config{destdir}/".rsspage($page),
  188. genrss($content, $page, mtime("$config{srcdir}/$file")));
  189. }
  190. }
  191. else {
  192. $links{$file}=[];
  193. check_overwrite("$config{destdir}/$file", $file);
  194. writefile("$config{destdir}/$file", $content);
  195. $oldpagemtime{$file}=time;
  196. $renderedfiles{$file}=$file;
  197. }
  198. } #}}}
  199. sub prune ($) { #{{{
  200. my $file=shift;
  201. unlink($file);
  202. my $dir=dirname($file);
  203. while (rmdir($dir)) {
  204. $dir=dirname($dir);
  205. }
  206. } #}}}
  207. sub refresh () { #{{{
  208. # find existing pages
  209. my %exists;
  210. my @files;
  211. eval q{use File::Find};
  212. find({
  213. no_chdir => 1,
  214. wanted => sub {
  215. if (/$config{wiki_file_prune_regexp}/) {
  216. no warnings 'once';
  217. $File::Find::prune=1;
  218. use warnings "all";
  219. }
  220. elsif (! -d $_ && ! -l $_) {
  221. my ($f)=/$config{wiki_file_regexp}/; # untaint
  222. if (! defined $f) {
  223. warn("skipping bad filename $_\n");
  224. }
  225. else {
  226. $f=~s/^\Q$config{srcdir}\E\/?//;
  227. push @files, $f;
  228. $exists{pagename($f)}=1;
  229. }
  230. }
  231. },
  232. }, $config{srcdir});
  233. my %rendered;
  234. # check for added or removed pages
  235. my @add;
  236. foreach my $file (@files) {
  237. my $page=pagename($file);
  238. if (! $oldpagemtime{$page}) {
  239. debug("new page $page");
  240. push @add, $file;
  241. $links{$page}=[];
  242. $pagesources{$page}=$file;
  243. }
  244. }
  245. my @del;
  246. foreach my $page (keys %oldpagemtime) {
  247. if (! $exists{$page}) {
  248. debug("removing old page $page");
  249. push @del, $pagesources{$page};
  250. prune($config{destdir}."/".$renderedfiles{$page});
  251. delete $renderedfiles{$page};
  252. $oldpagemtime{$page}=0;
  253. delete $pagesources{$page};
  254. }
  255. }
  256. # render any updated files
  257. foreach my $file (@files) {
  258. my $page=pagename($file);
  259. if (! exists $oldpagemtime{$page} ||
  260. mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
  261. debug("rendering changed file $file");
  262. render($file);
  263. $rendered{$file}=1;
  264. }
  265. }
  266. # if any files were added or removed, check to see if each page
  267. # needs an update due to linking to them
  268. # TODO: inefficient; pages may get rendered above and again here;
  269. # problem is the bestlink may have changed and we won't know until
  270. # now
  271. if (@add || @del) {
  272. FILE: foreach my $file (@files) {
  273. my $page=pagename($file);
  274. foreach my $f (@add, @del) {
  275. my $p=pagename($f);
  276. foreach my $link (@{$links{$page}}) {
  277. if (bestlink($page, $link) eq $p) {
  278. debug("rendering $file, which links to $p");
  279. render($file);
  280. $rendered{$file}=1;
  281. next FILE;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. # handle backlinks; if a page has added/removed links, update the
  288. # pages it links to
  289. # TODO: inefficient; pages may get rendered above and again here;
  290. # problem is the backlinks could be wrong in the first pass render
  291. # above
  292. if (%rendered) {
  293. my %linkchanged;
  294. foreach my $file (keys %rendered, @del) {
  295. my $page=pagename($file);
  296. if (exists $links{$page}) {
  297. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  298. if (length $link &&
  299. ! exists $oldlinks{$page} ||
  300. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  301. $linkchanged{$link}=1;
  302. }
  303. }
  304. }
  305. if (exists $oldlinks{$page}) {
  306. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  307. if (length $link &&
  308. ! exists $links{$page} ||
  309. ! grep { $_ eq $link } @{$links{$page}}) {
  310. $linkchanged{$link}=1;
  311. }
  312. }
  313. }
  314. }
  315. foreach my $link (keys %linkchanged) {
  316. my $linkfile=$pagesources{$link};
  317. if (defined $linkfile) {
  318. debug("rendering $linkfile, to update its backlinks");
  319. render($linkfile);
  320. }
  321. }
  322. }
  323. } #}}}
  324. 1