summaryrefslogtreecommitdiffstats
path: root/util/fix-pod2html.pl
blob: ff0176eb0a8945474a18a2c1917291f43321773b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl -w

use strict;
use HTML::Parser;

# fix pod2html output:
# v1.0: defer </dd> and </dt> tags until
# the next <dd>, <dt> or </dl>

# v1.1: don't nest any <a> elements; 
# end one before beginning another

# v1.2: insert <dd> tags if <dl> occurs
# inside <dt>

# v1.3: <a> anchors must not start with a digit;
# insert a letter "N" at the start if they do

# v1.4: insert the "N" letter into <a href="#xxx"> too.

my $p = HTML::Parser->new(api_version => 3);
$p->handler(start => \&startsub, 'tagname, text');
$p->handler(end => \&endsub, 'tagname, text');
$p->handler(default => sub { print shift() }, 'text');
$p->parse_file(shift||\*STDIN) or die("parse: $!");

my @stack;
my $a=0;

sub startsub {
        my $tag = shift;
        my $text = shift;
        if ($tag eq "dl") {
		if (@stack and $stack[0] eq "dt") {
			$stack[0] = "dd";
			print "</dt><dd>";
		}
                unshift @stack, 0;
        }
        if (($tag eq "dt" or $tag eq "dd") and $stack[0]) {
                print "</$stack[0]>";
                $stack[0] = 0;
        }
	if ($tag eq "a") {
		if ($a) {
			print "</a>";
		} else {
			$a++;
		}
		$text =~ s/(name="|href="#)(\d)/$1N$2/;
	}
        print $text;
}
                

sub endsub {
        my $tag = shift;
        my $text = shift;
        if ($tag eq "dl") {
                print "</$stack[0]>" if $stack[0];
                shift @stack;
        }
	if ($tag eq "a") {
		if ($a) {
			print "</a>";
			$a--;
		}
	} elsif ($tag eq "dd" or $tag eq "dt") {
                $stack[0] = $tag;
        } else {
                print $text;
        }
}