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