2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:d="http://docbook.org/ns/docbook"
4 xmlns:fo="http://www.w3.org/1999/XSL/Format"
5 xmlns:sverb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Verbatim"
6 xmlns:xverb="com.nwalsh.xalan.Verbatim"
7 xmlns:lxslt="http://xml.apache.org/xslt"
8 xmlns:exsl="http://exslt.org/common"
9 exclude-result-prefixes="sverb xverb lxslt exsl d"
12 <!-- ********************************************************************
14 This file is part of the XSL DocBook Stylesheet distribution.
15 See ../README or http://cdn.docbook.org/release/xsl/current/ for
16 copyright and other information.
18 ******************************************************************** -->
20 <!-- XSLTHL highlighting is turned off by default. See highlighting/README
21 for instructions on how to turn on XSLTHL -->
22 <xsl:template name="apply-highlighting">
23 <xsl:apply-templates/>
26 <lxslt:component prefix="xverb"
27 functions="numberLines"/>
29 <xsl:template match="d:programlisting|d:screen|d:synopsis">
30 <xsl:param name="suppress-numbers" select="'0'"/>
31 <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
33 <xsl:variable name="content">
35 <xsl:when test="$suppress-numbers = '0'
36 and @linenumbering = 'numbered'
37 and $use.extensions != '0'
38 and $linenumbering.extension != '0'">
39 <xsl:call-template name="number.rtf.lines">
40 <xsl:with-param name="rtf">
42 <xsl:when test="$highlight.source != 0">
43 <xsl:call-template name="apply-highlighting"/>
46 <xsl:apply-templates/>
54 <xsl:when test="$highlight.source != 0">
55 <xsl:call-template name="apply-highlighting"/>
58 <xsl:apply-templates/>
65 <xsl:variable name="keep.together">
66 <xsl:call-template name="pi.dbfo_keep-together"/>
69 <xsl:variable name="font.size">
70 <xsl:call-template name="pi.dbfo_font-size">
71 <xsl:with-param name="node"
72 select="(self::*[processing-instruction('dbfo')]|
73 parent::*[processing-instruction('dbfo')])[last()]"/>
77 <xsl:variable name="block.content">
79 <xsl:when test="$shade.verbatim != 0">
81 xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
82 <xsl:if test="$keep.together != ''">
83 <xsl:attribute name="keep-together.within-column"><xsl:value-of
84 select="$keep.together"/></xsl:attribute>
86 <xsl:if test="$font.size != ''">
87 <xsl:attribute name="font-size"><xsl:value-of select="$font.size"/></xsl:attribute>
90 <xsl:when test="$hyphenate.verbatim != 0 and
91 $exsl.node.set.available != 0">
92 <xsl:apply-templates select="exsl:node-set($content)"
93 mode="hyphenate.verbatim"/>
96 <xsl:copy-of select="$content"/>
103 xsl:use-attribute-sets="monospace.verbatim.properties">
104 <xsl:if test="$keep.together != ''">
105 <xsl:attribute name="keep-together.within-column"><xsl:value-of
106 select="$keep.together"/></xsl:attribute>
109 <xsl:when test="$hyphenate.verbatim != 0 and
110 $exsl.node.set.available != 0">
111 <xsl:apply-templates select="exsl:node-set($content)"
112 mode="hyphenate.verbatim"/>
115 <xsl:copy-of select="$content"/>
124 <!-- Need a block-container for these features -->
125 <xsl:when test="@width != '' or
126 (self::d:programlisting and
127 starts-with($writing.mode, 'rl'))">
128 <fo:block-container start-indent="0pt" end-indent="0pt">
129 <xsl:if test="@width != ''">
130 <xsl:attribute name="width">
131 <xsl:value-of select="concat(@width, '*', $monospace.verbatim.font.width)"/>
134 <!-- All known program code is left-to-right -->
135 <xsl:if test="self::d:programlisting and
136 starts-with($writing.mode, 'rl')">
137 <xsl:attribute name="writing-mode">lr-tb</xsl:attribute>
139 <xsl:copy-of select="$block.content"/>
140 </fo:block-container>
143 <xsl:copy-of select="$block.content"/>
149 <xsl:template match="d:literallayout">
150 <xsl:param name="suppress-numbers" select="'0'"/>
152 <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
154 <xsl:variable name="keep.together">
155 <xsl:call-template name="pi.dbfo_keep-together"/>
158 <xsl:variable name="content">
160 <xsl:when test="$suppress-numbers = '0'
161 and @linenumbering = 'numbered'
162 and $use.extensions != '0'
163 and $linenumbering.extension != '0'">
164 <xsl:call-template name="number.rtf.lines">
165 <xsl:with-param name="rtf">
166 <xsl:apply-templates/>
171 <xsl:apply-templates/>
177 <xsl:when test="@class='monospaced'">
179 <xsl:when test="$shade.verbatim != 0">
181 xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
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"/>
191 xsl:use-attribute-sets="monospace.verbatim.properties">
192 <xsl:if test="$keep.together != ''">
193 <xsl:attribute name="keep-together.within-column"><xsl:value-of
194 select="$keep.together"/></xsl:attribute>
196 <xsl:copy-of select="$content"/>
203 <xsl:when test="$shade.verbatim != 0">
205 xsl:use-attribute-sets="verbatim.properties shade.verbatim.style">
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"/>
215 xsl:use-attribute-sets="verbatim.properties">
216 <xsl:if test="$keep.together != ''">
217 <xsl:attribute name="keep-together.within-column"><xsl:value-of
218 select="$keep.together"/></xsl:attribute>
220 <xsl:copy-of select="$content"/>
228 <xsl:template match="d:address">
229 <xsl:param name="suppress-numbers" select="'0'"/>
231 <xsl:variable name="content">
233 <xsl:when test="$suppress-numbers = '0'
234 and @linenumbering = 'numbered'
235 and $use.extensions != '0'
236 and $linenumbering.extension != '0'">
237 <xsl:call-template name="number.rtf.lines">
238 <xsl:with-param name="rtf">
239 <xsl:apply-templates/>
244 <xsl:apply-templates/>
249 <fo:block xsl:use-attribute-sets="verbatim.properties">
250 <xsl:copy-of select="$content"/>
254 <xsl:template name="number.rtf.lines">
255 <xsl:param name="rtf" select="''"/>
256 <xsl:param name="pi.context" select="."/>
258 <!-- Save the global values -->
259 <xsl:variable name="global.linenumbering.everyNth"
260 select="$linenumbering.everyNth"/>
262 <xsl:variable name="global.linenumbering.separator"
263 select="$linenumbering.separator"/>
265 <xsl:variable name="global.linenumbering.width"
266 select="$linenumbering.width"/>
268 <!-- Extract the <?dbfo linenumbering.*?> PI values -->
269 <xsl:variable name="pi.linenumbering.everyNth">
270 <xsl:call-template name="pi.dbfo_linenumbering.everyNth">
271 <xsl:with-param name="node" select="$pi.context"/>
275 <xsl:variable name="pi.linenumbering.separator">
276 <xsl:call-template name="pi.dbfo_linenumbering.separator">
277 <xsl:with-param name="node" select="$pi.context"/>
281 <xsl:variable name="pi.linenumbering.width">
282 <xsl:call-template name="pi.dbfo_linenumbering.width">
283 <xsl:with-param name="node" select="$pi.context"/>
287 <!-- Construct the 'in-context' values -->
288 <xsl:variable name="linenumbering.everyNth">
290 <xsl:when test="$pi.linenumbering.everyNth != ''">
291 <xsl:value-of select="$pi.linenumbering.everyNth"/>
294 <xsl:value-of select="$global.linenumbering.everyNth"/>
299 <xsl:variable name="linenumbering.separator">
301 <xsl:when test="$pi.linenumbering.separator != ''">
302 <xsl:value-of select="$pi.linenumbering.separator"/>
305 <xsl:value-of select="$global.linenumbering.separator"/>
310 <xsl:variable name="linenumbering.width">
312 <xsl:when test="$pi.linenumbering.width != ''">
313 <xsl:value-of select="$pi.linenumbering.width"/>
316 <xsl:value-of select="$global.linenumbering.width"/>
321 <xsl:variable name="linenumbering.startinglinenumber">
323 <xsl:when test="$pi.context/@startinglinenumber">
324 <xsl:value-of select="$pi.context/@startinglinenumber"/>
326 <xsl:when test="$pi.context/@continuation='continues'">
327 <xsl:variable name="lastLine">
329 <xsl:when test="$pi.context/self::d:programlisting">
330 <xsl:call-template name="lastLineNumber">
331 <xsl:with-param name="listings"
332 select="preceding::d:programlisting[@linenumbering='numbered']"/>
335 <xsl:when test="$pi.context/self::d:screen">
336 <xsl:call-template name="lastLineNumber">
337 <xsl:with-param name="listings"
338 select="preceding::d:screen[@linenumbering='numbered']"/>
341 <xsl:when test="$pi.context/self::d:literallayout">
342 <xsl:call-template name="lastLineNumber">
343 <xsl:with-param name="listings"
344 select="preceding::d:literallayout[@linenumbering='numbered']"/>
347 <xsl:when test="$pi.context/self::d:address">
348 <xsl:call-template name="lastLineNumber">
349 <xsl:with-param name="listings"
350 select="preceding::d:address[@linenumbering='numbered']"/>
353 <xsl:when test="$pi.context/self::d:synopsis">
354 <xsl:call-template name="lastLineNumber">
355 <xsl:with-param name="listings"
356 select="preceding::d:synopsis[@linenumbering='numbered']"/>
361 <xsl:text>Unexpected verbatim environment: </xsl:text>
362 <xsl:value-of select="local-name(.)"/>
364 <xsl:value-of select="0"/>
369 <xsl:value-of select="$lastLine + 1"/>
371 <xsl:otherwise>1</xsl:otherwise>
376 <xsl:when test="function-available('sverb:numberLines')">
377 <xsl:copy-of select="sverb:numberLines($rtf)"/>
379 <xsl:when test="function-available('xverb:numberLines')">
380 <xsl:copy-of select="xverb:numberLines($rtf)"/>
383 <xsl:message terminate="yes">
384 <xsl:text>No numberLines function available.</xsl:text>
390 <!-- ======================================================================== -->
392 <xsl:template name="lastLineNumber">
393 <xsl:param name="listings"/>
394 <xsl:param name="number" select="0"/>
396 <xsl:variable name="lines">
397 <xsl:call-template name="countLines">
398 <xsl:with-param name="listing" select="string($listings[1])"/>
403 <xsl:when test="not($listings)">
404 <xsl:value-of select="$number"/>
406 <xsl:when test="$listings[1]/@startinglinenumber">
407 <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
409 <xsl:when test="$listings[1]/@continuation='continues'">
410 <xsl:call-template name="lastLineNumber">
411 <xsl:with-param name="listings" select="$listings[position() > 1]"/>
412 <xsl:with-param name="number" select="$number + $lines"/>
416 <xsl:value-of select="$lines"/>
421 <xsl:template name="countLines">
422 <xsl:param name="listing"/>
423 <xsl:param name="count" select="1"/>
426 <xsl:when test="contains($listing, ' ')">
427 <xsl:call-template name="countLines">
428 <xsl:with-param name="listing" select="substring-after($listing, ' ')"/>
429 <xsl:with-param name="count" select="$count + 1"/>
433 <xsl:value-of select="$count"/>
438 <!-- ======================================================================== -->
440 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
442 <xsl:copy-of select="@*"/>
443 <xsl:apply-templates mode="hyphenate.verbatim"/>
447 <xsl:template match="text()" mode="hyphenate.verbatim" priority="2">
448 <xsl:call-template name="hyphenate.verbatim.block">
449 <xsl:with-param name="content" select="."/>
453 <xsl:template name="hyphenate.verbatim.block">
454 <xsl:param name="content" select="''"/>
455 <xsl:param name="count" select="1"/>
457 <!-- recurse on lines first to keep recursion depth reasonable -->
459 <xsl:when test="contains($content, '
')">
460 <xsl:variable name="line" select="substring-before($content, '
')"/>
461 <xsl:variable name="rest" select="substring-after($content, '
')"/>
462 <xsl:call-template name="hyphenate.verbatim">
463 <xsl:with-param name="content" select="concat($line, '
')"/>
465 <xsl:call-template name="hyphenate.verbatim.block">
466 <xsl:with-param name="content" select="$rest"/>
467 <xsl:with-param name="count" select="$count + 1"/>
471 <xsl:call-template name="hyphenate.verbatim">
472 <xsl:with-param name="content" select="$content"/>
479 <xsl:template name="hyphenate.verbatim">
480 <xsl:param name="content"/>
481 <xsl:variable name="head" select="substring($content, 1, 1)"/>
482 <xsl:variable name="tail" select="substring($content, 2)"/>
484 <!-- Place soft-hyphen after space or non-breakable space. -->
485 <xsl:when test="$head = ' ' or $head = ' '">
486 <xsl:text> </xsl:text>
487 <xsl:text>­</xsl:text>
489 <xsl:when test="$hyphenate.verbatim.characters != '' and
490 translate($head, $hyphenate.verbatim.characters, '') = '' and not($tail = '')">
491 <xsl:value-of select="$head"/>
492 <xsl:text>­</xsl:text>
495 <xsl:value-of select="$head"/>
498 <xsl:if test="$tail">
499 <xsl:call-template name="hyphenate.verbatim">
500 <xsl:with-param name="content" select="$tail"/>