]>
Commit | Line | Data |
---|---|---|
6251188c ZW |
1 | #! /usr/bin/perl -w |
2 | ||
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. | |
6 | ||
6251188c | 7 | $output = 0; |
3d6f4d76 | 8 | $skipping = 0; |
6251188c ZW |
9 | %sects = (); |
10 | $section = ""; | |
11 | @icstack = (); | |
12 | @endwstack = (); | |
3d6f4d76 | 13 | @skstack = (); |
6251188c | 14 | $shift = ""; |
3d6f4d76 | 15 | %defs = (); |
b75d998f | 16 | $fnno = 1; |
6251188c | 17 | |
b75d998f | 18 | while ($_ = shift) { |
3d6f4d76 ZW |
19 | if (/^-D(.*)$/) { |
20 | if ($1 ne "") { | |
21 | $flag = $1; | |
22 | } else { | |
23 | $flag = shift; | |
24 | } | |
25 | die "no flag specified for -D\n" | |
26 | unless $flag ne ""; | |
27 | die "flags may only contain letters, digits, hyphens, and underscores\n" | |
28 | unless $flag =~ /^[a-zA-Z0-9_-]+$/; | |
29 | $defs{$flag} = ""; | |
30 | } elsif (/^-/) { | |
31 | usage(); | |
32 | } else { | |
33 | $in = $_, next unless defined $in; | |
34 | $out = $_, next unless defined $out; | |
35 | usage(); | |
36 | } | |
37 | } | |
38 | ||
39 | if (defined $in) { | |
40 | open(STDIN, $in) or die "opening \"$in\": $!\n"; | |
41 | } | |
42 | if (defined $out) { | |
43 | open(STDOUT, ">$out") or die "opening \"$out\": $!\n"; | |
44 | } | |
45 | ||
46 | while(<STDIN>) | |
6251188c | 47 | { |
b75d998f ZW |
48 | # Certain commands are discarded without further processing. |
49 | /^\@(?: | |
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 | |
53 | |page # @page: ditto | |
54 | |node # @node: useful only in .info file | |
55 | )\b/x and next; | |
56 | ||
6251188c | 57 | chomp; |
b75d998f ZW |
58 | |
59 | # Look for filename and title markers. | |
60 | /^\@setfilename\s+([^.]+)/ and $fn = $1, next; | |
61 | /^\@settitle\s+([^.]+)/ and $tl = $1, next; | |
62 | ||
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. | |
6251188c ZW |
66 | /^\@c man begin ([A-Z]+)/ and $sect = $1, $output = 1, next; |
67 | /^\@c man end/ and do { | |
f4e8dec6 ZW |
68 | $sects{$sect} = "" unless exists $sects{$sect}; |
69 | $sects{$sect} .= postprocess($section); | |
6251188c ZW |
70 | $section = ""; |
71 | $output = 0; | |
72 | next; | |
73 | }; | |
b75d998f | 74 | next unless $output; |
3d6f4d76 | 75 | |
b75d998f ZW |
76 | # Discard comments. (Can't do it above, because then we'd never see |
77 | # @c man lines.) | |
78 | /^\@c\b/ and next; | |
6251188c | 79 | |
b75d998f ZW |
80 | # End-block handler goes up here because it needs to operate even |
81 | # if we are skipping. | |
82 | /^\@end\s+([a-z]+)/ and do { | |
f4e8dec6 ZW |
83 | # Ignore @end foo, where foo is not an operation which may |
84 | # cause us to skip, if we are presently skipping. | |
85 | my $ended = $1; | |
86 | next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu)$/; | |
87 | ||
88 | die "\@end $ended without \@$ended at line $.\n" unless defined $endw; | |
89 | die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw; | |
6251188c | 90 | |
b75d998f | 91 | $endw = pop @endwstack; |
6251188c | 92 | |
f4e8dec6 | 93 | if ($ended =~ /^(?:ifset|ifclear|ignore|menu)$/) { |
b75d998f ZW |
94 | $skipping = pop @skstack; |
95 | next; | |
f4e8dec6 ZW |
96 | } elsif ($ended =~ /^(?:example|smallexample)$/) { |
97 | $shift = ""; | |
98 | $_ = ""; # need a paragraph break | |
99 | } elsif ($ended =~ /^(?:itemize|enumerate|table)$/) { | |
b75d998f ZW |
100 | $_ = "\n=back\n"; |
101 | $ic = pop @icstack; | |
f4e8dec6 ZW |
102 | } else { |
103 | die "unknown command \@end $ended at line $.\n"; | |
3d6f4d76 ZW |
104 | } |
105 | }; | |
f4e8dec6 ZW |
106 | |
107 | # We must handle commands which can cause skipping even while we | |
108 | # are skipping, otherwise we will not process nested conditionals | |
109 | # correctly. | |
110 | /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do { | |
111 | push @endwstack, $endw; | |
112 | push @skstack, $skipping; | |
113 | $endw = "ifset"; | |
114 | $skipping = 1 unless exists $defs{$1}; | |
115 | next; | |
116 | }; | |
117 | ||
118 | /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do { | |
119 | push @endwstack, $endw; | |
120 | push @skstack, $skipping; | |
121 | $endw = "ifclear"; | |
122 | $skipping = 1 if exists $defs{$1}; | |
123 | next; | |
124 | }; | |
125 | ||
126 | /^\@(ignore|menu)\b/ and do { | |
127 | push @endwstack, $endw; | |
128 | push @skstack, $skipping; | |
129 | $endw = $1; | |
130 | $skipping = 1; | |
131 | next; | |
132 | }; | |
133 | ||
3d6f4d76 ZW |
134 | next if $skipping; |
135 | ||
b75d998f ZW |
136 | # Character entities. First the ones that can be replaced by raw text |
137 | # or discarded outright: | |
138 | s/\@copyright\{\}/(c)/g; | |
139 | s/\@dots\{\}/.../g; | |
140 | s/\@enddots\{\}/..../g; | |
141 | s/\@([.!? ])/$1/g; | |
142 | s/\@[:-]//g; | |
143 | s/\@bullet(?:\{\})?/*/g; | |
144 | s/\@TeX\{\}/TeX/g; | |
145 | s/\@pounds\{\}/\#/g; | |
146 | s/\@minus(?:\{\})?/-/g; | |
445c435a | 147 | s/\\,/,/g; |
6251188c | 148 | |
b75d998f ZW |
149 | # Now the ones that have to be replaced by special escapes |
150 | # (which will be turned back into text by unmunge()) | |
151 | s/&/&/g; | |
152 | s/\@\{/{/g; | |
153 | s/\@\}/}/g; | |
154 | s/\@\@/&at;/g; | |
155 | # POD doesn't interpret E<> inside a verbatim block. | |
156 | if ($shift eq "") { | |
157 | s/</</g; | |
158 | s/>/>/g; | |
159 | } else { | |
160 | s/</</g; | |
161 | s/>/>/g; | |
162 | } | |
163 | ||
164 | # Single line command handlers. | |
3d6f4d76 ZW |
165 | /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and $defs{$1} = $2, next; |
166 | /^\@clear\s+([a-zA-Z0-9_-]+)/ and delete $defs{$1}, next; | |
167 | ||
b75d998f ZW |
168 | /^\@section\s+(.+)$/ and $_ = "\n=head2 $1\n"; |
169 | /^\@subsection\s+(.+)$/ and $_ = "\n=head3 $1\n"; | |
170 | ||
171 | # Block command handlers: | |
b75d998f | 172 | /^\@itemize\s+(\@[a-z]+|\*|-)/ and do { |
6251188c ZW |
173 | push @endwstack, $endw; |
174 | push @icstack, $ic; | |
175 | $ic = $1; | |
6251188c ZW |
176 | $_ = "\n=over 4\n"; |
177 | $endw = "itemize"; | |
178 | }; | |
179 | ||
b75d998f | 180 | /^\@enumerate(?:\s+([A-Z0-9]+))?/ and do { |
6251188c ZW |
181 | push @endwstack, $endw; |
182 | push @icstack, $ic; | |
b75d998f ZW |
183 | if (defined $1) { |
184 | $ic = $1 . "."; | |
185 | } else { | |
186 | $ic = "1."; | |
187 | } | |
6251188c ZW |
188 | $_ = "\n=over 4\n"; |
189 | $endw = "enumerate"; | |
190 | }; | |
191 | ||
192 | /^\@table\s+(\@[a-z]+)/ and do { | |
193 | push @endwstack, $endw; | |
194 | push @icstack, $ic; | |
195 | $ic = $1; | |
2642624b | 196 | $ic =~ s/\@(?:samp|strong|key|gcctabopt|env)/B/; |
6251188c | 197 | $ic =~ s/\@(?:code|kbd)/C/; |
b75d998f | 198 | $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/; |
6251188c ZW |
199 | $ic =~ s/\@(?:file)/F/; |
200 | $_ = "\n=over 4\n"; | |
201 | $endw = "table"; | |
202 | }; | |
203 | ||
204 | /^\@((?:small)?example)/ and do { | |
205 | push @endwstack, $endw; | |
206 | $endw = $1; | |
207 | $shift = "\t"; | |
f4e8dec6 | 208 | $_ = ""; # need a paragraph break |
6251188c ZW |
209 | }; |
210 | ||
6251188c | 211 | /^\@itemx?\s*(.+)?$/ and do { |
b75d998f ZW |
212 | if (defined $1) { |
213 | # Entity escapes prevent munging by the <> processing below. | |
214 | $_ = "\n=item $ic\<$1\>\n"; | |
215 | } else { | |
216 | $_ = "\n=item $ic\n"; | |
6251188c ZW |
217 | $ic =~ y/A-Ya-y1-8/B-Zb-z2-9/; |
218 | } | |
219 | }; | |
3d6f4d76 | 220 | |
6251188c ZW |
221 | $section .= $shift.$_."\n"; |
222 | } | |
223 | ||
b75d998f ZW |
224 | die "No filename or title\n" unless defined $fn && defined $tl; |
225 | ||
6251188c | 226 | $sects{NAME} = "$fn \- $tl\n"; |
b75d998f | 227 | $sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES}; |
6251188c ZW |
228 | |
229 | for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES | |
b75d998f ZW |
230 | BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) { |
231 | if(exists $sects{$sect}) { | |
6251188c ZW |
232 | $head = $sect; |
233 | $head =~ s/SEEALSO/SEE ALSO/; | |
234 | print "=head1 $head\n\n"; | |
b75d998f | 235 | print scalar unmunge ($sects{$sect}); |
6251188c ZW |
236 | print "\n"; |
237 | } | |
238 | } | |
3d6f4d76 ZW |
239 | |
240 | sub usage | |
241 | { | |
242 | die "usage: $0 [-D toggle...] [infile [outfile]]\n"; | |
243 | } | |
b75d998f ZW |
244 | |
245 | sub postprocess | |
246 | { | |
247 | local $_ = $_[0]; | |
248 | ||
249 | # @value{foo} is replaced by whatever 'foo' is defined as. | |
250 | s/\@value\{([a-zA-Z0-9_-]+)\}/$defs{$1}/g; | |
251 | ||
252 | # Formatting commands. | |
4bc1997b JM |
253 | # Temporary escape for @r. |
254 | s/\@r\{([^\}]*)\}/R<$1>/g; | |
b75d998f ZW |
255 | s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g; |
256 | s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g; | |
4bc1997b | 257 | s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g; |
b75d998f ZW |
258 | s/\@sc\{([^\}]*)\}/\U$1/g; |
259 | s/\@file\{([^\}]*)\}/F<$1>/g; | |
260 | s/\@w\{([^\}]*)\}/S<$1>/g; | |
261 | s/\@(?:dmn|math)\{([^\}]*)\}/$1/g; | |
262 | ||
4bc1997b | 263 | # Handle @r inside bold. |
3f896fc2 | 264 | 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B</g; |
4bc1997b | 265 | |
b75d998f ZW |
266 | # Cross references are thrown away, as are @noindent and @refill. |
267 | # (@noindent is impossible in .pod, and @refill is unnecessary.) | |
268 | # @* is also impossible in .pod; we discard it and any newline that | |
4bc1997b | 269 | # follows it. Similarly, our macro @gol must be discarded. |
b75d998f | 270 | |
4bc1997b | 271 | s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g; |
b75d998f ZW |
272 | s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g; |
273 | s/;\s+\@pxref\{(?:[^\}]*)\}//g; | |
274 | s/\@noindent\s*//g; | |
275 | s/\@refill//g; | |
4bc1997b | 276 | s/\@gol//g; |
b75d998f ZW |
277 | s/\@\*\s*\n?//g; |
278 | ||
279 | # @uref can take one, two, or three arguments, with different | |
280 | # semantics each time. @url and @email are just like @uref with | |
281 | # one argument, for our purposes. | |
2642624b | 282 | s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g; |
b75d998f ZW |
283 | s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g; |
284 | s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g; | |
285 | ||
286 | # Turn B<blah I<blah> blah> into B<blah> I<blah> B<blah> to | |
287 | # match Texinfo semantics of @emph inside @samp. | |
288 | s/</</g; | |
289 | s/>/>/g; | |
290 | 1 while (s/B<([^<>]*)I<([^>]+)>/B<$1>I<$2>B</g); | |
291 | 1 while (s/I<([^<>]*)B<([^>]+)>/I<$1>B<$2>I</g); | |
292 | s/[BI]<>//g; | |
293 | s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g; | |
294 | s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g; | |
295 | ||
296 | # Extract footnotes. This has to be done after all other | |
297 | # processing because otherwise the regexp will choke on formatting | |
298 | # inside @footnote. | |
299 | while (/\@footnote/g) { | |
300 | s/\@footnote\{([^\}]+)\}/[$fnno]/; | |
301 | add_footnote($1, $fnno); | |
302 | $fnno++; | |
303 | } | |
304 | ||
305 | return $_; | |
306 | } | |
307 | ||
308 | sub unmunge | |
309 | { | |
310 | # Replace escaped symbols with their equivalents. | |
311 | local $_ = $_[0]; | |
312 | ||
313 | s/</E<lt>/g; | |
314 | s/>/E<gt>/g; | |
315 | s/{/\{/g; | |
316 | s/}/\}/g; | |
317 | s/&at;/\@/g; | |
318 | s/&/&/g; | |
319 | return $_; | |
320 | } | |
321 | ||
322 | sub add_footnote | |
323 | { | |
324 | unless (exists $sects{FOOTNOTES}) { | |
325 | $sects{FOOTNOTES} = "\n=over 4\n\n"; | |
326 | } | |
327 | ||
328 | $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++; | |
329 | $sects{FOOTNOTES} .= $_[0]; | |
330 | $sects{FOOTNOTES} .= "\n\n"; | |
331 | } | |
332 |