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