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                xmlns:exsl="http://exslt.org/common"
7                exclude-result-prefixes="sverb xverb lxslt exsl"
8                version='1.0'>
9
10<!-- ********************************************************************
11     $Id: verbatim.xsl,v 1.14 2005/11/20 02:44:34 bobstayton Exp $
12     ********************************************************************
13
14     This file is part of the XSL DocBook Stylesheet distribution.
15     See /README or http://nwalsh.com/docbook/xsl/ for copyright
16     and other information.
17
18     ******************************************************************** -->
19
20<lxslt:component prefix="xverb"
21                 functions="numberLines"/>
22
23<xsl:template match="programlisting|screen|synopsis">
24  <xsl:param name="suppress-numbers" select="'0'"/>
25  <xsl:variable name="id">
26    <xsl:call-template name="object.id"/>
27  </xsl:variable>
28
29  <xsl:call-template name="anchor"/>
30
31  <xsl:if test="$shade.verbatim != 0">
32    <xsl:message>
33      <xsl:text>The shade.verbatim parameter is deprecated. </xsl:text>
34      <xsl:text>Use CSS instead,</xsl:text>
35    </xsl:message>
36    <xsl:message>
37      <xsl:text>for example: pre.</xsl:text>
38      <xsl:value-of select="local-name(.)"/>
39      <xsl:text> { background-color: #E0E0E0; }</xsl:text>
40    </xsl:message>
41  </xsl:if>
42
43  <xsl:choose>
44    <xsl:when test="$suppress-numbers = '0'
45		    and @linenumbering = 'numbered'
46		    and $use.extensions != '0'
47		    and $linenumbering.extension != '0'">
48      <xsl:variable name="rtf">
49	<xsl:apply-templates/>
50      </xsl:variable>
51      <pre class="{name(.)}">
52	<xsl:call-template name="number.rtf.lines">
53	  <xsl:with-param name="rtf" select="$rtf"/>
54	</xsl:call-template>
55      </pre>
56    </xsl:when>
57    <xsl:otherwise>
58      <pre class="{name(.)}">
59	<xsl:apply-templates/>
60      </pre>
61    </xsl:otherwise>
62  </xsl:choose>
63</xsl:template>
64
65<xsl:template match="literallayout">
66  <xsl:param name="suppress-numbers" select="'0'"/>
67
68  <xsl:variable name="rtf">
69    <xsl:apply-templates/>
70  </xsl:variable>
71
72  <xsl:if test="$shade.verbatim != 0 and @class='monospaced'">
73    <xsl:message>
74      <xsl:text>The shade.verbatim parameter is deprecated. </xsl:text>
75      <xsl:text>Use CSS instead,</xsl:text>
76    </xsl:message>
77    <xsl:message>
78      <xsl:text>for example: pre.</xsl:text>
79      <xsl:value-of select="local-name(.)"/>
80      <xsl:text> { background-color: #E0E0E0; }</xsl:text>
81    </xsl:message>
82  </xsl:if>
83
84  <xsl:choose>
85    <xsl:when test="$suppress-numbers = '0'
86		    and @linenumbering = 'numbered'
87		    and $use.extensions != '0'
88		    and $linenumbering.extension != '0'">
89      <xsl:choose>
90	<xsl:when test="@class='monospaced'">
91	  <pre class="{name(.)}">
92	    <xsl:call-template name="number.rtf.lines">
93	      <xsl:with-param name="rtf" select="$rtf"/>
94	    </xsl:call-template>
95	  </pre>
96	</xsl:when>
97	<xsl:otherwise>
98	  <div class="{name(.)}">
99	    <p>
100	      <xsl:call-template name="number.rtf.lines">
101		<xsl:with-param name="rtf" select="$rtf"/>
102	      </xsl:call-template>
103	    </p>
104	  </div>
105	</xsl:otherwise>
106      </xsl:choose>
107    </xsl:when>
108    <xsl:otherwise>
109      <xsl:choose>
110	<xsl:when test="@class='monospaced'">
111	  <pre class="{name(.)}">
112	    <xsl:copy-of select="$rtf"/>
113	  </pre>
114	</xsl:when>
115	<xsl:otherwise>
116	  <div class="{name(.)}">
117	    <p>
118	      <xsl:call-template name="make-verbatim">
119		<xsl:with-param name="rtf" select="$rtf"/>
120	      </xsl:call-template>
121	    </p>
122	  </div>
123	</xsl:otherwise>
124      </xsl:choose>
125    </xsl:otherwise>
126  </xsl:choose>
127</xsl:template>
128
129<xsl:template match="address">
130  <xsl:param name="suppress-numbers" select="'0'"/>
131
132  <xsl:variable name="rtf">
133    <xsl:apply-templates/>
134  </xsl:variable>
135
136  <xsl:choose>
137    <xsl:when test="$suppress-numbers = '0'
138                    and @linenumbering = 'numbered'
139                    and $use.extensions != '0'
140                    and $linenumbering.extension != '0'">
141      <div class="{name(.)}">
142        <p>
143          <xsl:call-template name="number.rtf.lines">
144            <xsl:with-param name="rtf" select="$rtf"/>
145          </xsl:call-template>
146        </p>
147      </div>
148    </xsl:when>
149
150    <xsl:otherwise>
151      <div class="{name(.)}">
152        <p>
153          <xsl:call-template name="make-verbatim">
154            <xsl:with-param name="rtf" select="$rtf"/>
155          </xsl:call-template>
156        </p>
157      </div>
158    </xsl:otherwise>
159  </xsl:choose>
160</xsl:template>
161
162<xsl:template name="number.rtf.lines">
163  <xsl:param name="rtf" select="''"/>
164  <xsl:param name="pi.context" select="."/>
165
166  <!-- Save the global values -->
167  <xsl:variable name="global.linenumbering.everyNth"
168                select="$linenumbering.everyNth"/>
169
170  <xsl:variable name="global.linenumbering.separator"
171                select="$linenumbering.separator"/>
172
173  <xsl:variable name="global.linenumbering.width"
174                select="$linenumbering.width"/>
175
176  <!-- Extract the <?dbhtml linenumbering.*?> PI values -->
177  <xsl:variable name="pi.linenumbering.everyNth">
178    <xsl:call-template name="dbhtml-attribute">
179      <xsl:with-param name="pis"
180                      select="$pi.context/processing-instruction('dbhtml')"/>
181      <xsl:with-param name="attribute" select="'linenumbering.everyNth'"/>
182    </xsl:call-template>
183  </xsl:variable>
184
185  <xsl:variable name="pi.linenumbering.separator">
186    <xsl:call-template name="dbhtml-attribute">
187      <xsl:with-param name="pis"
188                      select="$pi.context/processing-instruction('dbhtml')"/>
189      <xsl:with-param name="attribute" select="'linenumbering.separator'"/>
190    </xsl:call-template>
191  </xsl:variable>
192
193  <xsl:variable name="pi.linenumbering.width">
194    <xsl:call-template name="dbhtml-attribute">
195      <xsl:with-param name="pis"
196                      select="$pi.context/processing-instruction('dbhtml')"/>
197      <xsl:with-param name="attribute" select="'linenumbering.width'"/>
198    </xsl:call-template>
199  </xsl:variable>
200
201  <!-- Construct the 'in-context' values -->
202  <xsl:variable name="linenumbering.everyNth">
203    <xsl:choose>
204      <xsl:when test="$pi.linenumbering.everyNth != ''">
205        <xsl:value-of select="$pi.linenumbering.everyNth"/>
206      </xsl:when>
207      <xsl:otherwise>
208        <xsl:value-of select="$global.linenumbering.everyNth"/>
209      </xsl:otherwise>
210    </xsl:choose>
211  </xsl:variable>
212
213  <xsl:variable name="linenumbering.separator">
214    <xsl:choose>
215      <xsl:when test="$pi.linenumbering.separator != ''">
216        <xsl:value-of select="$pi.linenumbering.separator"/>
217      </xsl:when>
218      <xsl:otherwise>
219        <xsl:value-of select="$global.linenumbering.separator"/>
220      </xsl:otherwise>
221    </xsl:choose>
222  </xsl:variable>
223
224  <xsl:variable name="linenumbering.width">
225    <xsl:choose>
226      <xsl:when test="$pi.linenumbering.width != ''">
227        <xsl:value-of select="$pi.linenumbering.width"/>
228      </xsl:when>
229      <xsl:otherwise>
230        <xsl:value-of select="$global.linenumbering.width"/>
231      </xsl:otherwise>
232    </xsl:choose>
233  </xsl:variable>
234
235  <xsl:variable name="linenumbering.startinglinenumber">
236    <xsl:choose>
237      <xsl:when test="$pi.context/@startinglinenumber">
238        <xsl:value-of select="$pi.context/@startinglinenumber"/>
239      </xsl:when>
240      <xsl:when test="$pi.context/@continuation='continues'">
241        <xsl:variable name="lastLine">
242          <xsl:choose>
243            <xsl:when test="$pi.context/self::programlisting">
244              <xsl:call-template name="lastLineNumber">
245                <xsl:with-param name="listings"
246                     select="preceding::programlisting[@linenumbering='numbered']"/>
247              </xsl:call-template>
248            </xsl:when>
249            <xsl:when test="$pi.context/self::screen">
250              <xsl:call-template name="lastLineNumber">
251                <xsl:with-param name="listings"
252                     select="preceding::screen[@linenumbering='numbered']"/>
253              </xsl:call-template>
254            </xsl:when>
255            <xsl:when test="$pi.context/self::literallayout">
256              <xsl:call-template name="lastLineNumber">
257                <xsl:with-param name="listings"
258                     select="preceding::literallayout[@linenumbering='numbered']"/>
259              </xsl:call-template>
260            </xsl:when>
261            <xsl:when test="$pi.context/self::address">
262              <xsl:call-template name="lastLineNumber">
263                <xsl:with-param name="listings"
264                     select="preceding::address[@linenumbering='numbered']"/>
265              </xsl:call-template>
266            </xsl:when>
267            <xsl:when test="$pi.context/self::synopsis">
268              <xsl:call-template name="lastLineNumber">
269                <xsl:with-param name="listings"
270                     select="preceding::synopsis[@linenumbering='numbered']"/>
271              </xsl:call-template>
272            </xsl:when>
273            <xsl:otherwise>
274              <xsl:message>
275                <xsl:text>Unexpected verbatim environment: </xsl:text>
276                <xsl:value-of select="local-name($pi.context)"/>
277              </xsl:message>
278              <xsl:value-of select="0"/>
279            </xsl:otherwise>
280          </xsl:choose>
281        </xsl:variable>
282
283        <xsl:value-of select="$lastLine + 1"/>
284      </xsl:when>
285      <xsl:otherwise>1</xsl:otherwise>
286    </xsl:choose>
287  </xsl:variable>
288
289  <xsl:choose>
290    <xsl:when test="function-available('sverb:numberLines')">
291      <xsl:copy-of select="sverb:numberLines($rtf)"/>
292    </xsl:when>
293    <xsl:when test="function-available('xverb:numberLines')">
294      <xsl:copy-of select="xverb:numberLines($rtf)"/>
295    </xsl:when>
296    <xsl:otherwise>
297      <xsl:message terminate="yes">
298        <xsl:text>No numberLines function available.</xsl:text>
299      </xsl:message>
300    </xsl:otherwise>
301  </xsl:choose>
302</xsl:template>
303
304<xsl:template name="make-verbatim">
305  <xsl:param name="rtf"/>
306
307  <!-- I want to make this RTF verbatim. There are two possibilities: either
308       I have access to the exsl:node-set extension function and I can "do it right"
309       or I have to rely on CSS. -->
310
311  <xsl:choose>
312    <xsl:when test="function-available('exsl:node-set')">
313      <xsl:apply-templates select="exsl:node-set($rtf)" mode="make.verbatim.mode"/>
314    </xsl:when>
315    <xsl:otherwise>
316      <span style="white-space: pre;">
317        <xsl:copy-of select="$rtf"/>
318      </span>
319    </xsl:otherwise>
320  </xsl:choose>
321</xsl:template>
322
323<!-- ======================================================================== -->
324
325<xsl:template name="lastLineNumber">
326  <xsl:param name="listings"/>
327  <xsl:param name="number" select="0"/>
328
329  <xsl:variable name="lines">
330    <xsl:call-template name="countLines">
331      <xsl:with-param name="listing" select="string($listings[1])"/>
332    </xsl:call-template>
333  </xsl:variable>
334
335  <xsl:choose>
336    <xsl:when test="not($listings)">
337      <xsl:value-of select="$number"/>
338    </xsl:when>
339    <xsl:when test="$listings[1]/@startinglinenumber">
340      <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
341    </xsl:when>
342    <xsl:when test="$listings[1]/@continuation='continues'">
343      <xsl:call-template name="lastLineNumber">
344        <xsl:with-param name="listings" select="listings[position() &gt; 1]"/>
345        <xsl:with-param name="number" select="$number + $lines"/>
346      </xsl:call-template>
347    </xsl:when>
348    <xsl:otherwise>
349      <xsl:value-of select="$lines"/>
350    </xsl:otherwise>
351  </xsl:choose>
352</xsl:template>
353
354<xsl:template name="countLines">
355  <xsl:param name="listing"/>
356  <xsl:param name="count" select="1"/>
357
358  <xsl:choose>
359    <xsl:when test="contains($listing, '&#10;')">
360      <xsl:call-template name="countLines">
361        <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
362        <xsl:with-param name="count" select="$count + 1"/>
363      </xsl:call-template>
364    </xsl:when>
365    <xsl:otherwise>
366      <xsl:value-of select="$count"/>
367    </xsl:otherwise>
368  </xsl:choose>
369</xsl:template>
370
371</xsl:stylesheet>
372