summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 2808ccc78eab6074c2c8d1a23f8d2f775cee2f91 (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. sub usage {
  14. die "usage: ikiwiki [options] source dest\n";
  15. }
  16. my $link=qr/\[\[([^\s]+)\]\]/;
  17. my $verbose=0;
  18. my $rebuild=0;
  19. my $wikiname="wiki";
  20. if (grep /^-/, @ARGV) {
  21. eval {use Getopt::Long};
  22. GetOptions(
  23. "wikiname=s" => \$wikiname,
  24. "verbose|v" => \$verbose,
  25. "rebuild" => \$rebuild,
  26. ) || usage();
  27. }
  28. usage() unless @ARGV == 2;
  29. my ($srcdir) = shift =~ /(.*)/; # untaint
  30. my ($destdir) = shift =~ /(.*)/; # untaint
  31. my %links;
  32. my %oldlinks;
  33. my %oldpagemtime;
  34. my %renderedfiles;
  35. my %pagesources;
  36. sub error ($) {
  37. die @_;
  38. }
  39. sub debug ($) {
  40. print "@_\n" if $verbose;
  41. }
  42. sub mtime ($) {
  43. my $page=shift;
  44. return (stat($page))[9];
  45. }
  46. sub basename {
  47. my $file=shift;
  48. $file=~s!.*/!!;
  49. return $file;
  50. }
  51. sub dirname {
  52. my $file=shift;
  53. $file=~s!/?[^/]+$!!;
  54. return $file;
  55. }
  56. sub pagetype ($) {
  57. my $page=shift;
  58. if ($page =~ /\.mdwn$/) {
  59. return ".mdwn";
  60. }
  61. else {
  62. return "unknown";
  63. }
  64. }
  65. sub pagename ($) {
  66. my $file=shift;
  67. my $type=pagetype($file);
  68. my $page=$file;
  69. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  70. return $page;
  71. }
  72. sub htmlpage ($) {
  73. my $page=shift;
  74. return $page.".html";
  75. }
  76. sub readpage ($) {
  77. my $page=shift;
  78. local $/=undef;
  79. open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
  80. my $ret=<PAGE>;
  81. close PAGE;
  82. return $ret;
  83. }
  84. sub writepage ($$) {
  85. my $page=shift;
  86. my $content=shift;
  87. my $dir=dirname("$destdir/$page");
  88. if (! -d $dir) {
  89. my $d="";
  90. foreach my $s (split(m!/+!, $dir)) {
  91. $d.="$s/";
  92. if (! -d $d) {
  93. mkdir($d) || error("failed to create directory $d: $!");
  94. }
  95. }
  96. }
  97. open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
  98. print PAGE $content;
  99. close PAGE;
  100. }
  101. sub findlinks {
  102. my $content=shift;
  103. my @links;
  104. while ($content =~ /$link/g) {
  105. push @links, lc($1);
  106. }
  107. return @links;
  108. }
  109. # Given a page and the text of a link on the page, determine which existing
  110. # page that link best points to. Prefers pages under a subdirectory with
  111. # the same name as the source page, failing that goes down the directory tree
  112. # to the base looking for matching pages.
  113. sub bestlink ($$) {
  114. my $page=shift;
  115. my $link=lc(shift);
  116. my $cwd=$page;
  117. do {
  118. my $l=$cwd;
  119. $l.="/" if length $l;
  120. $l.=$link;
  121. if (exists $links{$l}) {
  122. #debug("for $page, \"$link\", use $l");
  123. return $l;
  124. }
  125. } while $cwd=~s!/?[^/]+$!!;
  126. print STDERR "warning: page $page, broken link: $link\n";
  127. return "";
  128. }
  129. sub isinlinableimage ($) {
  130. my $file=shift;
  131. $file=~/\.(png|gif|jpg|jpeg)$/;
  132. }
  133. sub htmllink ($$) {
  134. my $page=shift;
  135. my $link=shift;
  136. my $bestlink=bestlink($page, $link);
  137. return $link if $page eq $bestlink;
  138. # TODO BUG: %renderedfiles may not have it, if the linked to page
  139. # was also added and isn't yet rendered!
  140. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  141. $bestlink=htmlpage($bestlink);
  142. }
  143. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  144. return "<a href=\"?\">?</a>$link"
  145. }
  146. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  147. if (isinlinableimage($bestlink)) {
  148. return "<img src=\"$bestlink\">";
  149. }
  150. return "<a href=\"$bestlink\">$link</a>";
  151. }
  152. sub linkify ($$) {
  153. my $content=shift;
  154. my $file=shift;
  155. $content =~ s/$link/htmllink(pagename($file), $1)/eg;
  156. return $content;
  157. }
  158. sub htmlize ($$) {
  159. my $type=shift;
  160. my $content=shift;
  161. if ($type eq '.mdwn') {
  162. return Markdown::Markdown($content);
  163. }
  164. else {
  165. error("htmlization of $type not supported");
  166. }
  167. }
  168. sub linkbacks ($$) {
  169. my $content=shift;
  170. my $page=shift;
  171. my @links;
  172. foreach my $p (keys %links) {
  173. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  174. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  175. # TODO: if $p is joey/foo/bar and
  176. # the $page is joey/foo/baz, make the link text
  177. # just be "bar".
  178. push @links, "<a href=\"$href\">$p</a>";
  179. }
  180. }
  181. $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
  182. return $content;
  183. }
  184. sub finalize ($$) {
  185. my $content=shift;
  186. my $page=shift;
  187. my $title=basename($page);
  188. $title=~s/_/ /g;
  189. my $pagelink="";
  190. my $path="";
  191. foreach my $dir (reverse split("/", $page)) {
  192. if (length($pagelink)) {
  193. $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
  194. }
  195. else {
  196. $pagelink=$dir;
  197. }
  198. $path.="../";
  199. }
  200. $path=~s/\.\.\/$/index.html/;
  201. $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
  202. $content="<html>\n<head><title>$title</title></head>\n<body>\n".
  203. "<h1>$pagelink</h1>\n".
  204. $content.
  205. "</body>\n</html>\n";
  206. return $content;
  207. }
  208. sub render ($) {
  209. my $file=shift;
  210. my $type=pagetype($file);
  211. my $content=readpage($file);
  212. if ($type ne 'unknown') {
  213. my $page=pagename($file);
  214. $links{$page}=[findlinks($content)];
  215. $content=linkify($content, $file);
  216. $content=htmlize($type, $content);
  217. $content=linkbacks($content, $page);
  218. $content=finalize($content, $page);
  219. writepage(htmlpage($page), $content);
  220. $oldpagemtime{$page}=time;
  221. $renderedfiles{$page}=htmlpage($page);
  222. }
  223. else {
  224. $links{$file}=[];
  225. writepage($file, $content);
  226. $oldpagemtime{$file}=time;
  227. $renderedfiles{$file}=$file;
  228. }
  229. }
  230. sub loadindex () {
  231. open (IN, "$srcdir/.index") || return;
  232. while (<IN>) {
  233. ($_)=/(.*)/; # untaint
  234. chomp;
  235. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  236. my $page=pagename($file);
  237. $pagesources{$page}=$file;
  238. $oldpagemtime{$page}=$mtime;
  239. $oldlinks{$page}=[@links];
  240. $links{$page}=[@links];
  241. $renderedfiles{$page}=$rendered;
  242. }
  243. close IN;
  244. }
  245. sub saveindex () {
  246. open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
  247. foreach my $page (keys %oldpagemtime) {
  248. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  249. join(" ", @{$links{$page}})."\n"
  250. if $oldpagemtime{$page};
  251. }
  252. close OUT;
  253. }
  254. sub prune ($) {
  255. my $file=shift;
  256. unlink($file);
  257. my $dir=dirname($file);
  258. while (rmdir($dir)) {
  259. $dir=dirname($dir);
  260. }
  261. }
  262. sub refresh () {
  263. # Find existing pages.
  264. my %exists;
  265. my @files;
  266. find({
  267. no_chdir => 1,
  268. wanted => sub {
  269. if (/\/\.svn\//) {
  270. $File::Find::prune=1;
  271. }
  272. elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
  273. my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
  274. if (! defined $f) {
  275. warn("skipping bad filename $_\n");
  276. }
  277. else {
  278. $f=~s/^\Q$srcdir\E\/?//;
  279. push @files, $f;
  280. $exists{pagename($f)}=1;
  281. }
  282. }
  283. },
  284. }, $srcdir);
  285. my %rendered;
  286. # check for added or removed pages
  287. my @add;
  288. foreach my $file (@files) {
  289. my $page=pagename($file);
  290. if (! $oldpagemtime{$page}) {
  291. debug("new page $page");
  292. push @add, $file;
  293. $links{$page}=[];
  294. $pagesources{$page}=$file;
  295. }
  296. }
  297. my @del;
  298. foreach my $page (keys %oldpagemtime) {
  299. if (! $exists{$page}) {
  300. debug("removing old page $page");
  301. push @del, $renderedfiles{$page};
  302. prune($destdir."/".$renderedfiles{$page});
  303. delete $renderedfiles{$page};
  304. $oldpagemtime{$page}=0;
  305. delete $pagesources{$page};
  306. }
  307. }
  308. # render any updated files
  309. foreach my $file (@files) {
  310. my $page=pagename($file);
  311. if (! exists $oldpagemtime{$page} ||
  312. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  313. debug("rendering changed file $file");
  314. render($file);
  315. $rendered{$file}=1;
  316. }
  317. }
  318. # if any files were added or removed, check to see if each page
  319. # needs an update due to linking to them
  320. # TODO: inefficient; pages may get rendered above and again here;
  321. # problem is the bestlink may have changes and we won't know until
  322. # now
  323. if (@add || @del) {
  324. FILE: foreach my $file (@files) {
  325. my $page=pagename($file);
  326. foreach my $f (@add, @del) {
  327. my $p=pagename($f);
  328. foreach my $link (@{$links{$page}}) {
  329. if (bestlink($page, $link) eq $p) {
  330. debug("rendering $file, which links to $p");
  331. render($file);
  332. $rendered{$file}=1;
  333. next FILE;
  334. }
  335. }
  336. }
  337. }
  338. }
  339. # handle linkbacks; if a page has added/removed links, update the
  340. # pages it links to
  341. # TODO: inefficient; pages may get rendered above and again here;
  342. # problem is the linkbacks could be wrong in the first pass render
  343. # above.
  344. if (%rendered) {
  345. my %linkchanged;
  346. foreach my $file (keys %rendered, @del) {
  347. my $page=pagename($file);
  348. if (exists $links{$page}) {
  349. foreach my $link (@{$links{$page}}) {
  350. $link=bestlink($page, $link);
  351. if (length $link &&
  352. ! exists $oldlinks{$page} ||
  353. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  354. $linkchanged{$link}=1;
  355. }
  356. }
  357. }
  358. if (exists $oldlinks{$page}) {
  359. foreach my $link (@{$oldlinks{$page}}) {
  360. $link=bestlink($page, $link);
  361. if (length $link &&
  362. ! exists $links{$page} ||
  363. ! grep { $_ eq $link } @{$links{$page}}) {
  364. $linkchanged{$link}=1;
  365. }
  366. }
  367. }
  368. }
  369. foreach my $link (keys %linkchanged) {
  370. my $linkfile=$pagesources{$link};
  371. if (defined $linkfile) {
  372. debug("rendering $linkfile, to update its linkbacks");
  373. render($linkfile);
  374. }
  375. }
  376. }
  377. }
  378. loadindex() unless $rebuild;
  379. refresh();
  380. saveindex();