Statistics
| Branch: | Revision:

root / scripts / texi2pod.pl @ 9943e0ec

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