#!/usr/bin/perl # name all dt elements and compile a table of contents based on them my @doc = (); my @arr = (); while(){ if( m/
([^<]*)<\/dt>/ ){ my $esc = $1; my $txt = $1; $esc =~ s/\W//g; push(@arr, "
  • $txt
  • "); s/
    ([^<]*)<\/dt>/
    $txt<\/dt>/; } elsif( m/([^<]*)<\/h.>/ && $1 ne "1"){ my $degree = $1; my $txt = $2; my $esc = $2; $esc =~ s/\W//g; push(@arr, "
  • $txt
  • "); $_ = "$txt\n"; } # handles indentation within the table of contents if( m/]*))?>/ ) { push(@arr,"
    1. "); } if( m/<\/dl>/ ) { push(@arr,"
  • "); } push(@doc,$_); } my $tocElements = ""; foreach(@arr){ $tocElements .= "$_\n"; } my $toc = <Table of Contents
      $tocElements
    END foreach(@doc){ s//$toc/; print $_; }