summaryrefslogtreecommitdiff
path: root/scripts/deprecated/html2man.in
blob: adc484820a08fc30c39eb374fcddd622c5d23551 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#! @PATH_PERL@ -w
#
# html2man: Converts the NTP HTML documentation to man page format
#
# This file require the Perl HTML::TokeParser module:
# http://search.cpan.org/search?module=HTML::TokeParser
#
# Depending on where this is run from, you might need to modify $MANDIR below.
#
# Hacked together by Peter Boettcher <boettcher@ll.mit.edu>
# Last modified: <Mon Jan 28 17:24:38 2002 by pwb>

require HTML::TokeParser;

# use strict;		# I can dream...

$MANDIR = "./man";

# HTML files to convert.  Also include per-file info here: 
#   name of man page, man section, 'see also' section
%manfiles = (
	     'ntpd' => ['ntpd', @NTPD_MS@, 'ntp.conf(5), ntpq(@NTPQ_MS@), ntpdc(@NTPDC_MS@)'],
	     'ntpq' => ['ntpq', @NTPQ_MS@, 'ntp_decode(5), ntpd(@NTPD_MS@), ntpdc(@NTPDC_MS@)'],
	     'ntpdate' => ['ntpdate', @NTPDATE_MS@, 'ntpd(@NTPD_MS@)'],
	     'ntpdc' => ['ntpdc', @NTPDC_MS@, 'ntpd(@NTPD_MS@)'],
	     'ntptime' => ['ntptime', @NTPTIME_MS@, 'ntpd(@NTPD_MS@), ntpdate(@NTPDATE_MS@)'],
	     'ntptrace' => ['ntptrace', @NTPTRACE_MS@, 'ntpd(@NTPD_MS@)'],
	     'ntp-wait' => ['ntp-wait', @NTP_WAIT_MS@, 'ntpd(@NTPD_MS@)'],
	     'keygen' => ['ntp-keygen', @NTP_KEYGEN_MS@, 'ntpd(@NTPD_MS@), ntp_auth(5)'],
	     'tickadj' => ['tickadj', @TICKADJ_MS@, 'ntpd(@NTPD_MS@)'],
	     'confopt' => ['ntp.conf', 5, 'ntpd(@NTPD_MS@), ntp_auth(5), ntp_mon(5), ntp_acc(5), ntp_clock(5), ntp_misc(5)'],
	     'authopt' => ['ntp_auth', 5, 'ntp.conf(5), ntpd(@NTPD_MS@)'],
	     'monopt' => ['ntp_mon', 5, 'ntp.conf(5), ntp_decode(5)'],
	     'accopt' => ['ntp_acc', 5, 'ntp.conf(5)'],
	     'clockopt' => ['ntp_clock', 5, 'ntp.conf(5)'],
	     'decode' => ['ntp_decode', 5, 'ntpq(@NTPQ_MS@), ntp_mon(5)'],
	     'miscopt' => ['ntp_misc', 5, 'ntp.conf(5)']);

%table_headers = (
    'ntpd' => 'l l l l.',
    'ntpq' => 'l l.',
    'monopt' => 'l l l.',
    'decode' => 'l l l l.',
    'authopt' => 'c c c c c c.'
);

# Disclaimer to go in SEE ALSO section of the man page
$seealso_disclaimer = "The official HTML documentation.\n\n" .
    "This file was automatically generated from HTML source.\n";

mkdir $MANDIR, 0777;
mkdir "$MANDIR/man8", 0777;
mkdir "$MANDIR/man5", 0777;

# Do the actual processing
foreach $file (keys %manfiles) {
    process($file);
}
# End of main function



