summaryrefslogtreecommitdiffstats
path: root/mantools/postconf2html
blob: 5ad038f46bb66c48406ff45f324ef3f2e3f43eca (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
96
97
98
99
#!/usr/bin/perl

# postconf2html - add HTML paragraphs

# Basic operation:
#
# - Process input as text blocks separated by one or more empty
# (or all whitespace) lines.
#
# - Remove text between <!-- and -->; each may be on a different line.
#
# - Optionally remove <nroffescape> pass-through requests (unless
#   the -n option is specified).
#
# - Don't touch blocks that start with `<' in column zero.
#
# The only changes made are:
#
# - Emit "<DT><a name="parametername">parametername</a>...</DT><DD>" at
#   the top of each parameter description.
#
# All other non-comment input is flagged as an error.

use Getopt::Std;

$opt_h = undef;
$opt_v = undef;
$opt_n = undef;
getopts("hnv");

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

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

while(<>) {

    # Skip comments.
    next if /^#/;

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

    # Gobble up the next text block.
    $block = "";
    $comment = 0;
    do {
	$_ =~ s/\s+\n$/\n/;
	$block .= $_;
	if ($_ =~ /<!--/)
	    { $comment = 1; } 
	if ($comment && $_ =~ /-->/)
	    { $comment = 0; $block =~ s/<!--.*-->//sg; }
    } while((($_ = <>) && /\S/) || $comment);

    # Strip nroff escapes.
    $block =~ s/<\s*nroffescape[^>]+>//g unless $opt_n;

    # Skip blanks after comment elimination.
    if ($block =~ /^\s/) {
	$block =~ s/^\s+//s;
	next if ($block eq "");
    }

    # Don't touch a text block starting with < in column zero.
    if ($block =~ /^</) {
	print "$block\n";
    }

    # Meta block. Emit upper case tags for html2man.
    elsif ($block =~ /^%PARAM/) {
	print "\n</DD>\n\n" if ($param);
	print "\n<DL>\n\n" if ($need_dl);
	$need_dl = 0;
	($junk, $param, $defval) = split(/\s+/, $block, 3);
	$defval =~ s/\s+$//s;
	$defval = "empty" if ($defval eq "");
	$defval = "default: $defval" unless ($defval eq "read-only");
	print "<DT><b><a name=\"$param\">$param</a>\n($defval)</b></DT><DD>\n\n";
    }

    # Meta block. Emit upper case tags for html2man.
    elsif ($block =~ /^%CLASS/) {
	print "\n</DD>\n\n" if ($param);
	print "\n</DL>\n\n" if ($class);
	$param ="";
	($junk, $class, $text) = split(/\s+/, $block, 3);
	$text =~ s/\s+$//s;
	print "<H2><a name=\"$class\">$text</a></H2>\n\n";
	$need_dl = 1;
    }

    # Can't happen.
    else {
	die "Unrecognized text block:\n$block";
    }
}

print "\n</DD>\n\n" if ($param);
print "\n</DL>\n\n" if ($class);