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