Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / ntp-html2mdoc.pl
blobb813af9cd678be078935ce5c0b3599f11303d03f
1 #!/usr/pkg/bin/perl
3 @longmonth = ( "January", "February", "March", "April", "May", "June", "July",
4 "August", "September", "October", "November", "December" );
5 # require "ctime.pl";
7 $sec = 8; # XXX
9 sub swallow {
10 my ($what) = @_;
12 while (<>) {
13 chomp;
14 return if /$what/i;
18 sub chew {
19 my ($match, $reset) = @_;
21 $_ = undef if $reset;
22 while (!(/$match/i)) {
23 chomp($_ .= " " . <>);
27 sub detag {
28 s/<[^>]+>//g;
31 sub dehtmlchar {
32 s/&nbsp;/ /g;
33 s/&lt;/</g;
34 s/&gt;/>/g;
37 ($mday, $mon, $year) = (localtime(time))[3 .. 5];
38 $date = sprintf "%s %02d, %d", $longmonth[$mon], $mday, $year + 1900;
40 swallow("<H3>");
41 chomp($_ = <>);
42 detag;
43 ($name, $descr) = split(' ', $_, 2);
44 $descr =~ s/^\s*-\s*//;
45 ($NAME = $name) =~ tr/a-z/A-Z/;
47 print <<EOF;
48 .\\" \$NetBSD\$
49 .\\" Converted from HTML to mandoc by ntp-html2mdoc.pl
50 .\\"
51 .Dd $date
52 .Dt $NAME $sec
53 .Os
54 .Sh NAME
55 .Nm $name
56 .Nd $descr
57 .Sh SYNOPSIS
58 .Nm
59 EOF
61 swallow("Synopsis");
62 chew("</TT>", 1);
63 detag;
64 s/^\s*$name\s*//;
65 @args = split;
66 while ($_ = shift @args) {
67 next if /\[/;
68 s/^-//;
69 print ".Op Fl $_";
70 if (defined($args[0]) && $args[0] ne "]") {
71 print " Ar ", shift @args;
73 print "\n";
74 shift @args; # remove the "]"
77 while (<>) {
78 chomp;
79 next if length == 0;
80 if (/^<H4>/i) {
81 chew("</H4>");
82 detag;
83 tr/a-z/A-Z/;
84 s/^\s+//;
85 dehtmlchar;
86 print ".Sh $_\n";
87 next;
89 if (/<ADDRESS>/i) {
90 chew("</ADDRESS>");
91 detag;
92 s/^\s+//;
93 dehtmlchar;
94 print ".Sh AUTHOR\n$_\n";
95 next;
97 if (/<DL>/i) {
98 print ".Bl -tag -width indent\n";
99 next;
101 if (m#</DL>#i) {
102 print ".El\n";
103 next;
105 if (/<DT>/i) {
106 chew("</DT>");
107 detag;
108 s/^\s*-//;
109 s/ / Ar /;
110 dehtmlchar;
111 print ".It Fl $_\n";
112 next;
114 if (/<TT>-/) {
115 # command line option
116 chew("</TT>");
117 s#<TT>-([^<]*)#\n.Fl $1\n#ig;
118 s#</TT>##ig;
119 s#<I>([^<]+)</I>#\n.Ar $1\n#ig;
121 if (/<A HREF/) {
122 # html reference to another ntp page
123 chew("</A>");
124 chomp($_ .= " " . <>) if (/<A$/); # another reference on the next line
125 s#<TT><A HREF="(.*).htm">\1</A></TT>#\n.Xr \1 $sec\n#ig;
126 s#<A HREF="([^"]*)">(.*?) +</A> *page#\n.%T "$2"\npage in\n.Pa /usr/share/doc/html/ntp/$1\n#ig;
127 s#^<BR>##g;
128 s#<A HREF="([^"]*)">(.*?)</A>#For\n.%T "$2"\n, refer to\n.Pa /usr/share/doc/html/ntp/$1 .\n.Pp\n#ig;
129 detag;
131 s#<TT>$name</TT>#\n.Nm\n#ig;
132 s#<TT>([^<]*)</TT>#\n.Pa $1\n#ig;
133 s#<TT><A HREF=[^>]*>([^<]*)</A></TT>#\n.Pa $1\n#ig;
134 s#<PRE>#.Pp\n.nf\n#ig;
135 s#</PRE>#\n.fi\n.Pp\n#ig;
136 s#<P>#\n.Pp\n#ig;
137 s#<(HR|DD)>##ig;
138 s#</(BODY|HTML|DD)>##ig;
139 s#<I>([^<]+)</I>#\n.Ar $1\n#ig;
140 dehtmlchar;
141 s/^\s+//;
142 s/\n+\s*/\n/g;
143 s/\n\.(Pa|%T) (.*)\n([,\.:]) /\n.$1 $2 $3\n/g;
144 s/\s+\n/\n/g;
145 s/\n$//;
146 print $_, "\n" if length > 0;