/contrib/ntp/scripts/html2man.in

https://bitbucket.org/freebsd/freebsd-head/ · Autoconf · 172 lines · 131 code · 16 blank · 25 comment · 29 complexity · 4a35ed7890d22336ed5d7f04cef93655 MD5 · raw file

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