9 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
12 $blosxom::version="is a proper perl module too much to ask?";
13 do "/usr/bin/markdown";
16 my ($srcdir, $destdir, %links, %oldlinks, %oldpagemtime, %renderedfiles,
18 my $link=qr/\[\[([^\s]+)\]\]/;
23 die "usage: ikiwiki [options] source dest\n";
31 print "@_\n" if $verbose;
37 return (stat($page))[9];
57 if ($page =~ /\.mdwn$/) {
68 my $type=pagetype($file);
70 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
84 open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
94 my $dir=dirname("$destdir/$page");
97 foreach my $s (split(m!/+!, $dir)) {
100 mkdir($d) || error("failed to create directory $d: $!");
105 open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
114 while ($content =~ /$link/g) {
120 # Given a page and the text of a link on the page, determine which existing
121 # page that link best points to. Prefers pages under a subdirectory with
122 # the same name as the source page, failing that goes down the directory tree
123 # to the base looking for matching pages.
131 $l.="/" if length $l;
134 if (exists $links{$l}) {
135 #debug("for $page, \"$link\", use $l");
138 } while $cwd=~s!/?[^/]+$!!;
140 #print STDERR "warning: page $page, broken link: $link\n";
144 sub isinlinableimage ($) {
147 $file=~/\.(png|gif|jpg|jpeg)$/;
154 my $bestlink=bestlink($page, $link);
156 return $link if $page eq $bestlink;
158 # TODO BUG: %renderedfiles may not have it, if the linked to page
159 # was also added and isn't yet rendered! Note that this bug is
160 # masked by the bug mentioned below that makes all new files
162 if (! grep { $_ eq $bestlink } values %renderedfiles) {
163 $bestlink=htmlpage($bestlink);
165 if (! grep { $_ eq $bestlink } values %renderedfiles) {
166 return "<a href=\"?\">?</a>$link"
169 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
171 if (isinlinableimage($bestlink)) {
172 return "<img src=\"$bestlink\">";
174 return "<a href=\"$bestlink\">$link</a>";
181 $content =~ s/$link/htmllink(pagename($file), $1)/eg;
190 if ($type eq '.mdwn') {
191 return Markdown::Markdown($content);
194 error("htmlization of $type not supported");
203 foreach my $p (keys %links) {
204 next if bestlink($page, $p) eq $page;
205 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
206 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
208 # Trim common dir prefixes from both pages.
210 my $page_trimmed=$page;
212 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
214 $p_trimmed=~s/^\Q$dir\E// &&
215 $page_trimmed=~s/^\Q$dir\E//;
217 push @links, "<a href=\"$href\">$p_trimmed</a>";
221 $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
229 my $title=basename($page);
234 foreach my $dir (reverse split("/", $page)) {
235 if (length($pagelink)) {
236 $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
243 $path=~s/\.\.\/$/index.html/;
244 $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
246 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
247 "<h1>$pagelink</h1>\n".
249 "</body>\n</html>\n";
257 my $type=pagetype($file);
258 my $content=readpage($file);
259 if ($type ne 'unknown') {
260 my $page=pagename($file);
262 $links{$page}=[findlinks($content)];
264 $content=linkify($content, $file);
265 $content=htmlize($type, $content);
266 $content=linkbacks($content, $page);
267 $content=finalize($content, $page);
269 writepage(htmlpage($page), $content);
270 $oldpagemtime{$page}=time;
271 $renderedfiles{$page}=htmlpage($page);
275 writepage($file, $content);
276 $oldpagemtime{$file}=time;
277 $renderedfiles{$file}=$file;
282 open (IN, "$srcdir/.index") || return;
284 ($_)=/(.*)/; # untaint
286 my ($mtime, $file, $rendered, @links)=split(' ', $_);
287 my $page=pagename($file);
288 $pagesources{$page}=$file;
289 $oldpagemtime{$page}=$mtime;
290 $oldlinks{$page}=[@links];
291 $links{$page}=[@links];
292 $renderedfiles{$page}=$rendered;
298 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
299 foreach my $page (keys %oldpagemtime) {
300 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
301 join(" ", @{$links{$page}})."\n"
302 if $oldpagemtime{$page};
311 my $dir=dirname($file);
312 while (rmdir($dir)) {
318 # Find existing pages.
325 $File::Find::prune=1;
327 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
328 my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
330 warn("skipping bad filename $_\n");
333 $f=~s/^\Q$srcdir\E\/?//;
335 $exists{pagename($f)}=1;
343 # check for added or removed pages
345 foreach my $file (@files) {
346 my $page=pagename($file);
347 if (! $oldpagemtime{$page}) {
348 debug("new page $page");
351 $pagesources{$page}=$file;
355 foreach my $page (keys %oldpagemtime) {
356 if (! $exists{$page}) {
357 debug("removing old page $page");
358 push @del, $renderedfiles{$page};
359 prune($destdir."/".$renderedfiles{$page});
360 delete $renderedfiles{$page};
361 $oldpagemtime{$page}=0;
362 delete $pagesources{$page};
366 # render any updated files
367 foreach my $file (@files) {
368 my $page=pagename($file);
370 if (! exists $oldpagemtime{$page} ||
371 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
372 debug("rendering changed file $file");
378 # if any files were added or removed, check to see if each page
379 # needs an update due to linking to them
380 # TODO: inefficient; pages may get rendered above and again here;
381 # problem is the bestlink may have changed and we won't know until
384 FILE: foreach my $file (@files) {
385 my $page=pagename($file);
386 foreach my $f (@add, @del) {
388 foreach my $link (@{$links{$page}}) {
389 if (bestlink($page, $link) eq $p) {
390 debug("rendering $file, which links to $p");
400 # handle linkbacks; if a page has added/removed links, update the
402 # TODO: inefficient; pages may get rendered above and again here;
403 # problem is the linkbacks could be wrong in the first pass render
407 foreach my $file (keys %rendered, @del) {
408 my $page=pagename($file);
409 if (exists $links{$page}) {
410 foreach my $link (@{$links{$page}}) {
411 $link=bestlink($page, $link);
413 ! exists $oldlinks{$page} ||
414 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
415 $linkchanged{$link}=1;
419 if (exists $oldlinks{$page}) {
420 foreach my $link (@{$oldlinks{$page}}) {
421 $link=bestlink($page, $link);
423 ! exists $links{$page} ||
424 ! grep { $_ eq $link } @{$links{$page}}) {
425 $linkchanged{$link}=1;
430 foreach my $link (keys %linkchanged) {
431 my $linkfile=$pagesources{$link};
432 if (defined $linkfile) {
433 debug("rendering $linkfile, to update its linkbacks");
440 # Generates a C wrapper program for running ikiwiki in a specific way.
441 # The wrapper may be safely made suid.
442 sub gen_wrapper ($$) {
443 my ($offline, $rebuild)=@_;
445 eval {use Cwd 'abs_path'};
446 $srcdir=abs_path($srcdir);
447 $destdir=abs_path($destdir);
448 my $this=abs_path($0);
450 error("$this doesn't seem to be executable");
453 my $call=qq{"$this", "$this", "$srcdir", "$destdir", "--wikiname=$wikiname"};
454 $call.=', "--verbose"' if $verbose;
455 $call.=', "--rebuild"' if $rebuild;
456 $call.=', "--offline"' if $offline;
458 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
460 /* A suid wraper for ikiwiki */
466 unsetenv("PERLIO_DEBUG"); /* CAN-2005-0155 */
468 perror("failed to run $this");
473 if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
474 error("failed to compile ikiwiki-wrap.c");
476 unlink("ikiwiki-wrap.c");
477 print "successfully generated ikiwiki-wrap\n";
482 if (-d "$srcdir/.svn") {
483 if (system("svn", "update", "--quiet", $srcdir) != 0) {
484 warn("svn update failed\n");
492 if (grep /^-/, @ARGV) {
493 eval {use Getopt::Long};
495 "wikiname=s" => \$wikiname,
496 "verbose|v" => \$verbose,
497 "rebuild" => \$rebuild,
498 "gen-wrapper" => \$gen_wrapper,
499 "offline" => \$offline,
502 usage() unless @ARGV == 2;
503 ($srcdir) = shift =~ /(.*)/; # untaint
504 ($destdir) = shift =~ /(.*)/; # untaint
506 gen_wrapper($offline, $rebuild) if $gen_wrapper;
509 update() unless $offline;
510 loadindex() unless $rebuild;