Project

General

Profile

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">
7

    
8
<!-- ********************************************************************
9
     $Id: html-rtf.xsl 8345 2009-03-16 06:44:07Z bobstayton $
10
     ********************************************************************
11

    
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.
15

    
16
     ******************************************************************** -->
17

    
18
<!-- ==================================================================== -->
19

    
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(). -->
24

    
25
<!-- ==================================================================== -->
26

    
27
<!-- insert.html.p mode templates insert a particular RTF at the beginning
28
     of the first paragraph in the primary RTF. -->
29

    
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>
36

    
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>
46

    
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>
60

    
61
<xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
62
  <xsl:param name="mark" select="'?'"/>
63
  <xsl:copy/>
64
</xsl:template>
65

    
66
<!-- ==================================================================== -->
67

    
68
<!-- insert.html.text mode templates insert a particular RTF at the beginning
69
     of the first text-node in the primary RTF. -->
70

    
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>
77

    
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>
87

    
88
<xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
89
  <xsl:param name="mark" select="'?'"/>
90

    
91
  <xsl:if test="not(preceding::text())">
92
    <xsl:copy-of select="$mark"/>
93
  </xsl:if>
94

    
95
  <xsl:copy/>
96
</xsl:template>
97

    
98
<xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
99
  <xsl:param name="mark" select="'?'"/>
100
  <xsl:copy/>
101
</xsl:template>
102

    
103
<!-- ==================================================================== -->
104

    
105
<!-- unwrap.p mode templates remove blocks from HTML p elements (and
106
     other places where blocks aren't allowed) -->
107

    
108
<xsl:template name="unwrap.p">
109
  <xsl:param name="p"/>
110
  <xsl:choose>
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"/>
115
    </xsl:when>
116
    <xsl:otherwise>
117
      <xsl:copy-of select="$p"/>
118
    </xsl:otherwise>
119
  </xsl:choose>
120
</xsl:template>
121

    
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>
149

    
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>
156

    
157
<xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
158
  <xsl:copy/>
159
</xsl:template>
160

    
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]"/>
167

    
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)"/>
177

    
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>
189

    
190
        <xsl:apply-templates select="$block" mode="unwrap.p"/>
191

    
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>
200

    
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>
216

    
217
<!-- ==================================================================== -->
218
<!-- make.verbatim.mode replaces spaces and newlines -->
219

    
220
<xsl:template match="/" mode="make.verbatim.mode">
221
  <xsl:apply-templates mode="make.verbatim.mode"/>
222
</xsl:template>
223

    
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>
230

    
231
<xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
232
  <xsl:copy/>
233
</xsl:template>
234

    
235
<xsl:template match="text()" mode="make.verbatim.mode">
236
  <xsl:variable name="text" select="translate(., ' ', '&#160;')"/>
237

    
238
  <xsl:choose>
239
    <xsl:when test="not(contains($text, '&#xA;'))">
240
      <xsl:value-of select="$text"/>
241
    </xsl:when>
242

    
243
    <xsl:otherwise>
244
      <xsl:variable name="len" select="string-length($text)"/>
245

    
246
      <xsl:choose>
247
        <xsl:when test="$len = 1">
248
          <br/><xsl:text>&#xA;</xsl:text>
249
        </xsl:when>
250

    
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>
265

    
266
<xsl:template name="make-verbatim-recursive">
267
  <xsl:param name="text" select="''"/>
268

    
269
  <xsl:choose>
270
    <xsl:when test="not(contains($text, '&#xA;'))">
271
      <xsl:value-of select="$text"/>
272
    </xsl:when>
273

    
274
    <xsl:otherwise>
275
      <xsl:variable name="len" select="string-length($text)"/>
276

    
277
      <xsl:choose>
278
        <xsl:when test="$len = 1">
279
          <br/><xsl:text>&#xA;</xsl:text>
280
        </xsl:when>
281

    
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>
296

    
297
<!-- ==================================================================== -->
298

    
299
<!-- remove.empty.div mode templates remove empty blocks -->
300

    
301
<xsl:template name="remove.empty.div">
302
  <xsl:param name="div"/>
303
  <xsl:choose>
304
    <xsl:when test="$exsl.node.set.available != 0">
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>
312

    
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>
322

    
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>
329

    
330
<xsl:template match="text()|processing-instruction()|comment()" mode="remove.empty.div">
331
  <xsl:copy/>
332
</xsl:template>
333

    
334
<!-- ==================================================================== -->
335

    
336
</xsl:stylesheet>
(18-18/39)