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"
11 <!-- ********************************************************************
12 $Id: verbatim.xsl 9804 2013-09-09 15:00:31Z tom_schr $
13 ********************************************************************
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.
19 ******************************************************************** -->
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/>
27 <lxslt:component prefix="xverb"
28 functions="numberLines"/>
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>
34 <xsl:variable name="content">
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">
43 <xsl:when test="$highlight.source != 0">
44 <xsl:call-template name="apply-highlighting"/>
47 <xsl:apply-templates/>
55 <xsl:when test="$highlight.source != 0">
56 <xsl:call-template name="apply-highlighting"/>
59 <xsl:apply-templates/>
66 <xsl:variable name="keep.together">
67 <xsl:call-template name="pi.dbfo_keep-together"/>
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()]"/>
78 <xsl:variable name="block.content">
80 <xsl:when test="$shade.verbatim != 0">
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>
87 <xsl:if test="$font.size != ''">
88 <xsl:attribute name="font-size"><xsl:value-of select="$font.size"/></xsl:attribute>
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"/>
97 <xsl:copy-of select="$content"/>
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>
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"/>
116 <xsl:copy-of select="$content"/>
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)"/>
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>
140 <xsl:copy-of select="$block.content"/>
141 </fo:block-container>
144 <xsl:copy-of select="$block.content"/>
150 <xsl:template match="literallayout">
151 <xsl:param name="suppress-numbers" select="'0'"/>
153 <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
155 <xsl:variable name="keep.together">
156 <xsl:call-template name="pi.dbfo_keep-together"/>
159 <xsl:variable name="content">
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/>
172 <xsl:apply-templates/>
178 <xsl:when test="@class='monospaced'">
180 <xsl:when test="$shade.verbatim != 0">
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>
187 <xsl:copy-of select="$content"/>
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>
197 <xsl:copy-of select="$content"/>
204 <xsl:when test="$shade.verbatim != 0">
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>
211 <xsl:copy-of select="$content"/>
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>
221 <xsl:copy-of select="$content"/>
229 <xsl:template match="address">
230 <xsl:param name="suppress-numbers" select="'0'"/>
232 <xsl:variable name="content">
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/>
245 <xsl:apply-templates/>
250 <fo:block xsl:use-attribute-sets="verbatim.properties">
251 <xsl:copy-of select="$content"/>
255 <xsl:template name="number.rtf.lines">
256 <xsl:param name="rtf" select="''"/>
257 <xsl:param name="pi.context" select="."/>
259 <!-- Save the global values -->
260 <xsl:variable name="global.linenumbering.everyNth"
261 select="$linenumbering.everyNth"/>
263 <xsl:variable name="global.linenumbering.separator"
264 select="$linenumbering.separator"/>
266 <xsl:variable name="global.linenumbering.width"
267 select="$linenumbering.width"/>
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"/>
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"/>
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"/>
288 <!-- Construct the 'in-context' values -->
289 <xsl:variable name="linenumbering.everyNth">
291 <xsl:when test="$pi.linenumbering.everyNth != ''">
292 <xsl:value-of select="$pi.linenumbering.everyNth"/>
295 <xsl:value-of select="$global.linenumbering.everyNth"/>
300 <xsl:variable name="linenumbering.separator">
302 <xsl:when test="$pi.linenumbering.separator != ''">
303 <xsl:value-of select="$pi.linenumbering.separator"/>
306 <xsl:value-of select="$global.linenumbering.separator"/>
311 <xsl:variable name="linenumbering.width">
313 <xsl:when test="$pi.linenumbering.width != ''">
314 <xsl:value-of select="$pi.linenumbering.width"/>
317 <xsl:value-of select="$global.linenumbering.width"/>
322 <xsl:variable name="linenumbering.startinglinenumber">
324 <xsl:when test="$pi.context/@startinglinenumber">
325 <xsl:value-of select="$pi.context/@startinglinenumber"/>
327 <xsl:when test="$pi.context/@continuation='continues'">
328 <xsl:variable name="lastLine">
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']"/>
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']"/>
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']"/>
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']"/>
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']"/>
362 <xsl:text>Unexpected verbatim environment: </xsl:text>
363 <xsl:value-of select="local-name(.)"/>
365 <xsl:value-of select="0"/>
370 <xsl:value-of select="$lastLine + 1"/>
372 <xsl:otherwise>1</xsl:otherwise>
377 <xsl:when test="function-available('sverb:numberLines')">
378 <xsl:copy-of select="sverb:numberLines($rtf)"/>
380 <xsl:when test="function-available('xverb:numberLines')">
381 <xsl:copy-of select="xverb:numberLines($rtf)"/>
384 <xsl:message terminate="yes">
385 <xsl:text>No numberLines function available.</xsl:text>
391 <!-- ======================================================================== -->
393 <xsl:template name="lastLineNumber">
394 <xsl:param name="listings"/>
395 <xsl:param name="number" select="0"/>
397 <xsl:variable name="lines">
398 <xsl:call-template name="countLines">
399 <xsl:with-param name="listing" select="string($listings[1])"/>
404 <xsl:when test="not($listings)">
405 <xsl:value-of select="$number"/>
407 <xsl:when test="$listings[1]/@startinglinenumber">
408 <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
410 <xsl:when test="$listings[1]/@continuation='continues'">
411 <xsl:call-template name="lastLineNumber">
412 <xsl:with-param name="listings" select="$listings[position() > 1]"/>
413 <xsl:with-param name="number" select="$number + $lines"/>
417 <xsl:value-of select="$lines"/>
422 <xsl:template name="countLines">
423 <xsl:param name="listing"/>
424 <xsl:param name="count" select="1"/>
427 <xsl:when test="contains($listing, ' ')">
428 <xsl:call-template name="countLines">
429 <xsl:with-param name="listing" select="substring-after($listing, ' ')"/>
430 <xsl:with-param name="count" select="$count + 1"/>
434 <xsl:value-of select="$count"/>
439 <!-- ======================================================================== -->
441 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
443 <xsl:copy-of select="@*"/>
444 <xsl:apply-templates mode="hyphenate.verbatim"/>
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="."/>
454 <xsl:template name="hyphenate.verbatim.block">
455 <xsl:param name="content" select="''"/>
456 <xsl:param name="count" select="1"/>
458 <!-- recurse on lines first to keep recursion depth reasonable -->
460 <xsl:when test="contains($content, '
')">
461 <xsl:variable name="line" select="substring-before($content, '
')"/>
462 <xsl:variable name="rest" select="substring-after($content, '
')"/>
463 <xsl:call-template name="hyphenate.verbatim">
464 <xsl:with-param name="content" select="concat($line, '
')"/>
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"/>
472 <xsl:call-template name="hyphenate.verbatim">
473 <xsl:with-param name="content" select="$content"/>
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)"/>
485 <!-- Place soft-hyphen after space or non-breakable space. -->
486 <xsl:when test="$head = ' ' or $head = ' '">
487 <xsl:text> </xsl:text>
488 <xsl:text>­</xsl:text>
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>­</xsl:text>
496 <xsl:value-of select="$head"/>
499 <xsl:if test="$tail">
500 <xsl:call-template name="hyphenate.verbatim">
501 <xsl:with-param name="content" select="$tail"/>