# Do the real work
sub process {
    my($filename) = @_;
    $fileinfo = $manfiles{$filename};

    $p = HTML::TokeParser->new("$filename.html") || die "Can't open $filename.html: $!";
    $fileout = "$MANDIR/man$fileinfo->[1]/$fileinfo->[0].$fileinfo->[1]";
    open(MANOUT, ">$fileout")
	|| die "Can't open: $!";

    $p->get_tag("title");
    $name = $p->get_text("/title");
    $p->get_tag("hr");		# Skip past image and quote, hopefully

    # Setup man header
    print MANOUT ".TH " . $fileinfo->[0] . " " . $fileinfo->[1] .  "\n";
    print MANOUT ".SH NAME\n";
    $pat = $fileinfo->[0];
    if ($name =~ /$pat/) {
    } else {
	# Add the manpage name, if not in the HTML title already
	print MANOUT "$fileinfo->[0] - ";
    }
    print MANOUT "$name\n.SH \\ \n\n";

    @fontstack = ();
    $deflevel = 0;
    $pre = 0;
    $ignore = 0;
    $first_td = 1;
    # Now start scanning.  We basically print everything after translating some tags.
    # $token->[0] has "T", "S", "E" for Text, Start, End
    # $token->[1] has the tag name, or text (for "T" case)
    #  Theres lots more in the world of tokens, but who cares?
    while (my $token = $p->get_token) {
	if($token->[0] eq "T") {
	    my $text = $token->[1];
	    if (!$pre) {
		if($tag) {
		    $text =~ s/^[\n\t ]*//;
		}
		$text =~ s/^[\n\t ][\n\t ]+$//;
		$text =~ s/[\n\t ]+/ /g;
		$text =~ s/&nbsp\;/ /g;
		$text =~ s/&gt\;/>/g;
		$text =~ s/&lt\;/</g;
		$text =~ s/&quot\;/"/g;
		$text =~ s/&amp\;/&/g;
		$text =~ s/^\./\\[char46]/;
	    }
	    print MANOUT "$text";
	    $tag = 0;
	}
	if($token->[0] eq "S") {
	    if($token->[1] eq "h4") {
		my $text = uc($p->get_trimmed_text("/h4"));
		# ignore these sections in ntpd.html
		if ($filename eq "ntpd" &&
		    ($text eq "CONFIGURATION OPTIONS")) {
			$ignore = 1;
			close(MANOUT);
			open(MANOUT, ">/dev/null");
		} elsif ($ignore) {
		    $ignore = 0;
		    close(MANOUT);
		    open(MANOUT, ">>$fileout");
		}
		print MANOUT "\n\n.SH $text\n";
	    }
	    if($token->[1] eq "tt") {
		push @fontstack, "tt";
		print MANOUT "\\fB";
	    }
	    if($token->[1] eq "i") {
		push @fontstack, "i";
		print MANOUT "\\fI";
	    }
	    if($token->[1] eq "address") {
		my $text = $p->get_trimmed_text("/address");
		print MANOUT "\n.SH AUTHOR\n$text\n";
	    }
	    if($token->[1] eq "dt" || $token->[1] eq "br" && $deflevel > 0) {
		print MANOUT "\n.TP 8\n";
		$tag = 1;
	    }
	    if($token->[1] eq "dd") {
		print MANOUT "\n";
		$tag = 1;
	    }
	    if($token->[1] eq "dl") {
		$deflevel+=1;
		if ($deflevel > 0) {
		    print MANOUT "\n.RS ", $deflevel > 1 ? 8 : 0;
		}
	    }
	    if($token->[1] eq "p") {
		print MANOUT "\n";
	    }
	    if($token->[1] eq "pre") {
		print MANOUT "\n.nf";
		$pre = 1;
	    }
	    if($token->[1] eq "table") {
		print MANOUT "\n.TS\n";
		print MANOUT "expand allbox tab(%);\n";
		print MANOUT $table_headers{$filename};
		print MANOUT "\n";
	    }
	    if($token->[1] eq "td") {
		if ($first_td == 0) {
		    print MANOUT " % ";
		}
		$first_td = 0;
	    }
	}
	elsif($token->[0] eq "E") {
	    if($token->[1] eq "h4") {
		$tag = 1;
	    }
	    if($token->[1] eq "tt") {
		$f = pop @fontstack;
		if($f ne "tt") {
		    warn "Oops, mismatched font!  Trying to continue\n";
		}
		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
		else { $fontswitch = "\\fI"; }
		print MANOUT "$fontswitch";
	    }
	    if($token->[1] eq "i") {
		$f = pop @fontstack;
		if($f ne "i") {
		    warn "Oops, mismatched font!  Trying to continue\n";
		}
		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
		else { $fontswitch = "\\fI"; }
		print MANOUT "$fontswitch";
	    }
	    if($token->[1] eq "dl") {
		if ($deflevel > 0) {
		    print MANOUT "\n.RE";
		}
		print MANOUT "\n";
		$deflevel-=1;
	    }
	    if($token->[1] eq "p") {
		print MANOUT "\n";
		$tag = 1;
	    }
	    if($token->[1] eq "pre") {
		print MANOUT "\n.fi";
		$pre = 0;
	    }
	    if($token->[1] eq "table") {
		print MANOUT ".TE\n";
	    }
	    if($token->[1] eq "tr") {
		print MANOUT "\n";
		$first_td = 1;
	    }
	}
    }
    if ($ignore) {
	close(MANOUT);
	open(MANOUT, ">>$fileout");
    }
    print MANOUT "\n.SH SEE ALSO\n\n";
    print MANOUT "$fileinfo->[2]\n\n";
    print MANOUT "$seealso_disclaimer\n";
    close(MANOUT);
}