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