3 # $Cambridge: exim/doc/doc-docbook/Pre-xml,v 1.4 2007/04/11 15:26:09 ph10 Exp $
5 # Script to pre-process XML input before processing it for various purposes.
6 # Options specify which transformations are to be done. Monospaced literal
7 # layout blocks are never touched.
11 # -ascii: Replace ’ by '
12 # Replace © by (c)
13 # Replace † by *
14 # Replace ‡ by **
15 # Replace by a space
16 # Replace – by -
17 # Put quotes round <quote> text
20 # Put quotes round <literal> text
22 # -bookinfo: Remove the <bookinfo> element from the file
24 # -fi: Replace "fi" by fi except when it is in an XML element, or
27 # -html: Certain things are done only for HTML output:
29 # If <literallayout> is followed by optional space and then a
30 # newline, the space and newline are removed, because otherwise you
31 # get a blank line in the HTML output.
33 # -noindex Remove the XML that generates indexes.
34 # -oneindex Ditto, but add XML to generate a single index.
36 # -optbreak Insert an optional line break (zero-width space, ​) after
37 # every underscore in text within <option> and <variable> elements,
38 # except when preceded by <entry> (i.e. not in tables). The same is
39 # also done within a word of four or more upper-case letters (for
40 # compile-time options).
44 # The function that processes non-literal, non-monospaced text
50 $s =~ s/fi(?![^<>]*>)/fi/g if $ligatures;
54 $s =~ s%(?<!<entry>)(<option>|<varname>)([^<]+)%
55 my($x,$y) = ($1,$2); $y =~ s/_/_​/g; "$x"."$y"%gex;
57 $s =~ s?\b([A-Z_]{4,})\b?
58 my($x) = $1; $x =~ s/_/_​/g; "$x"?gex;
66 $s =~ s/‡/**/g;
70 $s =~ s/<\/quote>/"/g;
93 if ($arg eq "-fi") { $ligatures = 1; }
94 elsif ($arg eq "-ascii") { $ascii = 1; }
95 elsif ($arg eq "-bookinfo") { $bookinfo = 1; }
96 elsif ($arg eq "-html") { $html = 1; }
97 elsif ($arg eq "-noindex") { $noindex = 1; }
98 elsif ($arg eq "-oneindex") { $oneindex = 1; }
99 elsif ($arg eq "-optbreak") { $optbreak = 1; }
100 elsif ($arg eq "-quoteliteral") { $quoteliteral = 1; }
101 else { die "** Pre-xml: Unknown option \"$arg\"\n"; }
106 # Remove <bookinfo> if required
108 if ($bookinfo && /^<bookinfo/)
110 while (<STDIN>) { last if /^<\/bookinfo/; }
114 # Copy monospaced literallayout blocks
116 if (/^<literallayout class="monospaced">/)
118 $_ = substr($_, 0, -1) if $html;
123 last if /^<\/literallayout>/;
128 # Adjust index-generation code if required
130 if (($noindex || $oneindex) && /^<index[\s>]/)
134 last if /^<\/index>/;
137 if ($oneindex && !$madeindex)
140 print "<index><title>Index</title></index>\n";
146 # A line that is not in a monospaced literal block; keep track of which
147 # parts are in <literal> and which not. The latter get processed by the
148 # function above. Items in <literal> get quoted unless they are also in
149 # a <literallayout> block, or are already being quoted.
153 $_ = substr($_, 0, -1) if $html && /^<literallayout[^>]*>\s*\n$/;
154 $inliterallayout = 1 if /^<literallayout/;
155 $inliterallayout = 0 if /^<\/literallayout/;
159 if (/^(.*?)<\/literal>(?!<\/quote>)(.*)$/)
162 print "\"" if $quoteliteral && !$inliterallayout;
174 # Not in literal state
178 if (/^(.*?)(?<!<quote>)<literal>(.*)$/)
182 print "\"" if $quoteliteral && !$inliterallayout;
192 } # Loop for different parts of one line
193 } # Loop for multiple lines