2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
xmlns:exsl="http://exslt.org/common"
4
xmlns:set="http://exslt.org/sets"
5
exclude-result-prefixes="exsl set"
8
<!-- ********************************************************************
9
$Id: html-rtf.xsl 384406 2005-01-31 00:11:27Z raabe $
10
********************************************************************
12
This file is part of the XSL DocBook Stylesheet distribution.
13
See ../README or http://nwalsh.com/docbook/xsl/ for copyright
14
and other information.
16
******************************************************************** -->
18
<!-- ==================================================================== -->
20
<!-- This module contains templates that match against HTML nodes. It is used
21
to post-process result tree fragments for some sorts of cleanup.
22
These templates can only ever be fired by a processor that supports
25
<!-- ==================================================================== -->
27
<!-- insert.html.p mode templates insert a particular RTF at the beginning
28
of the first paragraph in the primary RTF. -->
30
<xsl:template match="/" mode="insert.html.p">
31
<xsl:param name="mark" select="'?'"/>
32
<xsl:apply-templates mode="insert.html.p">
33
<xsl:with-param name="mark" select="$mark"/>
34
</xsl:apply-templates>
37
<xsl:template match="*" mode="insert.html.p">
38
<xsl:param name="mark" select="'?'"/>
40
<xsl:copy-of select="@*"/>
41
<xsl:apply-templates mode="insert.html.p">
42
<xsl:with-param name="mark" select="$mark"/>
43
</xsl:apply-templates>
47
<xsl:template xmlns:html="http://www.w3.org/1999/xhtml"
48
match="html:p|p" mode="insert.html.p">
49
<xsl:param name="mark" select="'?'"/>
51
<xsl:copy-of select="@*"/>
52
<xsl:if test="not(preceding::p|preceding::html:p)">
53
<xsl:copy-of select="$mark"/>
55
<xsl:apply-templates mode="insert.html.p">
56
<xsl:with-param name="mark" select="$mark"/>
57
</xsl:apply-templates>
61
<xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
62
<xsl:param name="mark" select="'?'"/>
66
<!-- ==================================================================== -->
68
<!-- insert.html.text mode templates insert a particular RTF at the beginning
69
of the first text-node in the primary RTF. -->
71
<xsl:template match="/" mode="insert.html.text">
72
<xsl:param name="mark" select="'?'"/>
73
<xsl:apply-templates mode="insert.html.text">
74
<xsl:with-param name="mark" select="$mark"/>
75
</xsl:apply-templates>
78
<xsl:template match="*" mode="insert.html.text">
79
<xsl:param name="mark" select="'?'"/>
81
<xsl:copy-of select="@*"/>
82
<xsl:apply-templates mode="insert.html.text">
83
<xsl:with-param name="mark" select="$mark"/>
84
</xsl:apply-templates>
88
<xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
89
<xsl:param name="mark" select="'?'"/>
91
<xsl:if test="not(preceding::text())">
92
<xsl:copy-of select="$mark"/>
98
<xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
99
<xsl:param name="mark" select="'?'"/>
103
<!-- ==================================================================== -->
105
<!-- unwrap.p mode templates remove blocks from HTML p elements (and
106
other places where blocks aren't allowed) -->
108
<xsl:template name="unwrap.p">
109
<xsl:param name="p"/>
111
<xsl:when test="function-available('exsl:node-set')
112
and function-available('set:leading')
113
and function-available('set:trailing')">
114
<xsl:apply-templates select="exsl:node-set($p)" mode="unwrap.p"/>
117
<xsl:copy-of select="$p"/>
122
<xsl:template xmlns:html="http://www.w3.org/1999/xhtml"
123
match="html:p|p" mode="unwrap.p">
124
<!-- xmlns:html is necessary for the xhtml stylesheet case -->
125
<xsl:variable name="blocks" xmlns:html="http://www.w3.org/1999/xhtml"
126
select="address|blockquote|div|hr|h1|h2|h3|h4|h5|h6
127
|layer|p|pre|table|dl|menu|ol|ul|form
128
|html:address|html:blockquote|html:div|html:hr
129
|html:h1|html:h2|html:h3|html:h4|html:h5|html:h6
130
|html:layer|html:p|html:pre|html:table|html:dl
131
|html:menu|html:ol|html:ul|html:form"/>
133
<xsl:when test="$blocks">
134
<xsl:call-template name="unwrap.p.nodes">
135
<xsl:with-param name="wrap" select="."/>
136
<xsl:with-param name="first" select="1"/>
137
<xsl:with-param name="nodes" select="node()"/>
138
<xsl:with-param name="blocks" select="$blocks"/>
143
<xsl:copy-of select="@*"/>
144
<xsl:apply-templates mode="unwrap.p"/>
150
<xsl:template match="*" mode="unwrap.p">
152
<xsl:copy-of select="@*"/>
153
<xsl:apply-templates mode="unwrap.p"/>
157
<xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
161
<xsl:template name="unwrap.p.nodes">
162
<xsl:param name="wrap" select="."/>
163
<xsl:param name="first" select="0"/>
164
<xsl:param name="nodes"/>
165
<xsl:param name="blocks"/>
166
<xsl:variable name="block" select="$blocks[1]"/>
168
<!-- This template should never get called if these functions aren't available -->
169
<!-- but this test is still necessary so that processors don't choke on the -->
170
<!-- function calls if they don't support the set: functions -->
171
<xsl:if test="function-available('set:leading')
172
and function-available('set:trailing')">
174
<xsl:when test="$blocks">
175
<xsl:variable name="leading" select="set:leading($nodes,$block)"/>
176
<xsl:variable name="trailing" select="set:trailing($nodes,$block)"/>
178
<xsl:if test="($wrap/@id and $first = 1) or $leading">
179
<xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
180
<xsl:for-each select="$wrap/@*">
181
<xsl:if test="$first != 0 or local-name(.) != 'id'">
185
<xsl:apply-templates select="$leading" mode="unwrap.p"/>
189
<xsl:apply-templates select="$block" mode="unwrap.p"/>
191
<xsl:if test="$trailing">
192
<xsl:call-template name="unwrap.p.nodes">
193
<xsl:with-param name="wrap" select="$wrap"/>
194
<xsl:with-param name="nodes" select="$trailing"/>
195
<xsl:with-param name="blocks" select="$blocks[position() > 1]"/>
201
<xsl:if test="($wrap/@id and $first = 1) or $nodes">
202
<xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
203
<xsl:for-each select="$wrap/@*">
204
<xsl:if test="$first != 0 or local-name(.) != 'id'">
208
<xsl:apply-templates select="$nodes" mode="unwrap.p"/>
216
<!-- ==================================================================== -->
217
<!-- make.verbatim.mode replaces spaces and newlines -->
219
<xsl:template match="/" mode="make.verbatim.mode">
220
<xsl:apply-templates mode="make.verbatim.mode"/>
223
<xsl:template match="*" mode="make.verbatim.mode">
225
<xsl:copy-of select="@*"/>
226
<xsl:apply-templates mode="make.verbatim.mode"/>
230
<xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
234
<xsl:template match="text()" mode="make.verbatim.mode">
235
<xsl:variable name="text" select="translate(., ' ', ' ')"/>
238
<xsl:when test="not(contains($text, '
'))">
239
<xsl:value-of select="$text"/>
243
<xsl:variable name="len" select="string-length($text)"/>
246
<xsl:when test="$len = 1">
247
<br/><xsl:text>
</xsl:text>
251
<xsl:variable name="half" select="$len div 2"/>
252
<xsl:call-template name="make-verbatim-recursive">
253
<xsl:with-param name="text" select="substring($text, 1, $half)"/>
255
<xsl:call-template name="make-verbatim-recursive">
256
<xsl:with-param name="text"
257
select="substring($text, ($half + 1), $len)"/>
265
<xsl:template name="make-verbatim-recursive">
266
<xsl:param name="text" select="''"/>
269
<xsl:when test="not(contains($text, '
'))">
270
<xsl:value-of select="$text"/>
274
<xsl:variable name="len" select="string-length($text)"/>
277
<xsl:when test="$len = 1">
278
<br/><xsl:text>
</xsl:text>
282
<xsl:variable name="half" select="$len div 2"/>
283
<xsl:call-template name="make-verbatim-recursive">
284
<xsl:with-param name="text" select="substring($text, 1, $half)"/>
286
<xsl:call-template name="make-verbatim-recursive">
287
<xsl:with-param name="text"
288
select="substring($text, ($half + 1), $len)"/>
296
<!-- ==================================================================== -->
298
<!-- remove.empty.div mode templates remove empty blocks -->
300
<xsl:template name="remove.empty.div">
301
<xsl:param name="div"/>
303
<xsl:when test="function-available('exsl:node-set')">
304
<xsl:apply-templates select="exsl:node-set($div)" mode="remove.empty.div"/>
307
<xsl:copy-of select="$div"/>
312
<xsl:template xmlns:html="http://www.w3.org/1999/xhtml"
313
match="html:p|p|html:div|div" mode="remove.empty.div">
314
<xsl:if test="node()">
316
<xsl:copy-of select="@*"/>
317
<xsl:apply-templates mode="remove.empty.div"/>
322
<xsl:template match="*" mode="remove.empty.div">
324
<xsl:copy-of select="@*"/>
325
<xsl:apply-templates mode="remove.empty.div"/>
329
<xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
333
<!-- ==================================================================== -->