132html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #! /usr/bin/perl -w
  2. # Script to turn PCRE man pages into HTML
  3. # Subroutine to handle font changes and other escapes
  4. sub do_line {
  5. my($s) = $_[0];
  6. $s =~ s/</&#60;/g; # Deal with < and >
  7. $s =~ s/>/&#62;/g;
  8. $s =~ s"\\fI(.*?)\\f[RP]"<i>$1</i>"g;
  9. $s =~ s"\\fB(.*?)\\f[RP]"<b>$1</b>"g;
  10. $s =~ s"\\e"\\"g;
  11. $s =~ s/(?<=Copyright )\(c\)/&copy;/g;
  12. $s;
  13. }
  14. # Subroutine to ensure not in a paragraph
  15. sub end_para {
  16. if ($inpara)
  17. {
  18. print TEMP "</PRE>\n" if ($inpre);
  19. print TEMP "</P>\n";
  20. }
  21. $inpara = $inpre = 0;
  22. $wrotetext = 0;
  23. }
  24. # Subroutine to start a new paragraph
  25. sub new_para {
  26. &end_para();
  27. print TEMP "<P>\n";
  28. $inpara = 1;
  29. }
  30. # Main program
  31. $innf = 0;
  32. $inpara = 0;
  33. $inpre = 0;
  34. $wrotetext = 0;
  35. $toc = 0;
  36. $ref = 1;
  37. while ($#ARGV >= 0 && $ARGV[0] =~ /^-/)
  38. {
  39. $toc = 1 if $ARGV[0] eq "-toc";
  40. shift;
  41. }
  42. # Initial output to STDOUT
  43. print <<End ;
  44. <html>
  45. <head>
  46. <title>$ARGV[0] specification</title>
  47. </head>
  48. <body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
  49. <h1>$ARGV[0] man page</h1>
  50. <p>
  51. Return to the <a href="index.html">PCRE index page</a>.
  52. </p>
  53. <p>
  54. This page is part of the PCRE HTML documentation. It was generated automatically
  55. from the original man page. If there is any nonsense in it, please consult the
  56. man page, in case the conversion went wrong.
  57. <br>
  58. End
  59. print "<ul>\n" if ($toc);
  60. open(TEMP, ">/tmp/$$") || die "Can't open /tmp/$$ for output\n";
  61. while (<STDIN>)
  62. {
  63. # Handle lines beginning with a dot
  64. if (/^\./)
  65. {
  66. # Some of the PCRE man pages used to contain instances of .br. However,
  67. # they should have all been removed because they cause trouble in some
  68. # (other) automated systems that translate man pages to HTML. Complain if
  69. # we find .br or .in (another macro that is deprecated).
  70. if (/^\.br/ || /^\.in/)
  71. {
  72. print STDERR "\n*** Deprecated macro encountered - rewrite needed\n";
  73. print STDERR "*** $_\n";
  74. die "*** Processing abandoned\n";
  75. }
  76. # Instead of .br, relevent "literal" sections are enclosed in .nf/.fi.
  77. elsif (/^\.nf/)
  78. {
  79. $innf = 1;
  80. }
  81. elsif (/^\.fi/)
  82. {
  83. $innf = 0;
  84. }
  85. # Handling .sp is subtle. If it is inside a literal section, do nothing if
  86. # the next line is a non literal text line; similarly, if not inside a
  87. # literal section, do nothing if a literal follows, unless we are inside
  88. # a .nf/.ne section. The point being that the <pre> and </pre> that delimit
  89. # literal sections will do the spacing. Always skip if no previous output.
  90. elsif (/^\.sp/)
  91. {
  92. if ($wrotetext)
  93. {
  94. $_ = <STDIN>;
  95. if ($inpre)
  96. {
  97. print TEMP "\n" if (/^[\s.]/);
  98. }
  99. else
  100. {
  101. print TEMP "<br>\n<br>\n" if ($innf || !/^[\s.]/);
  102. }
  103. redo; # Now process the lookahead line we just read
  104. }
  105. }
  106. elsif (/^\.TP/ || /^\.PP/ || /^\.P/)
  107. {
  108. &new_para();
  109. }
  110. elsif (/^\.SH\s*("?)(.*)\1/)
  111. {
  112. # Ignore the NAME section
  113. if ($2 =~ /^NAME\b/)
  114. {
  115. <STDIN>;
  116. next;
  117. }
  118. &end_para();
  119. my($title) = &do_line($2);
  120. if ($toc)
  121. {
  122. printf("<li><a name=\"TOC%d\" href=\"#SEC%d\">$title</a>\n",
  123. $ref, $ref);
  124. printf TEMP ("<br><a name=\"SEC%d\" href=\"#TOC1\">$title</a><br>\n",
  125. $ref);
  126. $ref++;
  127. }
  128. else
  129. {
  130. print TEMP "<br><b>\n$title\n</b><br>\n";
  131. }
  132. }
  133. elsif (/^\.SS\s*("?)(.*)\1/)
  134. {
  135. &end_para();
  136. my($title) = &do_line($2);
  137. print TEMP "<br><b>\n$title\n</b><br>\n";
  138. }
  139. elsif (/^\.B\s*(.*)/)
  140. {
  141. &new_para() if (!$inpara);
  142. $_ = &do_line($1);
  143. s/"(.*?)"/$1/g;
  144. print TEMP "<b>$_</b>\n";
  145. $wrotetext = 1;
  146. }
  147. elsif (/^\.I\s*(.*)/)
  148. {
  149. &new_para() if (!$inpara);
  150. $_ = &do_line($1);
  151. s/"(.*?)"/$1/g;
  152. print TEMP "<i>$_</i>\n";
  153. $wrotetext = 1;
  154. }
  155. # A comment that starts "HREF" takes the next line as a name that
  156. # is turned into a hyperlink, using the text given, which might be
  157. # in a special font. If it ends in () or (digits) or punctuation, they
  158. # aren't part of the link.
  159. elsif (/^\.\\"\s*HREF/)
  160. {
  161. $_=<STDIN>;
  162. chomp;
  163. $_ = &do_line($_);
  164. $_ =~ s/\s+$//;
  165. $_ =~ /^(?:<.>)?([^<(]+)(?:\(\))?(?:<\/.>)?(?:\(\d+\))?[.,;:]?$/;
  166. print TEMP "<a href=\"$1.html\">$_</a>\n";
  167. }
  168. # A comment that starts "HTML" inserts literal HTML
  169. elsif (/^\.\\"\s*HTML\s*(.*)/)
  170. {
  171. print TEMP $1;
  172. }
  173. # A comment that starts < inserts that HTML at the end of the
  174. # *next* input line - so as not to get a newline between them.
  175. elsif (/^\.\\"\s*(<.*>)/)
  176. {
  177. my($markup) = $1;
  178. $_=<STDIN>;
  179. chomp;
  180. $_ = &do_line($_);
  181. $_ =~ s/\s+$//;
  182. print TEMP "$_$markup\n";
  183. }
  184. # A comment that starts JOIN joins the next two lines together, with one
  185. # space between them. Then that line is processed. This is used in some
  186. # displays where two lines are needed for the "man" version. JOINSH works
  187. # the same, except that it assumes this is a shell command, so removes
  188. # continuation backslashes.
  189. elsif (/^\.\\"\s*JOIN(SH)?/)
  190. {
  191. my($one,$two);
  192. $one = <STDIN>;
  193. $two = <STDIN>;
  194. $one =~ s/\s*\\e\s*$// if (defined($1));
  195. chomp($one);
  196. $two =~ s/^\s+//;
  197. $_ = "$one $two";
  198. redo; # Process the joined lines
  199. }
  200. # .EX/.EE are used in the pcredemo page to bracket the entire program,
  201. # which is unmodified except for turning backslash into "\e".
  202. elsif (/^\.EX\s*$/)
  203. {
  204. print TEMP "<PRE>\n";
  205. while (<STDIN>)
  206. {
  207. last if /^\.EE\s*$/;
  208. s/\\e/\\/g;
  209. s/&/&amp;/g;
  210. s/</&lt;/g;
  211. s/>/&gt;/g;
  212. print TEMP;
  213. }
  214. }
  215. # Ignore anything not recognized
  216. next;
  217. }
  218. # Line does not begin with a dot. Replace blank lines with new paragraphs
  219. if (/^\s*$/)
  220. {
  221. &end_para() if ($wrotetext);
  222. next;
  223. }
  224. # Convert fonts changes and output an ordinary line. Ensure that indented
  225. # lines are marked as literal.
  226. $_ = &do_line($_);
  227. &new_para() if (!$inpara);
  228. if (/^\s/)
  229. {
  230. if (!$inpre)
  231. {
  232. print TEMP "<pre>\n";
  233. $inpre = 1;
  234. }
  235. }
  236. elsif ($inpre)
  237. {
  238. print TEMP "</pre>\n";
  239. $inpre = 0;
  240. }
  241. # Add <br> to the end of a non-literal line if we are within .nf/.fi
  242. $_ .= "<br>\n" if (!$inpre && $innf);
  243. print TEMP;
  244. $wrotetext = 1;
  245. }
  246. # The TOC, if present, will have been written - terminate it
  247. print "</ul>\n" if ($toc);
  248. # Copy the remainder to the standard output
  249. close(TEMP);
  250. open(TEMP, "/tmp/$$") || die "Can't open /tmp/$$ for input\n";
  251. print while (<TEMP>);
  252. print <<End ;
  253. <p>
  254. Return to the <a href="index.html">PCRE index page</a>.
  255. </p>
  256. End
  257. close(TEMP);
  258. unlink("/tmp/$$");
  259. # End