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 8344 2009-03-16 06:35:43Z bobstayton $
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="block.content">
68 <xsl:when test="$shade.verbatim != 0">
70 xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
72 <xsl:when test="$hyphenate.verbatim != 0 and
73 $exsl.node.set.available != 0">
74 <xsl:apply-templates select="exsl:node-set($content)"
75 mode="hyphenate.verbatim"/>
78 <xsl:copy-of select="$content"/>
85 xsl:use-attribute-sets="monospace.verbatim.properties">
87 <xsl:when test="$hyphenate.verbatim != 0 and
88 $exsl.node.set.available != 0">
89 <xsl:apply-templates select="exsl:node-set($content)"
90 mode="hyphenate.verbatim"/>
93 <xsl:copy-of select="$content"/>
102 <!-- Need a block-container for these features -->
103 <xsl:when test="@width != '' or
104 (self::programlisting and
105 starts-with($writing.mode, 'rl'))">
106 <fo:block-container start-indent="0pt" end-indent="0pt">
107 <xsl:if test="@width != ''">
108 <xsl:attribute name="width">
109 <xsl:value-of select="concat(@width, '*', $monospace.verbatim.font.width)"/>
112 <!-- All known program code is left-to-right -->
113 <xsl:if test="self::programlisting and
114 starts-with($writing.mode, 'rl')">
115 <xsl:attribute name="writing-mode">lr-tb</xsl:attribute>
117 <xsl:copy-of select="$block.content"/>
118 </fo:block-container>
121 <xsl:copy-of select="$block.content"/>
127 <xsl:template match="literallayout">
128 <xsl:param name="suppress-numbers" select="'0'"/>
130 <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
132 <xsl:variable name="content">
134 <xsl:when test="$suppress-numbers = '0'
135 and @linenumbering = 'numbered'
136 and $use.extensions != '0'
137 and $linenumbering.extension != '0'">
138 <xsl:call-template name="number.rtf.lines">
139 <xsl:with-param name="rtf">
140 <xsl:apply-templates/>
145 <xsl:apply-templates/>
151 <xsl:when test="@class='monospaced'">
153 <xsl:when test="$shade.verbatim != 0">
155 xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
157 <xsl:copy-of select="$content"/>
162 xsl:use-attribute-sets="monospace.verbatim.properties">
163 <xsl:copy-of select="$content"/>
170 <xsl:when test="$shade.verbatim != 0">
172 xsl:use-attribute-sets="verbatim.properties shade.verbatim.style">
173 <xsl:copy-of select="$content"/>
178 xsl:use-attribute-sets="verbatim.properties">
179 <xsl:copy-of select="$content"/>
187 <xsl:template match="address">
188 <xsl:param name="suppress-numbers" select="'0'"/>
190 <xsl:variable name="content">
192 <xsl:when test="$suppress-numbers = '0'
193 and @linenumbering = 'numbered'
194 and $use.extensions != '0'
195 and $linenumbering.extension != '0'">
196 <xsl:call-template name="number.rtf.lines">
197 <xsl:with-param name="rtf">
198 <xsl:apply-templates/>
203 <xsl:apply-templates/>
208 <fo:block xsl:use-attribute-sets="verbatim.properties">
209 <xsl:copy-of select="$content"/>
213 <xsl:template name="number.rtf.lines">
214 <xsl:param name="rtf" select="''"/>
215 <xsl:param name="pi.context" select="."/>
217 <!-- Save the global values -->
218 <xsl:variable name="global.linenumbering.everyNth"
219 select="$linenumbering.everyNth"/>
221 <xsl:variable name="global.linenumbering.separator"
222 select="$linenumbering.separator"/>
224 <xsl:variable name="global.linenumbering.width"
225 select="$linenumbering.width"/>
227 <!-- Extract the <?dbfo linenumbering.*?> PI values -->
228 <xsl:variable name="pi.linenumbering.everyNth">
229 <xsl:call-template name="pi.dbfo_linenumbering.everyNth">
230 <xsl:with-param name="node" select="$pi.context"/>
234 <xsl:variable name="pi.linenumbering.separator">
235 <xsl:call-template name="pi.dbfo_linenumbering.separator">
236 <xsl:with-param name="node" select="$pi.context"/>
240 <xsl:variable name="pi.linenumbering.width">
241 <xsl:call-template name="pi.dbfo_linenumbering.width">
242 <xsl:with-param name="node" select="$pi.context"/>
246 <!-- Construct the 'in-context' values -->
247 <xsl:variable name="linenumbering.everyNth">
249 <xsl:when test="$pi.linenumbering.everyNth != ''">
250 <xsl:value-of select="$pi.linenumbering.everyNth"/>
253 <xsl:value-of select="$global.linenumbering.everyNth"/>
258 <xsl:variable name="linenumbering.separator">
260 <xsl:when test="$pi.linenumbering.separator != ''">
261 <xsl:value-of select="$pi.linenumbering.separator"/>
264 <xsl:value-of select="$global.linenumbering.separator"/>
269 <xsl:variable name="linenumbering.width">
271 <xsl:when test="$pi.linenumbering.width != ''">
272 <xsl:value-of select="$pi.linenumbering.width"/>
275 <xsl:value-of select="$global.linenumbering.width"/>
280 <xsl:variable name="linenumbering.startinglinenumber">
282 <xsl:when test="$pi.context/@startinglinenumber">
283 <xsl:value-of select="$pi.context/@startinglinenumber"/>
285 <xsl:when test="$pi.context/@continuation='continues'">
286 <xsl:variable name="lastLine">
288 <xsl:when test="$pi.context/self::programlisting">
289 <xsl:call-template name="lastLineNumber">
290 <xsl:with-param name="listings"
291 select="preceding::programlisting[@linenumbering='numbered']"/>
294 <xsl:when test="$pi.context/self::screen">
295 <xsl:call-template name="lastLineNumber">
296 <xsl:with-param name="listings"
297 select="preceding::screen[@linenumbering='numbered']"/>
300 <xsl:when test="$pi.context/self::literallayout">
301 <xsl:call-template name="lastLineNumber">
302 <xsl:with-param name="listings"
303 select="preceding::literallayout[@linenumbering='numbered']"/>
306 <xsl:when test="$pi.context/self::address">
307 <xsl:call-template name="lastLineNumber">
308 <xsl:with-param name="listings"
309 select="preceding::address[@linenumbering='numbered']"/>
312 <xsl:when test="$pi.context/self::synopsis">
313 <xsl:call-template name="lastLineNumber">
314 <xsl:with-param name="listings"
315 select="preceding::synopsis[@linenumbering='numbered']"/>
320 <xsl:text>Unexpected verbatim environment: </xsl:text>
321 <xsl:value-of select="local-name(.)"/>
323 <xsl:value-of select="0"/>
328 <xsl:value-of select="$lastLine + 1"/>
330 <xsl:otherwise>1</xsl:otherwise>
335 <xsl:when test="function-available('sverb:numberLines')">
336 <xsl:copy-of select="sverb:numberLines($rtf)"/>
338 <xsl:when test="function-available('xverb:numberLines')">
339 <xsl:copy-of select="xverb:numberLines($rtf)"/>
342 <xsl:message terminate="yes">
343 <xsl:text>No numberLines function available.</xsl:text>
349 <!-- ======================================================================== -->
351 <xsl:template name="lastLineNumber">
352 <xsl:param name="listings"/>
353 <xsl:param name="number" select="0"/>
355 <xsl:variable name="lines">
356 <xsl:call-template name="countLines">
357 <xsl:with-param name="listing" select="string($listings[1])"/>
362 <xsl:when test="not($listings)">
363 <xsl:value-of select="$number"/>
365 <xsl:when test="$listings[1]/@startinglinenumber">
366 <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
368 <xsl:when test="$listings[1]/@continuation='continues'">
369 <xsl:call-template name="lastLineNumber">
370 <xsl:with-param name="listings" select="listings[position() > 1]"/>
371 <xsl:with-param name="number" select="$number + $lines"/>
375 <xsl:value-of select="$lines"/>
380 <xsl:template name="countLines">
381 <xsl:param name="listing"/>
382 <xsl:param name="count" select="1"/>
385 <xsl:when test="contains($listing, ' ')">
386 <xsl:call-template name="countLines">
387 <xsl:with-param name="listing" select="substring-after($listing, ' ')"/>
388 <xsl:with-param name="count" select="$count + 1"/>
392 <xsl:value-of select="$count"/>
397 <!-- ======================================================================== -->
399 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
401 <xsl:copy-of select="@*"/>
402 <xsl:apply-templates mode="hyphenate.verbatim"/>
406 <xsl:template match="text()" mode="hyphenate.verbatim" priority="2">
407 <xsl:call-template name="hyphenate.verbatim.block">
408 <xsl:with-param name="content" select="."/>
412 <xsl:template name="hyphenate.verbatim.block">
413 <xsl:param name="content" select="''"/>
414 <xsl:param name="count" select="1"/>
416 <!-- recurse on lines first to keep recursion depth reasonable -->
418 <xsl:when test="contains($content, '
')">
419 <xsl:variable name="line" select="substring-before($content, '
')"/>
420 <xsl:variable name="rest" select="substring-after($content, '
')"/>
421 <xsl:call-template name="hyphenate.verbatim">
422 <xsl:with-param name="content" select="concat($line, '
')"/>
424 <xsl:call-template name="hyphenate.verbatim.block">
425 <xsl:with-param name="content" select="$rest"/>
426 <xsl:with-param name="count" select="$count + 1"/>
430 <xsl:call-template name="hyphenate.verbatim">
431 <xsl:with-param name="content" select="$content"/>
438 <xsl:template name="hyphenate.verbatim">
439 <xsl:param name="content"/>
440 <xsl:variable name="head" select="substring($content, 1, 1)"/>
441 <xsl:variable name="tail" select="substring($content, 2)"/>
443 <!-- Place soft-hyphen after space or non-breakable space. -->
444 <xsl:when test="$head = ' ' or $head = ' '">
445 <xsl:text> </xsl:text>
446 <xsl:text>­</xsl:text>
448 <xsl:when test="$hyphenate.verbatim.characters != '' and
449 translate($head, $hyphenate.verbatim.characters, '') = '' and not($tail = '')">
450 <xsl:value-of select="$head"/>
451 <xsl:text>­</xsl:text>
454 <xsl:value-of select="$head"/>
457 <xsl:if test="$tail">
458 <xsl:call-template name="hyphenate.verbatim">
459 <xsl:with-param name="content" select="$tail"/>