1 #!/usr/bin/perl -w
3 use strict;
4 use HTML::Parser;
6 # fix pod2html output:
7 # v1.0: defer </dd> and </dt> tags until
8 # the next <dd>, <dt> or </dl>
10 # v1.1: don't nest any <a> elements;
11 # end one before beginning another
13 # v1.2: insert <dd> tags if <dl> occurs
14 # inside <dt>
16 # v1.3: <a> anchors must not start with a digit;
17 # insert a letter "N" at the start if they do
19 # v1.4: insert the "N" letter into <a href="#xxx"> too.
21 my $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||"-") or die("parse: $!");
27 my @stack;
28 my $a=0;
30 sub 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 }
56 sub 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 }