Jitterbug no more.
[fvwm.git] / doc / docbook-xsl / html / html-rtf.xsl
blobc97f8ea3c151ee4462288e0e8e033fb15e82e656
1 <?xml version="1.0"?>
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"
6 version="1.0">
8 <!-- ********************************************************************
9 $Id: html-rtf.xsl,v 1.1 2007/03/10 05:15:13 scott Exp $
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
23 exslt:node-set(). -->
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>
35 </xsl:template>
37 <xsl:template match="*" mode="insert.html.p">
38 <xsl:param name="mark" select="'?'"/>
39 <xsl:copy>
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>
44 </xsl:copy>
45 </xsl:template>
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="'?'"/>
50 <xsl:copy>
51 <xsl:copy-of select="@*"/>
52 <xsl:if test="not(preceding::p|preceding::html:p)">
53 <xsl:copy-of select="$mark"/>
54 </xsl:if>
55 <xsl:apply-templates mode="insert.html.p">
56 <xsl:with-param name="mark" select="$mark"/>
57 </xsl:apply-templates>
58 </xsl:copy>
59 </xsl:template>
61 <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
62 <xsl:param name="mark" select="'?'"/>
63 <xsl:copy/>
64 </xsl:template>
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>
76 </xsl:template>
78 <xsl:template match="*" mode="insert.html.text">
79 <xsl:param name="mark" select="'?'"/>
80 <xsl:copy>
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>
85 </xsl:copy>
86 </xsl:template>
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"/>
93 </xsl:if>
95 <xsl:copy/>
96 </xsl:template>
98 <xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
99 <xsl:param name="mark" select="'?'"/>
100 <xsl:copy/>
101 </xsl:template>
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"/>
110 <xsl:choose>
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"/>
115 </xsl:when>
116 <xsl:otherwise>
117 <xsl:copy-of select="$p"/>
118 </xsl:otherwise>
119 </xsl:choose>
120 </xsl:template>
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"/>
132 <xsl:choose>
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"/>
139 </xsl:call-template>
140 </xsl:when>
141 <xsl:otherwise>
142 <xsl:copy>
143 <xsl:copy-of select="@*"/>
144 <xsl:apply-templates mode="unwrap.p"/>
145 </xsl:copy>
146 </xsl:otherwise>
147 </xsl:choose>
148 </xsl:template>
150 <xsl:template match="*" mode="unwrap.p">
151 <xsl:copy>
152 <xsl:copy-of select="@*"/>
153 <xsl:apply-templates mode="unwrap.p"/>
154 </xsl:copy>
155 </xsl:template>
157 <xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
158 <xsl:copy/>
159 </xsl:template>
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')">
173 <xsl:choose>
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'">
183 <xsl:copy/>
184 </xsl:if>
185 </xsl:for-each>
186 <xsl:apply-templates select="$leading" mode="unwrap.p"/>
187 </xsl:element>
188 </xsl:if>
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() &gt; 1]"/>
197 </xsl:call-template>
198 </xsl:if>
199 </xsl:when>
201 <xsl:otherwise>
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'">
206 <xsl:copy/>
207 </xsl:if>
208 </xsl:for-each>
209 <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
210 </xsl:element>
211 </xsl:if>
212 </xsl:otherwise>
213 </xsl:choose>
214 </xsl:if>
215 </xsl:template>
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"/>
222 </xsl:template>
224 <xsl:template match="*" mode="make.verbatim.mode">
225 <xsl:copy>
226 <xsl:copy-of select="@*"/>
227 <xsl:apply-templates mode="make.verbatim.mode"/>
228 </xsl:copy>
229 </xsl:template>
231 <xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
232 <xsl:copy/>
233 </xsl:template>
235 <xsl:template match="text()" mode="make.verbatim.mode">
236 <xsl:variable name="text" select="translate(., ' ', '&#160;')"/>
238 <xsl:choose>
239 <xsl:when test="not(contains($text, '&#xA;'))">
240 <xsl:value-of select="$text"/>
241 </xsl:when>
243 <xsl:otherwise>
244 <xsl:variable name="len" select="string-length($text)"/>
246 <xsl:choose>
247 <xsl:when test="$len = 1">
248 <br/><xsl:text>&#xA;</xsl:text>
249 </xsl:when>
251 <xsl:otherwise>
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)"/>
255 </xsl:call-template>
256 <xsl:call-template name="make-verbatim-recursive">
257 <xsl:with-param name="text"
258 select="substring($text, ($half + 1), $len)"/>
259 </xsl:call-template>
260 </xsl:otherwise>
261 </xsl:choose>
262 </xsl:otherwise>
263 </xsl:choose>
264 </xsl:template>
266 <xsl:template name="make-verbatim-recursive">
267 <xsl:param name="text" select="''"/>
269 <xsl:choose>
270 <xsl:when test="not(contains($text, '&#xA;'))">
271 <xsl:value-of select="$text"/>
272 </xsl:when>
274 <xsl:otherwise>
275 <xsl:variable name="len" select="string-length($text)"/>
277 <xsl:choose>
278 <xsl:when test="$len = 1">
279 <br/><xsl:text>&#xA;</xsl:text>
280 </xsl:when>
282 <xsl:otherwise>
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)"/>
286 </xsl:call-template>
287 <xsl:call-template name="make-verbatim-recursive">
288 <xsl:with-param name="text"
289 select="substring($text, ($half + 1), $len)"/>
290 </xsl:call-template>
291 </xsl:otherwise>
292 </xsl:choose>
293 </xsl:otherwise>
294 </xsl:choose>
295 </xsl:template>
297 <!-- ==================================================================== -->
299 <!-- remove.empty.div mode templates remove empty blocks -->
301 <xsl:template name="remove.empty.div">
302 <xsl:param name="div"/>
303 <xsl:choose>
304 <xsl:when test="function-available('exsl:node-set')">
305 <xsl:apply-templates select="exsl:node-set($div)" mode="remove.empty.div"/>
306 </xsl:when>
307 <xsl:otherwise>
308 <xsl:copy-of select="$div"/>
309 </xsl:otherwise>
310 </xsl:choose>
311 </xsl:template>
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()">
316 <xsl:copy>
317 <xsl:copy-of select="@*"/>
318 <xsl:apply-templates mode="remove.empty.div"/>
319 </xsl:copy>
320 </xsl:if>
321 </xsl:template>
323 <xsl:template match="*" mode="remove.empty.div">
324 <xsl:copy>
325 <xsl:copy-of select="@*"/>
326 <xsl:apply-templates mode="remove.empty.div"/>
327 </xsl:copy>
328 </xsl:template>
330 <xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
331 <xsl:copy/>
332 </xsl:template>
334 <!-- ==================================================================== -->
336 </xsl:stylesheet>