#!/usr/bin/perl { package MyParser; use base HTML::Parser; my @Numbers; my $Separator = "."; my $Alphabet = join("", (' ', 'a'..'z')); my $Style = 1; my $ThisSection = "none"; my %References; my $StoreOnly = 0; my $Debug = 0; sub NumberStyle { my ($Number, $Part, $Level) = @_; # Example: Display chapter numbers, where

is a chapter if($Style == 4) { return("Chapter $Number") if($Level == 1); return(""); } # Example: Alphabetic numbering return(substr(uc($Alphabet), $Number, 1)) if($Style == 2); return(substr(lc($Alphabet), $Number, 1)) if($Style == 3); # Example: Number only down to a depth of 1.1.1 if($Style == 5) { $Depth = 3; return($Number) if($Level <= $Depth); return(""); } # Example: Use the format "1.1.a" if($Style == 6) { $Depth = 3; return("") if($Level > $Depth); return($Number) if($Part < $Depth); return(substr(lc($Alphabet), $Number, 1)) if($Part == $Depth); return(""); } # Default: Numeric "1.1.1.1.1" return($Number); } sub start { my($self, $tagname, $attr, $attrseq, $origtext) = @_; # Handle the start of heading tags if($tagname =~ /^h([1-6])$/i) { $Level = $1; # Update the numbering $Numbers[$Level]++; if($Level < 6) { foreach $LowerLevel ($Level + 1 .. 6) { $Numbers[$LowerLevel] = 0; } } # Append the numbering information to the tag my @Parts; foreach $Loop (1..$Level) { $Part = NumberStyle($Numbers[$Loop], $Loop, $Level); push(@Parts, $Part) if($Part); } $Numbering = join($Separator, @Parts); $ThisSection = $Numbering; print "" . $Numbering . " " unless $StoreOnly; } elsif($tagname =~ /^body$/) { for(1..6) { $Numbers[$_] = 0; } } elsif($tagname =~ /^ref$/) { $ReferenceName = $$attr{"name"}; $References{$ReferenceName} = $ThisSection; print " [Stored reference "$ReferenceName" as $ThisSection] " if( $Debug && ! $StoreOnly); } elsif($tagname =~ /^xref$/) { $ReferenceName = $$attr{"name"}; print " !!! X-ref: $ReferenceName: " if( $Debug && ! $StoreOnly); print $References{$ReferenceName} . "" unless $StoreOnly; } else { # Start of tags which are not headers print $origtext unless $StoreOnly; } } sub end { my($self, $tagname, $origtext) = @_; print $origtext unless $StoreOnly; } sub text { my($self, $origtext, $is_cdata) = @_; print $origtext unless $StoreOnly; } sub storeOnly { $StoreOnly = 1; } sub storeAndDisplay { $StoreOnly = 0; } sub debugMode { $Debug = 1; } } $Parser = MyParser->new(); my $Data = join('', <>); $Parser->debugMode() if(0); $Parser->storeOnly(); $Parser->parse($Data); $Parser->storeAndDisplay(); $Parser->parse($Data);