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