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 8345 2009-03-16 06:44:07Z bobstayton $
10 ********************************************************************
12 This file is part of the XSL DocBook Stylesheet distribution.
13 See ../README or http://docbook.sf.net/release/xsl/current/ for
14 copyright 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="$exsl.node.set.available != 0
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 or $wrap/@xml:id)
179 and $first = 1) or $leading">
180 <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
181 <xsl:for-each select="$wrap/@*">
182 <xsl:if test="$first != 0 or local-name(.) != 'id'">
186 <xsl:apply-templates select="$leading" mode="unwrap.p"/>
190 <xsl:apply-templates select="$block" mode="unwrap.p"/>
192 <xsl:if test="$trailing">
193 <xsl:call-template name="unwrap.p.nodes">
194 <xsl:with-param name="wrap" select="$wrap"/>
195 <xsl:with-param name="nodes" select="$trailing"/>
196 <xsl:with-param name="blocks" select="$blocks[position() > 1]"/>
202 <xsl:if test="(($wrap/@id or $wrap/@xml:id) and $first = 1) or $nodes">
203 <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
204 <xsl:for-each select="$wrap/@*">
205 <xsl:if test="$first != 0 or local-name(.) != 'id'">
209 <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
217 <!-- ==================================================================== -->
218 <!-- make.verbatim.mode replaces spaces and newlines -->
220 <xsl:template match="/" mode="make.verbatim.mode">
221 <xsl:apply-templates mode="make.verbatim.mode"/>
224 <xsl:template match="*" mode="make.verbatim.mode">
226 <xsl:copy-of select="@*"/>
227 <xsl:apply-templates mode="make.verbatim.mode"/>
231 <xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
235 <xsl:template match="text()" mode="make.verbatim.mode">
236 <xsl:variable name="text" select="translate(., ' ', ' ')"/>
239 <xsl:when test="not(contains($text, '
'))">
240 <xsl:value-of select="$text"/>
244 <xsl:variable name="len" select="string-length($text)"/>
247 <xsl:when test="$len = 1">
248 <br/><xsl:text>
</xsl:text>
252 <xsl:variable name="half" select="$len div 2"/>
253 <xsl:call-template name="make-verbatim-recursive">
254 <xsl:with-param name="text" select="substring($text, 1, $half)"/>
256 <xsl:call-template name="make-verbatim-recursive">
257 <xsl:with-param name="text"
258 select="substring($text, ($half + 1), $len)"/>
266 <xsl:template name="make-verbatim-recursive">
267 <xsl:param name="text" select="''"/>
270 <xsl:when test="not(contains($text, '
'))">
271 <xsl:value-of select="$text"/>
275 <xsl:variable name="len" select="string-length($text)"/>
278 <xsl:when test="$len = 1">
279 <br/><xsl:text>
</xsl:text>
283 <xsl:variable name="half" select="$len div 2"/>
284 <xsl:call-template name="make-verbatim-recursive">
285 <xsl:with-param name="text" select="substring($text, 1, $half)"/>
287 <xsl:call-template name="make-verbatim-recursive">
288 <xsl:with-param name="text"
289 select="substring($text, ($half + 1), $len)"/>
297 <!-- ==================================================================== -->
299 <!-- remove.empty.div mode templates remove empty blocks -->
301 <xsl:template name="remove.empty.div">
302 <xsl:param name="div"/>
304 <xsl:when test="$exsl.node.set.available != 0">
305 <xsl:apply-templates select="exsl:node-set($div)" mode="remove.empty.div"/>
308 <xsl:copy-of select="$div"/>
313 <xsl:template xmlns:html="http://www.w3.org/1999/xhtml"
314 match="html:p|p|html:div|div" mode="remove.empty.div">
315 <xsl:if test="node()">
317 <xsl:copy-of select="@*"/>
318 <xsl:apply-templates mode="remove.empty.div"/>
323 <xsl:template match="*" mode="remove.empty.div">
325 <xsl:copy-of select="@*"/>
326 <xsl:apply-templates mode="remove.empty.div"/>
330 <xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
334 <!-- ==================================================================== -->