]> git.lyx.org Git - lyx.git/blob - lib/docbook/xhtml/html-rtf.xsl
Release notes
[lyx.git] / lib / docbook / xhtml / html-rtf.xsl
1 <?xml version="1.0" encoding="ASCII"?><!--This file was created automatically by html2xhtml--><!--from the HTML stylesheets.--><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:set="http://exslt.org/sets" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="exsl set" version="1.0">
2
3 <!-- ********************************************************************
4
5      This file is part of the XSL DocBook Stylesheet distribution.
6      See ../README or http://cdn.docbook.org/release/xsl/current/ for
7      copyright and other information.
8
9      ******************************************************************** -->
10
11 <!-- ==================================================================== -->
12
13 <!-- This module contains templates that match against HTML nodes. It is used
14      to post-process result tree fragments for some sorts of cleanup.
15      These templates can only ever be fired by a processor that supports
16      exslt:node-set(). -->
17
18 <!-- ==================================================================== -->
19
20 <!-- insert.html.p mode templates insert a particular RTF at the beginning
21      of the first paragraph in the primary RTF. -->
22
23 <xsl:template match="/" mode="insert.html.p">
24   <xsl:param name="mark" select="'?'"/>
25   <xsl:apply-templates mode="insert.html.p">
26     <xsl:with-param name="mark" select="$mark"/>
27   </xsl:apply-templates>
28 </xsl:template>
29
30 <xsl:template match="*" mode="insert.html.p">
31   <xsl:param name="mark" select="'?'"/>
32   <xsl:copy>
33     <xsl:copy-of select="@*"/>
34     <xsl:apply-templates mode="insert.html.p">
35       <xsl:with-param name="mark" select="$mark"/>
36     </xsl:apply-templates>
37   </xsl:copy>
38 </xsl:template>
39
40 <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p" mode="insert.html.p">
41   <xsl:param name="mark" select="'?'"/>
42   <xsl:copy>
43     <xsl:copy-of select="@*"/>
44     <xsl:if test="not(preceding::p|preceding::html:p)">
45       <xsl:copy-of select="$mark"/>
46     </xsl:if>
47     <xsl:apply-templates mode="insert.html.p">
48       <xsl:with-param name="mark" select="$mark"/>
49     </xsl:apply-templates>
50   </xsl:copy>
51 </xsl:template>
52
53 <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
54   <xsl:param name="mark" select="'?'"/>
55   <xsl:copy/>
56 </xsl:template>
57
58 <!-- ==================================================================== -->
59
60 <!-- insert.html.text mode templates insert a particular RTF at the beginning
61      of the first text-node in the primary RTF. -->
62
63 <xsl:template match="/" mode="insert.html.text">
64   <xsl:param name="mark" select="'?'"/>
65   <xsl:apply-templates mode="insert.html.text">
66     <xsl:with-param name="mark" select="$mark"/>
67   </xsl:apply-templates>
68 </xsl:template>
69
70 <xsl:template match="*" mode="insert.html.text">
71   <xsl:param name="mark" select="'?'"/>
72   <xsl:copy>
73     <xsl:copy-of select="@*"/>
74     <xsl:apply-templates mode="insert.html.text">
75       <xsl:with-param name="mark" select="$mark"/>
76     </xsl:apply-templates>
77   </xsl:copy>
78 </xsl:template>
79
80 <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
81   <xsl:param name="mark" select="'?'"/>
82
83   <xsl:if test="not(preceding::text())">
84     <xsl:copy-of select="$mark"/>
85   </xsl:if>
86
87   <xsl:copy/>
88 </xsl:template>
89
90 <xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
91   <xsl:param name="mark" select="'?'"/>
92   <xsl:copy/>
93 </xsl:template>
94
95 <!-- ==================================================================== -->
96
97 <!-- unwrap.p mode templates remove blocks from HTML p elements (and
98      other places where blocks aren't allowed) -->
99
100 <xsl:template name="unwrap.p">
101   <xsl:param name="p"/>
102   <xsl:choose>
103     <xsl:when test="$exsl.node.set.available != 0                     and function-available('set:leading')                     and function-available('set:trailing')">
104       <xsl:apply-templates select="exsl:node-set($p)" mode="unwrap.p"/>
105     </xsl:when>
106     <xsl:otherwise>
107       <xsl:copy-of select="$p"/>
108     </xsl:otherwise>
109   </xsl:choose>
110 </xsl:template>
111
112 <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p" mode="unwrap.p">
113   <!-- xmlns:html is necessary for the xhtml stylesheet case -->
114   <xsl:variable name="blocks" select="address|blockquote|div|hr|h1|h2|h3|h4|h5|h6                         |layer|p|pre|table|dl|menu|ol|ul|form                         |html:address|html:blockquote|html:div|html:hr                         |html:h1|html:h2|html:h3|html:h4|html:h5|html:h6                         |html:layer|html:p|html:pre|html:table|html:dl                         |html:menu|html:ol|html:ul|html:form"/>
115   <xsl:choose>
116     <xsl:when test="$blocks">
117       <xsl:call-template name="unwrap.p.nodes">
118         <xsl:with-param name="wrap" select="."/>
119         <xsl:with-param name="first" select="1"/>
120         <xsl:with-param name="nodes" select="node()"/>
121         <xsl:with-param name="blocks" select="$blocks"/>
122       </xsl:call-template>
123     </xsl:when>
124     <xsl:otherwise>
125       <xsl:copy>
126         <xsl:copy-of select="@*"/>
127         <xsl:apply-templates mode="unwrap.p"/>
128       </xsl:copy>
129     </xsl:otherwise>
130   </xsl:choose>
131 </xsl:template>
132
133 <xsl:template match="*" mode="unwrap.p">
134   <xsl:copy>
135     <xsl:copy-of select="@*"/>
136     <xsl:apply-templates mode="unwrap.p"/>
137   </xsl:copy>
138 </xsl:template>
139
140 <xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
141   <xsl:copy/>
142 </xsl:template>
143
144 <xsl:template name="unwrap.p.nodes">
145   <xsl:param name="wrap" select="."/>
146   <xsl:param name="first" select="0"/>
147   <xsl:param name="nodes"/>
148   <xsl:param name="blocks"/>
149   <xsl:variable name="block" select="$blocks[1]"/>
150
151   <!-- This template should never get called if these functions aren't available -->
152   <!-- but this test is still necessary so that processors don't choke on the -->
153   <!-- function calls if they don't support the set: functions -->
154   <xsl:if test="function-available('set:leading')                 and function-available('set:trailing')">
155     <xsl:choose>
156       <xsl:when test="$blocks">
157         <xsl:variable name="leading" select="set:leading($nodes,$block)"/>
158         <xsl:variable name="trailing" select="set:trailing($nodes,$block)"/>
159
160         <xsl:if test="(($wrap/@id or $wrap/@xml:id)                          and $first = 1) or $leading">
161           <xsl:element name="{local-name($wrap)}" namespace="http://www.w3.org/1999/xhtml">
162             <xsl:for-each select="$wrap/@*">
163               <xsl:if test="$first != 0 or local-name(.) != 'id'">
164                 <xsl:copy/>
165               </xsl:if>
166             </xsl:for-each>
167             <xsl:apply-templates select="$leading" mode="unwrap.p"/>
168           </xsl:element>
169         </xsl:if>
170
171         <xsl:apply-templates select="$block" mode="unwrap.p"/>
172
173         <xsl:if test="$trailing">
174           <xsl:call-template name="unwrap.p.nodes">
175             <xsl:with-param name="wrap" select="$wrap"/>
176             <xsl:with-param name="nodes" select="$trailing"/>
177             <xsl:with-param name="blocks" select="$blocks[position() &gt; 1]"/>
178           </xsl:call-template>
179         </xsl:if>
180       </xsl:when>
181
182       <xsl:otherwise>
183         <xsl:if test="(($wrap/@id or $wrap/@xml:id) and $first = 1) or $nodes">
184           <xsl:element name="{local-name($wrap)}" namespace="http://www.w3.org/1999/xhtml">
185             <xsl:for-each select="$wrap/@*">
186               <xsl:if test="$first != 0 or local-name(.) != 'id'">
187                 <xsl:copy/>
188               </xsl:if>
189             </xsl:for-each>
190             <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
191           </xsl:element>
192         </xsl:if>
193       </xsl:otherwise>
194     </xsl:choose>
195   </xsl:if>
196 </xsl:template>
197
198 <!-- ==================================================================== -->
199 <!-- make.verbatim.mode replaces spaces and newlines -->
200
201 <xsl:template match="/" mode="make.verbatim.mode">
202   <xsl:apply-templates mode="make.verbatim.mode"/>
203 </xsl:template>
204
205 <xsl:template match="*" mode="make.verbatim.mode">
206   <xsl:copy>
207     <xsl:copy-of select="@*"/>
208     <xsl:apply-templates mode="make.verbatim.mode"/>
209   </xsl:copy>
210 </xsl:template>
211
212 <xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
213   <xsl:copy/>
214 </xsl:template>
215
216 <xsl:template match="text()" mode="make.verbatim.mode">
217   <xsl:variable name="text" select="translate(., ' ', '&#160;')"/>
218
219   <xsl:choose>
220     <xsl:when test="not(contains($text, '&#xA;'))">
221       <xsl:value-of select="$text"/>
222     </xsl:when>
223
224     <xsl:otherwise>
225       <xsl:variable name="len" select="string-length($text)"/>
226
227       <xsl:choose>
228         <xsl:when test="$len = 1">
229           <br/><xsl:text>
230 </xsl:text>
231         </xsl:when>
232
233         <xsl:otherwise>
234           <xsl:variable name="half" select="$len div 2"/>
235           <xsl:call-template name="make-verbatim-recursive">
236             <xsl:with-param name="text" select="substring($text, 1, $half)"/>
237           </xsl:call-template>
238           <xsl:call-template name="make-verbatim-recursive">
239             <xsl:with-param name="text" select="substring($text, ($half + 1), $len)"/>
240           </xsl:call-template>
241         </xsl:otherwise>
242       </xsl:choose>
243     </xsl:otherwise>
244   </xsl:choose>
245 </xsl:template>
246
247 <xsl:template name="make-verbatim-recursive">
248   <xsl:param name="text" select="''"/>
249
250   <xsl:choose>
251     <xsl:when test="not(contains($text, '&#xA;'))">
252       <xsl:value-of select="$text"/>
253     </xsl:when>
254
255     <xsl:otherwise>
256       <xsl:variable name="len" select="string-length($text)"/>
257
258       <xsl:choose>
259         <xsl:when test="$len = 1">
260           <br/><xsl:text>
261 </xsl:text>
262         </xsl:when>
263
264         <xsl:otherwise>
265           <xsl:variable name="half" select="$len div 2"/>
266           <xsl:call-template name="make-verbatim-recursive">
267             <xsl:with-param name="text" select="substring($text, 1, $half)"/>
268           </xsl:call-template>
269           <xsl:call-template name="make-verbatim-recursive">
270             <xsl:with-param name="text" select="substring($text, ($half + 1), $len)"/>
271           </xsl:call-template>
272         </xsl:otherwise>
273       </xsl:choose>
274     </xsl:otherwise>
275   </xsl:choose>
276 </xsl:template>
277
278 <!-- ==================================================================== -->
279
280 <!-- remove.empty.div mode templates remove empty blocks -->
281
282 <xsl:template name="remove.empty.div">
283   <xsl:param name="div"/>
284   <xsl:choose>
285     <xsl:when test="$exsl.node.set.available != 0">
286       <xsl:apply-templates select="exsl:node-set($div)" mode="remove.empty.div"/>
287     </xsl:when>
288     <xsl:otherwise>
289       <xsl:copy-of select="$div"/>
290     </xsl:otherwise>
291   </xsl:choose>
292 </xsl:template>
293
294 <xsl:template xmlns:html="http://www.w3.org/1999/xhtml" match="html:p|p|html:div|div" mode="remove.empty.div">
295   <xsl:if test="node()">
296     <xsl:copy>
297       <xsl:copy-of select="@*"/>
298       <xsl:apply-templates mode="remove.empty.div"/>
299     </xsl:copy>
300   </xsl:if>
301 </xsl:template>
302
303 <xsl:template match="*" mode="remove.empty.div">
304   <xsl:copy>
305     <xsl:copy-of select="@*"/>
306     <xsl:apply-templates mode="remove.empty.div"/>
307   </xsl:copy>
308 </xsl:template>
309
310 <xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
311   <xsl:copy/>
312 </xsl:template>
313
314 <!-- ==================================================================== -->
315
316 </xsl:stylesheet>