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