10 $blosxom::version="is a proper perl module too much to ask?";
11 do "/usr/bin/markdown";
18 die "usage: ikiwiki [options] source dest\n";
21 my $link=qr/\[\[([^\s]+)\]\]/;
25 if (grep /^-/, @ARGV) {
26 eval {use Getopt::Long};
28 "wikiname=s" => \$wikiname,
29 "verbose|v" => \$verbose,
30 "rebuild" => \$rebuild,
33 usage() unless @ARGV == 2;
34 my ($srcdir) = shift =~ /(.*)/; # untaint
35 my ($destdir) = shift =~ /(.*)/; # untaint
48 print "@_\n" if $verbose;
54 return (stat($page))[9];
74 if ($page =~ /\.mdwn$/) {
85 my $type=pagetype($file);
87 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
101 open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
111 my $dir=dirname("$destdir/$page");
114 foreach my $s (split(m!/+!, $dir)) {
117 mkdir($d) || error("failed to create directory $d: $!");
122 open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
131 while ($content =~ /$link/g) {
137 # Given a page and the text of a link on the page, determine which existing
138 # page that link best points to. Prefers pages under a subdirectory with
139 # the same name as the source page, failing that goes down the directory tree
140 # to the base looking for matching pages.
148 $l.="/" if length $l;
151 if (exists $links{$l}) {
152 #debug("for $page, \"$link\", use $l");
155 } while $cwd=~s!/?[^/]+$!!;
157 print STDERR "warning: page $page, broken link: $link\n";
161 sub isinlinableimage ($) {
164 $file=~/\.(png|gif|jpg|jpeg)$/;
171 my $bestlink=bestlink($page, $link);
173 return $link if $page eq $bestlink;
175 # TODO BUG: %renderedfiles may not have it, if the linked to page
176 # was also added and isn't yet rendered!
177 if (! grep { $_ eq $bestlink } values %renderedfiles) {
178 $bestlink=htmlpage($bestlink);
180 if (! grep { $_ eq $bestlink } values %renderedfiles) {
181 return "<a href=\"?\">?</a>$link"
184 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
186 if (isinlinableimage($bestlink)) {
187 return "<img src=\"$bestlink\">";
189 return "<a href=\"$bestlink\">$link</a>";
196 $content =~ s/$link/htmllink(pagename($file), $1)/eg;
205 if ($type eq '.mdwn') {
206 return Markdown::Markdown($content);
209 error("htmlization of $type not supported");
218 foreach my $p (keys %links) {
219 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
220 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
221 # TODO: if $p is joey/foo/bar and
222 # the $page is joey/foo/baz, make the link text
224 push @links, "<a href=\"$href\">$p</a>";
228 $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
236 my $title=basename($page);
241 foreach my $dir (reverse split("/", $page)) {
242 if (length($pagelink)) {
243 $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
250 $path=~s/\.\.\/$/index.html/;
251 $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
253 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
254 "<h1>$pagelink</h1>\n".
256 "</body>\n</html>\n";
264 my $type=pagetype($file);
265 my $content=readpage($file);
266 if ($type ne 'unknown') {
267 my $page=pagename($file);
269 $links{$page}=[findlinks($content)];
271 $content=linkify($content, $file);
272 $content=htmlize($type, $content);
273 $content=linkbacks($content, $page);
274 $content=finalize($content, $page);
276 writepage(htmlpage($page), $content);
277 $oldpagemtime{$page}=time;
278 $renderedfiles{$page}=htmlpage($page);
282 writepage($file, $content);
283 $oldpagemtime{$file}=time;
284 $renderedfiles{$file}=$file;
289 open (IN, "$srcdir/.index") || return;
291 ($_)=/(.*)/; # untaint
293 my ($mtime, $file, $rendered, @links)=split(' ', $_);
294 my $page=pagename($file);
295 $pagesources{$page}=$file;
296 $oldpagemtime{$page}=$mtime;
297 $oldlinks{$page}=[@links];
298 $links{$page}=[@links];
299 $renderedfiles{$page}=$rendered;
305 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
306 foreach my $page (keys %oldpagemtime) {
307 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
308 join(" ", @{$links{$page}})."\n"
309 if $oldpagemtime{$page};
318 my $dir=dirname($file);
319 while (rmdir($dir)) {
325 # Find existing pages.
332 $File::Find::prune=1;
334 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
335 my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
337 warn("skipping bad filename $_\n");
340 $f=~s/^\Q$srcdir\E\/?//;
342 $exists{pagename($f)}=1;
350 # check for added or removed pages
352 foreach my $file (@files) {
353 my $page=pagename($file);
354 if (! $oldpagemtime{$page}) {
355 debug("new page $page");
358 $pagesources{$page}=$file;
362 foreach my $page (keys %oldpagemtime) {
363 if (! $exists{$page}) {
364 debug("removing old page $page");
365 push @del, $renderedfiles{$page};
366 prune($destdir."/".$renderedfiles{$page});
367 delete $renderedfiles{$page};
368 $oldpagemtime{$page}=0;
369 delete $pagesources{$page};
373 # render any updated files
374 foreach my $file (@files) {
375 my $page=pagename($file);
377 if (! exists $oldpagemtime{$page} ||
378 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
379 debug("rendering changed file $file");
385 # if any files were added or removed, check to see if each page
386 # needs an update due to linking to them
387 # TODO: inefficient; pages may get rendered above and again here;
388 # problem is the bestlink may have changes and we won't know until
391 FILE: foreach my $file (@files) {
392 my $page=pagename($file);
393 foreach my $f (@add, @del) {
395 foreach my $link (@{$links{$page}}) {
396 if (bestlink($page, $link) eq $p) {
397 debug("rendering $file, which links to $p");
407 # handle linkbacks; if a page has added/removed links, update the
409 # TODO: inefficient; pages may get rendered above and again here;
410 # problem is the linkbacks could be wrong in the first pass render
414 foreach my $file (keys %rendered, @del) {
415 my $page=pagename($file);
416 if (exists $links{$page}) {
417 foreach my $link (@{$links{$page}}) {
418 $link=bestlink($page, $link);
420 ! exists $oldlinks{$page} ||
421 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
422 $linkchanged{$link}=1;
426 if (exists $oldlinks{$page}) {
427 foreach my $link (@{$oldlinks{$page}}) {
428 $link=bestlink($page, $link);
430 ! exists $links{$page} ||
431 ! grep { $_ eq $link } @{$links{$page}}) {
432 $linkchanged{$link}=1;
437 foreach my $link (keys %linkchanged) {
438 my $linkfile=$pagesources{$link};
439 if (defined $linkfile) {
440 debug("rendering $linkfile, to update its linkbacks");
447 loadindex() unless $rebuild;