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