SHARE
Facebook X Pinterest WhatsApp

Web Automation: Generating Dynamic Tables of Contents Page 3

Written By
thumbnail Matthew Keller
Matthew Keller
Jul 20, 2010
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



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/htmlnn";
6: print "Table of Contentsn";
7: print "
    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 "
    n"; }
    13: if($currdepth
    14: my $diff=$depth - $currdepth;
    15: print "

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 "

  • $title
  • n";

    23: }
    24: print "n";

    thumbnail Matthew Keller

    Matthew Keller is a ServerWatch contributor.

    Recommended for you...

    What Is a Container? Understanding Containerization
    What Is a Print Server? | How It Works and What It Does
    Nisar Ahmad
    Dec 8, 2023
    What Is a Network Policy Server (NPS)? | Essential Guide
    Virtual Servers vs. Physical Servers: Comparison and Use Cases
    Ray Fernandez
    Nov 14, 2023
    ServerWatch Logo

    ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

    Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.