10 $blosxom::version="is a proper perl module too much to ask?";
11 do "/usr/bin/markdown";
17 my ($srcdir)= shift =~ /(.*)/; # untaint
18 my ($destdir)= shift =~ /(.*)/; # untaint
19 my $link=qr/\[\[([^\s]+)\]\]/;
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 $page if $page eq $bestlink;
158 if (! grep { $_ eq $bestlink } values %renderedfiles) {
159 $bestlink=htmlpage($bestlink);
161 if (! grep { $_ eq $bestlink } values %renderedfiles) {
162 return "<a href=\"?\">?</a>$link"
165 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
167 if (isinlinableimage($bestlink)) {
168 return "<img src=\"$bestlink\">";
170 return "<a href=\"$bestlink\">$link</a>";
177 $content =~ s/$link/htmllink(pagename($file), $1)/eg;
186 if ($type eq '.mdwn') {
187 return Markdown::Markdown($content);
190 error("htmlization of $type not supported");
198 my $title=basename($page);
201 $content="<html>\n<head><title>$title</title></head>\n<body>\n".
203 "</body>\n</html>\n";
211 my $type=pagetype($file);
212 my $content=readpage($file);
213 if ($type ne 'unknown') {
214 my $page=pagename($file);
215 $links{$page}=[findlinks($content)];
217 $content=linkify($content, $file);
218 $content=htmlize($type, $content);
219 $content=finalize($content, $page);
221 writepage(htmlpage($page), $content);
222 $oldpagemtime{$page}=time;
223 $renderedfiles{$page}=htmlpage($page);
227 writepage($file, $content);
228 $oldpagemtime{$file}=time;
229 $renderedfiles{$file}=$file;
234 open (IN, "$srcdir/.index") || return;
237 my ($mtime, $page, $rendered, @links)=split(' ', $_);
238 $oldpagemtime{$page}=$mtime;
239 $links{$page}=\@links;
240 ($renderedfiles{$page})=$rendered=~m/(.*)/; # untaint
246 open (OUT, ">$srcdir/.index") || error("cannot write to .index: $!");
247 foreach my $page (keys %oldpagemtime) {
248 print OUT "$oldpagemtime{$page} $page $renderedfiles{$page} ".
249 join(" ", @{$links{$page}})."\n"
250 if $oldpagemtime{$page};
259 my $dir=dirname($file);
260 while (rmdir($dir)) {
266 # Find existing pages.
273 $File::Find::prune=1;
275 elsif (! -d $_ && ! /\.html$/ && ! /\/\./) {
276 my ($f)=/(^[-A-Za-z0-9_.:\/+]+$)/; # untaint
278 warn("skipping bad filename $_\n");
281 $f=~s/^\Q$srcdir\E\/?//;
283 $exists{pagename($f)}=1;
289 # check for added or removed pages
291 foreach my $file (@files) {
292 my $page=pagename($file);
293 if (! $oldpagemtime{$page}) {
294 debug("new page $page");
299 foreach my $page (keys %oldpagemtime) {
300 if (! $exists{$page}) {
301 debug("removing old page $page");
302 prune($destdir."/".$renderedfiles{$page});
303 delete $renderedfiles{$page};
304 $oldpagemtime{$page}=0;
309 # render any updated files
310 foreach my $file (@files) {
311 my $page=pagename($file);
313 if (! exists $oldpagemtime{$page} ||
314 mtime("$srcdir/$file") > $oldpagemtime{$page}) {
315 debug("rendering changed file $file");
320 # if any files were added or removed, check to see if each page
321 # needs an update due to linking to them
323 FILE: foreach my $file (@files) {
324 my $page=pagename($file);
325 foreach my $p (@adddel) {
326 foreach my $link (@{$links{$page}}) {
327 if (bestlink($page, $link) eq $p) {
328 debug("rendering $file, which links to $p");