summaryrefslogtreecommitdiff
path: root/docs-xml/xslt/db2latex-xsl/xsl/normalize-scape.mod.xsl
blob: c561bd88e3f4def116f7a423617992fccf71712a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?xml version='1.0'?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY % xsldoc.ent SYSTEM "./xsldoc.ent"> %xsldoc.ent; ]>
<!--############################################################################# 
|	$Id: normalize-scape.mod.xsl,v 1.33 2004/01/26 09:40:12 j-devenish Exp $
|- #############################################################################
|	$Author: j-devenish $
|														
|   PURPOSE:
|	Escape LaTeX and normalize-space templates.
|    < > # $ % & ~ _ ^ \ { } |
+ ############################################################################## -->

<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
	exclude-result-prefixes="doc" version='1.0'>

	<doc:reference id="normalize-scape" xmlns="">
		<referenceinfo>
			<releaseinfo role="meta">
				$Id: normalize-scape.mod.xsl,v 1.33 2004/01/26 09:40:12 j-devenish Exp $
			</releaseinfo>
			<authorgroup>
				&ramon;
				&james;
			</authorgroup>
			<copyright>
				<year>2000</year><year>2001</year><year>2002</year><year>2003</year>
				<holder>Ramon Casellas</holder>
			</copyright>
			<revhistory>
				<doc:revision rcasver="1.30">&rev_2003_05;</doc:revision>
			</revhistory>
		</referenceinfo>
		<title>Whitespace Normalization and Character Encoding <filename>normalize-scape.mod.xsl</filename></title>
		<partintro>

			<para>Normalize whitespace and and escape <quote>active</quote> &latex; characters.</para>
			<para>Includes the auto-generated <filename>scape.mod.xsl</filename> module.</para>

		</partintro>
	</doc:reference>

	<xsl:include href="scape.mod.xsl"/>

	<doc:template match="text()" xmlns="">
		<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
		<doc:description>
			<para>

				Handles regular text content (i.e. <literal>#PCDATA</literal>)
				from &docbook; documents.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:notes>
			<para>

				For most elements, &latex; active characters
				will be escaped using the <xref linkend="template.scape"/>
				template.
				However, for text within the following elements, the
				<xref linkend="template.scape-verbatim"/> template
				will be used to typeset monospace text:
				<doc:db>literal</doc:db>,
				<doc:db>filename</doc:db>,
				<doc:db>userinput</doc:db>,
				<doc:db>systemitem</doc:db>,
				<doc:db>prompt</doc:db>,
				<doc:db>email</doc:db>,
				<doc:db>sgmltag</doc:db>.

			</para>
			<para>

				In all cases, interior whitespace will be normalised according
				to the XSLT specification with the additional feature that
				leading and trailing whitespace will be elided (as expected
				with SGML parsers).

			</para>
		</doc:notes>
		<doc:seealso>
			<itemizedlist>
				<listitem><simpara><xref linkend="gentext"/></simpara></listitem>
				<listitem><simpara><xref linkend="param.latex.inputenc"/></simpara></listitem>
			</itemizedlist>
		</doc:seealso>
	</doc:template>
	<xsl:template match="text()" name="text">
		<xsl:call-template name="trim-outer">
			<xsl:with-param name="string">
				<xsl:choose>
					<xsl:when test="ancestor::literal|ancestor::filename|ancestor::userinput|ancestor::systemitem|ancestor::prompt|ancestor::email|ancestor::sgmltag">
						<xsl:call-template name="scape-verbatim">
							<xsl:with-param name="string" select="."/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="scape">
							<xsl:with-param name="string" select="."/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>

	<!--
	<xsl:template match="abbrev/text()">
		<xsl:variable name="string">
			<xsl:call-template name="text"/>
		</xsl:variable>
		<xsl:call-template name="string-replace">
			<xsl:with-param name="to">.\ </xsl:with-param>
			<xsl:with-param name="from">. </xsl:with-param>
			<xsl:with-param name="string" select="$string"/>
		</xsl:call-template>
	</xsl:template>
	-->

	<doc:template match="text()" mode="xref.text" xmlns="">
		<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
		<doc:description>
			<para>

				Handles regular text content (i.e. <literal>#PCDATA</literal>)
				from &docbook; documents when they are forming the displayed
				text of an <doc:db>xref</doc:db>.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:notes>
			<para>

				&latex; active characters will be escaped using the <xref
				linkend="template.scape"/> template.

			</para>
			<para>

				In all cases, interior whitespace will be normalised according
				to the XSLT specification with the additional feature that
				leading and trailing whitespace will be elided (as expected
				with SGML parsers).

			</para>
		</doc:notes>
	</doc:template>
    <xsl:template match="text()" mode="xref.text">
		<xsl:call-template name="trim-outer">
			<xsl:with-param name="string">
				<xsl:call-template name="scape">
					<xsl:with-param name="string" select="."/>
				</xsl:call-template>
			</xsl:with-param>
		</xsl:call-template>
    </xsl:template>

	<doc:template match="text()" mode="xref-to" xmlns="">
		<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
		<doc:description>
			<para>

				Handles regular text content (i.e. <literal>#PCDATA</literal>)
				from &docbook; documents when they are forming the displayed
				text of an <doc:db>xref</doc:db>.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:notes>
			<para>

				&latex; active characters will be escaped using the <xref
				linkend="template.scape"/> template.

			</para>
			<para>

				In all cases, interior whitespace will be normalised according
				to the XSLT specification with the additional feature that
				leading and trailing whitespace will be elided (as expected
				with SGML parsers).

			</para>
		</doc:notes>
	</doc:template>
    <xsl:template match="text()" mode="xref-to">
		<xsl:call-template name="trim-outer">
			<xsl:with-param name="string">
				<xsl:call-template name="scape">
					<xsl:with-param name="string" select="."/>
				</xsl:call-template>
			</xsl:with-param>
		</xsl:call-template>
    </xsl:template>

	<doc:template match="text()" mode="latex.verbatim" xmlns="">
		<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
		<doc:description>
			<para>

				Handles regular text content (i.e. <literal>#PCDATA</literal>)
				from &docbook; documents with they occur within certain
				<quote>verbatim</quote>-mode elements.

			</para>
		</doc:description>
		<doc:variables>
			<variablelist>
				<varlistentry>
					<term><xref linkend="param.latex.trim.verbatim"/></term>
					<listitem><simpara>
						When this variable is enabled, leading and trailing whitespace
						will be elided. Otherwise, all text is used verbatim.
					</simpara></listitem>
				</varlistentry>
			</variablelist>
		</doc:variables>
		<doc:notes>
			<para>

				Unlike other <literal>text()</literal> templates, &latex;
				characters are not escaped by this template. This will result
				in invalid output in some instances. However, it is currently
				necessary for <quote>verbatim</quote>-mode support. Whitespace
				is neither normalised nor elided.

			</para>
		</doc:notes>
	</doc:template>
    <xsl:template match="text()" mode="latex.verbatim">
		<xsl:choose>
			<xsl:when test="$latex.trim.verbatim=1">
				<xsl:call-template name="trim.verbatim"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/> 
			</xsl:otherwise>
		</xsl:choose>
    </xsl:template>

	<doc:template match="text()" mode="slash.hyphen" xmlns="">
		<refpurpose>Process <literal>text()</literal> nodes</refpurpose>
		<doc:description>
			<para>

				Handles URL text content from &docbook; documents.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:notes>
			<para>

				This template is only used by <xref
				linkend="template.generate.string.url"/> and only when <xref
				linkend="param.latex.hyphenation.tttricks"/> is disabled.
				&latex; active characters will be escaped or hyphenated in a
				fashion that is tailored for URLs via
				<xref linkend="template.scape.slash.hyphen"/>.

			</para>
		</doc:notes>
	</doc:template>
    <xsl:template match="text()" mode="slash.hyphen">
		<xsl:call-template name="trim-outer">
			<xsl:with-param name="string">
				<xsl:call-template name="scape.slash.hyphen">
					<xsl:with-param name="string" select="." />
				</xsl:call-template>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>

	<doc:template name="trim-outer" xmlns="">
		<refpurpose>Whitespace Normalization and Discretionary Elision</refpurpose>
		<doc:description>
			<para>

				This template is used by various <literal>text()</literal>
				templates to normalise interior whitespace and trim whitespace
				that occurs at the start or end of a &docbook; element's
				content.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:params>
			<variablelist>
				<varlistentry>
					<term>string</term>
					<listitem><simpara>The text to be processed.</simpara></listitem>
				</varlistentry>
			</variablelist>
		</doc:params>
		<doc:notes>
			<para>

				The template is normally called when the context node is within
				a &docbook; document. The elision of leading or trailing
				whitespace is dependent on values of the XPath functions
				<function condition="xpath">position()</function> and <function
				condition="xpath">last()</function>. This is similar to the
				handling of whitespace by SGML parsers and allows authors to
				format their XML documents with <quote>pretty</quote>
				indentation without causing spurious whitespace in &latex;.

			</para>
			<para>

				In all cases, interiour whitespace will be normalised with the
				XPath <function condition="xpath">normalize-space()</function>
				function. This is necessary to prevent blank-line problems in
				&latex;.

			</para>
		</doc:notes>
	</doc:template>
	<xsl:template name="trim-outer">
		<xsl:param name="string"/>
		<xsl:variable name="trimleft" select="position()=1"/>
		<xsl:variable name="trimright" select="position()=last()"/>
		<xsl:choose>
			<xsl:when test="$trimleft and not($trimright)">
				<xsl:value-of select="substring-before(normalize-space(concat($string,'$$')),'$$')"/>
			</xsl:when>
			<xsl:when test="$trimright and not($trimleft)">
				<xsl:value-of select="substring-after(normalize-space(concat('$$',$string)),'$$')"/>
			</xsl:when>
			<xsl:when test="$trimleft and $trimright">
				<xsl:value-of select="normalize-space($string)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="substring-after(substring-before(normalize-space(concat('$$',$string,'$$$')),'$$$'),'$$')"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<doc:template name="scape.slash.hyphen" xmlns="">
		<refpurpose>Process URL text</refpurpose>
		<doc:description>
			<para>

				Escapes or hyphenates &latex; active characters is URLs.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:params>
			<variablelist>
				<varlistentry>
					<term>string</term>
					<listitem><simpara>The URL text to be processed.</simpara></listitem>
				</varlistentry>
			</variablelist>
		</doc:params>
		<doc:notes>
			<para>

				This template is called by <xref
				linkend="template.text()-slash.hyphen"/>. Text will be escaped
				and hyphenated by the <xref linkend="template.scape-slash"/>
				template, except that any portion up to <literal>://</literal>
				will not be treated specially.

			</para>
		</doc:notes>
	</doc:template>
	<xsl:template name="scape.slash.hyphen">
	<xsl:param name="string" />
	<xsl:choose>
		<xsl:when test="contains($string,'://')">
			<xsl:call-template name="scape-slash">
				<xsl:with-param name="string">
					<xsl:value-of select="substring-before($string,'://')"/>
					<xsl:value-of select="'://'"/>
					<xsl:call-template name="scape">
						<xsl:with-param name="string" select="substring-after($string,'://')"/>
					</xsl:call-template>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="scape-slash">
				<xsl:with-param name="string">
					<xsl:call-template name="scape">
						<xsl:with-param name="string" select="$string"/>
					</xsl:call-template>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
	</xsl:template>

	<doc:template name="normalize-scape" xmlns="">
		<refpurpose>Character Escaping and Whitespace Normalization</refpurpose>
		<doc:description>
			<para>

				This template is used by various templates to escape &latex;
				active characters and to normalise whitespace.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:params>
			<variablelist>
				<varlistentry>
					<term>string</term>
					<listitem><simpara>The text to be processed.</simpara></listitem>
				</varlistentry>
			</variablelist>
		</doc:params>
		<doc:notes>
			<para>

				This template will call the <xref linkend="template.scape"/>
				template and process its output with the XPath
				<function condition="xpath">normalize-space</function>
				function.

			</para>
		</doc:notes>
	</doc:template>
	<xsl:template name="normalize-scape">
		<xsl:param name="string"/>
		<xsl:variable name="result">
			<xsl:call-template name="scape">
				<xsl:with-param name="string" select="$string"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select="normalize-space($result)"/>
	</xsl:template>

	<doc:template name="string-replace" xmlns="">
		<refpurpose>Search-and-replace</refpurpose>
		<doc:description>
			<para>

				This template performs search-and-replace to modify all
				instances of a substring.

			</para>
		</doc:description>
		<doc:variables>
			&no_var;
		</doc:variables>
		<doc:params>
			<variablelist>
				<varlistentry>
					<term>string</term>
					<listitem><simpara>The text to be searched.</simpara></listitem>
				</varlistentry>
				<varlistentry>
					<term>from</term>
					<listitem><simpara>The text (substring) to be replaced.</simpara></listitem>
				</varlistentry>
				<varlistentry>
					<term>to</term>
					<listitem><simpara>The text that replaces the <literal>from</literal> text.</simpara></listitem>
				</varlistentry>
			</variablelist>
		</doc:params>
		<doc:notes>
			<para>

				This template will search within the <literal>string</literal>
				text for all occurrences of <literal>from</literal> and replace
				them with the <literal>to</literal> text.

			</para>
		</doc:notes>
	</doc:template>
	<xsl:template name="string-replace">
		<xsl:param name="string"/>
		<xsl:param name="from"/>
		<xsl:param name="to"/>

		<xsl:choose>
			<xsl:when test="contains($string, $from)">

			<xsl:variable name="before" select="substring-before($string, $from)"/>
			<xsl:variable name="after" select="substring-after($string, $from)"/>
			<xsl:variable name="prefix" select="concat($before, $to)"/>

			<xsl:value-of select="$before"/>
			<xsl:value-of select="$to"/>
			<xsl:call-template name="string-replace">
				<xsl:with-param name="string" select="$after"/>
				<xsl:with-param name="from" select="$from"/>
				<xsl:with-param name="to" select="$to"/>
			</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
			<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

    <!--  
    (c) David Carlisle
    replace all occurences of the character(s) `from'
    by the string `to' in the string `string'.
    <xsl:template name="string-replace" >
	<xsl:param name="string"/>
	<xsl:param name="from"/>
	<xsl:param name="to"/>
	<xsl:choose>
	    <xsl:when test="contains($string,$from)">
		<xsl:value-of select="substring-before($string,$from)"/>
		<xsl:value-of select="$to"/>
		<xsl:call-template name="string-replace">
		    <xsl:with-param name="string" select="substring-after($string,$from)"/>
		    <xsl:with-param name="from" select="$from"/>
		    <xsl:with-param name="to" select="$to"/>
		</xsl:call-template>
	    </xsl:when>
	    <xsl:otherwise>
		<xsl:value-of select="$string"/>
	    </xsl:otherwise>
	</xsl:choose>
    </xsl:template>
    -->

	<xsl:template name="trim.verbatim">
		<xsl:variable name="before" select="preceding-sibling::node()"/>
		<xsl:variable name="after" select="following-sibling::node()"/>

		<xsl:variable name="conts" select="."/>

		<xsl:variable name="contsl">
			<xsl:choose>
				<xsl:when test="count($before) = 0">
					<xsl:call-template name="remove-lf-left">
						<xsl:with-param name="astr" select="$conts"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$conts"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="contslr">
			<xsl:choose>
				<xsl:when test="count($after) = 0">
					<xsl:call-template name="remove-ws-right">
						<xsl:with-param name="astr" select="$contsl"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$contsl"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:value-of select="$contslr"/>
	</xsl:template>

	<xsl:template name="remove-lf-left">
		<xsl:param name="astr"/>
		<xsl:choose>
			<xsl:when test="starts-with($astr,'&#xA;') or
			                starts-with($astr,'&#xD;') or
			                starts-with($astr,'&#x20;') or
			                starts-with($astr,'&#x9;')">
				<xsl:call-template name="remove-lf-left">
					<xsl:with-param name="astr" select="substring($astr, 2)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$astr"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="remove-ws-right">
		<xsl:param name="astr"/>
		<xsl:variable name="last-char">
			<xsl:value-of select="substring($astr, string-length($astr), 1)"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="($last-char = '&#xA;') or
			                ($last-char = '&#xD;') or
			                ($last-char = '&#x20;') or
			                ($last-char = '&#x9;')">
				<xsl:call-template name="remove-ws-right">
					<xsl:with-param name="astr" select="substring($astr, 1, string-length($astr) - 1)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$astr"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>