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