]> git.stg.codes - stg.git/blob - doc/xslt/fo/verbatim.xsl
Removed some unused stuff from build script.
[stg.git] / doc / xslt / 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 8344 2009-03-16 06:35:43Z bobstayton $
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="block.content">
67     <xsl:choose>
68       <xsl:when test="$shade.verbatim != 0">
69         <fo:block id="{$id}"
70              xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
71           <xsl:choose>
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"/>
76             </xsl:when>
77             <xsl:otherwise>
78               <xsl:copy-of select="$content"/>
79             </xsl:otherwise>
80           </xsl:choose>
81         </fo:block>
82       </xsl:when>
83       <xsl:otherwise>
84         <fo:block id="{$id}"
85                   xsl:use-attribute-sets="monospace.verbatim.properties">
86           <xsl:choose>
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"/>
91             </xsl:when>
92             <xsl:otherwise>
93               <xsl:copy-of select="$content"/>
94             </xsl:otherwise>
95           </xsl:choose>
96         </fo:block>
97       </xsl:otherwise>
98     </xsl:choose>
99   </xsl:variable>
100
101   <xsl:choose>
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)"/>
110           </xsl:attribute>
111         </xsl:if>
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>
116         </xsl:if>
117         <xsl:copy-of select="$block.content"/>
118       </fo:block-container>
119     </xsl:when>
120     <xsl:otherwise>
121       <xsl:copy-of select="$block.content"/>
122     </xsl:otherwise>
123   </xsl:choose>
124
125 </xsl:template>
126
127 <xsl:template match="literallayout">
128   <xsl:param name="suppress-numbers" select="'0'"/>
129
130   <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
131
132   <xsl:variable name="content">
133     <xsl:choose>
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/>
141           </xsl:with-param>
142         </xsl:call-template>
143       </xsl:when>
144       <xsl:otherwise>
145         <xsl:apply-templates/>
146       </xsl:otherwise>
147     </xsl:choose>
148   </xsl:variable>
149
150   <xsl:choose>
151     <xsl:when test="@class='monospaced'">
152       <xsl:choose>
153         <xsl:when test="$shade.verbatim != 0">
154           <fo:block id="{$id}"
155                     xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style">
156
157             <xsl:copy-of select="$content"/>
158           </fo:block>
159         </xsl:when>
160         <xsl:otherwise>
161           <fo:block id="{$id}"
162                     xsl:use-attribute-sets="monospace.verbatim.properties">
163             <xsl:copy-of select="$content"/>
164           </fo:block>
165         </xsl:otherwise>
166       </xsl:choose>
167     </xsl:when>
168     <xsl:otherwise>
169       <xsl:choose>
170         <xsl:when test="$shade.verbatim != 0">
171           <fo:block id="{$id}"
172                     xsl:use-attribute-sets="verbatim.properties shade.verbatim.style">
173             <xsl:copy-of select="$content"/>
174           </fo:block>
175         </xsl:when>
176         <xsl:otherwise>
177           <fo:block id="{$id}"
178                     xsl:use-attribute-sets="verbatim.properties">
179             <xsl:copy-of select="$content"/>
180           </fo:block>
181         </xsl:otherwise>
182       </xsl:choose>
183     </xsl:otherwise>
184   </xsl:choose>
185 </xsl:template>
186
187 <xsl:template match="address">
188   <xsl:param name="suppress-numbers" select="'0'"/>
189
190   <xsl:variable name="content">
191     <xsl:choose>
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/>
199           </xsl:with-param>
200         </xsl:call-template>
201       </xsl:when>
202       <xsl:otherwise>
203         <xsl:apply-templates/>
204       </xsl:otherwise>
205     </xsl:choose>
206   </xsl:variable>
207
208   <fo:block xsl:use-attribute-sets="verbatim.properties">
209     <xsl:copy-of select="$content"/>
210   </fo:block>
211 </xsl:template>
212
213 <xsl:template name="number.rtf.lines">
214   <xsl:param name="rtf" select="''"/>
215   <xsl:param name="pi.context" select="."/>
216
217   <!-- Save the global values -->
218   <xsl:variable name="global.linenumbering.everyNth"
219                 select="$linenumbering.everyNth"/>
220
221   <xsl:variable name="global.linenumbering.separator"
222                 select="$linenumbering.separator"/>
223
224   <xsl:variable name="global.linenumbering.width"
225                 select="$linenumbering.width"/>
226
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"/>
231     </xsl:call-template>
232   </xsl:variable>
233
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"/>
237     </xsl:call-template>
238   </xsl:variable>
239
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"/>
243     </xsl:call-template>
244   </xsl:variable>
245
246   <!-- Construct the 'in-context' values -->
247   <xsl:variable name="linenumbering.everyNth">
248     <xsl:choose>
249       <xsl:when test="$pi.linenumbering.everyNth != ''">
250         <xsl:value-of select="$pi.linenumbering.everyNth"/>
251       </xsl:when>
252       <xsl:otherwise>
253         <xsl:value-of select="$global.linenumbering.everyNth"/>
254       </xsl:otherwise>
255     </xsl:choose>
256   </xsl:variable>
257
258   <xsl:variable name="linenumbering.separator">
259     <xsl:choose>
260       <xsl:when test="$pi.linenumbering.separator != ''">
261         <xsl:value-of select="$pi.linenumbering.separator"/>
262       </xsl:when>
263       <xsl:otherwise>
264         <xsl:value-of select="$global.linenumbering.separator"/>
265       </xsl:otherwise>
266     </xsl:choose>
267   </xsl:variable>
268
269   <xsl:variable name="linenumbering.width">
270     <xsl:choose>
271       <xsl:when test="$pi.linenumbering.width != ''">
272         <xsl:value-of select="$pi.linenumbering.width"/>
273       </xsl:when>
274       <xsl:otherwise>
275         <xsl:value-of select="$global.linenumbering.width"/>
276       </xsl:otherwise>
277     </xsl:choose>
278   </xsl:variable>
279
280   <xsl:variable name="linenumbering.startinglinenumber">
281     <xsl:choose>
282       <xsl:when test="$pi.context/@startinglinenumber">
283         <xsl:value-of select="$pi.context/@startinglinenumber"/>
284       </xsl:when>
285       <xsl:when test="$pi.context/@continuation='continues'">
286         <xsl:variable name="lastLine">
287           <xsl:choose>
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']"/>
292               </xsl:call-template>
293             </xsl:when>
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']"/>
298               </xsl:call-template>
299             </xsl:when>
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']"/>
304               </xsl:call-template>
305             </xsl:when>
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']"/>
310               </xsl:call-template>
311             </xsl:when>
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']"/>
316               </xsl:call-template>
317             </xsl:when>
318             <xsl:otherwise>
319               <xsl:message>
320                 <xsl:text>Unexpected verbatim environment: </xsl:text>
321                 <xsl:value-of select="local-name(.)"/>
322               </xsl:message>
323               <xsl:value-of select="0"/>
324             </xsl:otherwise>
325           </xsl:choose>
326         </xsl:variable>
327
328         <xsl:value-of select="$lastLine + 1"/>
329       </xsl:when>
330       <xsl:otherwise>1</xsl:otherwise>
331     </xsl:choose>
332   </xsl:variable>
333
334   <xsl:choose>
335     <xsl:when test="function-available('sverb:numberLines')">
336       <xsl:copy-of select="sverb:numberLines($rtf)"/>
337     </xsl:when>
338     <xsl:when test="function-available('xverb:numberLines')">
339       <xsl:copy-of select="xverb:numberLines($rtf)"/>
340     </xsl:when>
341     <xsl:otherwise>
342       <xsl:message terminate="yes">
343         <xsl:text>No numberLines function available.</xsl:text>
344       </xsl:message>
345     </xsl:otherwise>
346   </xsl:choose>
347 </xsl:template>
348
349 <!-- ======================================================================== -->
350
351 <xsl:template name="lastLineNumber">
352   <xsl:param name="listings"/>
353   <xsl:param name="number" select="0"/>
354
355   <xsl:variable name="lines">
356     <xsl:call-template name="countLines">
357       <xsl:with-param name="listing" select="string($listings[1])"/>
358     </xsl:call-template>
359   </xsl:variable>
360
361   <xsl:choose>
362     <xsl:when test="not($listings)">
363       <xsl:value-of select="$number"/>
364     </xsl:when>
365     <xsl:when test="$listings[1]/@startinglinenumber">
366       <xsl:value-of select="$number + $listings[1]/@startinglinenumber + $lines - 1"/>
367     </xsl:when>
368     <xsl:when test="$listings[1]/@continuation='continues'">
369       <xsl:call-template name="lastLineNumber">
370         <xsl:with-param name="listings" select="listings[position() &gt; 1]"/>
371         <xsl:with-param name="number" select="$number + $lines"/>
372       </xsl:call-template>
373     </xsl:when>
374     <xsl:otherwise>
375       <xsl:value-of select="$lines"/>
376     </xsl:otherwise>
377   </xsl:choose>
378 </xsl:template>
379
380 <xsl:template name="countLines">
381   <xsl:param name="listing"/>
382   <xsl:param name="count" select="1"/>
383
384   <xsl:choose>
385     <xsl:when test="contains($listing, '&#10;')">
386       <xsl:call-template name="countLines">
387         <xsl:with-param name="listing" select="substring-after($listing, '&#10;')"/>
388         <xsl:with-param name="count" select="$count + 1"/>
389       </xsl:call-template>
390     </xsl:when>
391     <xsl:otherwise>
392       <xsl:value-of select="$count"/>
393     </xsl:otherwise>
394   </xsl:choose>
395 </xsl:template>
396
397 <!-- ======================================================================== -->
398
399 <xsl:template match="node()|@*" mode="hyphenate.verbatim">
400   <xsl:copy>
401     <xsl:copy-of select="@*"/>
402     <xsl:apply-templates mode="hyphenate.verbatim"/>
403   </xsl:copy>
404 </xsl:template>
405
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="."/>
409   </xsl:call-template>
410 </xsl:template>
411
412 <xsl:template name="hyphenate.verbatim.block">
413   <xsl:param name="content" select="''"/>
414   <xsl:param name="count" select="1"/>
415
416   <!-- recurse on lines first to keep recursion depth reasonable -->
417   <xsl:choose>
418     <xsl:when test="contains($content, '&#xA;')">
419       <xsl:variable name="line" select="substring-before($content, '&#xA;')"/>
420       <xsl:variable name="rest" select="substring-after($content, '&#xA;')"/>
421       <xsl:call-template name="hyphenate.verbatim">
422         <xsl:with-param name="content" select="concat($line, '&#xA;')"/>
423       </xsl:call-template>
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"/>
427       </xsl:call-template>
428     </xsl:when>
429     <xsl:otherwise>
430       <xsl:call-template name="hyphenate.verbatim">
431         <xsl:with-param name="content" select="$content"/>
432       </xsl:call-template>
433     </xsl:otherwise>
434   </xsl:choose>
435   
436 </xsl:template>
437
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)"/>
442   <xsl:choose>
443     <!-- Place soft-hyphen after space or non-breakable space. -->
444     <xsl:when test="$head = ' ' or $head = '&#160;'">
445       <xsl:text>&#160;</xsl:text>
446       <xsl:text>&#x00AD;</xsl:text>
447     </xsl:when>
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>&#x00AD;</xsl:text>
452     </xsl:when>
453     <xsl:otherwise>
454       <xsl:value-of select="$head"/>
455     </xsl:otherwise>
456   </xsl:choose>
457   <xsl:if test="$tail">
458     <xsl:call-template name="hyphenate.verbatim">
459       <xsl:with-param name="content" select="$tail"/>
460     </xsl:call-template>
461   </xsl:if>
462 </xsl:template>
463
464
465 </xsl:stylesheet>