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