3 # Script to pre-process XML input before processing it for various purposes.
4 # Options specify which transformations are to be done. Monospaced literal
5 # layout blocks are never touched.
9 # -ascii: Replace ’ by '
10 # Replace © by (c)
11 # Replace † by *
12 # Replace ‡ by **
13 # Replace by a space
14 # Replace – by -
15 # Put quotes round <quote> text
18 # Put quotes round <literal> text
20 # -bookinfo: Remove the <bookinfo> element from the file
22 # -fi: Replace "fi" by fi except when it is in an XML element, or
25 # -html: Certain things are done only for HTML output:
27 # If <literallayout> is followed by optional space and then a
28 # newline, the space and newline are removed, because otherwise you
29 # get a blank line in the HTML output.
31 # -noindex Remove the XML that generates indexes.
32 # -oneindex Ditto, but add XML to generate a single index.
34 # -optbreak Insert an optional line break (zero-width space, ​) after
35 # every underscore in text within <option> and <variable> elements,
36 # except when preceded by <entry> (i.e. not in tables). The same is
37 # also done within a word of four or more upper-case letters (for
38 # compile-time options).
42 # The function that processes non-literal, non-monospaced text
48 $s =~ s/fi(?![^<>]*>)/fi/g if $ligatures;
52 $s =~ s%(?<!<entry>)(<option>|<varname>)([^<]+)%
53 my($x,$y) = ($1,$2); $y =~ s/_/_​/g; "$x"."$y"%gex;
55 $s =~ s?\b([A-Z_]{4,})\b?
56 my($x) = $1; $x =~ s/_/_​/g; "$x"?gex;
64 $s =~ s/‡/**/g;
68 $s =~ s/<\/quote>/"/g;
91 if ($arg eq "-fi") { $ligatures = 1; }
92 elsif ($arg eq "-ascii") { $ascii = 1; }
93 elsif ($arg eq "-bookinfo") { $bookinfo = 1; }
94 elsif ($arg eq "-html") { $html = 1; }
95 elsif ($arg eq "-noindex") { $noindex = 1; }
96 elsif ($arg eq "-oneindex") { $oneindex = 1; }
97 elsif ($arg eq "-optbreak") { $optbreak = 1; }
98 elsif ($arg eq "-quoteliteral") { $quoteliteral = 1; }
99 else { die "** Pre-xml: Unknown option \"$arg\"\n"; }
104 # Remove <bookinfo> if required
106 if ($bookinfo && /^<bookinfo/)
108 while (<STDIN>) { last if /^<\/bookinfo/; }
112 # Copy monospaced literallayout blocks
114 if (/^<literallayout class="monospaced">/)
116 $_ = substr($_, 0, -1) if $html;
121 last if /^<\/literallayout>/;
126 # Adjust index-generation code if required
128 if (($noindex || $oneindex) && /^<index[\s>]/)
132 last if /^<\/index>/;
135 if ($oneindex && !$madeindex)
138 print "<index><title>Index</title></index>\n";
144 # A line that is not in a monospaced literal block; keep track of which
145 # parts are in <literal> and which not. The latter get processed by the
146 # function above. Items in <literal> get quoted unless they are also in
147 # a <literallayout> block, or are already being quoted.
151 $_ = substr($_, 0, -1) if $html && /^<literallayout[^>]*>\s*\n$/;
152 $inliterallayout = 1 if /^<literallayout/;
153 $inliterallayout = 0 if /^<\/literallayout/;
157 if (/^(.*?)<\/literal>(?!<\/quote>)(.*)$/)
160 print "\"" if $quoteliteral && !$inliterallayout;
172 # Not in literal state
176 if (/^(.*?)(?<!<quote>)<literal>(.*)$/)
180 print "\"" if $quoteliteral && !$inliterallayout;
190 } # Loop for different parts of one line
191 } # Loop for multiple lines