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