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 9043 2011-08-30 13:30:50Z dcramer $
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="block.content">
72 <xsl:when test="$shade.verbatim != 0">
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>
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"/>
86 <xsl:copy-of select="$content"/>
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>
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"/>
105 <xsl:copy-of select="$content"/>
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)"/>
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>
129 <xsl:copy-of select="$block.content"/>
130 </fo:block-container>
133 <xsl:copy-of select="$block.content"/>
139 <xsl:template match="literallayout">
140 <xsl:param name="suppress-numbers" select="'0'"/>
142 <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
144 <xsl:variable name="keep.together">
145 <xsl:call-template name="pi.dbfo_keep-together"/>
148 <xsl:variable name="content">
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/>
161 <xsl:apply-templates/>
167 <xsl:when test="@class='monospaced'">
169 <xsl:when test="$shade.verbatim != 0">
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>
176 <xsl:copy-of select="$content"/>
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>
186 <xsl:copy-of select="$content"/>
193 <xsl:when test="$shade.verbatim != 0">
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>
200 <xsl:copy-of select="$content"/>
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>
210 <xsl:copy-of select="$content"/>
218 <xsl:template match="address">
219 <xsl:param name="suppress-numbers" select="'0'"/>
221 <xsl:variable name="content">
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/>
234 <xsl:apply-templates/>
239 <fo:block xsl:use-attribute-sets="verbatim.properties">
240 <xsl:copy-of select="$content"/>
244 <xsl:template name="number.rtf.lines">
245 <xsl:param name="rtf" select="''"/>
246 <xsl:param name="pi.context" select="."/>
248 <!-- Save the global values -->
249 <xsl:variable name="global.linenumbering.everyNth"
250 select="$linenumbering.everyNth"/>
252 <xsl:variable name="global.linenumbering.separator"
253 select="$linenumbering.separator"/>
255 <xsl:variable name="global.linenumbering.width"
256 select="$linenumbering.width"/>
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"/>
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"/>
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"/>
277 <!-- Construct the 'in-context' values -->
278 <xsl:variable name="linenumbering.everyNth">
280 <xsl:when test="$pi.linenumbering.everyNth != ''">
281 <xsl:value-of select="$pi.linenumbering.everyNth"/>
284 <xsl:value-of select="$global.linenumbering.everyNth"/>
289 <xsl:variable name="linenumbering.separator">
291 <xsl:when test="$pi.linenumbering.separator != ''">
292 <xsl:value-of select="$pi.linenumbering.separator"/>
295 <xsl:value-of select="$global.linenumbering.separator"/>
300 <xsl:variable name="linenumbering.width">
302 <xsl:when test="$pi.linenumbering.width != ''">
303 <xsl:value-of select="$pi.linenumbering.width"/>
306 <xsl:value-of select="$global.linenumbering.width"/>
311 <xsl:variable name="linenumbering.startinglinenumber">
313 <xsl:when test="$pi.context/@startinglinenumber">
314 <xsl:value-of select="$pi.context/@startinglinenumber"/>
316 <xsl:when test="$pi.context/@continuation='continues'">
317 <xsl:variable name="lastLine">
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']"/>
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']"/>
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']"/>
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']"/>
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']"/>
351 <xsl:text>Unexpected verbatim environment: </xsl:text>
352 <xsl:value-of select="local-name(.)"/>
354 <xsl:value-of select="0"/>
359 <xsl:value-of select="$lastLine + 1"/>
361 <xsl:otherwise>1</xsl:otherwise>
366 <xsl:when test="function-available('sverb:numberLines')">
367 <xsl:copy-of select="sverb:numberLines($rtf)"/>
369 <xsl:when test="function-available('xverb:numberLines')">
370 <xsl:copy-of select="xverb:numberLines($rtf)"/>
373 <xsl:message terminate="yes">
374 <xsl:text>No numberLines function available.</xsl:text>
380 <!-- ======================================================================== -->
382 <xsl:template name="lastLineNumber">
383 <xsl:param name="listings"/>
384 <xsl:param name="number" select="0"/>
386 <xsl:variable name="lines">
387 <xsl:call-template name="countLines">
388 <xsl:with-param name="listing" select="string($listings[1])"/>
393 <xsl:when test="not($listings)">
394 <xsl:value-of select="$number"/>
396 <xsl:when test="$listings[1]/@startinglinenumber">
397 <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
399 <xsl:when test="$listings[1]/@continuation='continues'">
400 <xsl:call-template name="lastLineNumber">
401 <xsl:with-param name="listings" select="listings[position() > 1]"/>
402 <xsl:with-param name="number" select="$number + $lines"/>
406 <xsl:value-of select="$lines"/>
411 <xsl:template name="countLines">
412 <xsl:param name="listing"/>
413 <xsl:param name="count" select="1"/>
416 <xsl:when test="contains($listing, ' ')">
417 <xsl:call-template name="countLines">
418 <xsl:with-param name="listing" select="substring-after($listing, ' ')"/>
419 <xsl:with-param name="count" select="$count + 1"/>
423 <xsl:value-of select="$count"/>
428 <!-- ======================================================================== -->
430 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
432 <xsl:copy-of select="@*"/>
433 <xsl:apply-templates mode="hyphenate.verbatim"/>
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="."/>
443 <xsl:template name="hyphenate.verbatim.block">
444 <xsl:param name="content" select="''"/>
445 <xsl:param name="count" select="1"/>
447 <!-- recurse on lines first to keep recursion depth reasonable -->
449 <xsl:when test="contains($content, '
')">
450 <xsl:variable name="line" select="substring-before($content, '
')"/>
451 <xsl:variable name="rest" select="substring-after($content, '
')"/>
452 <xsl:call-template name="hyphenate.verbatim">
453 <xsl:with-param name="content" select="concat($line, '
')"/>
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"/>
461 <xsl:call-template name="hyphenate.verbatim">
462 <xsl:with-param name="content" select="$content"/>
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)"/>
474 <!-- Place soft-hyphen after space or non-breakable space. -->
475 <xsl:when test="$head = ' ' or $head = ' '">
476 <xsl:text> </xsl:text>
477 <xsl:text>­</xsl:text>
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>­</xsl:text>
485 <xsl:value-of select="$head"/>
488 <xsl:if test="$tail">
489 <xsl:call-template name="hyphenate.verbatim">
490 <xsl:with-param name="content" select="$tail"/>