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