texi2pod.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #!/usr/bin/env perl
  2. # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
  3. # This file is part of GNU CC.
  4. # GNU CC is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. # GNU CC is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with GNU CC; see the file COPYING. If not, write to
  14. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  15. # Boston, MA 02110-1301 USA
  16. # This does trivial (and I mean _trivial_) conversion of Texinfo
  17. # markup to Perl POD format. It's intended to be used to extract
  18. # something suitable for a manpage from a Texinfo document.
  19. use warnings;
  20. $output = 0;
  21. $skipping = 0;
  22. %chapters = ();
  23. @chapters_sequence = ();
  24. $chapter = "";
  25. @icstack = ();
  26. @endwstack = ();
  27. @skstack = ();
  28. @instack = ();
  29. $shift = "";
  30. %defs = ();
  31. $fnno = 1;
  32. $inf = "";
  33. @ibase = ();
  34. while ($_ = shift) {
  35. if (/^-D(.*)$/) {
  36. if ($1 ne "") {
  37. $flag = $1;
  38. } else {
  39. $flag = shift;
  40. }
  41. $value = "";
  42. ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/);
  43. die "no flag specified for -D\n"
  44. unless $flag ne "";
  45. die "flags may only contain letters, digits, hyphens, dashes and underscores\n"
  46. unless $flag =~ /^[a-zA-Z0-9_-]+$/;
  47. $defs{$flag} = $value;
  48. } elsif (/^-I(.*)$/) {
  49. push @ibase, $1 ne "" ? $1 : shift;
  50. } elsif (/^-/) {
  51. usage();
  52. } else {
  53. $in = $_, next unless defined $in;
  54. $out = $_, next unless defined $out;
  55. usage();
  56. }
  57. }
  58. push @ibase, ".";
  59. if (defined $in) {
  60. $inf = gensym();
  61. open($inf, "<$in") or die "opening \"$in\": $!\n";
  62. push @ibase, $1 if $in =~ m|^(.+)/[^/]+$|;
  63. } else {
  64. $inf = \*STDIN;
  65. }
  66. if (defined $out) {
  67. open(STDOUT, ">$out") or die "opening \"$out\": $!\n";
  68. }
  69. while(defined $inf) {
  70. INF: while(<$inf>) {
  71. # Certain commands are discarded without further processing.
  72. /^\@(?:
  73. [a-z]+index # @*index: useful only in complete manual
  74. |need # @need: useful only in printed manual
  75. |(?:end\s+)?group # @group .. @end group: ditto
  76. |page # @page: ditto
  77. |node # @node: useful only in .info file
  78. |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents
  79. )\b/x and next;
  80. chomp;
  81. # Look for filename and title markers.
  82. /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
  83. /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next;
  84. # Identify a man title but keep only the one we are interested in.
  85. /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do {
  86. if (exists $defs{$1}) {
  87. $fn = $1;
  88. $tl = postprocess($2);
  89. }
  90. next;
  91. };
  92. /^\@include\s+(.+)$/ and do {
  93. push @instack, $inf;
  94. $inf = gensym();
  95. for (@ibase) {
  96. open($inf, "<" . $_ . "/" . $1) and next INF;
  97. }
  98. die "cannot open $1: $!\n";
  99. };
  100. /^\@chapter\s+([A-Za-z ]+)/ and do {
  101. # close old chapter
  102. $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
  103. # start new chapter
  104. $chapter_name = $1, push (@chapters_sequence, $chapter_name) unless $skipping;
  105. $chapters{$chapter_name} = "" unless exists $chapters{$chapter_name};
  106. $chapter = "";
  107. $output = 1;
  108. next;
  109. };
  110. /^\@bye/ and do {
  111. # close old chapter
  112. $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
  113. last INF;
  114. };
  115. # handle variables
  116. /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do {
  117. $defs{$1} = $2;
  118. next;
  119. };
  120. /^\@clear\s+([a-zA-Z0-9_-]+)/ and do {
  121. delete $defs{$1};
  122. next;
  123. };
  124. next unless $output;
  125. # Discard comments. (Can't do it above, because then we'd never see
  126. # @c man lines.)
  127. /^\@c\b/ and next;
  128. # End-block handler goes up here because it needs to operate even
  129. # if we are skipping.
  130. /^\@end\s+([a-z]+)/ and do {
  131. # Ignore @end foo, where foo is not an operation which may
  132. # cause us to skip, if we are presently skipping.
  133. my $ended = $1;
  134. next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/;
  135. die "\@end $ended without \@$ended at line $.\n" unless defined $endw;
  136. die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw;
  137. $endw = pop @endwstack;
  138. if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex|ifhtml|ifnothtml)$/) {
  139. $skipping = pop @skstack;
  140. next;
  141. } elsif ($ended =~ /^(?:example|smallexample|verbatim|display)$/) {
  142. $shift = "";
  143. $_ = ""; # need a paragraph break
  144. } elsif ($ended =~ /^(?:itemize|enumerate|(?:multi|[fv])?table)$/) {
  145. $_ = "\n=back\n";
  146. $ic = pop @icstack;
  147. } elsif ($ended =~ /^float$/) {
  148. $_ = "\n=back\n";
  149. $ic = pop @icstack;
  150. } else {
  151. die "unknown command \@end $ended at line $.\n";
  152. }
  153. };
  154. # We must handle commands which can cause skipping even while we
  155. # are skipping, otherwise we will not process nested conditionals
  156. # correctly.
  157. /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
  158. push @endwstack, $endw;
  159. push @skstack, $skipping;
  160. $endw = "ifset";
  161. $skipping = 1 unless exists $defs{$1};
  162. next;
  163. };
  164. /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
  165. push @endwstack, $endw;
  166. push @skstack, $skipping;
  167. $endw = "ifclear";
  168. $skipping = 1 if exists $defs{$1};
  169. next;
  170. };
  171. /^\@(ignore|menu|iftex|ifhtml|ifnothtml)\b/ and do {
  172. push @endwstack, $endw;
  173. push @skstack, $skipping;
  174. $endw = $1;
  175. $skipping = $endw !~ /ifnothtml/;
  176. next;
  177. };
  178. next if $skipping;
  179. # Character entities. First the ones that can be replaced by raw text
  180. # or discarded outright:
  181. s/\@copyright\{\}/(c)/g;
  182. s/\@dots\{\}/.../g;
  183. s/\@enddots\{\}/..../g;
  184. s/\@([.!? ])/$1/g;
  185. s/\@[:-]//g;
  186. s/\@bullet(?:\{\})?/*/g;
  187. s/\@TeX\{\}/TeX/g;
  188. s/\@pounds\{\}/\#/g;
  189. s/\@minus(?:\{\})?/-/g;
  190. # Now the ones that have to be replaced by special escapes
  191. # (which will be turned back into text by unmunge())
  192. s/&/&amp;/g;
  193. s/\@\{/&lbrace;/g;
  194. s/\@\}/&rbrace;/g;
  195. s/\@\@/&at;/g;
  196. # Inside a verbatim block, handle @var specially.
  197. if ($shift ne "") {
  198. s/\@var\{([^\}]*)\}/<$1>/g;
  199. }
  200. # POD doesn't interpret E<> inside a verbatim block.
  201. if ($shift eq "") {
  202. s/</&lt;/g;
  203. s/>/&gt;/g;
  204. } else {
  205. s/</&LT;/g;
  206. s/>/&GT;/g;
  207. }
  208. # Single line command handlers.
  209. /^\@(?:section|unnumbered|unnumberedsec|center|heading)\s+(.+)$/
  210. and $_ = "\n=head2 $1\n";
  211. /^\@(?:subsection|subheading)\s+(.+)$/
  212. and $_ = "\n=head3 $1\n";
  213. /^\@(?:subsubsection|subsubheading)\s+(.+)$/
  214. and $_ = "\n=head4 $1\n";
  215. # Block command handlers:
  216. /^\@itemize\s*(\@[a-z]+|\*|-)?/ and do {
  217. push @endwstack, $endw;
  218. push @icstack, $ic;
  219. $ic = $1 ? $1 : "*";
  220. $_ = "\n=over 4\n";
  221. $endw = "itemize";
  222. };
  223. /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do {
  224. push @endwstack, $endw;
  225. push @icstack, $ic;
  226. if (defined $1) {
  227. $ic = $1 . ".";
  228. } else {
  229. $ic = "1.";
  230. }
  231. $_ = "\n=over 4\n";
  232. $endw = "enumerate";
  233. };
  234. /^\@((?:multi|[fv])?table)\s+(\@[a-z]+)/ and do {
  235. push @endwstack, $endw;
  236. push @icstack, $ic;
  237. $endw = $1;
  238. $ic = $2;
  239. $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env|command)/B/;
  240. $ic =~ s/\@(?:code|kbd)/C/;
  241. $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
  242. $ic =~ s/\@(?:file)/F/;
  243. $ic =~ s/\@(?:columnfractions)//;
  244. $_ = "\n=over 4\n";
  245. };
  246. /^\@(multitable)\s+{.*/ and do {
  247. push @endwstack, $endw;
  248. push @icstack, $ic;
  249. $endw = $1;
  250. $ic = "";
  251. $_ = "\n=over 4\n";
  252. };
  253. /^\@((?:small)?example|verbatim|display)/ and do {
  254. push @endwstack, $endw;
  255. $endw = $1;
  256. $shift = "\t";
  257. $_ = ""; # need a paragraph break
  258. };
  259. /^\@(float)\s+\w+/ and do {
  260. push @endwstack, $endw;
  261. $endw = $1;
  262. $_ = "\n=over 4\n";
  263. };
  264. /^\@item\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
  265. my $columns = $1;
  266. $columns =~ s/\@tab/ : /;
  267. $_ = "\n=item B&LT;". $columns ."&GT;\n";
  268. };
  269. /^\@tab\s+(.*\S)\s*$/ and $endw eq "multitable" and do {
  270. my $columns = $1;
  271. $columns =~ s/\@tab//;
  272. $_ = $columns;
  273. $chapter =~ s/$//;
  274. };
  275. /^\@itemx?\s*(.+)?$/ and do {
  276. if (defined $1) {
  277. # Entity escapes prevent munging by the <> processing below.
  278. $_ = "\n=item $ic\&LT;$1\&GT;\n";
  279. } else {
  280. $_ = "\n=item $ic\n";
  281. $ic =~ y/A-Ya-y/B-Zb-z/;
  282. $ic =~ s/(\d+)/$1 + 1/eg;
  283. }
  284. };
  285. $chapter .= $shift.$_."\n";
  286. }
  287. # End of current file.
  288. close($inf);
  289. $inf = pop @instack;
  290. }
  291. die "No filename or title\n" unless defined $fn && defined $tl;
  292. # always use utf8
  293. print "=encoding utf8\n\n";
  294. $chapters{NAME} = "$fn \- $tl\n";
  295. $chapters{FOOTNOTES} .= "=back\n" if exists $chapters{FOOTNOTES};
  296. unshift @chapters_sequence, "NAME";
  297. for $chapter (@chapters_sequence) {
  298. if (exists $chapters{$chapter}) {
  299. $head = uc($chapter);
  300. print "=head1 $head\n\n";
  301. print scalar unmunge ($chapters{$chapter});
  302. print "\n";
  303. }
  304. }
  305. sub usage
  306. {
  307. die "usage: $0 [-D toggle...] [infile [outfile]]\n";
  308. }
  309. sub postprocess
  310. {
  311. local $_ = $_[0];
  312. # @value{foo} is replaced by whatever 'foo' is defined as.
  313. while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) {
  314. if (! exists $defs{$2}) {
  315. print STDERR "Option $2 not defined\n";
  316. s/\Q$1\E//;
  317. } else {
  318. $value = $defs{$2};
  319. s/\Q$1\E/$value/;
  320. }
  321. }
  322. # Formatting commands.
  323. # Temporary escape for @r.
  324. s/\@r\{([^\}]*)\}/R<$1>/g;
  325. s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
  326. s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
  327. s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g;
  328. s/\@sc\{([^\}]*)\}/\U$1/g;
  329. s/\@file\{([^\}]*)\}/F<$1>/g;
  330. s/\@w\{([^\}]*)\}/S<$1>/g;
  331. s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
  332. # Cross references are thrown away, as are @noindent and @refill.
  333. # (@noindent is impossible in .pod, and @refill is unnecessary.)
  334. # @* is also impossible in .pod; we discard it and any newline that
  335. # follows it. Similarly, our macro @gol must be discarded.
  336. s/\@anchor\{(?:[^\}]*)\}//g;
  337. s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
  338. s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
  339. s/;\s+\@pxref\{(?:[^\}]*)\}//g;
  340. s/\@ref\{(?:[^,\}]*,)(?:[^,\}]*,)([^,\}]*).*\}/B<$1>/g;
  341. s/\@ref\{([^\}]*)\}/B<$1>/g;
  342. s/\@noindent\s*//g;
  343. s/\@refill//g;
  344. s/\@gol//g;
  345. s/\@\*\s*\n?//g;
  346. # @uref can take one, two, or three arguments, with different
  347. # semantics each time. @url and @email are just like @uref with
  348. # one argument, for our purposes.
  349. s/\@(?:uref|url|email)\{([^\},]*),?[^\}]*\}/&lt;B<$1>&gt;/g;
  350. s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
  351. s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
  352. # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
  353. # match Texinfo semantics of @emph inside @samp. Also handle @r
  354. # inside bold.
  355. s/&LT;/</g;
  356. s/&GT;/>/g;
  357. 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g;
  358. 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
  359. 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
  360. s/[BI]<>//g;
  361. s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
  362. s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
  363. # Extract footnotes. This has to be done after all other
  364. # processing because otherwise the regexp will choke on formatting
  365. # inside @footnote.
  366. while (/\@footnote/g) {
  367. s/\@footnote\{([^\}]+)\}/[$fnno]/;
  368. add_footnote($1, $fnno);
  369. $fnno++;
  370. }
  371. return $_;
  372. }
  373. sub unmunge
  374. {
  375. # Replace escaped symbols with their equivalents.
  376. local $_ = $_[0];
  377. s/&lt;/E<lt>/g;
  378. s/&gt;/E<gt>/g;
  379. s/&lbrace;/\{/g;
  380. s/&rbrace;/\}/g;
  381. s/&at;/\@/g;
  382. s/&amp;/&/g;
  383. return $_;
  384. }
  385. sub add_footnote
  386. {
  387. unless (exists $chapters{FOOTNOTES}) {
  388. $chapters{FOOTNOTES} = "\n=over 4\n\n";
  389. }
  390. $chapters{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++;
  391. $chapters{FOOTNOTES} .= $_[0];
  392. $chapters{FOOTNOTES} .= "\n\n";
  393. }
  394. # stolen from Symbol.pm
  395. {
  396. my $genseq = 0;
  397. sub gensym
  398. {
  399. my $name = "GEN" . $genseq++;
  400. my $ref = \*{$name};
  401. delete $::{$name};
  402. return $ref;
  403. }
  404. }