summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 65934edd1af0a0da450d7ab78bcbef6a7acbf247 (plain)
  1. #!/usr/bin/perl -T
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use Memoize;
  6. use File::Spec;
  7. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  8. BEGIN {
  9. $blosxom::version="is a proper perl module too much to ask?";
  10. do "/usr/bin/markdown";
  11. }
  12. my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles,
  13. %pagesources);
  14. my $link=qr/\[\[([^\s]+)\]\]/;
  15. my $verbose=0;
  16. my $wikiname="wiki";
  17. sub usage {
  18. die "usage: ikiwiki [options] source dest\n";
  19. }
  20. sub error ($) {
  21. die @_;
  22. }
  23. sub debug ($) {
  24. print "@_\n" if $verbose;
  25. }
  26. sub mtime ($) {
  27. my $page=shift;
  28. return (stat($page))[9];
  29. }
  30. sub basename {
  31. my $file=shift;
  32. $file=~s!.*/!!;
  33. return $file;
  34. }
  35. sub dirname {
  36. my $file=shift;
  37. $file=~s!/?[^/]+$!!;
  38. return $file;
  39. }
  40. sub pagetype ($) {
  41. my $page=shift;
  42. if ($page =~ /\.mdwn$/) {
  43. return ".mdwn";
  44. }
  45. else {
  46. return "unknown";
  47. }
  48. }
  49. sub pagename ($) {
  50. my $file=shift;
  51. my $type=pagetype($file);
  52. my $page=$file;
  53. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  54. return $page;
  55. }
  56. sub htmlpage ($) {
  57. my $page=shift;
  58. return $page.".html";
  59. }
  60. sub readpage ($) {
  61. my $page=shift;
  62. local $/=undef;
  63. open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
  64. my $ret=<PAGE>;
  65. close PAGE;
  66. return $ret;
  67. }
  68. sub writepage ($$) {
  69. my $page=shift;
  70. my $content=shift;
  71. my $dir=dirname("$destdir/$page");
  72. if (! -d $dir) {
  73. my $d="";
  74. foreach my $s (split(m!/+!, $dir)) {
  75. $d.="$s/";
  76. if (! -d $d) {
  77. mkdir($d) || error("failed to create directory $d: $!");
  78. }
  79. }
  80. }
  81. open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
  82. print PAGE $content;
  83. close PAGE;
  84. }
  85. sub findlinks {
  86. my $content=shift;
  87. my @links;
  88. while ($content =~ /$link/g) {
  89. push @links, lc($1);
  90. }
  91. return @links;
  92. }
  93. # Given a page and the text of a link on the page, determine which existing
  94. # page that link best points to. Prefers pages under a subdirectory with
  95. # the same name as the source page, failing that goes down the directory tree
  96. # to the base looking for matching pages.
  97. sub bestlink ($$) {
  98. my $page=shift;
  99. my $link=lc(shift);
  100. my $cwd=$page;
  101. do {
  102. my $l=$cwd;
  103. $l.="/" if length $l;
  104. $l.=$link;
  105. if (exists $links{$l}) {
  106. #debug("for $page, \"$link\", use $l");
  107. return $l;
  108. }
  109. } while $cwd=~s!/?[^/]+$!!;
  110. #print STDERR "warning: page $page, broken link: $link\n";
  111. return "";
  112. }
  113. sub isinlinableimage ($) {
  114. my $file=shift;
  115. $file=~/\.(png|gif|jpg|jpeg)$/;
  116. }
  117. sub htmllink ($$) {
  118. my $page=shift;
  119. my $link=shift;
  120. my $bestlink=bestlink($page, $link);
  121. return $link if $page eq $bestlink;
  122. # TODO BUG: %renderedfiles may not have it, if the linked to page
  123. # was also added and isn't yet rendered! Note that this bug is
  124. # masked by the bug mentioned below that makes all new files
  125. # be rendered twice.
  126. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  127. $bestlink=htmlpage($bestlink);
  128. }
  129. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  130. return "<a href=\"?\">?</a>$link"
  131. }
  132. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  133. if (isinlinableimage($bestlink)) {
  134. return "<img src=\"$bestlink\">";
  135. }
  136. return "<a href=\"$bestlink\">$link</a>";
  137. }
  138. sub linkify ($$) {
  139. my $content=shift;
  140. my $file=shift;
  141. $content =~ s/$link/htmllink(pagename($file), $1)/eg;
  142. return $content;
  143. }
  144. sub htmlize ($$) {
  145. my $type=shift;
  146. my $content=shift;
  147. if ($type eq '.mdwn') {
  148. return Markdown::Markdown($content);
  149. }
  150. else {
  151. error("htmlization of $type not supported");
  152. }
  153. }
  154. sub linkbacks ($$) {
  155. my $content=shift;
  156. my $page=shift;
  157. my @links;
  158. foreach my $p (keys %links) {
  159. next if bestlink($page, $p) eq $page;
  160. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  161. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  162. # Trim common dir prefixes from both pages.
  163. my $p_trimmed=$p;
  164. my $page_trimmed=$page;
  165. my $dir;
  166. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  167. defined $dir &&
  168. $p_trimmed=~s/^\Q$dir\E// &&
  169. $page_trimmed=~s/^\Q$dir\E//;
  170. push @links, "<a href=\"$href\">$p_trimmed</a>";
  171. }
  172. }
  173. $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
  174. return $content;
  175. }
  176. sub finalize ($$) {
  177. my $content=shift;
  178. my $page=shift;
  179. my $title=basename($page);
  180. $title=~s/_/ /g;
  181. my $pagelink="";
  182. my $path="";
  183. foreach my $dir (reverse split("/", $page)) {
  184. if (length($pagelink)) {
  185. $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
  186. }
  187. else {
  188. $pagelink=$dir;
  189. }
  190. $path.="../";
  191. }
  192. $path=~s/\.\.\/$/index.html/;
  193. $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
  194. $content="<html>\n<head><title>$title</title></head>\n<body>\n".
  195. "<h1>$pagelink</h1>\n".
  196. $content.
  197. "</body>\n</html>\n";
  198. return $content;
  199. }
  200. sub render ($) {
  201. my $file=shift;
  202. my $type=pagetype($file);
  203. my $content=readpage($file);
  204. if ($type ne 'unknown') {
  205. my $page=pagename($file);
  206. $links{$page}=[findlinks($content)];
  207. $content=linkify($content, $file);
  208. $content=htmlize($type, $content);
  209. $content=linkbacks($content, $page);
  210. $content=finalize($content, $page);
  211. writepage(htmlpage($page), $content);
  212. $oldpagemtime{$page}=time;
  213. $renderedfiles{$page}=htmlpage($page);
  214. }
  215. else {
  216. $links{$file}=[];
  217. writepage($file, $content);
  218. $oldpagemtime{$file}=time;
  219. $renderedfiles{$file}=$file;
  220. }
  221. }
  222. sub loadindex () {
  223. open (IN, "$srcdir/.index") || return;
  224. while (<IN>) {
  225. ($_)=/(.*)/; # untaint
  226. chomp;
  227. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  228. my $page=pagename($file);
  229. $pagesources{$page}=$file;
  230. $oldpagemtime{$page}=$mtime;
  231. $oldlinks{$page}=[@links];
  232. $links{$page}=[@links];
  233. $renderedfiles{$page}=$rendered;
  234. }
  235. close IN;
  236. }
  237. sub saveindex () {
  238. open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
  239. foreach my $page (keys %oldpagemtime) {
  240. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  241. join(" ", @{$links{$page}})."\n"
  242. if $oldpagemtime{$page};
  243. }
  244. close OUT;
  245. }
  246. sub prune ($) {
  247. my $file=shift;
  248. unlink($file);
  249. my $dir=dirname($file);
  250. while (rmdir($dir)) {
  251. $dir=dirname($dir);
  252. }
  253. }
  254. sub refresh () {
  255. # Find existing pages.
  256. my %exists;
  257. my @files;
  258. find({
  259. no_chdir => 1,
  260. wanted => sub {
  261. if (/\/\.svn\//) {
  262. $File::Find::prune=1;
  263. }
  264. elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
  265. my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
  266. if (! defined $f) {
  267. warn("skipping bad filename $_\n");
  268. }
  269. else {
  270. $f=~s/^\Q$srcdir\E\/?//;
  271. push @files, $f;
  272. $exists{pagename($f)}=1;
  273. }
  274. }
  275. },
  276. }, $srcdir);
  277. my %rendered;
  278. # check for added or removed pages
  279. my @add;
  280. foreach my $file (@files) {
  281. my $page=pagename($file);
  282. if (! $oldpagemtime{$page}) {
  283. debug("new page $page");
  284. push @add, $file;
  285. $links{$page}=[];
  286. $pagesources{$page}=$file;
  287. }
  288. }
  289. my @del;
  290. foreach my $page (keys %oldpagemtime) {
  291. if (! $exists{$page}) {
  292. debug("removing old page $page");
  293. push @del, $renderedfiles{$page};
  294. prune($destdir."/".$renderedfiles{$page});
  295. delete $renderedfiles{$page};
  296. $oldpagemtime{$page}=0;
  297. delete $pagesources{$page};
  298. }
  299. }
  300. # render any updated files
  301. foreach my $file (@files) {
  302. my $page=pagename($file);
  303. if (! exists $oldpagemtime{$page} ||
  304. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  305. debug("rendering changed file $file");
  306. render($file);
  307. $rendered{$file}=1;
  308. }
  309. }
  310. # if any files were added or removed, check to see if each page
  311. # needs an update due to linking to them
  312. # TODO: inefficient; pages may get rendered above and again here;
  313. # problem is the bestlink may have changed and we won't know until
  314. # now
  315. if (@add || @del) {
  316. FILE: foreach my $file (@files) {
  317. my $page=pagename($file);
  318. foreach my $f (@add, @del) {
  319. my $p=pagename($f);
  320. foreach my $link (@{$links{$page}}) {
  321. if (bestlink($page, $link) eq $p) {
  322. debug("rendering $file, which links to $p");
  323. render($file);
  324. $rendered{$file}=1;
  325. next FILE;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. # handle linkbacks; if a page has added/removed links, update the
  332. # pages it links to
  333. # TODO: inefficient; pages may get rendered above and again here;
  334. # problem is the linkbacks could be wrong in the first pass render
  335. # above
  336. if (%rendered) {
  337. my %linkchanged;
  338. foreach my $file (keys %rendered, @del) {
  339. my $page=pagename($file);
  340. if (exists $links{$page}) {
  341. foreach my $link (@{$links{$page}}) {
  342. $link=bestlink($page, $link);
  343. if (length $link &&
  344. ! exists $oldlinks{$page} ||
  345. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  346. $linkchanged{$link}=1;
  347. }
  348. }
  349. }
  350. if (exists $oldlinks{$page}) {
  351. foreach my $link (@{$oldlinks{$page}}) {
  352. $link=bestlink($page, $link);
  353. if (length $link &&
  354. ! exists $links{$page} ||
  355. ! grep { $_ eq $link } @{$links{$page}}) {
  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. # Generates a C wrapper program for running ikiwiki in a specific way.
  371. # The wrapper may be safely made suid.
  372. sub gen_wrapper ($$) {
  373. my ($offline, $rebuild)=@_;
  374. eval {use Cwd 'abs_path'};
  375. $srcdir=abs_path($srcdir);
  376. $destdir=abs_path($destdir);
  377. my $this=abs_path($0);
  378. if (! -x $this) {
  379. error("$this doesn't seem to be executable");
  380. }
  381. my $call=qq{"$this", "$this", "$srcdir", "$destdir", "--wikiname=$wikiname"};
  382. $call.=', "--verbose"' if $verbose;
  383. $call.=', "--rebuild"' if $rebuild;
  384. $call.=', "--offline"' if $offline;
  385. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  386. print OUT <<"EOF";
  387. /* A suid wraper for ikiwiki */
  388. #include <stdio.h>
  389. #include <unistd.h>
  390. #include <stdlib.h>
  391. int main (void) {
  392. unsetenv("PERLIO_DEBUG"); /* CAN-2005-0155 */
  393. execl($call, NULL);
  394. perror("failed to run $this");
  395. exit(1);
  396. }
  397. EOF
  398. close OUT;
  399. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  400. error("failed to compile ikiwiki-wrap.c");
  401. }
  402. unlink("ikiwiki-wrap.c");
  403. print "successfully generated ikiwiki-wrap\n";
  404. exit 0;
  405. }
  406. sub update () {
  407. if (-d "$srcdir/.svn") {
  408. if (system("svn", "update", "--quiet", $srcdir) != 0) {
  409. warn("svn update failed\n");
  410. }
  411. }
  412. }
  413. my $rebuild=0;
  414. my $offline=0;
  415. my $gen_wrapper=0;
  416. if (grep /^-/, @ARGV) {
  417. eval {use Getopt::Long};
  418. GetOptions(
  419. "wikiname=s" => \$wikiname,
  420. "verbose|v" => \$verbose,
  421. "rebuild" => \$rebuild,
  422. "gen-wrapper" => \$gen_wrapper,
  423. "offline" => \$offline,
  424. ) || usage();
  425. }
  426. usage() unless @ARGV == 2;
  427. ($srcdir) = shift =~ /(.*)/; # untaint
  428. ($destdir) = shift =~ /(.*)/; # untaint
  429. gen_wrapper($offline, $rebuild) if $gen_wrapper;
  430. memoize('pagename');
  431. memoize('bestlink');
  432. update() unless $offline;
  433. loadindex() unless $rebuild;
  434. refresh();
  435. saveindex();