1275970Scy#! @PATH_PERL@ -w
2275970Scy#
3275970Scy# html2man: Converts the NTP HTML documentation to man page format
4275970Scy#
5275970Scy# This file require the Perl HTML::TokeParser module:
6275970Scy# http://search.cpan.org/search?module=HTML::TokeParser
7275970Scy#
8275970Scy# Depending on where this is run from, you might need to modify $MANDIR below.
9275970Scy#
10275970Scy# Hacked together by Peter Boettcher <boettcher@ll.mit.edu>
11275970Scy# Last modified: <Mon Jan 28 17:24:38 2002 by pwb>
12275970Scy
13275970Scyrequire HTML::TokeParser;
14275970Scy
15275970Scy# use strict;		# I can dream...
16275970Scy
17275970Scy$MANDIR = "./man";
18275970Scy
19275970Scy# HTML files to convert.  Also include per-file info here: 
20275970Scy#   name of man page, man section, 'see also' section
21275970Scy%manfiles = (
22275970Scy	     'ntpd' => ['ntpd', @NTPD_MS@, 'ntp.conf(5), ntpq(@NTPQ_MS@), ntpdc(@NTPDC_MS@)'],
23275970Scy	     'ntpq' => ['ntpq', @NTPQ_MS@, 'ntp_decode(5), ntpd(@NTPD_MS@), ntpdc(@NTPDC_MS@)'],
24275970Scy	     'ntpdate' => ['ntpdate', @NTPDATE_MS@, 'ntpd(@NTPD_MS@)'],
25275970Scy	     'ntpdc' => ['ntpdc', @NTPDC_MS@, 'ntpd(@NTPD_MS@)'],
26275970Scy	     'ntptime' => ['ntptime', @NTPTIME_MS@, 'ntpd(@NTPD_MS@), ntpdate(@NTPDATE_MS@)'],
27275970Scy	     'ntptrace' => ['ntptrace', @NTPTRACE_MS@, 'ntpd(@NTPD_MS@)'],
28275970Scy	     'ntp-wait' => ['ntp-wait', @NTP_WAIT_MS@, 'ntpd(@NTPD_MS@)'],
29275970Scy	     'keygen' => ['ntp-keygen', @NTP_KEYGEN_MS@, 'ntpd(@NTPD_MS@), ntp_auth(5)'],
30275970Scy	     'tickadj' => ['tickadj', @TICKADJ_MS@, 'ntpd(@NTPD_MS@)'],
31275970Scy	     'confopt' => ['ntp.conf', 5, 'ntpd(@NTPD_MS@), ntp_auth(5), ntp_mon(5), ntp_acc(5), ntp_clock(5), ntp_misc(5)'],
32275970Scy	     'authopt' => ['ntp_auth', 5, 'ntp.conf(5), ntpd(@NTPD_MS@)'],
33275970Scy	     'monopt' => ['ntp_mon', 5, 'ntp.conf(5), ntp_decode(5)'],
34275970Scy	     'accopt' => ['ntp_acc', 5, 'ntp.conf(5)'],
35275970Scy	     'clockopt' => ['ntp_clock', 5, 'ntp.conf(5)'],
36275970Scy	     'decode' => ['ntp_decode', 5, 'ntpq(@NTPQ_MS@), ntp_mon(5)'],
37275970Scy	     'miscopt' => ['ntp_misc', 5, 'ntp.conf(5)']);
38275970Scy
39275970Scy%table_headers = (
40275970Scy    'ntpd' => 'l l l l.',
41275970Scy    'ntpq' => 'l l.',
42275970Scy    'monopt' => 'l l l.',
43275970Scy    'decode' => 'l l l l.',
44275970Scy    'authopt' => 'c c c c c c.'
45275970Scy);
46275970Scy
47275970Scy# Disclaimer to go in SEE ALSO section of the man page
48275970Scy$seealso_disclaimer = "The official HTML documentation.\n\n" .
49275970Scy    "This file was automatically generated from HTML source.\n";
50275970Scy
51275970Scymkdir $MANDIR, 0777;
52275970Scymkdir "$MANDIR/man8", 0777;
53275970Scymkdir "$MANDIR/man5", 0777;
54275970Scy
55275970Scy# Do the actual processing
56275970Scyforeach $file (keys %manfiles) {
57275970Scy    process($file);
58275970Scy}
59275970Scy# End of main function
60275970Scy
61275970Scy
62275970Scy
63275970Scy# Do the real work
64275970Scysub process {
65275970Scy    my($filename) = @_;
66275970Scy    $fileinfo = $manfiles{$filename};
67275970Scy
68275970Scy    $p = HTML::TokeParser->new("$filename.html") || die "Can't open $filename.html: $!";
69275970Scy    $fileout = "$MANDIR/man$fileinfo->[1]/$fileinfo->[0].$fileinfo->[1]";
70275970Scy    open(MANOUT, ">$fileout")
71275970Scy	|| die "Can't open: $!";
72275970Scy
73275970Scy    $p->get_tag("title");
74275970Scy    $name = $p->get_text("/title");
75275970Scy    $p->get_tag("hr");		# Skip past image and quote, hopefully
76275970Scy
77275970Scy    # Setup man header
78275970Scy    print MANOUT ".TH " . $fileinfo->[0] . " " . $fileinfo->[1] .  "\n";
79275970Scy    print MANOUT ".SH NAME\n";
80275970Scy    $pat = $fileinfo->[0];
81275970Scy    if ($name =~ /$pat/) {
82275970Scy    } else {
83275970Scy	# Add the manpage name, if not in the HTML title already
84275970Scy	print MANOUT "$fileinfo->[0] - ";
85275970Scy    }
86275970Scy    print MANOUT "$name\n.SH \\ \n\n";
87275970Scy
88275970Scy    @fontstack = ();
89275970Scy    $deflevel = 0;
90275970Scy    $pre = 0;
91275970Scy    $ignore = 0;
92275970Scy    $first_td = 1;
93275970Scy    # Now start scanning.  We basically print everything after translating some tags.
94275970Scy    # $token->[0] has "T", "S", "E" for Text, Start, End
95275970Scy    # $token->[1] has the tag name, or text (for "T" case)
96275970Scy    #  Theres lots more in the world of tokens, but who cares?
97275970Scy    while (my $token = $p->get_token) {
98275970Scy	if($token->[0] eq "T") {
99275970Scy	    my $text = $token->[1];
100275970Scy	    if (!$pre) {
101275970Scy		if($tag) {
102275970Scy		    $text =~ s/^[\n\t ]*//;
103275970Scy		}
104275970Scy		$text =~ s/^[\n\t ][\n\t ]+$//;
105275970Scy		$text =~ s/[\n\t ]+/ /g;
106275970Scy		$text =~ s/&nbsp\;/ /g;
107275970Scy		$text =~ s/&gt\;/>/g;
108275970Scy		$text =~ s/&lt\;/</g;
109275970Scy		$text =~ s/&quot\;/"/g;
110275970Scy		$text =~ s/&amp\;/&/g;
111275970Scy		$text =~ s/^\./\\[char46]/;
112275970Scy	    }
113275970Scy	    print MANOUT "$text";
114275970Scy	    $tag = 0;
115275970Scy	}
116275970Scy	if($token->[0] eq "S") {
117275970Scy	    if($token->[1] eq "h4") {
118275970Scy		my $text = uc($p->get_trimmed_text("/h4"));
119275970Scy		# ignore these sections in ntpd.html
120275970Scy		if ($filename eq "ntpd" &&
121275970Scy		    ($text eq "CONFIGURATION OPTIONS")) {
122275970Scy			$ignore = 1;
123275970Scy			close(MANOUT);
124275970Scy			open(MANOUT, ">/dev/null");
125275970Scy		} elsif ($ignore) {
126275970Scy		    $ignore = 0;
127275970Scy		    close(MANOUT);
128275970Scy		    open(MANOUT, ">>$fileout");
129275970Scy		}
130275970Scy		print MANOUT "\n\n.SH $text\n";
131275970Scy	    }
132275970Scy	    if($token->[1] eq "tt") {
133275970Scy		push @fontstack, "tt";
134275970Scy		print MANOUT "\\fB";
135275970Scy	    }
136275970Scy	    if($token->[1] eq "i") {
137275970Scy		push @fontstack, "i";
138275970Scy		print MANOUT "\\fI";
139275970Scy	    }
140275970Scy	    if($token->[1] eq "address") {
141275970Scy		my $text = $p->get_trimmed_text("/address");
142275970Scy		print MANOUT "\n.SH AUTHOR\n$text\n";
143275970Scy	    }
144275970Scy	    if($token->[1] eq "dt" || $token->[1] eq "br" && $deflevel > 0) {
145275970Scy		print MANOUT "\n.TP 8\n";
146275970Scy		$tag = 1;
147275970Scy	    }
148275970Scy	    if($token->[1] eq "dd") {
149275970Scy		print MANOUT "\n";
150275970Scy		$tag = 1;
151275970Scy	    }
152275970Scy	    if($token->[1] eq "dl") {
153275970Scy		$deflevel+=1;
154275970Scy		if ($deflevel > 0) {
155275970Scy		    print MANOUT "\n.RS ", $deflevel > 1 ? 8 : 0;
156275970Scy		}
157275970Scy	    }
158275970Scy	    if($token->[1] eq "p") {
159275970Scy		print MANOUT "\n";
160275970Scy	    }
161275970Scy	    if($token->[1] eq "pre") {
162275970Scy		print MANOUT "\n.nf";
163275970Scy		$pre = 1;
164275970Scy	    }
165275970Scy	    if($token->[1] eq "table") {
166275970Scy		print MANOUT "\n.TS\n";
167275970Scy		print MANOUT "expand allbox tab(%);\n";
168275970Scy		print MANOUT $table_headers{$filename};
169275970Scy		print MANOUT "\n";
170275970Scy	    }
171275970Scy	    if($token->[1] eq "td") {
172275970Scy		if ($first_td == 0) {
173275970Scy		    print MANOUT " % ";
174275970Scy		}
175275970Scy		$first_td = 0;
176275970Scy	    }
177275970Scy	}
178275970Scy	elsif($token->[0] eq "E") {
179275970Scy	    if($token->[1] eq "h4") {
180275970Scy		$tag = 1;
181275970Scy	    }
182275970Scy	    if($token->[1] eq "tt") {
183275970Scy		$f = pop @fontstack;
184275970Scy		if($f ne "tt") {
185275970Scy		    warn "Oops, mismatched font!  Trying to continue\n";
186275970Scy		}
187275970Scy		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
188275970Scy		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
189275970Scy		else { $fontswitch = "\\fI"; }
190275970Scy		print MANOUT "$fontswitch";
191275970Scy	    }
192275970Scy	    if($token->[1] eq "i") {
193275970Scy		$f = pop @fontstack;
194275970Scy		if($f ne "i") {
195275970Scy		    warn "Oops, mismatched font!  Trying to continue\n";
196275970Scy		}
197275970Scy		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
198275970Scy		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
199275970Scy		else { $fontswitch = "\\fI"; }
200275970Scy		print MANOUT "$fontswitch";
201275970Scy	    }
202275970Scy	    if($token->[1] eq "dl") {
203275970Scy		if ($deflevel > 0) {
204275970Scy		    print MANOUT "\n.RE";
205275970Scy		}
206275970Scy		print MANOUT "\n";
207275970Scy		$deflevel-=1;
208275970Scy	    }
209275970Scy	    if($token->[1] eq "p") {
210275970Scy		print MANOUT "\n";
211275970Scy		$tag = 1;
212275970Scy	    }
213275970Scy	    if($token->[1] eq "pre") {
214275970Scy		print MANOUT "\n.fi";
215275970Scy		$pre = 0;
216275970Scy	    }
217275970Scy	    if($token->[1] eq "table") {
218275970Scy		print MANOUT ".TE\n";
219275970Scy	    }
220275970Scy	    if($token->[1] eq "tr") {
221275970Scy		print MANOUT "\n";
222275970Scy		$first_td = 1;
223275970Scy	    }
224275970Scy	}
225275970Scy    }
226275970Scy    if ($ignore) {
227275970Scy	close(MANOUT);
228275970Scy	open(MANOUT, ">>$fileout");
229275970Scy    }
230275970Scy    print MANOUT "\n.SH SEE ALSO\n\n";
231275970Scy    print MANOUT "$fileinfo->[2]\n\n";
232275970Scy    print MANOUT "$seealso_disclaimer\n";
233275970Scy    close(MANOUT);
234275970Scy}
235