ServersWeb Automation: Generating Dynamic Tables of Contents Page 5

Web Automation: Generating Dynamic Tables of Contents Page 5




You don’t even have to run this as a CGI. You can run it from the command
line and output the results to an HTML file that you have in your webspace.
This allows you to periodically generate this table of contents file, without
having millions of users hammering your CGI. For example you could have cron,
or any other scheduling service periodically execute the command perl
toc.pl > /usr/local/apache/htdocs/toc.html
–which will rebuild your
table of contents. Pointing users to the toc.html page might not
give them a real-time view of your webspace, but might be more feasible.

I’d also like to shamelessly plug Lincoln Stein’s CGI.pm module, which is
available on CPAN, and probably is already in your Perl distribution. I use
this module to take a lot of the HTMLizing out of my hands. I wrote this
example using no modules, so you could see what was going on, but I highly
recommend using CGI.pm to do a lot of the HTML stuff for you.

Full Text of this Example

1: #!/usr/bin/perl -w
2: sub Get_Title {
3: my $filename=shift;
4: unless(-f "$filename") { return("NO INDEX"); }
5: open(HTML,"
6: while(){
7: if($_=~ /(.*)/i) {<br /> 8: close HTML;<br /> 9: return "$1";<br /> 10: }<br /> 11: }<br /> 12: close HTML;<br /> 13: return "Untitled";<br /> 14: }<br /> 15: sub Get_Dirs {<br /> 16: my $basedir=shift;<br /> 17: opendir(GD,"$basedir") or return;<br /> 18: my @DIRS;<br /> 19: for(readdir(GD)) {<br /> 20: my $temp="$_";<br /> 21: if($temp =~ /^./) { next; }<br /> 22: if(-d "$basedir$temp") {<br /> 23: push(@DIRS,"$basedir$temp/");<br /> 24: }<br /> 25: }<br /> 26: closedir GD;<br /> 27: return @DIRS;<br /> 28: }<br /> 29: sub Get_Depth {<br /> 30: $_ = shift;<br /> 31: return tr////;<br /> 32: }<br /> 33: my $dir="/home/html/mattwork/htdocs/";<br /> 34: my $url="http://mattwork.potsdam.edu/";<br /> 35: my $depth=Get_Depth("$dir")+1;<br /> 36: my @dirs=Get_Dirs("$dir"); <br /> 37: print "Content-Type: text/htmlnn";<br /> 38: print "<title>Table of Contentsn";
39: print "

    n";
    40: while(@dirs) {
    41: my $currdir=shift(@dirs);
    42: my $currdepth=Get_Depth("$currdir");
    43: unshift(@dirs,Get_Dirs("$currdir"));
    44: if($currdepth > $depth) { print "

      n"; }
      45: if($currdepth
      46: $diff=$depth - $currdepth;
      47: print "

    n" x $diff;
    48: }
    49: $depth=$currdepth;
    50: my $path="$currdir" . "index.html";
    51: unless(-e "$path") { next; }
    52: my $title=Get_Title("$path");
    53: $path =~ s/$dir/$url/i;
    54: print "

  • $title
  • n";

    55: }
    56: print "

n";

Latest Posts

Related Stories