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