|
#!/usr/bin/perl -w # # tindexer --crb3 03mar04/10mar04 # # from: dirls.cgi --crb3 26sep01 # # generate a static index.html page for a multichapter story, # working from actual directory contents, command-line # options, and perhaps a template and an argfile or rcfile. # v0.1 initial working version # todo: argfile, rcfile # v0.2 --crb3 12mar04 added meta generator line injection # v0.3: --crb3 15Feb07 added picture-line picimg. # allow multiline element files (using joins). # straighten out 'missing' complaint messages (duhh). # use strict; # my $debug=0; my $safety=0; my $chatty=0; my $noindex=1; my $tostd=0; use vars qw/$key $arg $basedir $author $outfname $title $blurb $subsum/; use vars qw/$cwd $tauthor $hauthor $tln $oH $tH $twidth $tptfname $picimg/; use vars qw/$inline $bgline %bglines $ftitle %ftitles @filelist $hidx/; if($debug){ foreach $arg (@ARGV){ print "arg=$arg\n"; } } $twidth="75%"; $cwd=`pwd`; chomp $cwd; $picimg=$tptfname=$title=$subsum=$author=""; $outfname="-"; $basedir="./"; unless(defined($ARGV[0])){ print <<EOT; usage: tindexer [options] dir-to-index -t title -a author -s subtitle/summary -b blurb (summary) -f template html file -d directory to index -w tablewidth -o output filename (default: '-' for STDOUT) -i toggle: include *index* in listing -p picture to place in index (complete IMG link) EOT exit 0; } while( (defined($ARGV[0])) and (substr($ARGV[0],0,1) eq '-') ){ $arg=shift(@ARGV); ($key,$arg)=unpack("xaa*",$arg); if($key eq 'D'){ # debug $debug ^= 1; next; # toggle it }elsif($key eq 'v'){ # verbose == chatty $chatty++; next; # ..in case levels are used }elsif($key eq 'S'){ # safety $safety ^= 1; next; }elsif($key eq 'i'){ # toggle to include 'index' $noindex ^= 1; next; # in the listing } # now switches that take arg if(defined($arg) and length($arg)){ $arg =~ s/^\s*\=\s*//; }else{ $arg=shift(@ARGV); } if($key eq 'd'){ # directory to index $basedir=$arg; }elsif($key eq 'a'){ # author $author=$arg; }elsif($key eq 'o'){ # output index filename $outfname=$arg; }elsif($key eq 't'){ # title $title=$arg; }elsif($key eq 'b'){ # blurb/summary $blurb=$arg; }elsif($key eq 's'){ # subtitle/summary $subsum=$arg; }elsif($key eq 'w'){ # table width $twidth=$arg; }elsif($key eq 'f'){ # template file $tptfname=$arg; }elsif($key eq 'p'){ # picture for index $picimg=$arg; }else{ print "unrecognized switch $key=$arg\n"; } } if(defined $ARGV[0]){ $basedir=$ARGV[0]; chomp $basedir; } $basedir .= "/" unless $basedir =~ /.+\/$/; $tostd=1 if $outfname eq '-'; substr($basedir,0,1)="" if index($basedir,"/") eq 0; #remove leading / $basedir =~ s/[^\.\\\/]+\.\w?htm\w?//i; $basedir="" if $basedir eq "./"; if($debug){ print "basedir=$basedir\n"; print "blurb=$blurb\n" if length($blurb); } if(substr($picimg,0,1) eq '@'){ substr($picimg,0,1)=""; open(BL,"<$picimg") or die "can't open picimg file $picimg\n"; $picimg=join("",(<BL>)); close(BL); } if(substr($blurb,0,1) eq '@'){ substr($blurb,0,1)=""; open(BL,"<$blurb") or die "can't open blurb file $blurb\n"; $blurb=join("",(<BL>)); close(BL); } if(substr($subsum,0,1) eq '@'){ substr($subsum,0,1)=""; open(BL,"<$subsum") or die "can't open subsum file $subsum\n"; $subsum=join("",(<BL>)); close(BL); } if(substr($title,0,1) eq '@'){ substr($title,0,1)=""; open(BL,"<$title") or die "can't open title file $title\n"; $title=join("",(<BL>)); close(BL); } if(substr($author,0,1) eq '@'){ substr($author,0,1)=""; open(BL,"<$author") or die "can't open author file $author\n"; $author=join("",(<BL>)); close(BL); } my @filenames=(sort(<$basedir*htm?>)); use vars qw/$file/; foreach $file (@filenames){ chomp $file; next if $file =~ /^\./; # ignore dotfiles next if $file =~ /\~$/; # ignore unix backups next if $file =~ $outfname; # ignore the effective calling page next if(($file =~ /index\./) and $noindex); # and indexes next unless $file =~ /\.\w?htm\w?$/i; # ignore non-HTML files $file =~ s/^\.\///; print "basedir $basedir file $file\n" if $debug; if(open(IFIL,"<$file")){ $bgline=""; while(defined($inline=<IFIL>)){ # look for <title>whatitis</title> chomp $inline; # allow for multiline source text $bgline = "" unless $bgline =~ /\<title\>/i; $bgline .= $inline; # grab what's between those tags last if $inline =~ /\<\/title\>/i; } # quit when you've found it close(IFIL); $bgline =~ s/\<\/?title\>//gi; print "bgline=$bgline\n" if $debug; }else{ $bgline=" "; # not found? leave it blank. print "...wouldn't open.\n" if $debug; } $bglines{$file}=$bgline; ($ftitle=$file) =~ s/^.+\///; $ftitles{$file}=$ftitle; push(@filelist,$file); } if($tostd){ $oH=*STDOUT; }else{ open(H,">$outfname") or die "can't make outfile $outfname\n"; $oH=*H; } #print $oH "Content-type: text/html\n\n"; if(length($author)){ $hauthor="$author"; $tauthor="$author"; }else{ $hauthor=$tauthor=""; } if(length($tptfname)){ print "opening template $tptfname\n" if ($debug or $chatty); open(T,"<$tptfname") or die "can't open template file $tptfname\n"; $tH=*T; }else{ print "using internal template\n" if ($debug or $chatty); $tH=*DATA; } while( (defined($tln=<$tH>)) and (index($tln,"__END__")!=0) ){ if( ($hidx = index($tln,'</head>')) >-1){ substr($tln,$hidx,0)= "<meta name=\"generator\" content=\"tindexer --crb3 12mar04\">\n"; } if($tln =~ /\#PAYLOAD\#/){ foreach $file (@filelist){ print $oH <<EOF; <tr><td align="right"> <a href="$file">$ftitles{$file}</a> </td><td> $bglines{$file} </td></tr> EOF } }else{ $tln =~ s/\#TITLE\#/$title/g if index($tln,'#TITLE#') >-1; $tln =~ s/\#AUTHOR\#/$author/g if index($tln,'#AUTHOR#') >-1; $tln =~ s/\#TAUTHOR\#/$tauthor/g if index($tln,'#TAUTHOR#') >-1; $tln =~ s/\#HAUTHOR\#/$hauthor/g if index($tln,'#HAUTHOR#') >-1; $tln =~ s/\#SUBSUM\#/$subsum/g if index($tln,'#SUBSUM#') >-1; $tln =~ s/\#BLURB\#/$blurb/g if index($tln,'#BLURB#') >-1; $tln =~ s/\#TWIDTH\#/$twidth/g if index($tln,'#TWIDTH#') >-1; $tln =~ s/\#PICIMG\#/$picimg/g if index($tln,'#PICIMG#') >-1; print $oH $tln; } } close $tH; close $oH; __END__ <html> <head> <title> #TITLE# - #TAUTHOR# </title> </head> <body text="#000000" bgcolor="#eeeeee" link="#0000ee" vlink="#008800" alink="#ffff00"> <font face="Arial,Swiss,Helvetica"> <center> <table align="center" width="#TWIDTH#" cellspacing="14"> <tr><td valign="top"> <table> <tr><td valign="top" align="left"> <h2>#TITLE#</h2> </td></tr> <tr><td align="right"> <h3>#HAUTHOR#</h3> </td></tr> </table> </td><td align="left" valign="top"> <p><i>#SUBSUM#</i></p> </td></tr> </table> <table align="center" width="#TWIDTH#"> #PAYLOAD# </table> </center> </font> </body> </html> __END__ <html> <head> <title>#TITLE# - #AUTHOR#</title> </head> <body> <center> <table width="#TWIDTH#"> <tr><td align="right"> <h2>#TITLE#</h2> <h3>#AUTHOR#</h3> </td> <td align="left"> <h4>#SUMSUM#</h4> </td></tr> </table> <hr width="#TWIDTH#" /> <table> #PAYLOAD# </table> <hr width="#TWIDTH#" /> </center> </body> </html> |
Grab a gzipped copy here |
| Syntax highlighting using Syntax::Highlight::Engine::Kate |