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