xref: /netbsd-src/external/bsd/ntp/dist/scripts/deprecated/html2man.in (revision 8585484ef87f5a04d32332313cdb799625f4faf8)
1*8585484eSchristos#! @PATH_PERL@ -w
2*8585484eSchristos#
3*8585484eSchristos# html2man: Converts the NTP HTML documentation to man page format
4*8585484eSchristos#
5*8585484eSchristos# This file require the Perl HTML::TokeParser module:
6*8585484eSchristos# http://search.cpan.org/search?module=HTML::TokeParser
7*8585484eSchristos#
8*8585484eSchristos# Depending on where this is run from, you might need to modify $MANDIR below.
9*8585484eSchristos#
10*8585484eSchristos# Hacked together by Peter Boettcher <boettcher@ll.mit.edu>
11*8585484eSchristos# Last modified: <Mon Jan 28 17:24:38 2002 by pwb>
12*8585484eSchristos
13*8585484eSchristosrequire HTML::TokeParser;
14*8585484eSchristos
15*8585484eSchristos# use strict;		# I can dream...
16*8585484eSchristos
17*8585484eSchristos$MANDIR = "./man";
18*8585484eSchristos
19*8585484eSchristos# HTML files to convert.  Also include per-file info here:
20*8585484eSchristos#   name of man page, man section, 'see also' section
21*8585484eSchristos%manfiles = (
22*8585484eSchristos	     'ntpd' => ['ntpd', @NTPD_MS@, 'ntp.conf(5), ntpq(@NTPQ_MS@), ntpdc(@NTPDC_MS@)'],
23*8585484eSchristos	     'ntpq' => ['ntpq', @NTPQ_MS@, 'ntp_decode(5), ntpd(@NTPD_MS@), ntpdc(@NTPDC_MS@)'],
24*8585484eSchristos	     'ntpdate' => ['ntpdate', @NTPDATE_MS@, 'ntpd(@NTPD_MS@)'],
25*8585484eSchristos	     'ntpdc' => ['ntpdc', @NTPDC_MS@, 'ntpd(@NTPD_MS@)'],
26*8585484eSchristos	     'ntptime' => ['ntptime', @NTPTIME_MS@, 'ntpd(@NTPD_MS@), ntpdate(@NTPDATE_MS@)'],
27*8585484eSchristos	     'ntptrace' => ['ntptrace', @NTPTRACE_MS@, 'ntpd(@NTPD_MS@)'],
28*8585484eSchristos	     'ntp-wait' => ['ntp-wait', @NTP_WAIT_MS@, 'ntpd(@NTPD_MS@)'],
29*8585484eSchristos	     'keygen' => ['ntp-keygen', @NTP_KEYGEN_MS@, 'ntpd(@NTPD_MS@), ntp_auth(5)'],
30*8585484eSchristos	     'tickadj' => ['tickadj', @TICKADJ_MS@, 'ntpd(@NTPD_MS@)'],
31*8585484eSchristos	     'confopt' => ['ntp.conf', 5, 'ntpd(@NTPD_MS@), ntp_auth(5), ntp_mon(5), ntp_acc(5), ntp_clock(5), ntp_misc(5)'],
32*8585484eSchristos	     'authopt' => ['ntp_auth', 5, 'ntp.conf(5), ntpd(@NTPD_MS@)'],
33*8585484eSchristos	     'monopt' => ['ntp_mon', 5, 'ntp.conf(5), ntp_decode(5)'],
34*8585484eSchristos	     'accopt' => ['ntp_acc', 5, 'ntp.conf(5)'],
35*8585484eSchristos	     'clockopt' => ['ntp_clock', 5, 'ntp.conf(5)'],
36*8585484eSchristos	     'decode' => ['ntp_decode', 5, 'ntpq(@NTPQ_MS@), ntp_mon(5)'],
37*8585484eSchristos	     'miscopt' => ['ntp_misc', 5, 'ntp.conf(5)']);
38*8585484eSchristos
39*8585484eSchristos%table_headers = (
40*8585484eSchristos    'ntpd' => 'l l l l.',
41*8585484eSchristos    'ntpq' => 'l l.',
42*8585484eSchristos    'monopt' => 'l l l.',
43*8585484eSchristos    'decode' => 'l l l l.',
44*8585484eSchristos    'authopt' => 'c c c c c c.'
45*8585484eSchristos);
46*8585484eSchristos
47*8585484eSchristos# Disclaimer to go in SEE ALSO section of the man page
48*8585484eSchristos$seealso_disclaimer = "The official HTML documentation.\n\n" .
49*8585484eSchristos    "This file was automatically generated from HTML source.\n";
50*8585484eSchristos
51*8585484eSchristosmkdir $MANDIR, 0777;
52*8585484eSchristosmkdir "$MANDIR/man8", 0777;
53*8585484eSchristosmkdir "$MANDIR/man5", 0777;
54*8585484eSchristos
55*8585484eSchristos# Do the actual processing
56*8585484eSchristosforeach $file (keys %manfiles) {
57*8585484eSchristos    process($file);
58*8585484eSchristos}
59*8585484eSchristos# End of main function
60*8585484eSchristos
61*8585484eSchristos
62*8585484eSchristos
63*8585484eSchristos# Do the real work
64*8585484eSchristossub process {
65*8585484eSchristos    my($filename) = @_;
66*8585484eSchristos    $fileinfo = $manfiles{$filename};
67*8585484eSchristos
68*8585484eSchristos    $p = HTML::TokeParser->new("$filename.html") || die "Can't open $filename.html: $!";
69*8585484eSchristos    $fileout = "$MANDIR/man$fileinfo->[1]/$fileinfo->[0].$fileinfo->[1]";
70*8585484eSchristos    open(MANOUT, ">$fileout")
71*8585484eSchristos	|| die "Can't open: $!";
72*8585484eSchristos
73*8585484eSchristos    $p->get_tag("title");
74*8585484eSchristos    $name = $p->get_text("/title");
75*8585484eSchristos    $p->get_tag("hr");		# Skip past image and quote, hopefully
76*8585484eSchristos
77*8585484eSchristos    # Setup man header
78*8585484eSchristos    print MANOUT ".TH " . $fileinfo->[0] . " " . $fileinfo->[1] .  "\n";
79*8585484eSchristos    print MANOUT ".SH NAME\n";
80*8585484eSchristos    $pat = $fileinfo->[0];
81*8585484eSchristos    if ($name =~ /$pat/) {
82*8585484eSchristos    } else {
83*8585484eSchristos	# Add the manpage name, if not in the HTML title already
84*8585484eSchristos	print MANOUT "$fileinfo->[0] - ";
85*8585484eSchristos    }
86*8585484eSchristos    print MANOUT "$name\n.SH \\ \n\n";
87*8585484eSchristos
88*8585484eSchristos    @fontstack = ();
89*8585484eSchristos    $deflevel = 0;
90*8585484eSchristos    $pre = 0;
91*8585484eSchristos    $ignore = 0;
92*8585484eSchristos    $first_td = 1;
93*8585484eSchristos    # Now start scanning.  We basically print everything after translating some tags.
94*8585484eSchristos    # $token->[0] has "T", "S", "E" for Text, Start, End
95*8585484eSchristos    # $token->[1] has the tag name, or text (for "T" case)
96*8585484eSchristos    #  Theres lots more in the world of tokens, but who cares?
97*8585484eSchristos    while (my $token = $p->get_token) {
98*8585484eSchristos	if($token->[0] eq "T") {
99*8585484eSchristos	    my $text = $token->[1];
100*8585484eSchristos	    if (!$pre) {
101*8585484eSchristos		if($tag) {
102*8585484eSchristos		    $text =~ s/^[\n\t ]*//;
103*8585484eSchristos		}
104*8585484eSchristos		$text =~ s/^[\n\t ][\n\t ]+$//;
105*8585484eSchristos		$text =~ s/[\n\t ]+/ /g;
106*8585484eSchristos		$text =~ s/&nbsp\;/ /g;
107*8585484eSchristos		$text =~ s/&gt\;/>/g;
108*8585484eSchristos		$text =~ s/&lt\;/</g;
109*8585484eSchristos		$text =~ s/&quot\;/"/g;
110*8585484eSchristos		$text =~ s/&amp\;/&/g;
111*8585484eSchristos		$text =~ s/^\./\\[char46]/;
112*8585484eSchristos	    }
113*8585484eSchristos	    print MANOUT "$text";
114*8585484eSchristos	    $tag = 0;
115*8585484eSchristos	}
116*8585484eSchristos	if($token->[0] eq "S") {
117*8585484eSchristos	    if($token->[1] eq "h4") {
118*8585484eSchristos		my $text = uc($p->get_trimmed_text("/h4"));
119*8585484eSchristos		# ignore these sections in ntpd.html
120*8585484eSchristos		if ($filename eq "ntpd" &&
121*8585484eSchristos		    ($text eq "CONFIGURATION OPTIONS")) {
122*8585484eSchristos			$ignore = 1;
123*8585484eSchristos			close(MANOUT);
124*8585484eSchristos			open(MANOUT, ">/dev/null");
125*8585484eSchristos		} elsif ($ignore) {
126*8585484eSchristos		    $ignore = 0;
127*8585484eSchristos		    close(MANOUT);
128*8585484eSchristos		    open(MANOUT, ">>$fileout");
129*8585484eSchristos		}
130*8585484eSchristos		print MANOUT "\n\n.SH $text\n";
131*8585484eSchristos	    }
132*8585484eSchristos	    if($token->[1] eq "tt") {
133*8585484eSchristos		push @fontstack, "tt";
134*8585484eSchristos		print MANOUT "\\fB";
135*8585484eSchristos	    }
136*8585484eSchristos	    if($token->[1] eq "i") {
137*8585484eSchristos		push @fontstack, "i";
138*8585484eSchristos		print MANOUT "\\fI";
139*8585484eSchristos	    }
140*8585484eSchristos	    if($token->[1] eq "address") {
141*8585484eSchristos		my $text = $p->get_trimmed_text("/address");
142*8585484eSchristos		print MANOUT "\n.SH AUTHOR\n$text\n";
143*8585484eSchristos	    }
144*8585484eSchristos	    if($token->[1] eq "dt" || $token->[1] eq "br" && $deflevel > 0) {
145*8585484eSchristos		print MANOUT "\n.TP 8\n";
146*8585484eSchristos		$tag = 1;
147*8585484eSchristos	    }
148*8585484eSchristos	    if($token->[1] eq "dd") {
149*8585484eSchristos		print MANOUT "\n";
150*8585484eSchristos		$tag = 1;
151*8585484eSchristos	    }
152*8585484eSchristos	    if($token->[1] eq "dl") {
153*8585484eSchristos		$deflevel+=1;
154*8585484eSchristos		if ($deflevel > 0) {
155*8585484eSchristos		    print MANOUT "\n.RS ", $deflevel > 1 ? 8 : 0;
156*8585484eSchristos		}
157*8585484eSchristos	    }
158*8585484eSchristos	    if($token->[1] eq "p") {
159*8585484eSchristos		print MANOUT "\n";
160*8585484eSchristos	    }
161*8585484eSchristos	    if($token->[1] eq "pre") {
162*8585484eSchristos		print MANOUT "\n.nf";
163*8585484eSchristos		$pre = 1;
164*8585484eSchristos	    }
165*8585484eSchristos	    if($token->[1] eq "table") {
166*8585484eSchristos		print MANOUT "\n.TS\n";
167*8585484eSchristos		print MANOUT "expand allbox tab(%);\n";
168*8585484eSchristos		print MANOUT $table_headers{$filename};
169*8585484eSchristos		print MANOUT "\n";
170*8585484eSchristos	    }
171*8585484eSchristos	    if($token->[1] eq "td") {
172*8585484eSchristos		if ($first_td == 0) {
173*8585484eSchristos		    print MANOUT " % ";
174*8585484eSchristos		}
175*8585484eSchristos		$first_td = 0;
176*8585484eSchristos	    }
177*8585484eSchristos	}
178*8585484eSchristos	elsif($token->[0] eq "E") {
179*8585484eSchristos	    if($token->[1] eq "h4") {
180*8585484eSchristos		$tag = 1;
181*8585484eSchristos	    }
182*8585484eSchristos	    if($token->[1] eq "tt") {
183*8585484eSchristos		$f = pop @fontstack;
184*8585484eSchristos		if($f ne "tt") {
185*8585484eSchristos		    warn "Oops, mismatched font!  Trying to continue\n";
186*8585484eSchristos		}
187*8585484eSchristos		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
188*8585484eSchristos		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
189*8585484eSchristos		else { $fontswitch = "\\fI"; }
190*8585484eSchristos		print MANOUT "$fontswitch";
191*8585484eSchristos	    }
192*8585484eSchristos	    if($token->[1] eq "i") {
193*8585484eSchristos		$f = pop @fontstack;
194*8585484eSchristos		if($f ne "i") {
195*8585484eSchristos		    warn "Oops, mismatched font!  Trying to continue\n";
196*8585484eSchristos		}
197*8585484eSchristos		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
198*8585484eSchristos		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
199*8585484eSchristos		else { $fontswitch = "\\fI"; }
200*8585484eSchristos		print MANOUT "$fontswitch";
201*8585484eSchristos	    }
202*8585484eSchristos	    if($token->[1] eq "dl") {
203*8585484eSchristos		if ($deflevel > 0) {
204*8585484eSchristos		    print MANOUT "\n.RE";
205*8585484eSchristos		}
206*8585484eSchristos		print MANOUT "\n";
207*8585484eSchristos		$deflevel-=1;
208*8585484eSchristos	    }
209*8585484eSchristos	    if($token->[1] eq "p") {
210*8585484eSchristos		print MANOUT "\n";
211*8585484eSchristos		$tag = 1;
212*8585484eSchristos	    }
213*8585484eSchristos	    if($token->[1] eq "pre") {
214*8585484eSchristos		print MANOUT "\n.fi";
215*8585484eSchristos		$pre = 0;
216*8585484eSchristos	    }
217*8585484eSchristos	    if($token->[1] eq "table") {
218*8585484eSchristos		print MANOUT ".TE\n";
219*8585484eSchristos	    }
220*8585484eSchristos	    if($token->[1] eq "tr") {
221*8585484eSchristos		print MANOUT "\n";
222*8585484eSchristos		$first_td = 1;
223*8585484eSchristos	    }
224*8585484eSchristos	}
225*8585484eSchristos    }
226*8585484eSchristos    if ($ignore) {
227*8585484eSchristos	close(MANOUT);
228*8585484eSchristos	open(MANOUT, ">>$fileout");
229*8585484eSchristos    }
230*8585484eSchristos    print MANOUT "\n.SH SEE ALSO\n\n";
231*8585484eSchristos    print MANOUT "$fileinfo->[2]\n\n";
232*8585484eSchristos    print MANOUT "$seealso_disclaimer\n";
233*8585484eSchristos    close(MANOUT);
234*8585484eSchristos}
235