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
46 print "@_\n" if $verbose;
52 return (stat($page))[9];
72 if ($page =~ /\.mdwn$/) {
83 my $type=pagetype($file);
85 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
99 open (PAGE, "$srcdir/$page") || error("failed to read $page: $!");
109 my $dir=dirname("$destdir/$page");
112 foreach my $s (split(m!/+!, $dir)) {
115 mkdir($d) || error("failed to create directory $d: $!");
120 open (PAGE, ">$destdir/$page") || error("failed to write $page: $!");
129 while ($content =~ /$link/g) {
135 # Given a page and the text of a link on the page, determine which existing
136 # page that link best points to. Prefers pages under a subdirectory with
137 # the same name as the source page, failing that goes down the directory tree
138 # to the base looking for matching pages.
146 $l.="/" if length $l;
149 if (exists $links{$l}) {
150 #debug("for $page, \"$link\", use $l");
153 } while $cwd=~s!/?[^/]+$!!;
155 print STDERR "warning: page $page, broken link: $link\n";
159 sub isinlinableimage ($) {
162 $file=~/\.(png|gif|jpg|jpeg)$/;
169 my $bestlink=bestlink($page, $link);
171 return $link if $page eq $bestlink;
173 if (! grep { $_ eq $bestlink } values %renderedfiles) {
174 $bestlink=htmlpage($bestlink);
176 if (! grep { $_ eq $bestlink } values %renderedfiles) {
177 return "<a href=\"?\">?</a>$link"
180 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
182 if (isinlinableimage($bestlink)) {
183 return "<img src=\"$bestlink\">";
185 return "<a href=\"$bestlink\">$link</a>";
192 $content =~ s/$link/htmllink(pagename($file), $1)/eg;
201 if ($type eq '.mdwn') {
202 return Markdown::Markdown($content);
205 error("htmlization of $type not supported");
213 my $title=basename($page);
218 foreach my $dir (reverse split("/", $page)) {
219 if (length($pagelink)) {
220 $pagelink="<a href=\"$path$dir.html\">$dir/</a> $pagelink";
227 $path=~s/\.\.\/$/index.html/;
228 $pagelink="<a href=\"$path\">$wikiname/</a> $pagelink";
230 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
231 "<h1>$pagelink</h1>\n".
233 "</body>\n</html>\n";
241 my $type=pagetype($file);
242 my $content=readpage($file);
243 if ($type ne 'unknown') {
244 my $page=pagename($file);
245 $links{$page}=[findlinks($content)];
247 $content=linkify($content, $file);
248 $content=htmlize($type, $content);
249 $content=finalize($content, $page);
251 writepage(htmlpage($page), $content);
252 $oldpagemtime{$page}=time;
253 $renderedfiles{$page}=htmlpage($page);
257 writepage($file, $content);
258 $oldpagemtime{$file}=time;
259 $renderedfiles{$file}=$file;
264 open (IN, "$srcdir/.index") || return;
267 my ($mtime, $page, $rendered, @links)=split(' ', $_);
268 $oldpagemtime{$page}=$mtime;
269 $links{$page}=\@links;
270 ($renderedfiles{$page})=$rendered=~m/(.*)/; # untaint
276 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
277 foreach my $page (keys %oldpagemtime) {
278 print OUT "$oldpagemtime{$page} $page $renderedfiles{$page} ".
279 join(" ", @{$links{$page}})."\n"
280 if $oldpagemtime{$page};
289 my $dir=dirname($file);
290 while (rmdir($dir)) {
296 # Find existing pages.
303 $File::Find::prune=1;
305 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
306 my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
308 warn("skipping bad filename $_\n");
311 $f=~s/^\Q$srcdir\E\/?//;
313 $exists{pagename($f)}=1;
319 # check for added or removed pages
321 foreach my $file (@files) {
322 my $page=pagename($file);
323 if (! $oldpagemtime{$page}) {
324 debug("new page $page");
329 foreach my $page (keys %oldpagemtime) {
330 if (! $exists{$page}) {
331 debug("removing old page $page");
332 prune($destdir."/".$renderedfiles{$page});
333 delete $renderedfiles{$page};
334 $oldpagemtime{$page}=0;
339 # render any updated files
340 foreach my $file (@files) {
341 my $page=pagename($file);
343 if (! exists $oldpagemtime{$page} ||
344 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
345 debug("rendering changed file $file");
349 debug("rebuilding unchanged file $file");
354 # if any files were added or removed, check to see if each page
355 # needs an update due to linking to them
357 FILE: foreach my $file (@files) {
358 my $page=pagename($file);
359 foreach my $p (@adddel) {
360 foreach my $link (@{$links{$page}}) {
361 if (bestlink($page, $link) eq $p) {
362 debug("rendering $file, which links to $p");