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