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', 8, 'ntp.conf(5), ntpq(8), ntpdc(8)'],
23	     'ntpq' => ['ntpq', 8, 'ntpd(8), ntpdc(8)'],
24	     'ntpdate' => ['ntpdate', 8, 'ntpd(8)'],
25	     'ntpdc' => ['ntpdc', 8, 'ntpd(8)'],
26	     'ntptime' => ['ntpdtime', 8, 'ntpd(8), ntpdate(8)'],
27	     'ntptrace' => ['ntptrace', 8, 'ntpd(8)'],
28	     'keygen' => ['ntp-keygen', 8, 'ntpd(8), ntp_auth(5)'],
29	     'confopt' => ['ntp.conf', 5, 'ntpd(8)'],
30	     'authopt' => ['ntp_auth', 5, 'ntp.conf(5), ntpd(8)'],
31	     'monopt' => ['ntp_mon', 5, 'ntp.conf(5)'],
32	     'accopt' => ['ntp_acc', 5, 'ntp.conf(5)'],
33	     'clockopt' => ['ntp_clock', 5, 'ntp.conf(5)'],
34	     'miscopt' => ['ntp_misc', 5, 'ntp.conf(5)']);
35
36# Disclaimer to go in SEE ALSO section of the man page
37$seealso_disclaimer = 'These man pages are automatically hacked from the main NTP ' .
38    'documentation pages, which are maintained in HTML format.  These files are ' .
39    'included in the NTP source distribution.  If you installed NTP from a binary ' .
40    'package, or it came pre-installed on your system, chances are the documentation ' .
41    'was also included in the usual place for your system.  The HTML files are more ' .
42    'correct and complete than these man pages, which are provided for your reference ' .
43    'only.';
44
45# Disclaimer to go right at the top
46$top_disclaimer = 'This file was automatically generated from HTML source, and may be ' .
47    'incorrect.  See the SEE ALSO section at the end of this file for more info';
48
49mkdir $MANDIR, 0777;
50mkdir "$MANDIR/man8", 0777;
51mkdir "$MANDIR/man5", 0777;
52
53# Do the actual processing
54foreach $file (keys %manfiles) {
55    process($file);
56}
57# End of main function
58
59
60
61# Do the real work
62sub process {
63    my($filename) = @_;
64    $fileinfo = $manfiles{$filename};
65
66    $p = HTML::TokeParser->new("$filename.html") || die "Can't open $filename.html: $!";
67    open(MANOUT, ">$MANDIR/man$fileinfo->[1]/$fileinfo->[0].$fileinfo->[1]")
68	|| die "Can't open: $!";
69
70    $p->get_tag("title");
71    $name = $p->get_text("/title");
72    $p->get_tag("hr");		# Skip past image and quote, hopefully
73
74    # Setup man header
75    print MANOUT ".TH " . $fileinfo->[0] . " " . $fileinfo->[1] .  "\n";
76    print MANOUT ".UC 4\n";
77    print MANOUT ".SH NAME\n";
78    $pat = $fileinfo->[0];
79    if ($name =~ /$pat/) {
80    } else {
81	# Add the manpage name, if not in the HTML title already
82	print MANOUT "$fileinfo->[0] - ";
83    }
84    print MANOUT "$name\n\n";
85
86    print MANOUT "$top_disclaimer\n";
87
88    # Now start scanning.  We basically print everything after translating some tags.
89    # $token->[0] has "T", "S", "E" for Text, Start, End
90    # $token->[1] has the tag name, or text (for "T" case)
91    #  Theres lots more in the world of tokens, but who cares?
92    while (my $token = $p->get_token) {
93	if($token->[0] eq "T") {
94	    my $text = $token->[1];
95	    if($tag) {
96		$text =~ s/^[\n ]*//;
97		$text =~ s/[\n ]*$/ /;
98	    }
99	    $text =~ s/&nbsp\;/ /g;
100	    $text =~ s/^\./\\./;
101	    print MANOUT "$text";
102	    $tag = 0;
103	}
104	if($token->[0] eq "S") {
105	    if($token->[1] eq "h4") {
106		my $text = uc($p->get_trimmed_text("/h4"));
107		print MANOUT ".SH $text\n";
108	    }
109	    if($token->[1] eq "tt") {
110		push @fontstack, "tt";
111		print MANOUT "\\fB";
112	    }
113	    if($token->[1] eq "i") {
114		push @fontstack, "i";
115		print MANOUT "\\fI";
116	    }
117	    if($token->[1] eq "address") {
118		my $text = $p->get_trimmed_text("/address");
119		print MANOUT "\n.SH AUTHOR\n$text\n";
120	    }
121	    if($token->[1] eq "dt") {
122		$tmp = $deflevel-4;
123		print MANOUT "\n.RS $tmp\n";
124		$tag = 1;
125	    }
126	    if($token->[1] eq "dd") {
127		print MANOUT "\n.RS $deflevel\n";
128		$tag = 1;
129	    }
130	    if($token->[1] eq "dl") {
131		$deflevel+=4;
132	    }
133	}
134	elsif($token->[0] eq "E") {
135	    if($token->[1] eq "dd") {
136		print MANOUT "\n.RE\n";
137		$tag = 1;
138	    }
139	    if($token->[1] eq "tt") {
140		$f = pop @fontstack;
141		if($f ne "tt") {
142		    warn "Oops, mismatched font!  Trying to continue\n";
143		}
144		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
145		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
146		else { $fontswitch = "\\fI"; }
147		print MANOUT "$fontswitch";
148	    }
149	    if($token->[1] eq "i") {
150		$f = pop @fontstack;
151		if($f ne "i") {
152		    warn "Oops, mismatched font!  Trying to continue\n";
153		}
154		if ($#fontstack < 0) { $fontswitch = "\\fR"; }
155		elsif ($fontstack[$#fontstack] eq "tt") { $fontswitch = "\\fB"; }
156		else { $fontswitch = "\\fI"; }
157		print MANOUT "$fontswitch";
158	    }
159	    if($token->[1] eq "dl") {
160		$deflevel-=4;
161	    }
162	    if($token->[1] eq "dt") {
163		print MANOUT "\n.RE";
164		$tag = 1;
165	    }
166	}
167    }
168    print MANOUT ".SH SEE ALSO\n\n";
169    print MANOUT "$fileinfo->[2]\n\n";
170    print MANOUT "$seealso_disclaimer\n";
171    close(MANOUT);
172}
173