2 <!--#############################################################################
3 |- #############################################################################
6 | Escape LaTeX and normalize-space templates.
7 | < > # $ % & ~ _ ^ \ { }
8 + ############################################################################## -->
11 xmlns:
xsl=
"http://www.w3.org/1999/XSL/Transform"
12 xmlns:
doc=
"http://nwalsh.com/xsl/documentation/1.0"
13 exclude-result-prefixes=
"doc" version='
1.0'
>
16 <!--############################################################################# -->
17 <!-- DOCUMENTATION -->
18 <doc:reference id=
"normalize-scape" xmlns=
"">
20 <releaseinfo role=
"meta">
23 <author> <firstname>Ramon
</firstname> <surname>Casellas
</surname> </author>
24 <author> <firstname>James
</firstname> <surname>Devenish
</surname> </author>
26 <copyright><year>2000</year><year>2001</year><year>2002</year><year>2003</year>
27 <holder>Ramon Casellas
</holder>
30 <title>Normalize and Scape strings.
</title>
32 <section><title>Introduction
</title>
37 <!--############################################################################# -->
39 <!--############################################################################# -->
40 <!-- DOCUMENTATION -->
41 <doc:template name=
"normalize-scape" xmlns=
"">
42 <refpurpose> Abstract XSL template.
</refpurpose>
46 <!--############################################################################# -->
48 <xsl:template match=
"text()">
49 <xsl:call-template name=
"trim-outer">
50 <xsl:with-param name=
"string">
52 <xsl:when test=
"ancestor::literal|ancestor::email|ancestor::sgmltag">
53 <xsl:call-template name=
"scape-verbatim">
54 <xsl:with-param name=
"string" select=
"."/>
58 <xsl:call-template name=
"scape">
59 <xsl:with-param name=
"string" select=
"."/>
67 <xsl:template match=
"text()" mode=
"xref.text">
68 <xsl:call-template name=
"trim-outer">
69 <xsl:with-param name=
"string">
70 <xsl:call-template name=
"scape">
71 <xsl:with-param name=
"string" select=
"."/>
77 <xsl:template match=
"text()" mode=
"xref-to">
78 <xsl:call-template name=
"trim-outer">
79 <xsl:with-param name=
"string">
80 <xsl:call-template name=
"scape">
81 <xsl:with-param name=
"string" select=
"."/>
87 <xsl:template match=
"text()" mode=
"latex.verbatim">
89 <xsl:call-template name="trim-outer">
90 <xsl:with-param name="string">
92 <xsl:value-of select=
"."/>
99 <!-- this template is noly used by xref.mod.xsl and only when
100 $latex.hyphenation.tttricks != 1. -->
101 <xsl:template match=
"text()" mode=
"slash.hyphen">
102 <xsl:call-template name=
"trim-outer">
103 <xsl:with-param name=
"string">
104 <xsl:call-template name=
"scape.slash.hyphen">
105 <xsl:with-param name=
"string" select=
"." />
111 <xsl:template name=
"trim-outer">
112 <xsl:param name=
"string"/>
113 <xsl:variable name=
"trimleft" select=
"position()=1"/>
114 <xsl:variable name=
"trimright" select=
"position()=last()"/>
116 <xsl:when test=
"$trimleft and not($trimright)">
117 <xsl:value-of select=
"substring-before(normalize-space(concat($string,'$$')),'$$')"/>
119 <xsl:when test=
"$trimright and not($trimleft)">
120 <xsl:value-of select=
"substring-after(normalize-space(concat('$$',$string)),'$$')"/>
122 <xsl:when test=
"$trimleft and $trimright">
123 <xsl:value-of select=
"normalize-space($string)"/>
126 <xsl:value-of select=
"$string"/>
131 <xsl:template name=
"scape.slash.hyphen">
132 <xsl:param name=
"string" />
134 <xsl:when test=
"contains($string,'://')">
135 <xsl:call-template name=
"string-replace">
136 <xsl:with-param name=
"to">/\-
</xsl:with-param>
137 <xsl:with-param name=
"from">/
</xsl:with-param>
138 <xsl:with-param name=
"string">
139 <xsl:value-of select=
"substring-before($string,'://')"/>
140 <xsl:value-of select=
"'://'"/>
141 <xsl:call-template name=
"scape">
142 <xsl:with-param name=
"string" select=
"substring-after($string,'://')"/>
143 </xsl:call-template></xsl:with-param>
147 <xsl:call-template name=
"string-replace">
148 <xsl:with-param name=
"to">/\-
</xsl:with-param>
149 <xsl:with-param name=
"from">/
</xsl:with-param>
150 <xsl:with-param name=
"string">
151 <xsl:call-template name=
"scape">
152 <xsl:with-param name=
"string" select=
"$string"/>
153 </xsl:call-template></xsl:with-param>
159 <xsl:template name=
"normalize-scape" >
160 <xsl:param name=
"string"/>
161 <xsl:variable name=
"result">
162 <xsl:call-template name=
"scape"><xsl:with-param name=
"string" select=
"$string"/></xsl:call-template>
164 <xsl:value-of select=
"normalize-space($result)"/>
168 <xsl:template name=
"scape" >
169 <xsl:param name=
"string"/>
170 <xsl:call-template name=
"string-replace">
171 <xsl:with-param name=
"to">\textless{}
</xsl:with-param>
172 <xsl:with-param name=
"from"><</xsl:with-param>
173 <xsl:with-param name=
"string">
174 <xsl:call-template name=
"string-replace">
175 <xsl:with-param name=
"to">\textgreater{}
</xsl:with-param>
176 <xsl:with-param name=
"from">></xsl:with-param>
177 <xsl:with-param name=
"string">
178 <xsl:call-template name=
"string-replace">
179 <xsl:with-param name=
"to">\textasciitilde{}
</xsl:with-param>
180 <xsl:with-param name=
"from">~
</xsl:with-param>
181 <xsl:with-param name=
"string">
182 <xsl:call-template name=
"string-replace">
183 <xsl:with-param name=
"to">\^{}
</xsl:with-param>
184 <xsl:with-param name=
"from">^
</xsl:with-param>
185 <xsl:with-param name=
"string">
186 <xsl:call-template name=
"string-replace">
187 <xsl:with-param name=
"to">\
&</xsl:with-param>
188 <xsl:with-param name=
"from">&</xsl:with-param>
189 <xsl:with-param name=
"string">
190 <xsl:call-template name=
"string-replace">
191 <xsl:with-param name=
"to">\#
</xsl:with-param>
192 <xsl:with-param name=
"from">#
</xsl:with-param>
193 <xsl:with-param name=
"string">
194 <xsl:call-template name=
"string-replace">
195 <xsl:with-param name=
"to">\_
</xsl:with-param>
196 <xsl:with-param name=
"from">_
</xsl:with-param>
197 <xsl:with-param name=
"string">
198 <xsl:call-template name=
"string-replace">
199 <xsl:with-param name=
"to">\$
</xsl:with-param>
200 <xsl:with-param name=
"from">$
</xsl:with-param>
201 <xsl:with-param name=
"string">
202 <xsl:call-template name=
"string-replace">
203 <xsl:with-param name=
"to">\%
</xsl:with-param>
204 <xsl:with-param name=
"from">%
</xsl:with-param>
205 <xsl:with-param name=
"string">
206 <xsl:call-template name=
"string-replace">
207 <xsl:with-param name=
"to">\{
</xsl:with-param>
208 <xsl:with-param name=
"from">{
</xsl:with-param>
209 <xsl:with-param name=
"string">
210 <xsl:call-template name=
"string-replace">
211 <xsl:with-param name=
"to">\}
</xsl:with-param>
212 <xsl:with-param name=
"from">}
</xsl:with-param>
213 <xsl:with-param name=
"string">
214 <xsl:call-template name=
"string-replace">
215 <xsl:with-param name=
"to">\textbackslash \
</xsl:with-param>
216 <xsl:with-param name=
"from">\textbackslash
</xsl:with-param>
217 <xsl:with-param name=
"string">
218 <xsl:call-template name=
"string-replace">
219 <xsl:with-param name=
"to">\textbackslash
</xsl:with-param>
220 <xsl:with-param name=
"from">\
</xsl:with-param>
221 <xsl:with-param name=
"string" select=
"$string"></xsl:with-param>
224 </xsl:call-template></xsl:with-param>
225 </xsl:call-template></xsl:with-param>
226 </xsl:call-template></xsl:with-param>
227 </xsl:call-template></xsl:with-param>
228 </xsl:call-template></xsl:with-param>
229 </xsl:call-template></xsl:with-param>
230 </xsl:call-template></xsl:with-param>
231 </xsl:call-template></xsl:with-param>
232 </xsl:call-template></xsl:with-param>
233 </xsl:call-template></xsl:with-param>
234 </xsl:call-template></xsl:with-param>
238 <xsl:template name=
"scape-verbatim" >
239 <xsl:param name=
"string"/>
240 <xsl:call-template name=
"string-replace">
241 <xsl:with-param name=
"to">\textasciitilde{}
</xsl:with-param>
242 <xsl:with-param name=
"from">~
</xsl:with-param>
243 <xsl:with-param name=
"string">
244 <xsl:call-template name=
"string-replace">
245 <xsl:with-param name=
"to">\^{}
</xsl:with-param>
246 <xsl:with-param name=
"from">^
</xsl:with-param>
247 <xsl:with-param name=
"string">
248 <xsl:call-template name=
"string-replace">
249 <xsl:with-param name=
"to">\
&</xsl:with-param>
250 <xsl:with-param name=
"from">&</xsl:with-param>
251 <xsl:with-param name=
"string">
252 <xsl:call-template name=
"string-replace">
253 <xsl:with-param name=
"to">\#
</xsl:with-param>
254 <xsl:with-param name=
"from">#
</xsl:with-param>
255 <xsl:with-param name=
"string">
256 <xsl:call-template name=
"string-replace">
257 <xsl:with-param name=
"to">\_
</xsl:with-param>
258 <xsl:with-param name=
"from">_
</xsl:with-param>
259 <xsl:with-param name=
"string">
260 <xsl:call-template name=
"string-replace">
261 <xsl:with-param name=
"to">\$
</xsl:with-param>
262 <xsl:with-param name=
"from">$
</xsl:with-param>
263 <xsl:with-param name=
"string">
264 <xsl:call-template name=
"string-replace">
265 <xsl:with-param name=
"to">\%
</xsl:with-param>
266 <xsl:with-param name=
"from">%
</xsl:with-param>
267 <xsl:with-param name=
"string">
268 <xsl:call-template name=
"string-replace">
269 <xsl:with-param name=
"to">\docbooktolatexgobble\string\{
</xsl:with-param>
270 <xsl:with-param name=
"from">{
</xsl:with-param>
271 <xsl:with-param name=
"string">
272 <xsl:call-template name=
"string-replace">
273 <xsl:with-param name=
"to">\docbooktolatexgobble\string\}
</xsl:with-param>
274 <xsl:with-param name=
"from">}
</xsl:with-param>
275 <xsl:with-param name=
"string">
276 <xsl:call-template name=
"string-replace">
277 <xsl:with-param name=
"to">\docbooktolatexgobble\string\\
</xsl:with-param>
278 <xsl:with-param name=
"from">\
</xsl:with-param>
279 <xsl:with-param name=
"string" select=
"$string"/>
282 </xsl:call-template></xsl:with-param>
283 </xsl:call-template></xsl:with-param>
284 </xsl:call-template></xsl:with-param>
285 </xsl:call-template></xsl:with-param>
286 </xsl:call-template></xsl:with-param>
287 </xsl:call-template></xsl:with-param>
288 </xsl:call-template></xsl:with-param>
289 </xsl:call-template></xsl:with-param>
293 <xsl:template name=
"scape-href" >
294 <xsl:param name=
"string"/>
295 <!-- maybe we should warn when there are invalid characters -->
296 <xsl:call-template name=
"string-replace">
297 <xsl:with-param name=
"to">\
&</xsl:with-param>
298 <xsl:with-param name=
"from">&</xsl:with-param>
299 <xsl:with-param name=
"string">
300 <xsl:call-template name=
"string-replace">
301 <xsl:with-param name=
"to">\%
</xsl:with-param>
302 <xsl:with-param name=
"from">%
</xsl:with-param>
303 <xsl:with-param name=
"string">
304 <xsl:call-template name=
"string-replace">
305 <xsl:with-param name=
"to">\{
</xsl:with-param>
306 <xsl:with-param name=
"from">{
</xsl:with-param>
307 <xsl:with-param name=
"string">
308 <xsl:call-template name=
"string-replace">
309 <xsl:with-param name=
"to">\{
</xsl:with-param>
310 <xsl:with-param name=
"from">{
</xsl:with-param>
311 <xsl:with-param name=
"string">
312 <xsl:call-template name=
"string-replace">
313 <xsl:with-param name=
"to">\docbooktolatexgobble\string\\
</xsl:with-param>
314 <xsl:with-param name=
"from">\
</xsl:with-param>
315 <xsl:with-param name=
"string" select=
"$string"/>
327 <doc:template name=
"scape-optionalarg" xmlns=
"">
328 <refpurpose> Escape the ] character in LaTeX optional arguments (experimental)
</refpurpose>
332 <xsl:template name=
"scape-optionalarg" >
333 <xsl:param name=
"string"/>
334 <xsl:call-template name=
"string-replace">
335 <xsl:with-param name=
"to">{\rbrack}
</xsl:with-param>
336 <xsl:with-param name=
"from">]
</xsl:with-param>
337 <xsl:with-param name=
"string" select=
"$string"/>
344 <xsl:template name=
"string-replace">
345 <xsl:param name=
"string"/>
346 <xsl:param name=
"from"/>
347 <xsl:param name=
"to"/>
350 <xsl:when test=
"contains($string, $from)">
352 <xsl:variable name=
"before" select=
"substring-before($string, $from)"/>
353 <xsl:variable name=
"after" select=
"substring-after($string, $from)"/>
354 <xsl:variable name=
"prefix" select=
"concat($before, $to)"/>
356 <xsl:value-of select=
"$before"/>
357 <xsl:value-of select=
"$to"/>
358 <xsl:call-template name=
"string-replace">
359 <xsl:with-param name=
"string" select=
"$after"/>
360 <xsl:with-param name=
"from" select=
"$from"/>
361 <xsl:with-param name=
"to" select=
"$to"/>
365 <xsl:value-of select=
"$string"/>
375 replace all occurences of the character(s) `from'
376 by the string `to' in the string `string'.
377 <xsl:template name="string-replace" >
378 <xsl:param name="string"/>
379 <xsl:param name="from"/>
380 <xsl:param name="to"/>
382 <xsl:when test="contains($string,$from)">
383 <xsl:value-of select="substring-before($string,$from)"/>
384 <xsl:value-of select="$to"/>
385 <xsl:call-template name="string-replace">
386 <xsl:with-param name="string" select="substring-after($string,$from)"/>
387 <xsl:with-param name="from" select="$from"/>
388 <xsl:with-param name="to" select="$to"/>
392 <xsl:value-of select="$string"/>