summaryrefslogtreecommitdiffstats
path: root/mantools/postconf2man
blob: cf5e16128054ebdc706be7581209066e1ebb29c3 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl

# postconf2man - convert postconf2html to nroff

# Basic operation: 
#
# - Process input as blocks of text separated by one or more empty
# (or all whitespace) lines. 
#
# - Process <nroffescape> pass-through requests for things that this
# script cannot do automatically.
#
# Caution: this depends heavily on the postconf2html output format.

#use Getopt::Std;

#$opt_h = undef;
#$opt_v = undef;
#getopts("hv");

#die "Usage: $0 [-hv]\n" if ($opt_h);

#push @ARGV, "/dev/null"; # XXX

while(<>) {

    # Skip blank lines before text block.
    next unless (/\S/);

    # Gobble up the next text block.
    $block = "";
    do {
	$_ =~ s/\s+\n$/\n/;
	$block .= $_;
    } while(($_ = <>) && /\S/);

    # How the %!#$^@ do I get a backslash substituted into a string?
    # Even \134 comes out as \e. What brain damage is this?
    #$block =~ s/\n\./\n\\\&./g;
    $block =~ s/\n\./\n\134\&./g;
    $block =~ s/\n'/\n\134\&'/g;
    if ($block =~ /<H2>/) {
	$block =~ s/<H2><a[^>]+>([^<]+)<\/a><\/H2>/\n.SH \1\n/g;
	$block =~ tr/a-z/A-Z/;
    }
    $block =~ s/<DT><b><a[^>]+>([^<]+)<\/a>\n(.*)<\/b><\/DT><DD>/\n.SH \1 \2\n/g;
    $block =~ s/<[Aa][ \n]+[Hh][Rr][Ee][Ff]="[^"]+">//g;
    $block =~ s/<[Aa][ \n]+[Nn][Aa][Mm][Ee]="[^"]+">//g;
    $block =~ s/<\/[Aa]>//g;
    $block =~ s/<\/DD>/\n/g;
    $block =~ s/<DL>/\n/g;
    $block =~ s/<\/DL>/\n/g;
    $block =~ s/\\/\\e/g;
    $block =~ s/<b>/\\fB/g;
    $block =~ s/<i>/\\fI/g;
    $block =~ s/<\/b>/\\fR/g;
    $block =~ s/<\/i>/\\fR/g;
    $block =~ s/^(<p(re)?>)/.PP\n\1/ if ($wantpp);
    $block =~ s/<p> */\n/g;
    $block =~ s/ *<\/p>/\n/g;
    $block =~ s/<pre>/\n.nf\n.na\n.ft C\n/g;
    $block =~ s/<\/pre>/\n.fi\n.ad\n.ft R\n/g;
    $block =~ s/<dl[^>]*>/\n/g;
    $block =~ s/<ul>/\n/g;
    #$block =~ s/<\/dl>/\n.PP\n/g;
    #$block =~ s/<\/ul>/\n.PP\n/g;
    $block =~ s/<\/dl>/\n.br\n/g;
    $block =~ s/<\/ul>/\n.br\n/g;
    $block =~ s/<dd>\s*/\n/g;
    $block =~ s/<\/dd>/\n.br\n/g;
    $block =~ s/<li>\s*/\n.IP \\(bu\n/g;
    $block =~ s/<dt>\s*/\n.IP "/g;
    $block =~ s/\s*<\/dt>/"/g;
    $block =~ s/<tt>\s*//g;
    $block =~ s/\s*<\/tt>//g;
    # Munge "-" here, so that we don't screw up ".in -4".
    $block =~ s/-/\\-/g;
    $block =~ s/<blockquote>/\n.sp\n.in +4\n/g;
    $block =~ s/<\/blockquote>/\n.in -4\n/g;
    $block =~ s/\n<br>\s*/\n.br\n/g;
    $block =~ s/<br>\s*/\n.br\n/g;
    $block =~ s/&le;/<=/g;
    $block =~ s/&lt;/</g;
    $block =~ s/&ge;/>=/g;
    $block =~ s/&gt;/>/g;
    $block =~ s/&amp;/\&/g;
    $block =~ s/&ndash;/-/g;
    $block =~ s/&mdash;/-/g;
    $block =~ s/\s+\n/\n/g;
    $block =~ s/^\n//g;
    $block =~ s/\s*<\s*nroffescape\s+([^ >]+)\s*>\s*/\n\1\n/g;
    $block =~ s/([A-Za-z][_a-zA-Z0-9-]*)(\([0-9]\))/\\fB\1\\fR\2/g;
    print $block;
    $wantpp = !($block =~ /^\.(SH|IP)/);
}