#!/usr/bin/perl # generatepages.pl, copyright Casey Muller, let me know if you find it # useful at generatepages(at)null-terminated.com and maybe I can give # you a more up to date version. # third beta version: Mar 1st, 2005 #directory constants $webdir = `pwd`; chomp $webdir; $rootdir = $webdir."/root"; $stagingdir = $webdir."/staging"; $commentdir = $webdir."/comments"; $imgprefix = "http://bigfiles.null-terminated.com:81/digitalcamera/"; $thumbpath = "/visuals/photography/by-date/"; $thumbprefix = "http://null-terminated.com".$thumbpath; #load the html templates require "templates.pl"; use File::Find; find sub { /^\./ && return; if (-d $File::Find::name) { push @{ $dirs{$File::Find::dir} }, $_; $newdir = $stagingdir.substr $File::Find::name, length($rootdir); if (!-d $newdir) { print "making: $newdir\n"; system("mkdir \"$newdir\""); } } }, $rootdir; # open the root directory. HTML files get processed, others assumed copied. find sub { if(/\.html$/) { print "processing $_ in $File::Find::dir\n"; # get just the portable part $dir = substr $File::Find::dir, length($rootdir); # get the comment name ($commentfile = $_) =~ s/\.html$/\.comments/; # open the output file open(OUTPUT, '>', "$stagingdir$dir/$_") || die "can't write $stagingdir$dir/$_"; # split it up @tracedirs = split m%/%, $dir; shift @tracedirs; $host = shift @tracedirs; $url = "http://".$host.".com".substr $File::Find::name, length($rootdir.$host)+1; # write out the html print OUTPUT $pretitle; # supply the title: $title = ""; if($_ ne "index.html") { # interesting html filename? ($stripfile = $_) =~ s/\.html$//; $title .= $stripfile." : "; } if($#tracedirs > -1) { # interesting subdir? $title .= $tracedirs[-1]." : "; } $title .= $host; print OUTPUT $title; #are there any subdirs? if(length @{ dirs{$File::Find::dir}}) { # print them out print OUTPUT $title2menu; #print each subdirectory: foreach(@{ $dirs{$File::Find::dir} }) { if(defined $dirs{"$File::Find::dir/$_"}) { print OUTPUT "
  • $_
  • \n"; } else { print OUTPUT "
  • $_\n"; } } print OUTPUT $menu2host; } else { # short circuit it print OUTPUT $title2host; } #print the big host title print OUTPUT $host.$host2traceback; #print each level we've come through: $backs = "../" x ($#tracedirs+1); foreach $tracedir (@tracedirs) { $fullpath = substr $File::Find::dir, 0, index $File::Find::dir, "/".$tracedir; print OUTPUT "
  • $tracedir"; # grab all our traceback dirs @tracebackdirs = @{ $dirs{$fullpath} }; if($#tracebackdirs) { # if we have any to output print OUTPUT " ("; if($#tracebackdirs > 9) { # if there are more than 9, find ourselves for($i = 0; $i < $#tracebackdirs; $i++) { if($tracedir eq $tracebackdirs[$i]) { $traceindex = $i; break; } } # we want the 9 closest, but no wrapping $tracestart = $traceindex - 4; $traceend = $traceindex + 4; if($tracestart < 0) { # need to adjust the beginning $traceend -= $tracestart; $tracestart = 0; } elsif($traceend > $#tracebackdirs) { # need to adjust the end $tracestart -= $traceend - $#tracebackdirs; $traceend = $#tracebackdirs; } # get the slice out @tracebackdirs = @tracebackdirs[$tracestart..$traceend]; } foreach(@tracebackdirs) { # for every traceback if($_ eq $tracedir) { # it's plan if it's us print OUTPUT " $_"; } else { # normal if it's not print OUTPUT " $_"; } } print OUTPUT " )\n"; } $backs = substr $backs, 3; } print OUTPUT $traceback2content; #dump the actual file: print OUTPUT "

    \n"; open(INPUT, "$File::Find::name"); while() { # duplicated code: s##$3#g; s##$3#g; if(/^$/) { print OUTPUT "

    \n\n

    "; } print OUTPUT $_; } print OUTPUT "\n

    \n"; close INPUT; print OUTPUT $content2comments; # open the comment file if it exists $commentfile = "$commentdir$dir/$commentfile"; if(-r $commentfile) { open(COMMENTS, "$commentfile"); print OUTPUT "Old-school comments:
    ";
    		while() {
    		    print OUTPUT "$_";
    		}
                    print OUPUT "
    "; close COMMENTS; } print OUTPUT $comments2disqus; print OUTPUT "var disqus_url = '".$url."';\n"; print OUTPUT "var disqus_title = '".$title."';\n"; print OUTPUT "\n"; print OUTPUT ''; print OUTPUT $disqus2tag; print OUTPUT $host; print OUTPUT $posttag; print OUTPUT $ad{$host}; print OUTPUT $adtoend; close OUTPUT; return; } }, $rootdir;