2
<!DOCTYPE xsl:stylesheet [ <!ENTITY % xsldoc.ent SYSTEM "./xsldoc.ent"> %xsldoc.ent; ]>
3
<!--#############################################################################
4
| $Id: normalize-scape.mod.xsl,v 1.33 2004/01/26 09:40:12 j-devenish Exp $
5
|- #############################################################################
6
| $Author: j-devenish $
9
| Escape LaTeX and normalize-space templates.
10
| < > # $ % & ~ _ ^ \ { } |
11
+ ############################################################################## -->
14
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
15
xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
16
exclude-result-prefixes="doc" version='1.0'>
18
<doc:reference id="normalize-scape" xmlns="">
20
<releaseinfo role="meta">
21
$Id: normalize-scape.mod.xsl,v 1.33 2004/01/26 09:40:12 j-devenish Exp $
28
<year>2000</year><year>2001</year><year>2002</year><year>2003</year>
29
<holder>Ramon Casellas</holder>
32
<doc:revision rcasver="1.30">&rev_2003_05;</doc:revision>
35
<title>Whitespace Normalization and Character Encoding <filename>normalize-scape.mod.xsl</filename></title>
38
<para>Normalize whitespace and and escape <quote>active</quote> &latex; characters.</para>
39
<para>Includes the auto-generated <filename>scape.mod.xsl</filename> module.</para>
44
<xsl:include href="scape.mod.xsl"/>
46
<doc:template match="text()" xmlns="">
47
<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
51
Handles regular text content (i.e. <literal>#PCDATA</literal>)
52
from &docbook; documents.
62
For most elements, &latex; active characters
63
will be escaped using the <xref linkend="template.scape"/>
65
However, for text within the following elements, the
66
<xref linkend="template.scape-verbatim"/> template
67
will be used to typeset monospace text:
68
<doc:db>literal</doc:db>,
69
<doc:db>filename</doc:db>,
70
<doc:db>userinput</doc:db>,
71
<doc:db>systemitem</doc:db>,
72
<doc:db>prompt</doc:db>,
73
<doc:db>email</doc:db>,
74
<doc:db>sgmltag</doc:db>.
79
In all cases, interior whitespace will be normalised according
80
to the XSLT specification with the additional feature that
81
leading and trailing whitespace will be elided (as expected
88
<listitem><simpara><xref linkend="gentext"/></simpara></listitem>
89
<listitem><simpara><xref linkend="param.latex.inputenc"/></simpara></listitem>
93
<xsl:template match="text()" name="text">
94
<xsl:call-template name="trim-outer">
95
<xsl:with-param name="string">
97
<xsl:when test="ancestor::literal|ancestor::filename|ancestor::userinput|ancestor::systemitem|ancestor::prompt|ancestor::email|ancestor::sgmltag">
98
<xsl:call-template name="scape-verbatim">
99
<xsl:with-param name="string" select="."/>
103
<xsl:call-template name="scape">
104
<xsl:with-param name="string" select="."/>
113
<xsl:template match="abbrev/text()">
114
<xsl:variable name="string">
115
<xsl:call-template name="text"/>
117
<xsl:call-template name="string-replace">
118
<xsl:with-param name="to">.\ </xsl:with-param>
119
<xsl:with-param name="from">. </xsl:with-param>
120
<xsl:with-param name="string" select="$string"/>
125
<doc:template match="text()" mode="xref.text" xmlns="">
126
<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
130
Handles regular text content (i.e. <literal>#PCDATA</literal>)
131
from &docbook; documents when they are forming the displayed
132
text of an <doc:db>xref</doc:db>.
142
&latex; active characters will be escaped using the <xref
143
linkend="template.scape"/> template.
148
In all cases, interior whitespace will be normalised according
149
to the XSLT specification with the additional feature that
150
leading and trailing whitespace will be elided (as expected
156
<xsl:template match="text()" mode="xref.text">
157
<xsl:call-template name="trim-outer">
158
<xsl:with-param name="string">
159
<xsl:call-template name="scape">
160
<xsl:with-param name="string" select="."/>
166
<doc:template match="text()" mode="xref-to" xmlns="">
167
<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
171
Handles regular text content (i.e. <literal>#PCDATA</literal>)
172
from &docbook; documents when they are forming the displayed
173
text of an <doc:db>xref</doc:db>.
183
&latex; active characters will be escaped using the <xref
184
linkend="template.scape"/> template.
189
In all cases, interior whitespace will be normalised according
190
to the XSLT specification with the additional feature that
191
leading and trailing whitespace will be elided (as expected
197
<xsl:template match="text()" mode="xref-to">
198
<xsl:call-template name="trim-outer">
199
<xsl:with-param name="string">
200
<xsl:call-template name="scape">
201
<xsl:with-param name="string" select="."/>
207
<doc:template match="text()" mode="latex.verbatim" xmlns="">
208
<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
212
Handles regular text content (i.e. <literal>#PCDATA</literal>)
213
from &docbook; documents with they occur within certain
214
<quote>verbatim</quote>-mode elements.
221
<term><xref linkend="param.latex.trim.verbatim"/></term>
223
When this variable is enabled, leading and trailing whitespace
224
will be elided. Otherwise, all text is used verbatim.
225
</simpara></listitem>
232
Unlike other <literal>text()</literal> templates, &latex;
233
characters are not escaped by this template. This will result
234
in invalid output in some instances. However, it is currently
235
necessary for <quote>verbatim</quote>-mode support. Whitespace
236
is neither normalised nor elided.
241
<xsl:template match="text()" mode="latex.verbatim">
243
<xsl:when test="$latex.trim.verbatim=1">
244
<xsl:call-template name="trim.verbatim"/>
247
<xsl:value-of select="."/>
252
<doc:template match="text()" mode="slash.hyphen" xmlns="">
253
<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
257
Handles URL text content from &docbook; documents.
267
This template is only used by <xref
268
linkend="template.generate.string.url"/> and only when <xref
269
linkend="param.latex.hyphenation.tttricks"/> is disabled.
270
&latex; active characters will be escaped or hyphenated in a
271
fashion that is tailored for URLs via
272
<xref linkend="template.scape.slash.hyphen"/>.
277
<xsl:template match="text()" mode="slash.hyphen">
278
<xsl:call-template name="trim-outer">
279
<xsl:with-param name="string">
280
<xsl:call-template name="scape.slash.hyphen">
281
<xsl:with-param name="string" select="." />
287
<doc:template name="trim-outer" xmlns="">
288
<refpurpose>Whitespace Normalization and Discretionary Elision</refpurpose>
292
This template is used by various <literal>text()</literal>
293
templates to normalise interior whitespace and trim whitespace
294
that occurs at the start or end of a &docbook; element's
306
<listitem><simpara>The text to be processed.</simpara></listitem>
313
The template is normally called when the context node is within
314
a &docbook; document. The elision of leading or trailing
315
whitespace is dependent on values of the XPath functions
316
<function condition="xpath">position()</function> and <function
317
condition="xpath">last()</function>. This is similar to the
318
handling of whitespace by SGML parsers and allows authors to
319
format their XML documents with <quote>pretty</quote>
320
indentation without causing spurious whitespace in &latex;.
325
In all cases, interiour whitespace will be normalised with the
326
XPath <function condition="xpath">normalize-space()</function>
327
function. This is necessary to prevent blank-line problems in
333
<xsl:template name="trim-outer">
334
<xsl:param name="string"/>
335
<xsl:variable name="trimleft" select="position()=1"/>
336
<xsl:variable name="trimright" select="position()=last()"/>
338
<xsl:when test="$trimleft and not($trimright)">
339
<xsl:value-of select="substring-before(normalize-space(concat($string,'$$')),'$$')"/>
341
<xsl:when test="$trimright and not($trimleft)">
342
<xsl:value-of select="substring-after(normalize-space(concat('$$',$string)),'$$')"/>
344
<xsl:when test="$trimleft and $trimright">
345
<xsl:value-of select="normalize-space($string)"/>
348
<xsl:value-of select="substring-after(substring-before(normalize-space(concat('$$',$string,'$$$')),'$$$'),'$$')"/>
353
<doc:template name="scape.slash.hyphen" xmlns="">
354
<refpurpose>Process URL text</refpurpose>
358
Escapes or hyphenates &latex; active characters is URLs.
369
<listitem><simpara>The URL text to be processed.</simpara></listitem>
376
This template is called by <xref
377
linkend="template.text()-slash.hyphen"/>. Text will be escaped
378
and hyphenated by the <xref linkend="template.scape-slash"/>
379
template, except that any portion up to <literal>://</literal>
380
will not be treated specially.
385
<xsl:template name="scape.slash.hyphen">
386
<xsl:param name="string" />
388
<xsl:when test="contains($string,'://')">
389
<xsl:call-template name="scape-slash">
390
<xsl:with-param name="string">
391
<xsl:value-of select="substring-before($string,'://')"/>
392
<xsl:value-of select="'://'"/>
393
<xsl:call-template name="scape">
394
<xsl:with-param name="string" select="substring-after($string,'://')"/>
400
<xsl:call-template name="scape-slash">
401
<xsl:with-param name="string">
402
<xsl:call-template name="scape">
403
<xsl:with-param name="string" select="$string"/>
411
<doc:template name="normalize-scape" xmlns="">
412
<refpurpose>Character Escaping and Whitespace Normalization</refpurpose>
416
This template is used by various templates to escape &latex;
417
active characters and to normalise whitespace.
428
<listitem><simpara>The text to be processed.</simpara></listitem>
435
This template will call the <xref linkend="template.scape"/>
436
template and process its output with the XPath
437
<function condition="xpath">normalize-space</function>
443
<xsl:template name="normalize-scape">
444
<xsl:param name="string"/>
445
<xsl:variable name="result">
446
<xsl:call-template name="scape">
447
<xsl:with-param name="string" select="$string"/>
450
<xsl:value-of select="normalize-space($result)"/>
453
<doc:template name="string-replace" xmlns="">
454
<refpurpose>Search-and-replace</refpurpose>
458
This template performs search-and-replace to modify all
459
instances of a substring.
470
<listitem><simpara>The text to be searched.</simpara></listitem>
474
<listitem><simpara>The text (substring) to be replaced.</simpara></listitem>
478
<listitem><simpara>The text that replaces the <literal>from</literal> text.</simpara></listitem>
485
This template will search within the <literal>string</literal>
486
text for all occurrences of <literal>from</literal> and replace
487
them with the <literal>to</literal> text.
492
<xsl:template name="string-replace">
493
<xsl:param name="string"/>
494
<xsl:param name="from"/>
495
<xsl:param name="to"/>
498
<xsl:when test="contains($string, $from)">
500
<xsl:variable name="before" select="substring-before($string, $from)"/>
501
<xsl:variable name="after" select="substring-after($string, $from)"/>
502
<xsl:variable name="prefix" select="concat($before, $to)"/>
504
<xsl:value-of select="$before"/>
505
<xsl:value-of select="$to"/>
506
<xsl:call-template name="string-replace">
507
<xsl:with-param name="string" select="$after"/>
508
<xsl:with-param name="from" select="$from"/>
509
<xsl:with-param name="to" select="$to"/>
513
<xsl:value-of select="$string"/>
520
replace all occurences of the character(s) `from'
521
by the string `to' in the string `string'.
522
<xsl:template name="string-replace" >
523
<xsl:param name="string"/>
524
<xsl:param name="from"/>
525
<xsl:param name="to"/>
527
<xsl:when test="contains($string,$from)">
528
<xsl:value-of select="substring-before($string,$from)"/>
529
<xsl:value-of select="$to"/>
530
<xsl:call-template name="string-replace">
531
<xsl:with-param name="string" select="substring-after($string,$from)"/>
532
<xsl:with-param name="from" select="$from"/>
533
<xsl:with-param name="to" select="$to"/>
537
<xsl:value-of select="$string"/>
543
<xsl:template name="trim.verbatim">
544
<xsl:variable name="before" select="preceding-sibling::node()"/>
545
<xsl:variable name="after" select="following-sibling::node()"/>
547
<xsl:variable name="conts" select="."/>
549
<xsl:variable name="contsl">
551
<xsl:when test="count($before) = 0">
552
<xsl:call-template name="remove-lf-left">
553
<xsl:with-param name="astr" select="$conts"/>
557
<xsl:value-of select="$conts"/>
562
<xsl:variable name="contslr">
564
<xsl:when test="count($after) = 0">
565
<xsl:call-template name="remove-ws-right">
566
<xsl:with-param name="astr" select="$contsl"/>
570
<xsl:value-of select="$contsl"/>
575
<xsl:value-of select="$contslr"/>
578
<xsl:template name="remove-lf-left">
579
<xsl:param name="astr"/>
581
<xsl:when test="starts-with($astr,'
') or
582
starts-with($astr,'
') or
583
starts-with($astr,' ') or
584
starts-with($astr,'	')">
585
<xsl:call-template name="remove-lf-left">
586
<xsl:with-param name="astr" select="substring($astr, 2)"/>
590
<xsl:value-of select="$astr"/>
595
<xsl:template name="remove-ws-right">
596
<xsl:param name="astr"/>
597
<xsl:variable name="last-char">
598
<xsl:value-of select="substring($astr, string-length($astr), 1)"/>
601
<xsl:when test="($last-char = '
') or
602
($last-char = '
') or
603
($last-char = ' ') or
604
($last-char = '	')">
605
<xsl:call-template name="remove-ws-right">
606
<xsl:with-param name="astr" select="substring($astr, 1, string-length($astr) - 1)"/>
610
<xsl:value-of select="$astr"/>