summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 9aaeed86ca64ae51ae535ab0928c563c3e28eaa1 (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. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  139. $bestlink=htmlpage($bestlink);
  140. }
  141. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  142. return "<a href=\"?\">?</a>$link"
  143. }
  144. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  145. if (isinlinableimage($bestlink)) {
  146. return "<img src=\"$bestlink\">";
  147. }
  148. return "<a href=\"$bestlink\">$link</a>";
  149. }
  150. sub linkify ($$) {
  151. my $content=shift;
  152. my $file=shift;
  153. $content =~ s/$link/htmllink(pagename($file), $1)/eg;
  154. return $content;
  155. }
  156. sub htmlize ($$) {
  157. my $type=shift;
  158. my $content=shift;
  159. if ($type eq '.mdwn') {
  160. return Markdown::Markdown($content);
  161. }
  162. else {
  163. error("htmlization of $type not supported");
  164. }
  165. }
  166. sub linkbacks ($$) {
  167. my $content=shift;
  168. my $page=shift;
  169. my @links;
  170. foreach my $p (keys %links) {
  171. if (grep { $_ eq $page } @{$links{$p}}) {
  172. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  173. push @links, "<a href=\"$href\">$p</a>";
  174. }
  175. }
  176. $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n";
  177. }
  178. sub finalize ($$) {
  179. my $content=shift;
  180. my $page=shift;
  181. my $title=basename($page);
  182. $title=~s/_/ /g;
  183. my $pagelink="";
  184. my $path="";
  185. foreach my $dir (reverse split("/", $page)) {
  186. if (length($pagelink)) {
  187. $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
  188. }
  189. else {
  190. $pagelink=$dir;
  191. }
  192. $path.="../";
  193. }
  194. $path=~s/\.\.\/$/index.html/;
  195. $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
  196. $content="<html>\n<head><title>$title</title></head>\n<body>\n".
  197. "<h1>$pagelink</h1>\n".
  198. $content.
  199. "</body>\n</html>\n";
  200. return $content;
  201. }
  202. sub render ($) {
  203. my $file=shift;
  204. my $type=pagetype($file);
  205. my $content=readpage($file);
  206. if ($type ne 'unknown') {
  207. my $page=pagename($file);
  208. $links{$page}=[findlinks($content)];
  209. $content=linkify($content, $file);
  210. $content=htmlize($type, $content);
  211. $content=linkbacks($content, $page);
  212. $content=finalize($content, $page);
  213. writepage(htmlpage($page), $content);
  214. $oldpagemtime{$page}=time;
  215. $renderedfiles{$page}=htmlpage($page);
  216. }
  217. else {
  218. $links{$file}=[];
  219. writepage($file, $content);
  220. $oldpagemtime{$file}=time;
  221. $renderedfiles{$file}=$file;
  222. }
  223. }
  224. sub loadindex () {
  225. open (IN, "$srcdir/.index") || return;
  226. while (<IN>) {
  227. ($_)=/(.*)/; # untaint
  228. chomp;
  229. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  230. my $page=pagename($file);
  231. $pagesources{$page}=$file;
  232. $oldpagemtime{$page}=$mtime;
  233. $links{$page}=[@links];
  234. $oldlinks{$page}=[@links];
  235. $renderedfiles{$page}=$rendered;
  236. }
  237. close IN;
  238. }
  239. sub saveindex () {
  240. open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
  241. foreach my $page (keys %oldpagemtime) {
  242. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  243. join(" ", @{$links{$page}})."\n"
  244. if $oldpagemtime{$page};
  245. }
  246. close OUT;
  247. }
  248. sub prune ($) {
  249. my $file=shift;
  250. unlink($file);
  251. my $dir=dirname($file);
  252. while (rmdir($dir)) {
  253. $dir=dirname($dir);
  254. }
  255. }
  256. sub refresh () {
  257. # Find existing pages.
  258. my %exists;
  259. my @files;
  260. find({
  261. no_chdir => 1,
  262. wanted => sub {
  263. if (/\/\.svn\//) {
  264. $File::Find::prune=1;
  265. }
  266. elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
  267. my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
  268. if (! defined $f) {
  269. warn("skipping bad filename $_\n");
  270. }
  271. else {
  272. $f=~s/^\Q$srcdir\E\/?//;
  273. push @files, $f;
  274. $exists{pagename($f)}=1;
  275. }
  276. }
  277. },
  278. }, $srcdir);
  279. my %rendered;
  280. # check for added or removed pages
  281. my @add;
  282. foreach my $file (@files) {
  283. my $page=pagename($file);
  284. if (! $oldpagemtime{$page}) {
  285. debug("new page $page");
  286. push @add, $file;
  287. $links{$page}=[];
  288. $pagesources{$page}=$file;
  289. }
  290. }
  291. my @del;
  292. foreach my $page (keys %oldpagemtime) {
  293. if (! $exists{$page}) {
  294. debug("removing old page $page");
  295. push @del, $renderedfiles{$page};
  296. prune($destdir."/".$renderedfiles{$page});
  297. delete $renderedfiles{$page};
  298. $oldpagemtime{$page}=0;
  299. }
  300. }
  301. # render any updated files
  302. foreach my $file (@files) {
  303. my $page=pagename($file);
  304. if (! exists $oldpagemtime{$page} ||
  305. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  306. debug("rendering changed file $file");
  307. render($file);
  308. $rendered{$file}=1;
  309. }
  310. elsif ($rebuild) {
  311. debug("rebuilding unchanged file $file");
  312. render($file);
  313. $rendered{$file}=1;
  314. }
  315. }
  316. # if any files were added or removed, check to see if each page
  317. # needs an update due to linking to them
  318. if (@add || @del) {
  319. FILE: foreach my $file (@files) {
  320. next if $rendered{$file};
  321. my $page=pagename($file);
  322. foreach my $f (@add, @del) {
  323. my $p=pagename($f);
  324. foreach my $link (@{$links{$page}}) {
  325. if (bestlink($page, $link) eq $p) {
  326. debug("rendering $file, which links to $p");
  327. render($file);
  328. next FILE;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. # handle linkbacks; if a page has added/removed links, update the
  335. # pages it links to
  336. # TODO: inefficient; pages may get rendered above and again here;
  337. # problem is the linkbacks could be wrong in the first pass render
  338. # above.
  339. if (%rendered) {
  340. my %linkchanged;
  341. foreach my $file (keys %rendered, @del) {
  342. my $pagename=pagename($file);
  343. if (exists $links{$pagename}) {
  344. foreach my $link (@{$links{$pagename}}) {
  345. if (! exists $oldlinks{$pagename} ||
  346. ! grep { $_ eq $link } @{$oldlinks{$pagename}}) {
  347. $linkchanged{$link}=1;
  348. }
  349. }
  350. }
  351. if (exists $oldlinks{$pagename}) {
  352. foreach my $link (@{$oldlinks{$pagename}}) {
  353. if (! exists $links{$pagename} ||
  354. ! grep { $_ eq $link } @{$links{$pagename}}) {
  355. $linkchanged{$link}=1;
  356. }
  357. }
  358. }
  359. }
  360. foreach my $link (keys %linkchanged) {
  361. my $linkfile=$pagesources{$link};
  362. if (defined $linkfile) {
  363. debug("rendering $linkfile, to update its linkbacks");
  364. render($linkfile);
  365. }
  366. }
  367. }
  368. }
  369. loadindex();
  370. refresh();
  371. saveindex();