man2html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. eval 'exec perl -Ssw $0 "$@"'
  2. if 0;
  3. # Usage $0 manpage
  4. # Parse my man page formats.
  5. die "Usage: $0 [ manpage ] \n" unless $#ARGV <= 0;
  6. $firstSH = 1;
  7. $inDL = 0;
  8. warn "Doing $ARGV[0]\n";
  9. open(STDIN, "$ARGV[0]") if ($#ARGV == 0);
  10. while (<>) {
  11. next if (/^\.\\"/);
  12. if (/^\.TH\s/) {
  13. # .TH BW_MEM_CP 8 "$Date$" "(c)1994 Larry McVoy" "LMBENCH"
  14. split;
  15. print "<TITLE>$_[1]($_[2]) - LMBENCH man page</TITLE>\n";
  16. print "<H2>$_[1]($_[2]) - LMBENCH man page</H2><HR>\n";
  17. next;
  18. }
  19. if (/^\.SH\s/) {
  20. s/.SH\s+//;
  21. s/"//g;
  22. chop;
  23. print "</DL>\n" unless $firstSH; $firstSH = 0;
  24. print "</DL>\n" if $inDL; $inDL = 0;
  25. print "<DL><DT><H4>$_</H4><DD>\n";
  26. next;
  27. }
  28. next if &fontfont;
  29. if (/^\.LP\s/ || /^\.PP/) {
  30. s/..P\s+//;
  31. chop;
  32. print "<P>\n";
  33. next;
  34. }
  35. if (/^\.TP/) { # treat as a DT list
  36. $_ = <>;
  37. &html;
  38. chop;
  39. print "</DL>\n" if ($inDL);
  40. print "<DL><DT>";
  41. print unless &fontfont;
  42. print "<DD><BR>\n";
  43. $inDL = 1;
  44. next;
  45. }
  46. if (/^\.IP/) { # treat as a DT list
  47. s/^\.IP\s*//;
  48. chop;
  49. s/"//;
  50. s/".*//;
  51. &html;
  52. print "</DL>\n" if ($inDL);
  53. print "<DL><DT>$_<DD><BR>\n";
  54. $inDL = 1;
  55. next;
  56. }
  57. if (/^\.sp/) {
  58. print "<PRE>\n</PRE>\n";
  59. next;
  60. }
  61. next if (/^\.in/ || /^\.ps/); # skip this stuff.
  62. if (/^\.br/) {
  63. print "<BR>\n";
  64. next;
  65. }
  66. if (/^\.nf/ || /^\.DS/) { # starting a display
  67. print "<PRE>\n";
  68. while (<>) {
  69. last if /^\.fi/;
  70. last if /^\.DE/;
  71. next if /^\./;
  72. &html;
  73. print "\t$_"; # XXX - a screwy way of indenting
  74. }
  75. print "</PRE>\n";
  76. next;
  77. }
  78. if (/^\.ft C[WB]/) {
  79. local($pre) = 0;
  80. print "<CODE>\n";
  81. while (<>) {
  82. last if /^\.ft\s*$/;
  83. if (/^\.nf/) {
  84. $pre = 1;
  85. print "<PRE>\n";
  86. next;
  87. }
  88. if ($pre && /^\.fi/) {
  89. print "</PRE>\n";
  90. $pre = 0;
  91. next;
  92. }
  93. next if /^\.br/;
  94. &html;
  95. print;
  96. }
  97. print "</CODE>\n";
  98. next;
  99. }
  100. if (/\\f\(C[WB]/) {
  101. &html;
  102. s/\\f\(C[WB]/<CODE>/;
  103. while (!/\\f/) {
  104. &html;
  105. print;
  106. $_ = <>;
  107. }
  108. s/\\fP/<\/CODE>/;
  109. print;
  110. next;
  111. }
  112. if (/\\fB/) {
  113. &html;
  114. s/\\fB/<STRONG>/;
  115. while (!/\\f/) {
  116. print;
  117. $_ = <>;
  118. &html;
  119. }
  120. s/\\fP/<\/STRONG>/;
  121. print;
  122. next;
  123. }
  124. if (/\\fI/) {
  125. &html;
  126. s/\\fB/<EM>/;
  127. while (!/\\f/) {
  128. print;
  129. $_ = <>;
  130. &html;
  131. }
  132. s/\\fP/<\/EM>/;
  133. print;
  134. next;
  135. }
  136. if (/^\.ti/) { # one line display
  137. print "<PRE>\n";
  138. $_ = <>;
  139. &html;
  140. print;
  141. print "</PRE>\n";
  142. next;
  143. }
  144. if (/^\.de\s+/) {
  145. s/^\.de\s+//;
  146. warn "$ARGV[0]: Ignoring definition: $_";
  147. while (<>) {
  148. last if /^\.\./;
  149. }
  150. next;
  151. }
  152. # Warn about unimplemented troff/man commands
  153. if (/^\./) {
  154. chop;
  155. warn "$ARGV[0] unimp: \"$_\"\n";
  156. next;
  157. }
  158. if (/\\f/) {
  159. warn "$ARGV[0]: missed font: \"$_\"\n";
  160. }
  161. # Catchall for all the weirdball things I do.
  162. s/^\\\&\.\\\|\.\\\|\./.../;
  163. s/\\-/-/;
  164. &html;
  165. print;
  166. }
  167. exit 0;
  168. sub html
  169. {
  170. # HTML things that I've encountered.
  171. s/"/&quot;/g;
  172. s/</&lt;/g;
  173. s/>/&gt;/g;
  174. }
  175. sub fontfont {
  176. if (/^\.BI\s/) {
  177. s/.BI\s+//;
  178. chop;
  179. split;
  180. print "<STRONG>$_[0]</STRONG><EM>$_[1]</EM>\n";
  181. return 1;
  182. }
  183. if (/^\.IB\s/) {
  184. s/.IB\s+//;
  185. chop;
  186. split;
  187. print "<EM>$_[0]</EM><STRONG>$_[1]</STRONG>\n";
  188. return 1;
  189. }
  190. if (/^\.IR\s/) {
  191. s/.IR\s+//;
  192. chop;
  193. split;
  194. print "<EM>$_[0]</EM>$_[1]\n";
  195. return 1;
  196. }
  197. if (/^\.BR\s/) {
  198. s/.BR\s+//;
  199. chop;
  200. split;
  201. print "<STRONG>$_[0]</STRONG>$_[1]\n";
  202. return 1;
  203. }
  204. if (/^\.B\s/) {
  205. s/.B\s+//;
  206. chop;
  207. print "<STRONG>$_</STRONG>\n";
  208. return 1;
  209. }
  210. if (/^\.I\s/) {
  211. s/.I\s+//;
  212. chop;
  213. print "<EM>$_</EM>\n";
  214. return 1;
  215. }
  216. return 0;
  217. }