1#!/usr/bin/perl -w 2 3use strict; 4use HTML::Parser; 5 6# fix pod2html output: 7# v1.0: defer </dd> and </dt> tags until 8# the next <dd>, <dt> or </dl> 9 10# v1.1: don't nest any <a> elements; 11# end one before beginning another 12 13# v1.2: insert <dd> tags if <dl> occurs 14# inside <dt> 15 16# v1.3: <a> anchors must not start with a digit; 17# insert a letter "N" at the start if they do 18 19# v1.4: insert the "N" letter into <a href="#xxx"> too. 20 21my $p = HTML::Parser->new(api_version => 3); 22$p->handler(start => \&startsub, 'tagname, text'); 23$p->handler(end => \&endsub, 'tagname, text'); 24$p->handler(default => sub { print shift() }, 'text'); 25$p->parse_file(shift||\*STDIN) or die("parse: $!"); 26 27my @stack; 28my $a=0; 29 30sub startsub { 31 my $tag = shift; 32 my $text = shift; 33 if ($tag eq "dl") { 34 if (@stack and $stack[0] eq "dt") { 35 $stack[0] = "dd"; 36 print "</dt><dd>"; 37 } 38 unshift @stack, 0; 39 } 40 if (($tag eq "dt" or $tag eq "dd") and $stack[0]) { 41 print "</$stack[0]>"; 42 $stack[0] = 0; 43 } 44 if ($tag eq "a") { 45 if ($a) { 46 print "</a>"; 47 } else { 48 $a++; 49 } 50 $text =~ s/(name="|href="#)(\d)/$1N$2/; 51 } 52 print $text; 53} 54 55 56sub endsub { 57 my $tag = shift; 58 my $text = shift; 59 if ($tag eq "dl") { 60 print "</$stack[0]>" if $stack[0]; 61 shift @stack; 62 } 63 if ($tag eq "a") { 64 if ($a) { 65 print "</a>"; 66 $a--; 67 } 68 } elsif ($tag eq "dd" or $tag eq "dt") { 69 $stack[0] = $tag; 70 } else { 71 print $text; 72 } 73} 74