#!/usr/bin/perl -w #Author: Scott Cox #Date: Dec 13, 1999 #Description: Takes an HTML file and prepends a TOC (table of contents) #based on heading levels 1-5 #Usage: # % gen.TOC.pl > # Input is a .html file. $usage = "\nusage: $0 > \n"; @ARGV || die "$usage\n"; # Check that an argument was given $num = 0; #Used to generate unique links $found = 0; #Used to determine whether file includes the tag $h2_indent = " " x 2; $h3_indent = " " x 4; $h4_indent = " " x 6; $h5_indent = " " x 8; #For each heading (level 1-5), add a unique "a name" string to the heading #and build an array of references to headings while(<>){ if(/

(.*)<\/h1>/i){ $num = $num + 1; push (@TOCarray, "$1
\n"); s/

/<\/a>

/i; } if(/

(.*)<\/h2>/i){ $num = $num + 1; push (@TOCarray, "$h2_indent $1
\n"); s/

/<\/a>

/i; } if(/

(.*)<\/h3>/i){ $num = $num + 1; push (@TOCarray, "$h3_indent $1
\n"); s/

/<\/a>

/i; } if(/

(.*)<\/h4>/i){ $num = $num + 1; push (@TOCarray, "$h4_indent $1
\n"); s/

/<\/a>

/i; } if(/

(.*)<\/h5>/i){ $num = $num + 1; push (@TOCarray, "$h5_indent $1
\n"); s/
/<\/a>
/i; } if(//){ $found = 1; } push (@BODYarray, $_); } if($found){ #Preserve header of the HTML file $header_string = shift(@BODYarray); until ($header_string =~ //){ print $header_string; $header_string = shift(@BODYarray); } print $header_string; #Print the TOC generated above print @TOCarray; #Print the remainder of the HTML file print @BODYarray; } else { die "\nInput file does not include tag. \nAdd and tags appropriately and try again.\n"; }