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