#!/usr/bin/perl # blosxom plugin which copies all referenced files into the target hierarchy # (c) Alexander Zangerl 2004 # licensed under the GPL v1. # az Wed Dec 31 22:52:52 2003 # $Id: 99fixup,v 1.4 2005/04/17 05:14:03 az Exp $ package fixup; use strict; use HTML::LinkExtor; use URI; use File::Basename; sub start { return ($blosxom::static_or_dynamic ne "dynamic"); } # regexp of directories you don't care about as they are outside # the scope of the blog my $dontcomplain="^jargon/"; my (%seen,%todo); my $debug=0; sub filter { my($pkg, $files) = @_; # fixme: interface violation. my @cand=keys %blosxom::indexes; unshift @cand,"/"; foreach (@cand) { s/\.$blosxom::file_extension$/\.$blosxom::flavour/ or $_.="/index.$blosxom::flavour"; s!/{2,}!/!g; $todo{$_}=1; } return 1; } sub end { my $parser=HTML::LinkExtor->new; # crawl files for links foreach my $fn (keys %todo) { handle_file($parser,$fn,"top"); } return 1; } sub handle_file { my ($parser,$fn,$src)=@_; $fn||="/index.html"; $fn=~s!/$!/index.html!; return if $seen{$fn}; $seen{$fn}=1; $debug and print "checking $fn\n"; # file not available in static_dir but in source? # then copy if (!-r "$blosxom::static_dir/$fn") { if (-r "$blosxom::datadir/$fn") { system("mkdir","-p",dirname("$blosxom::static_dir/$fn")); system("cp","$blosxom::datadir/$fn","$blosxom::static_dir/$fn"); $debug and print "copied $fn\n"; } else { print "missing: $fn <- $src\n" if ($fn!~m!$dontcomplain!o); return; } } my $ref=URI->new_abs($fn,"$blosxom::url/"); # file is css? parse it for urls if ($fn =~ /\.css$/) { open(F,"$blosxom::datadir/$fn") or die ("cant open $fn: $!\n"); my $old=$/; undef $/; my $text=; close F; $/=$old; while ($text =~ /url\(\s*(\"|\')(.+)\1\s*\)/g) { my $url=URI->new_abs($2,$ref); $debug and print "testing $url\n"; if ($url !~ s/^$blosxom::url\///) { $debug and print $url." is external\n" if (!$seen{$url}); $seen{$url}=1; next; } handle_file($parser,$url,$fn); } return; } # file is html? then parse it return if ($fn !~ /\.html$/); $parser->parse_file("$blosxom::static_dir/$fn"); my @links=$parser->links; foreach (@links) { $debug and print "links in $fn: ".join(",",@$_)."\n"; my $url=URI->new_abs($$_[2],$ref); if ($url->as_string !~ /^$blosxom::url/) { $debug and print $url." is external\n" if (!$seen{$url}); $seen{$url}=1; next; } my $path=$url; $url=~s/^$blosxom::url\///; # check this local file (and its children) handle_file($parser,$url,$fn); } return; } 1;