1<?xml version='1.0'?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                xmlns:sverb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Verbatim"
4                xmlns:xverb="com.nwalsh.xalan.Verbatim"
5                xmlns:lxslt="http://xml.apache.org/xslt"
6                exclude-result-prefixes="sverb xverb lxslt"
7                version='1.0'>
8
9<!-- ********************************************************************
10     $Id$
11     ********************************************************************
12
13     This file is part of the XSL DocBook Stylesheet distribution.
14     See /README or http://nwalsh.com/docbook/xsl/ for copyright
15     and other information.
16
17     ******************************************************************** -->
18
19<lxslt:component prefix="xverb"
20                 functions="numberLines"/>
21
22<xsl:template match="programlisting|screen|synopsis">
23  <xsl:param name="suppress-numbers" select="'0'"/>
24  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
25  <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
26
27  <xsl:if test="@id">
28    <a href="{$id}"/>
29  </xsl:if>
30
31  <xsl:choose>
32    <xsl:when test="$suppress-numbers = '0'
33                    and @linenumbering = 'numbered'
34                    and $use.extensions != '0'
35                    and $linenumbering.extension != '0'">
36      <xsl:variable name="rtf">
37        <xsl:apply-templates/>
38      </xsl:variable>
39      <pre class="{name(.)}">
40        <xsl:call-template name="number.rtf.lines">
41          <xsl:with-param name="rtf" select="$rtf"/>
42        </xsl:call-template>
43      </pre>
44    </xsl:when>
45    <xsl:otherwise>
46      <pre class="{name(.)}">
47        <xsl:apply-templates/>
48      </pre>
49    </xsl:otherwise>
50  </xsl:choose>
51</xsl:template>
52
53<xsl:template match="literallayout">
54  <xsl:param name="suppress-numbers" select="'0'"/>
55  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
56
57  <xsl:variable name="rtf">
58    <xsl:apply-templates/>
59  </xsl:variable>
60
61  <xsl:choose>
62    <xsl:when test="$suppress-numbers = '0'
63                    and @linenumbering = 'numbered'
64                    and $use.extensions != '0'
65                    and $linenumbering.extension != '0'">
66      <xsl:choose>
67        <xsl:when test="@class='monospaced'">
68          <pre class="{name(.)}">
69            <xsl:call-template name="number.rtf.lines">
70              <xsl:with-param name="rtf" select="$rtf"/>
71            </xsl:call-template>
72          </pre>
73        </xsl:when>
74        <xsl:otherwise>
75          <div class="{name(.)}">
76            <xsl:call-template name="number.rtf.lines">
77              <xsl:with-param name="rtf" select="$rtf"/>
78            </xsl:call-template>
79          </div>
80        </xsl:otherwise>
81      </xsl:choose>
82    </xsl:when>
83
84    <xsl:otherwise>
85      <xsl:choose>
86        <xsl:when test="@class='monospaced'">
87          <pre class="{name(.)}">
88            <xsl:copy-of select="$rtf"/>
89          </pre>
90        </xsl:when>
91        <xsl:otherwise>
92          <div class="{name(.)}">
93            <xsl:copy-of select="$rtf"/>
94          </div>
95        </xsl:otherwise>
96      </xsl:choose>
97    </xsl:otherwise>
98  </xsl:choose>
99</xsl:template>
100
101<xsl:template match="literallayout[not(@class)
102                                   or @class != 'monospaced']//text()">
103  <xsl:call-template name="make-verbatim">
104    <xsl:with-param name="text" select="."/>
105  </xsl:call-template>
106</xsl:template>
107
108<xsl:template match="address">
109  <xsl:param name="suppress-numbers" select="'0'"/>
110  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
111
112  <xsl:variable name="rtf">
113    <xsl:apply-templates/>
114  </xsl:variable>
115
116  <xsl:choose>
117    <xsl:when test="$suppress-numbers = '0'
118                    and @linenumbering = 'numbered'
119                    and $use.extensions != '0'
120                    and $linenumbering.extension != '0'">
121      <div class="{name(.)}">
122        <xsl:call-template name="number.rtf.lines">
123          <xsl:with-param name="rtf" select="$rtf"/>
124        </xsl:call-template>
125      </div>
126    </xsl:when>
127
128    <xsl:otherwise>
129      <div class="{name(.)}">
130        <xsl:apply-templates/>
131      </div>
132    </xsl:otherwise>
133  </xsl:choose>
134</xsl:template>
135
136<xsl:template name="number.rtf.lines">
137  <xsl:param name="rtf" select="''"/>
138  <xsl:param name="pi.context" select="."/>
139
140  <!-- Save the global values -->
141  <xsl:variable name="global.linenumbering.everyNth"
142                select="$linenumbering.everyNth"/>
143
144  <xsl:variable name="global.linenumbering.separator"
145                select="$linenumbering.separator"/>
146
147  <xsl:variable name="global.linenumbering.width"
148                select="$linenumbering.width"/>
149
150  <!-- Extract the <?dbhtml linenumbering.*?> PI values -->
151  <xsl:variable name="pi.linenumbering.everyNth">
152    <xsl:call-template name="dbhtml-attribute">
153      <xsl:with-param name="pis"
154                      select="$pi.context/processing-instruction('dbhtml')"/>
155      <xsl:with-param name="attribute" select="'linenumbering.everyNth'"/>
156    </xsl:call-template>
157  </xsl:variable>
158
159  <xsl:variable name="pi.linenumbering.separator">
160    <xsl:call-template name="dbhtml-attribute">
161      <xsl:with-param name="pis"
162                      select="$pi.context/processing-instruction('dbhtml')"/>
163      <xsl:with-param name="attribute" select="'linenumbering.separator'"/>
164    </xsl:call-template>
165  </xsl:variable>
166
167  <xsl:variable name="pi.linenumbering.width">
168    <xsl:call-template name="dbhtml-attribute">
169      <xsl:with-param name="pis"
170                      select="$pi.context/processing-instruction('dbhtml')"/>
171      <xsl:with-param name="attribute" select="'linenumbering.width'"/>
172    </xsl:call-template>
173  </xsl:variable>
174
175  <!-- Construct the 'in-context' values -->
176  <xsl:variable name="linenumbering.everyNth">
177    <xsl:choose>
178      <xsl:when test="$pi.linenumbering.everyNth != ''">
179        <xsl:value-of select="$pi.linenumbering.everyNth"/>
180      </xsl:when>
181      <xsl:otherwise>
182        <xsl:value-of select="$global.linenumbering.everyNth"/>
183      </xsl:otherwise>
184    </xsl:choose>
185  </xsl:variable>
186
187  <xsl:variable name="linenumbering.separator">
188    <xsl:choose>
189      <xsl:when test="$pi.linenumbering.separator != ''">
190        <xsl:value-of select="$pi.linenumbering.separator"/>
191      </xsl:when>
192      <xsl:otherwise>
193        <xsl:value-of select="$global.linenumbering.separator"/>
194      </xsl:otherwise>
195    </xsl:choose>
196  </xsl:variable>
197
198  <xsl:variable name="linenumbering.width">
199    <xsl:choose>
200      <xsl:when test="$pi.linenumbering.width != ''">
201        <xsl:value-of select="$pi.linenumbering.width"/>
202      </xsl:when>
203      <xsl:otherwise>
204        <xsl:value-of select="$global.linenumbering.width"/>
205      </xsl:otherwise>
206    </xsl:choose>
207  </xsl:variable>
208
209  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
210
211  <xsl:choose>
212    <xsl:when test="contains($vendor, 'SAXON ')">
213      <xsl:copy-of select="sverb:numberLines($rtf)"/>
214    </xsl:when>
215    <xsl:when test="contains($vendor, 'Apache Software Foundation')">
216      <xsl:copy-of select="xverb:numberLines($rtf)"/>
217    </xsl:when>
218    <xsl:otherwise>
219      <xsl:message terminate="yes">
220        <xsl:text>Don't know how to do line numbering with </xsl:text>
221        <xsl:value-of select="$vendor"/>
222      </xsl:message>
223    </xsl:otherwise>
224  </xsl:choose>
225</xsl:template>
226
227<xsl:template match="address//text()">
228  <xsl:call-template name="make-verbatim">
229    <xsl:with-param name="text" select="."/>
230  </xsl:call-template>
231</xsl:template>
232
233<xsl:template name="make-verbatim">
234  <xsl:param name="text" select="''"/>
235
236  <xsl:variable name="starts-with-space"
237                select="substring($text, 1, 1) = ' '"/>
238
239  <xsl:variable name="starts-with-nl"
240                select="substring($text, 1, 1) = '&#xA;'"/>
241
242  <xsl:variable name="before-space">
243    <xsl:if test="contains($text, ' ')">
244      <xsl:value-of select="substring-before($text, ' ')"/>
245    </xsl:if>
246  </xsl:variable>
247
248  <xsl:variable name="before-nl">
249    <xsl:if test="contains($text, '&#xA;')">
250      <xsl:value-of select="substring-before($text, '&#xA;')"/>
251    </xsl:if>
252  </xsl:variable>
253
254  <xsl:choose>
255    <xsl:when test="$starts-with-space">
256      <xsl:text>&#160;</xsl:text>
257      <xsl:call-template name="make-verbatim">
258        <xsl:with-param name="text" select="substring($text,2)"/>
259      </xsl:call-template>
260    </xsl:when>
261
262    <xsl:when test="$starts-with-nl">
263      <br/><xsl:text>&#xA;</xsl:text>
264      <xsl:call-template name="make-verbatim">
265        <xsl:with-param name="text" select="substring($text,2)"/>
266      </xsl:call-template>
267    </xsl:when>
268
269    <!-- if the string before a space is shorter than the string before
270         a newline, fix the space...-->
271    <xsl:when test="$before-space != ''
272                    and ((string-length($before-space)
273                          &lt; string-length($before-nl))
274                          or $before-nl = '')">
275      <xsl:value-of select="$before-space"/>
276      <xsl:text>&#160;</xsl:text>
277      <xsl:call-template name="make-verbatim">
278        <xsl:with-param name="text" select="substring-after($text, ' ')"/>
279      </xsl:call-template>
280    </xsl:when>
281
282    <!-- if the string before a newline is shorter than the string before
283         a space, fix the newline...-->
284    <xsl:when test="$before-nl != ''
285                    and ((string-length($before-nl)
286                          &lt; string-length($before-space))
287                          or $before-space = '')">
288      <xsl:value-of select="$before-nl"/>
289      <br/><xsl:text>&#xA;</xsl:text>
290      <xsl:call-template name="make-verbatim">
291        <xsl:with-param name="text" select="substring-after($text, '&#xA;')"/>
292      </xsl:call-template>
293    </xsl:when>
294
295    <!-- the string before the newline and the string before the
296         space are the same; which means they must both be empty -->
297    <xsl:otherwise>
298      <xsl:value-of select="$text"/>
299    </xsl:otherwise>
300  </xsl:choose>
301</xsl:template>
302
303</xsl:stylesheet>
304