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