Imported Upstream version 1.79.2
[platform/upstream/docbook-xsl-stylesheets.git] / fo / verbatim.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:d="http://docbook.org/ns/docbook"
4                 xmlns:fo="http://www.w3.org/1999/XSL/Format"
5                 xmlns:sverb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Verbatim"
6                 xmlns:xverb="com.nwalsh.xalan.Verbatim"
7                 xmlns:lxslt="http://xml.apache.org/xslt"
8                 xmlns:exsl="http://exslt.org/common"
9                 exclude-result-prefixes="sverb xverb lxslt exsl d"
10                 version='1.0'>
11
12 <!-- ********************************************************************
13
14      This file is part of the XSL DocBook Stylesheet distribution.
15      See ../README or http://cdn.docbook.org/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="d:programlisting|d:screen|d:synopsis">
30   <xsl:param name="suppress-numbers" select="'0'"/>
31   <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
32
33   <xsl:variable name="content">
34     <xsl:choose>
35       <xsl:when test="$suppress-numbers = '0'
36                       and @linenumbering = 'numbered'
37                       and $use.extensions != '0'
38                       and $linenumbering.extension != '0'">
39         <xsl:call-template name="number.rtf.lines">
40           <xsl:with-param name="rtf">
41             <xsl:choose>
42               <xsl:when test="$highlight.source != 0">
43                 <xsl:call-template name="apply-highlighting"/>
44               </xsl:when>
45               <xsl:otherwise>
46                 <xsl:apply-templates/>
47               </xsl:otherwise>
48             </xsl:choose>
49           </xsl:with-param>
50         </xsl:call-template>
51       </xsl:when>
52       <xsl:otherwise>
53         <xsl:choose>
54           <xsl:when test="$highlight.source != 0">
55             <xsl:call-template name="apply-highlighting"/>
56           </xsl:when>
57           <xsl:otherwise>
58             <xsl:apply-templates/>
59           </xsl:otherwise>
60         </xsl:choose>
61       </xsl:otherwise>
62     </xsl:choose>
63   </xsl:variable>
64
65   <xsl:variable name="keep.together">
66     <xsl:call-template name="pi.dbfo_keep-together"/>
67   </xsl:variable>
68   
69   <xsl:variable name="font.size">
70     <xsl:call-template name="pi.dbfo_font-size">
71       <xsl:with-param name="node"
72         select="(self::*[processing-instruction('dbfo')]|
73                  parent::*[processing-instruction('dbfo')])[last()]"/>
74     </xsl:call-template>
75   </xsl:variable>
76
77   <xsl:variable name="block.content">
78     <xsl:choose>
79       <xsl:when test="$shade.verbatim != 0">
80         <fo:block id="{$id}"
81              xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
82          <xsl:if test="$keep.together != ''">
83            <xsl:attribute name="keep-together.within-column"><xsl:value-of
84                select="$keep.together"/></xsl:attribute>
85          </xsl:if>
86          <xsl:if test="$font.size != ''">
87            <xsl:attribute name="font-size"><xsl:value-of select="$font.size"/></xsl:attribute>
88          </xsl:if>
89           <xsl:choose>
90             <xsl:when test="$hyphenate.verbatim != 0 and 
91                             $exsl.node.set.available != 0">
92               <xsl:apply-templates select="exsl:node-set($content)" 
93                                    mode="hyphenate.verbatim"/>
94             </xsl:when>
95             <xsl:otherwise>
96               <xsl:copy-of select="$content"/>
97             </xsl:otherwise>
98           </xsl:choose>
99         </fo:block>
100       </xsl:when>
101       <xsl:otherwise>
102         <fo:block id="{$id}"
103                   xsl:use-attribute-sets="monospace.verbatim.properties">
104           <xsl:if test="$keep.together != ''">
105             <xsl:attribute name="keep-together.within-column"><xsl:value-of
106             select="$keep.together"/></xsl:attribute>
107           </xsl:if>
108           <xsl:choose>
109             <xsl:when test="$hyphenate.verbatim != 0 and 
110                             $exsl.node.set.available != 0">
111               <xsl:apply-templates select="exsl:node-set($content)" 
112                                    mode="hyphenate.verbatim"/>
113             </xsl:when>
114             <xsl:otherwise>
115               <xsl:copy-of select="$content"/>
116             </xsl:otherwise>
117           </xsl:choose>
118         </fo:block>
119       </xsl:otherwise>
120     </xsl:choose>
121   </xsl:variable>
122
123   <xsl:choose>
124     <!-- Need a block-container for these features -->
125     <xsl:when test="@width != '' or
126                     (self::d:programlisting and
127                     starts-with($writing.mode, 'rl'))">
128       <fo:block-container start-indent="0pt" end-indent="0pt">
129         <xsl:if test="@width != ''">
130           <xsl:attribute name="width">
131             <xsl:value-of select="concat(@width, '*', $monospace.verbatim.font.width)"/>
132           </xsl:attribute>
133         </xsl:if>
134         <!-- All known program code is left-to-right -->
135         <xsl:if test="self::d:programlisting and
136                       starts-with($writing.mode, 'rl')">
137           <xsl:attribute name="writing-mode">lr-tb</xsl:attribute>
138         </xsl:if>
139         <xsl:copy-of select="$block.content"/>
140       </fo:block-container>
141     </xsl:when>
142     <xsl:otherwise>
143       <xsl:copy-of select="$block.content"/>
144     </xsl:otherwise>
145   </xsl:choose>
146
147 </xsl:template>
148
149 <xsl:template match="d:literallayout">
150   <xsl:param name="suppress-numbers" select="'0'"/>
151
152   <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
153
154   <xsl:variable name="keep.together">
155     <xsl:call-template name="pi.dbfo_keep-together"/>
156   </xsl:variable>
157
158   <xsl:variable name="content">
159     <xsl:choose>
160       <xsl:when test="$suppress-numbers = '0'
161                       and @linenumbering = 'numbered'
162                       and $use.extensions != '0'
163                       and $linenumbering.extension != '0'">
164         <xsl:call-template name="number.rtf.lines">
165           <xsl:with-param name="rtf">
166             <xsl:apply-templates/>
167           </xsl:with-param>
168         </xsl:call-template>
169       </xsl:when>
170       <xsl:otherwise>
171         <xsl:apply-templates/>
172       </xsl:otherwise>
173     </xsl:choose>
174   </xsl:variable>
175
176   <xsl:choose>
177     <xsl:when test="@class='monospaced'">
178       <xsl:choose>
179         <xsl:when test="$shade.verbatim != 0">
180           <fo:block id="{$id}"
181                     xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
182             <xsl:if test="$keep.together != ''">
183               <xsl:attribute name="keep-together.within-column"><xsl:value-of
184               select="$keep.together"/></xsl:attribute>
185             </xsl:if>
186             <xsl:copy-of select="$content"/>
187           </fo:block>
188         </xsl:when>
189         <xsl:otherwise>
190           <fo:block id="{$id}"
191                     xsl:use-attribute-sets="monospace.verbatim.properties">
192             <xsl:if test="$keep.together != ''">
193               <xsl:attribute name="keep-together.within-column"><xsl:value-of
194               select="$keep.together"/></xsl:attribute>
195             </xsl:if>
196             <xsl:copy-of select="$content"/>
197           </fo:block>
198         </xsl:otherwise>
199       </xsl:choose>
200     </xsl:when>
201     <xsl:otherwise>
202       <xsl:choose>
203         <xsl:when test="$shade.verbatim != 0">
204           <fo:block id="{$id}"
205                     xsl:use-attribute-sets="verbatim.properties shade.verbatim.style">
206             <xsl:if test="$keep.together != ''">
207               <xsl:attribute name="keep-together.within-column"><xsl:value-of
208               select="$keep.together"/></xsl:attribute>
209             </xsl:if>
210             <xsl:copy-of select="$content"/>
211           </fo:block>
212         </xsl:when>
213         <xsl:otherwise>
214           <fo:block id="{$id}"
215                     xsl:use-attribute-sets="verbatim.properties">
216             <xsl:if test="$keep.together != ''">
217               <xsl:attribute name="keep-together.within-column"><xsl:value-of
218               select="$keep.together"/></xsl:attribute>
219             </xsl:if>
220             <xsl:copy-of select="$content"/>
221           </fo:block>
222         </xsl:otherwise>
223       </xsl:choose>
224     </xsl:otherwise>
225   </xsl:choose>
226 </xsl:template>
227
228 <xsl:template match="d:address">
229   <xsl:param name="suppress-numbers" select="'0'"/>
230
231   <xsl:variable name="content">
232     <xsl:choose>
233       <xsl:when test="$suppress-numbers = '0'
234                       and @linenumbering = 'numbered'
235                       and $use.extensions != '0'
236                       and $linenumbering.extension != '0'">
237         <xsl:call-template name="number.rtf.lines">
238           <xsl:with-param name="rtf">
239             <xsl:apply-templates/>
240           </xsl:with-param>
241         </xsl:call-template>
242       </xsl:when>
243       <xsl:otherwise>
244         <xsl:apply-templates/>
245       </xsl:otherwise>
246     </xsl:choose>
247   </xsl:variable>
248
249   <fo:block xsl:use-attribute-sets="verbatim.properties">
250     <xsl:copy-of select="$content"/>
251   </fo:block>
252 </xsl:template>
253
254 <xsl:template name="number.rtf.lines">
255   <xsl:param name="rtf" select="''"/>
256   <xsl:param name="pi.context" select="."/>
257
258   <!-- Save the global values -->
259   <xsl:variable name="global.linenumbering.everyNth"
260                 select="$linenumbering.everyNth"/>
261
262   <xsl:variable name="global.linenumbering.separator"
263                 select="$linenumbering.separator"/>
264
265   <xsl:variable name="global.linenumbering.width"
266                 select="$linenumbering.width"/>
267
268   <!-- Extract the <?dbfo linenumbering.*?> PI values -->
269   <xsl:variable name="pi.linenumbering.everyNth">
270     <xsl:call-template name="pi.dbfo_linenumbering.everyNth">
271       <xsl:with-param name="node" select="$pi.context"/>
272     </xsl:call-template>
273   </xsl:variable>
274
275   <xsl:variable name="pi.linenumbering.separator">
276     <xsl:call-template name="pi.dbfo_linenumbering.separator">
277       <xsl:with-param name="node" select="$pi.context"/>
278     </xsl:call-template>
279   </xsl:variable>
280
281   <xsl:variable name="pi.linenumbering.width">
282     <xsl:call-template name="pi.dbfo_linenumbering.width">
283       <xsl:with-param name="node" select="$pi.context"/>
284     </xsl:call-template>
285   </xsl:variable>
286
287   <!-- Construct the 'in-context' values -->
288   <xsl:variable name="linenumbering.everyNth">
289     <xsl:choose>
290       <xsl:when test="$pi.linenumbering.everyNth != ''">
291         <xsl:value-of select="$pi.linenumbering.everyNth"/>
292       </xsl:when>
293       <xsl:otherwise>
294         <xsl:value-of select="$global.linenumbering.everyNth"/>
295       </xsl:otherwise>
296     </xsl:choose>
297   </xsl:variable>
298
299   <xsl:variable name="linenumbering.separator">
300     <xsl:choose>
301       <xsl:when test="$pi.linenumbering.separator != ''">
302         <xsl:value-of select="$pi.linenumbering.separator"/>
303       </xsl:when>
304       <xsl:otherwise>
305         <xsl:value-of select="$global.linenumbering.separator"/>
306       </xsl:otherwise>
307     </xsl:choose>
308   </xsl:variable>
309
310   <xsl:variable name="linenumbering.width">
311     <xsl:choose>
312       <xsl:when test="$pi.linenumbering.width != ''">
313         <xsl:value-of select="$pi.linenumbering.width"/>
314       </xsl:when>
315       <xsl:otherwise>
316         <xsl:value-of select="$global.linenumbering.width"/>
317       </xsl:otherwise>
318     </xsl:choose>
319   </xsl:variable>
320
321   <xsl:variable name="linenumbering.startinglinenumber">
322     <xsl:choose>
323       <xsl:when test="$pi.context/@startinglinenumber">
324         <xsl:value-of select="$pi.context/@startinglinenumber"/>
325       </xsl:when>
326       <xsl:when test="$pi.context/@continuation='continues'">
327         <xsl:variable name="lastLine">
328           <xsl:choose>
329             <xsl:when test="$pi.context/self::d:programlisting">
330               <xsl:call-template name="lastLineNumber">
331                 <xsl:with-param name="listings"
332                      select="preceding::d:programlisting[@linenumbering='numbered']"/>
333               </xsl:call-template>
334             </xsl:when>
335             <xsl:when test="$pi.context/self::d:screen">
336               <xsl:call-template name="lastLineNumber">
337                 <xsl:with-param name="listings"
338                      select="preceding::d:screen[@linenumbering='numbered']"/>
339               </xsl:call-template>
340             </xsl:when>
341             <xsl:when test="$pi.context/self::d:literallayout">
342               <xsl:call-template name="lastLineNumber">
343                 <xsl:with-param name="listings"
344                      select="preceding::d:literallayout[@linenumbering='numbered']"/>
345               </xsl:call-template>
346             </xsl:when>
347             <xsl:when test="$pi.context/self::d:address">
348               <xsl:call-template name="lastLineNumber">
349                 <xsl:with-param name="listings"
350                      select="preceding::d:address[@linenumbering='numbered']"/>
351               </xsl:call-template>
352             </xsl:when>
353             <xsl:when test="$pi.context/self::d:synopsis">
354               <xsl:call-template name="lastLineNumber">
355                 <xsl:with-param name="listings"
356                      select="preceding::d:synopsis[@linenumbering='numbered']"/>
357               </xsl:call-template>
358             </xsl:when>
359             <xsl:otherwise>
360               <xsl:message>
361                 <xsl:text>Unexpected verbatim environment: </xsl:text>
362                 <xsl:value-of select="local-name(.)"/>
363               </xsl:message>
364               <xsl:value-of select="0"/>
365             </xsl:otherwise>
366           </xsl:choose>
367         </xsl:variable>
368
369         <xsl:value-of select="$lastLine + 1"/>
370       </xsl:when>
371       <xsl:otherwise>1</xsl:otherwise>
372     </xsl:choose>
373   </xsl:variable>
374
375   <xsl:choose>
376     <xsl:when test="function-available('sverb:numberLines')">
377       <xsl:copy-of select="sverb:numberLines($rtf)"/>
378     </xsl:when>
379     <xsl:when test="function-available('xverb:numberLines')">
380       <xsl:copy-of select="xverb:numberLines($rtf)"/>
381     </xsl:when>
382     <xsl:otherwise>
383       <xsl:message terminate="yes">
384         <xsl:text>No numberLines function available.</xsl:text>
385       </xsl:message>
386     </xsl:otherwise>
387   </xsl:choose>
388 </xsl:template>
389
390 <!-- ======================================================================== -->
391
392 <xsl:template name="lastLineNumber">
393   <xsl:param name="listings"/>
394   <xsl:param name="number" select="0"/>
395
396   <xsl:variable name="lines">
397     <xsl:call-template name="countLines">
398       <xsl:with-param name="listing" select="string($listings[1])"/>
399     </xsl:call-template>
400   </xsl:variable>
401
402   <xsl:choose>
403     <xsl:when test="not($listings)">
404       <xsl:value-of select="$number"/>
405     </xsl:when>
406     <xsl:when test="$listings[1]/@startinglinenumber">
407       <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
408     </xsl:when>
409     <xsl:when test="$listings[1]/@continuation='continues'">
410       <xsl:call-template name="lastLineNumber">
411         <xsl:with-param name="listings" select="$listings[position() &gt; 1]"/>
412         <xsl:with-param name="number" select="$number + $lines"/>
413       </xsl:call-template>
414     </xsl:when>
415     <xsl:otherwise>
416       <xsl:value-of select="$lines"/>
417     </xsl:otherwise>
418   </xsl:choose>
419 </xsl:template>
420
421 <xsl:template name="countLines">
422   <xsl:param name="listing"/>
423   <xsl:param name="count" select="1"/>
424
425   <xsl:choose>
426     <xsl:when test="contains($listing, '&#10;')">
427       <xsl:call-template name="countLines">
428         <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
429         <xsl:with-param name="count" select="$count + 1"/>
430       </xsl:call-template>
431     </xsl:when>
432     <xsl:otherwise>
433       <xsl:value-of select="$count"/>
434     </xsl:otherwise>
435   </xsl:choose>
436 </xsl:template>
437
438 <!-- ======================================================================== -->
439
440 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
441   <xsl:copy>
442     <xsl:copy-of select="@*"/>
443     <xsl:apply-templates mode="hyphenate.verbatim"/>
444   </xsl:copy>
445 </xsl:template>
446
447 <xsl:template match="text()" mode="hyphenate.verbatim" priority="2">
448   <xsl:call-template name="hyphenate.verbatim.block">
449     <xsl:with-param name="content" select="."/>
450   </xsl:call-template>
451 </xsl:template>
452
453 <xsl:template name="hyphenate.verbatim.block">
454   <xsl:param name="content" select="''"/>
455   <xsl:param name="count" select="1"/>
456
457   <!-- recurse on lines first to keep recursion depth reasonable -->
458   <xsl:choose>
459     <xsl:when test="contains($content, '&#xA;')">
460       <xsl:variable name="line" select="substring-before($content, '&#xA;')"/>
461       <xsl:variable name="rest" select="substring-after($content, '&#xA;')"/>
462       <xsl:call-template name="hyphenate.verbatim">
463         <xsl:with-param name="content" select="concat($line, '&#xA;')"/>
464       </xsl:call-template>
465       <xsl:call-template name="hyphenate.verbatim.block">
466         <xsl:with-param name="content" select="$rest"/>
467         <xsl:with-param name="count" select="$count + 1"/>
468       </xsl:call-template>
469     </xsl:when>
470     <xsl:otherwise>
471       <xsl:call-template name="hyphenate.verbatim">
472         <xsl:with-param name="content" select="$content"/>
473       </xsl:call-template>
474     </xsl:otherwise>
475   </xsl:choose>
476   
477 </xsl:template>
478
479 <xsl:template name="hyphenate.verbatim">
480   <xsl:param name="content"/>
481   <xsl:variable name="head" select="substring($content, 1, 1)"/>
482   <xsl:variable name="tail" select="substring($content, 2)"/>
483   <xsl:choose>
484     <!-- Place soft-hyphen after space or non-breakable space. -->
485     <xsl:when test="$head = ' ' or $head = '&#160;'">
486       <xsl:text>&#160;</xsl:text>
487       <xsl:text>&#x00AD;</xsl:text>
488     </xsl:when>
489     <xsl:when test="$hyphenate.verbatim.characters != '' and
490                     translate($head, $hyphenate.verbatim.characters, '') = '' and not($tail = '')">
491       <xsl:value-of select="$head"/>
492       <xsl:text>&#x00AD;</xsl:text>
493     </xsl:when>
494     <xsl:otherwise>
495       <xsl:value-of select="$head"/>
496     </xsl:otherwise>
497   </xsl:choose>
498   <xsl:if test="$tail">
499     <xsl:call-template name="hyphenate.verbatim">
500       <xsl:with-param name="content" select="$tail"/>
501     </xsl:call-template>
502   </xsl:if>
503 </xsl:template>
504
505
506 </xsl:stylesheet>