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