#!/usr/bin/perl -w
#
# To index an entire directory use:
# perl toc.pl *.html
#
use strict;
# holds the name of each file
# as it is being processed.
my($file);
# holds the text of the heading
# (from the anchor tag).
my($heading);
# holds the last heading level
# for comparision.
my($oldLevel);
# holds each line of the file
# as it is being processed.
my($line);
# used as temporary variables
# to shorten script line widths
my($match);
my($href);
# holds the name of the heading
# from the anchor tag.
my($name);
# holds the level of the current heading.
my($newLevel);
# First, I open an output file and print the
# beginning of the HTML that is needed.
#
$outputFile = "fulltoc.htm";
open(OUT, ">",$outputFile) or die "couldn't open outputfile";
print OUT ("
");
print OUT ("Detailed Table of Contents\n");
print OUT ("\n");
# Now, loop through every file in the command
# line looking for Header tags. When found, Look
# for an Anchor tag so that the NAME attribute can
# be used. The NAME attribute might be different
# from the actual heading.
#
foreach $file (sort(@ARGV)) {
next if $file =~ m/^\.htm$/i;
print("$file\n");
open(INP,"+>", $file) or die "couldn't open input file";
print OUT ("\n");
$oldLevel = 1;
while () {
if (m!(.+?)!i) {
# remove anchors from header.
$line = $1;
$match = '(.+?)';
if ($line =~ m!$match!i) {
$name = $1;
$heading = $2;
}
else {
$match = '(.+?)';
$line =~ m!$match!i;
$name = $1;
$heading = $1;
}
m!!;
$newLevel = $1;
if ($oldLevel > $newLevel) {
print OUT ("
\n");
}
if ($oldLevel < $newLevel) {
print OUT ("\n");
}
$oldLevel = $newLevel;
my($href) = "\"$file#$name\"";
print OUT ("- ");
print OUT ("");
print OUT ("$heading\n");
}
}
while ($oldLevel--) {
print OUT ("
\n");
}
close(INP);
}
# End the HTML document and close the output file.
#
print OUT ("");
close(OUT);