Statistics
| Branch: | Revision:

root / texi2pod.pl @ 3f4cb3d3

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