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 if (! grep { $_ eq $bestlink } values %renderedfiles) {
176 $bestlink=htmlpage($bestlink);
178 if (! grep { $_ eq $bestlink } values %renderedfiles) {
179 return "<a href=\"?\">?</a>$link"
182 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
184 if (isinlinableimage($bestlink)) {
185 return "<img src=\"$bestlink\">";
187 return "<a href=\"$bestlink\">$link</a>";
194 $content =~ s/$link/htmllink(pagename($file), $1)/eg;
203 if ($type eq '.mdwn') {
204 return Markdown::Markdown($content);
207 error("htmlization of $type not supported");
216 foreach my $p (keys %links) {
217 if (grep { $_ eq $page } @{$links{$p}}) {
218 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
219 push @links, "<a href=\"$href\">$p</a>";
223 $content.="<hr><p>Links: ".join(" ", sort @links)."</p>\n" if @links;
231 my $title=basename($page);
236 foreach my $dir (reverse split("/", $page)) {
237 if (length($pagelink)) {
238 $pagelink="<a href=\"$path$dir.html\">$dir</a>/ $pagelink";
245 $path=~s/\.\.\/$/index.html/;
246 $pagelink="<a href=\"$path\">$wikiname</a>/ $pagelink";
248 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
249 "<h1>$pagelink</h1>\n".
251 "</body>\n</html>\n";
259 my $type=pagetype($file);
260 my $content=readpage($file);
261 if ($type ne 'unknown') {
262 my $page=pagename($file);
264 $links{$page}=[findlinks($content)];
266 $content=linkify($content, $file);
267 $content=htmlize($type, $content);
268 $content=linkbacks($content, $page);
269 $content=finalize($content, $page);
271 writepage(htmlpage($page), $content);
272 $oldpagemtime{$page}=time;
273 $renderedfiles{$page}=htmlpage($page);
277 writepage($file, $content);
278 $oldpagemtime{$file}=time;
279 $renderedfiles{$file}=$file;
284 open (IN, "$srcdir/.index") || return;
286 ($_)=/(.*)/; # untaint
288 my ($mtime, $file, $rendered, @links)=split(' ', $_);
289 my $page=pagename($file);
290 $pagesources{$page}=$file;
291 $oldpagemtime{$page}=$mtime;
292 $links{$page}=[@links];
293 $oldlinks{$page}=[@links];
294 $renderedfiles{$page}=$rendered;
300 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
301 foreach my $page (keys %oldpagemtime) {
302 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
303 join(" ", @{$links{$page}})."\n"
304 if $oldpagemtime{$page};
313 my $dir=dirname($file);
314 while (rmdir($dir)) {
320 # Find existing pages.
327 $File::Find::prune=1;
329 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
330 my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
332 warn("skipping bad filename $_\n");
335 $f=~s/^\Q$srcdir\E\/?//;
337 $exists{pagename($f)}=1;
345 # check for added or removed pages
347 foreach my $file (@files) {
348 my $page=pagename($file);
349 if (! $oldpagemtime{$page}) {
350 debug("new page $page");
353 $pagesources{$page}=$file;
357 foreach my $page (keys %oldpagemtime) {
358 if (! $exists{$page}) {
359 debug("removing old page $page");
360 push @del, $renderedfiles{$page};
361 prune($destdir."/".$renderedfiles{$page});
362 delete $renderedfiles{$page};
363 $oldpagemtime{$page}=0;
367 # render any updated files
368 foreach my $file (@files) {
369 my $page=pagename($file);
371 if (! exists $oldpagemtime{$page} ||
372 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
373 debug("rendering changed file $file");
378 debug("rebuilding unchanged file $file");
384 # if any files were added or removed, check to see if each page
385 # needs an update due to linking to them
387 FILE: foreach my $file (@files) {
388 next if $rendered{$file};
389 my $page=pagename($file);
390 foreach my $f (@add, @del) {
392 foreach my $link (@{$links{$page}}) {
393 if (bestlink($page, $link) eq $p) {
394 debug("rendering $file, which links to $p");
403 # handle linkbacks; if a page has added/removed links, update the
405 # TODO: inefficient; pages may get rendered above and again here;
406 # problem is the linkbacks could be wrong in the first pass render
410 foreach my $file (keys %rendered, @del) {
411 my $pagename=pagename($file);
412 if (exists $links{$pagename}) {
413 foreach my $link (@{$links{$pagename}}) {
414 if (! exists $oldlinks{$pagename} ||
415 ! grep { $_ eq $link } @{$oldlinks{$pagename}}) {
416 $linkchanged{$link}=1;
420 if (exists $oldlinks{$pagename}) {
421 foreach my $link (@{$oldlinks{$pagename}}) {
422 if (! exists $links{$pagename} ||
423 ! grep { $_ eq $link } @{$links{$pagename}}) {
424 $linkchanged{$link}=1;
429 foreach my $link (keys %linkchanged) {
430 my $linkfile=$pagesources{$link};
431 if (defined $linkfile) {
432 debug("rendering $linkfile, to update its linkbacks");