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