summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 4d736c6134a9e0a6516ead060402ef79474e9666 (plain)
  1. #!/usr/bin/perl -T
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use Memoize;
  6. use File::Spec;
  7. BEGIN {
  8. $blosxom::version="is a proper perl module too much to ask?";
  9. do "/usr/bin/markdown";
  10. }
  11. memoize('pagename');
  12. memoize('bestlink');
  13. my ($srcdir)= shift =~ /(.*)/; # untaint
  14. my ($destdir)= shift =~ /(.*)/; # untaint
  15. my $link=qr/\[\[([^\s]+)\]\]/;
  16. my $verbose=1;
  17. my %links;
  18. my %oldpagemtime;
  19. my %renderedfiles;
  20. sub error ($) {
  21. die @_;
  22. }
  23. sub debug ($) {
  24. print "@_\n" if $verbose;
  25. }
  26. sub mtime ($) {
  27. my $page=shift;
  28. return (stat($page))[9];
  29. }
  30. sub basename {
  31. my $file=shift;
  32. $file=~s!.*/!!;
  33. return $file;
  34. }
  35. sub dirname {
  36. my $file=shift;
  37. $file=~s!/?[^/]+$!!;
  38. return $file;
  39. }
  40. sub pagetype ($) {
  41. my $page=shift;
  42. if ($page =~ /\.mdwn$/) {
  43. return ".mdwn";
  44. }
  45. else {
  46. return "unknown";
  47. }
  48. }
  49. sub pagename ($) {
  50. my $file=shift;
  51. my $type=pagetype($file);
  52. my $page=$file;
  53. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  54. return $page;
  55. }
  56. sub htmlpage ($) {
  57. my $page=shift;
  58. return $page.".html";
  59. }
  60. sub readpage ($) {
  61. my $page=shift;
  62. local $/=undef;
  63. open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
  64. my $ret=<PAGE>;
  65. close PAGE;
  66. return $ret;
  67. }
  68. sub writepage ($$) {
  69. my $page=shift;
  70. my $content=shift;
  71. my $dir=dirname("$destdir/$page");
  72. if (! -d $dir) {
  73. my $d="";
  74. foreach my $s (split(m!/+!, $dir)) {
  75. $d.="$s/";
  76. if (! -d $d) {
  77. mkdir($d) || error("failed to create directory $d: $!");
  78. }
  79. }
  80. }
  81. open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
  82. print PAGE $content;
  83. close PAGE;
  84. }
  85. sub findlinks {
  86. my $content=shift;
  87. my @links;
  88. while ($content =~ /$link/g) {
  89. push @links, lc($1);
  90. }
  91. return @links;
  92. }
  93. # Given a page and the text of a link on the page, determine which existing
  94. # page that link best points to. Prefers pages under a subdirectory with
  95. # the same name as the source page, failing that goes down the directory tree
  96. # to the base looking for matching pages.
  97. sub bestlink ($$) {
  98. my $page=shift;
  99. my $link=lc(shift);
  100. my $cwd=$page;
  101. do {
  102. my $l=$cwd;
  103. $l.="/" if length $l;
  104. $l.=$link;
  105. if (exists $links{$l}) {
  106. #debug("for $page, \"$link\", use $l");
  107. return $l;
  108. }
  109. } while $cwd=~s!/?[^/]+$!!;
  110. print STDERR "warning: page $page, broken link: $link\n";
  111. return "";
  112. }
  113. sub isinlinableimage ($) {
  114. my $file=shift;
  115. $file=~/\.(png|gif|jpg|jpeg)$/;
  116. }
  117. sub htmllink ($$) {
  118. my $page=shift;
  119. my $link=shift;
  120. my $bestlink=bestlink($page, $link);
  121. return $page if $page eq $bestlink;
  122. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  123. $bestlink=htmlpage($bestlink);
  124. }
  125. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  126. return "<a href=\"?\">?</a>$link"
  127. }
  128. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  129. if (isinlinableimage($bestlink)) {
  130. return "<img src=\"$bestlink\">";
  131. }
  132. return "<a href=\"$bestlink\">$link</a>";
  133. }
  134. sub linkify ($$) {
  135. my $content=shift;
  136. my $file=shift;
  137. $content =~ s/$link/htmllink(pagename($file), $1)/eg;
  138. return $content;
  139. }
  140. sub htmlize ($$) {
  141. my $type=shift;
  142. my $content=shift;
  143. if ($type eq '.mdwn') {
  144. return Markdown::Markdown($content);
  145. }
  146. else {
  147. error("htmlization of $type not supported");
  148. }
  149. }
  150. sub finalize ($$) {
  151. my $content=shift;
  152. my $page=shift;
  153. my $title=basename($page);
  154. $title=~s/_/ /g;
  155. $content="<html>\n<head><title>$title</title></head>\n<body>\n".
  156. $content.
  157. "</body>\n</html>\n";
  158. return $content;
  159. }
  160. sub render ($) {
  161. my $file=shift;
  162. my $type=pagetype($file);
  163. my $content=readpage($file);
  164. if ($type ne 'unknown') {
  165. my $page=pagename($file);
  166. $links{$page}=[findlinks($content)];
  167. $content=linkify($content, $file);
  168. $content=htmlize($type, $content);
  169. $content=finalize($content, $page);
  170. writepage(htmlpage($page), $content);
  171. $oldpagemtime{$page}=time;
  172. $renderedfiles{$page}=htmlpage($page);
  173. }
  174. else {
  175. $links{$file}=[];
  176. writepage($file, $content);
  177. $oldpagemtime{$file}=time;
  178. $renderedfiles{$file}=$file;
  179. }
  180. }
  181. sub loadindex () {
  182. open (IN, "$srcdir/.index") || return;
  183. while (<IN>) {
  184. chomp;
  185. my ($mtime, $page, $rendered, @links)=split(' ', $_);
  186. $oldpagemtime{$page}=$mtime;
  187. $links{$page}=\@links;
  188. ($renderedfiles{$page})=$rendered=~m/(.*)/; # untaint
  189. }
  190. close IN;
  191. }
  192. sub saveindex () {
  193. open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
  194. foreach my $page (keys %oldpagemtime) {
  195. print OUT "$oldpagemtime{$page} $page $renderedfiles{$page} ".
  196. join(" ", @{$links{$page}})."\n"
  197. if $oldpagemtime{$page};
  198. }
  199. close OUT;
  200. }
  201. sub prune ($) {
  202. my $file=shift;
  203. unlink($file);
  204. my $dir=dirname($file);
  205. while (rmdir($dir)) {
  206. $dir=dirname($dir);
  207. }
  208. }
  209. sub refresh () {
  210. # Find existing pages.
  211. my %exists;
  212. my @files;
  213. find({
  214. no_chdir => 1,
  215. wanted => sub {
  216. if (/\/\.svn\//) {
  217. $File::Find::prune=1;
  218. }
  219. elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
  220. my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
  221. if (! defined $f) {
  222. warn("skipping bad filename $_\n");
  223. }
  224. else {
  225. $f=~s/^\Q$srcdir\E\/?//;
  226. push @files, $f;
  227. $exists{pagename($f)}=1;
  228. }
  229. }
  230. },
  231. }, $srcdir);
  232. # check for added or removed pages
  233. my @adddel;
  234. foreach my $file (@files) {
  235. my $page=pagename($file);
  236. if (! $oldpagemtime{$page}) {
  237. debug("new page $page");
  238. push @adddel, $page;
  239. $links{$page}=[];
  240. }
  241. }
  242. foreach my $page (keys %oldpagemtime) {
  243. if (! $exists{$page}) {
  244. debug("removing old page $page");
  245. prune($destdir."/".$renderedfiles{$page});
  246. delete $renderedfiles{$page};
  247. $oldpagemtime{$page}=0;
  248. push @adddel, $page;
  249. }
  250. }
  251. # render any updated files
  252. foreach my $file (@files) {
  253. my $page=pagename($file);
  254. if (! exists $oldpagemtime{$page} ||
  255. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  256. debug("rendering changed file $file");
  257. render($file);
  258. }
  259. }
  260. # if any files were added or removed, check to see if each page
  261. # needs an update due to linking to them
  262. if (@adddel) {
  263. FILE: foreach my $file (@files) {
  264. my $page=pagename($file);
  265. foreach my $p (@adddel) {
  266. foreach my $link (@{$links{$page}}) {
  267. if (bestlink($page, $link) eq $p) {
  268. debug("rendering $file, which links to $p");
  269. render($file);
  270. next FILE;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. loadindex();
  278. refresh();
  279. saveindex();