]>
git.ipfire.org Git - thirdparty/gcc.git/blob - contrib/texi2pod.pl
3 # This does trivial (and I mean _trivial_) conversion of Texinfo
4 # markup to Perl POD format. It's intended to be used to extract
5 # something suitable for a manpage from a Texinfo document.
25 die "no flag specified for -D\n"
27 die "flags may only contain letters, digits, hyphens, and underscores\n"
28 unless $flag =~ /^[a-zA-Z0-9_-]+$/;
33 $in = $_, next unless defined $in;
34 $out = $_, next unless defined $out;
40 open(STDIN
, $in) or die "opening \"$in\": $!\n";
43 open(STDOUT
, ">$out") or die "opening \"$out\": $!\n";
48 # Certain commands are discarded without further processing.
50 [a
-z
]+index # @*index: useful only in complete manual
51 |need
# @need: useful only in printed manual
52 |(?
:end\s
+)?group
# @group .. @end group: ditto
54 |node
# @node: useful only in .info file
59 # Look for filename and title markers.
60 /^\@setfilename\s+([^.]+)/ and $fn = $1, next;
61 /^\@settitle\s+([^.]+)/ and $tl = $1, next;
63 # Look for blocks surrounded by @c man begin SECTION ... @c man end.
64 # This really oughta be @ifman ... @end ifman and the like, but such
65 # would require rev'ing all other Texinfo translators.
66 /^\@c man begin ([A-Z]+)/ and $sect = $1, $output = 1, next;
67 /^\@c man end/ and do {
68 $sects{$sect} = postprocess
($section);
75 # Discard comments. (Can't do it above, because then we'd never see
79 # End-block handler goes up here because it needs to operate even
81 /^\@end\s+([a-z]+)/ and do {
82 die "\@end $1 without \@$1 at line $.\n" unless defined $endw;
83 die "\@$endw ended by \@end $1 at line $.\n" unless $1 eq $endw;
85 $endw = pop @endwstack;
87 if ($1 =~ /example$/) {
90 } elsif ($1 =~ /^(if|ignore|menu)/) {
91 $skipping = pop @skstack;
100 # Character entities. First the ones that can be replaced by raw text
101 # or discarded outright:
102 s/\@copyright\{\}/(c)/g;
104 s/\@enddots\{\}/..../g;
107 s/\@bullet(?:\{\})?/*/g;
110 s/\@minus(?:\{\})?/-/g;
112 # Now the ones that have to be replaced by special escapes
113 # (which will be turned back into text by unmunge())
118 # POD doesn't interpret E<> inside a verbatim block.
127 # Single line command handlers.
128 /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and $defs{$1} = $2, next;
129 /^\@clear\s+([a-zA-Z0-9_-]+)/ and delete $defs{$1}, next;
131 /^\@section\s+(.+)$/ and $_ = "\n=head2 $1\n";
132 /^\@subsection\s+(.+)$/ and $_ = "\n=head3 $1\n";
134 # Block command handlers:
135 /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do {
136 push @endwstack, $endw;
137 push @skstack, $skipping;
139 $skipping = 1 unless exists $defs{$1};
143 /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do {
144 push @endwstack, $endw;
145 push @skstack, $skipping;
147 $skipping = 1 if exists $defs{$1};
151 /^\@(ignore|menu)\b/ and do {
152 push @endwstack, $endw;
153 push @skstack, $skipping;
159 /^\@itemize\s+(\@[a-z]+|\*|-)/ and do {
160 push @endwstack, $endw;
167 /^\@enumerate(?:\s+([A-Z0-9]+))?/ and do {
168 push @endwstack, $endw;
179 /^\@table\s+(\@[a-z]+)/ and do {
180 push @endwstack, $endw;
183 $ic =~ s/\@(?:samp|strong|key)/B/;
184 $ic =~ s/\@(?:code|kbd)/C/;
185 $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/;
186 $ic =~ s/\@(?:file)/F/;
191 /^\@((?:small)?example)/ and do {
192 push @endwstack, $endw;
198 /^\@itemx?\s*(.+)?$/ and do {
200 # Entity escapes prevent munging by the <> processing below.
201 $_ = "\n=item $ic\<$1\>\n";
203 $_ = "\n=item $ic\n";
204 $ic =~ y/A-Ya-y1-8/B-Zb-z2-9/;
208 $section .= $shift.$_."\n";
211 die "No filename or title\n" unless defined $fn && defined $tl;
213 $sects{NAME
} = "$fn \- $tl\n";
214 $sects{FOOTNOTES
} .= "=back\n" if exists $sects{FOOTNOTES
};
216 for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES
217 BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) {
218 if(exists $sects{$sect}) {
220 $head =~ s/SEEALSO/SEE ALSO/;
221 print "=head1 $head\n\n";
222 print scalar unmunge
($sects{$sect});
229 die "usage: $0 [-D toggle...] [infile [outfile]]\n";
236 # @value{foo} is replaced by whatever 'foo' is defined as.
237 s/\@value\{([a-zA-Z0-9_-]+)\}/$defs{$1}/g;
239 # Formatting commands.
240 s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g;
241 s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g;
242 s/\@(?:samp|strong|key|b)\{([^\}]*)\}/B<$1>/g;
243 s/\@sc\{([^\}]*)\}/\U$1/g;
244 s/\@file\{([^\}]*)\}/F<$1>/g;
245 s/\@w\{([^\}]*)\}/S<$1>/g;
246 s/\@(?:dmn|math)\{([^\}]*)\}/$1/g;
248 # Cross references are thrown away, as are @noindent and @refill.
249 # (@noindent is impossible in .pod, and @refill is unnecessary.)
250 # @* is also impossible in .pod; we discard it and any newline that
253 s/\@xref\{(?:[^\}]*)\}[^.]*.//g;
254 s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
255 s/;\s+\@pxref\{(?:[^\}]*)\}//g;
260 # @uref can take one, two, or three arguments, with different
261 # semantics each time. @url and @email are just like @uref with
262 # one argument, for our purposes.
263 s/\@(?:uref|url|email)\{([^\},]*)\}/<C<$1>>/g;
264 s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g;
265 s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g;
267 # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to
268 # match Texinfo semantics of @emph inside @samp.
271 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g);
272 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g);
274 s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g;
275 s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g;
277 # Extract footnotes. This has to be done after all other
278 # processing because otherwise the regexp will choke on formatting
280 while (/\@footnote/g) {
281 s/\@footnote\{([^\}]+)\}/[$fnno]/;
282 add_footnote
($1, $fnno);
291 # Replace escaped symbols with their equivalents.
305 unless (exists $sects{FOOTNOTES
}) {
306 $sects{FOOTNOTES
} = "\n=over 4\n\n";
309 $sects{FOOTNOTES
} .= "=item $fnno.\n\n"; $fnno++;
310 $sects{FOOTNOTES
} .= $_[0];
311 $sects{FOOTNOTES
} .= "\n\n";