Web Automation: Generating Dynamic Tables of Contents Page 3

By Matthew Keller (Send Email)
Posted Jun 21, 2000


There are many ways to do anything- And this function is no exception. I initially wrote an more expanded, twelve-line version that essentially did the same thing, but decided that I should use a bit of Perl magic, and make it a little shorter.

The Glue: the "Main" function

Extracting those three functions from the code cleans it up quite a bit (although there is still room for another function or two). So now we have to build a system that utilizes those functions to perform our four tasks:

  1. Obtain a list of directories
  2. For every directory, open the index.html file and extract the title of the page
  3. Print back the title, with a link to the page
  4. Dive into the directory, and back to Step 1

If you were wondering why I was concerned with "depth" back in the Functions section, here's where it will make sense. I use the depth of the folder, to stratify where in an unordered list the item belongs (how "deep" in the list). This give our table of contents a more readable, and understandable feel, than just throwing out a flat list of links:

1: my $dir="/usr/local/apache/htdocs/";
2: my $url="http://mattwork.potsdam.edu/";
3: my $depth=Get_Depth("$dir")+1;
4: my @dirs=Get_Dirs("$dir");
5: print "Content-Type: text/html\n\n";
6: print "<html><head><title>Table of Contents</title></head><body>\n";
7: print "<ul>\n";
8: while(@dirs) {
9: my $currdir=shift(@dirs);
10: my $currdepth=Get_Depth("$currdir");
11
: unshift(@dirs,Get_Dirs("$currdir"));
12: if($currdepth > $depth) { print "<ul>\n"; }
13: if($currdepth < $depth) {
14: my $diff=$depth - $currdepth;
15: print "</ul>\n" x $diff;
16: }
17: $depth=$currdepth;
18: my $path="$currdir" . "index.html";
19: unless(-e "$path") { next; }
20: my $title=Get_Title("$path");
21: $path =~ s/$dir/$url/i;
22: print "<li><a href=\"$path\">$title</a></li>\n";
23: }
24: print "</ul>\n";



Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.