Debian Codegraph @

xsl/verbatimco.xsl

raw
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:exsl="http://exslt.org/common"
4 exclude-result-prefixes="exsl"
5 version='1.0'>
6
7 <!--############################################################################
8 XSLT Stylesheet DocBook -> LaTeX
9 ############################################################################ -->
10
11 <xsl:param name="listings.xml"/>
12
13
14 <xsl:template name="line.pad">
15 <xsl:param name="count"/>
16 <xsl:if test="$count &gt; 0">
17 <xsl:text> </xsl:text>
18 <xsl:call-template name="line.pad">
19 <xsl:with-param name="count" select="$count - 1"/>
20 </xsl:call-template>
21 </xsl:if>
22 </xsl:template>
23
24
25 <!-- Recursively insert <co> elements in the listing text -->
26 <xsl:template name="insert.co">
27 <xsl:param name="text"/>
28 <xsl:param name="areas"/>
29 <xsl:param name="areaid" select="'1'"/>
30 <xsl:param name="line" select="'1'"/>
31 <xsl:param name="col" select="'1'"/>
32
33 <xsl:variable name="area" select="$areas[position()=$areaid]"/>
34 <xsl:variable name="arealine">
35 <xsl:choose>
36 <xsl:when test="contains($area/@coords, ' ')">
37 <xsl:value-of select="substring-before($area/@coords, ' ')"/>
38 </xsl:when>
39 <xsl:otherwise>
40 <xsl:value-of select="$area/@coords"/>
41 </xsl:otherwise>
42 </xsl:choose>
43 </xsl:variable>
44 <xsl:variable name="areacol" select="substring-after($area/@coords, ' ')"/>
45
46 <xsl:choose>
47 <xsl:when test="string-length($text)=0">
48 <!-- no more text in this listing -->
49 </xsl:when>
50 <xsl:when test="not($area)">
51 <!-- no more <co> to insert, copy the rest of the text -->
52 <xsl:value-of select="$text"/>
53 </xsl:when>
54 <xsl:when test="$arealine &gt; $line">
55 <!-- print the lines until we reach the <area> coord line -->
56 <!-- print the end of the current line -->
57 <xsl:value-of select="substring-before($text, '&#10;')"/>
58 <xsl:text>&#10;</xsl:text>
59 <xsl:call-template name="insert.co">
60 <xsl:with-param name="text" select="substring-after($text, '&#10;')"/>
61 <xsl:with-param name="line" select="$line+1"/>
62 <xsl:with-param name="areaid" select="$areaid"/>
63 <xsl:with-param name="areas" select="$areas"/>
64 </xsl:call-template>
65 </xsl:when>
66 <xsl:otherwise>
67 <!-- line length -->
68 <xsl:variable name="strlen">
69 <xsl:choose>
70 <xsl:when test="contains($text, '&#10;')">
71 <xsl:value-of select="string-length(substring-before($text, '&#10;'))"/>
72 </xsl:when>
73 <xsl:otherwise>
74 <xsl:value-of select="string-length($text)"/>
75 </xsl:otherwise>
76 </xsl:choose>
77 </xsl:variable>
78
79 <!-- relative column position -->
80 <xsl:variable name="colpos">
81 <xsl:value-of select="$areacol - $col"/>
82 </xsl:variable>
83
84 <!-- padding count -->
85 <xsl:variable name="padlen">
86 <xsl:choose>
87 <xsl:when test="$areacol!='' and ($colpos &gt; $strlen)">
88 <xsl:value-of select="$colpos - $strlen"/>
89 </xsl:when>
90 <xsl:otherwise>
91 <xsl:value-of select="'0'"/>
92 </xsl:otherwise>
93 </xsl:choose>
94 </xsl:variable>
95
96 <!-- number of characters before <co> to print -->
97 <xsl:variable name="count">
98 <xsl:choose>
99 <xsl:when test="$areacol='' or ($padlen &gt; 0)">
100 <xsl:value-of select="$strlen"/>
101 </xsl:when>
102 <xsl:otherwise>
103 <xsl:value-of select="$colpos"/>
104 </xsl:otherwise>
105 </xsl:choose>
106 </xsl:variable>
107
108 <!-- line part to insert before <co> -->
109 <xsl:if test="$count &gt; 0">
110 <xsl:value-of select="substring($text,1,$count)"/>
111 </xsl:if>
112
113 <!-- insert padding spaces -->
114 <xsl:if test="$padlen &gt; 0">
115 <xsl:call-template name="line.pad">
116 <xsl:with-param name="count" select="$padlen"/>
117 </xsl:call-template>
118 </xsl:if>
119
120 <!-- <co> to insert for this line -->
121 <co>
122 <xsl:for-each select="$area/@id|$area/@xml:id|$area/@linkends">
123 <xsl:copy/></xsl:for-each>
124 </co>
125 <!-- continue, for the next <area> if any -->
126 <xsl:call-template name="insert.co">
127 <xsl:with-param name="text" select="substring($text,$count+1)"/>
128 <xsl:with-param name="line" select="$line"/>
129 <xsl:with-param name="areaid" select="$areaid+1"/>
130 <xsl:with-param name="areas" select="$areas"/>
131 <xsl:with-param name="col" select="$col+$count"/>
132 </xsl:call-template>
133 </xsl:otherwise>
134 </xsl:choose>
135
136 </xsl:template>
137
138
139 <xsl:template match="programlisting|screen" mode="build.listing.co">
140 <xsl:param name="listing"/>
141 <xsl:param name="areas"/>
142
143 <xsl:variable name="content">
144 <xsl:apply-templates mode="latex.programlisting"/>
145 </xsl:variable>
146
147 <xsl:element name="{local-name($listing)}">
148 <!-- Inherit the original attributes -->
149 <xsl:for-each select="$listing/@*">
150 <xsl:copy/>
151 </xsl:for-each>
152
153 <xsl:call-template name="insert.co">
154 <xsl:with-param name="text" select="$content"/>
155 <xsl:with-param name="areas" select="$areas"/>
156 </xsl:call-template>
157 </xsl:element>
158
159 </xsl:template>
160
161
162 <xsl:template match="programlistingco|screenco">
163 <!-- Build a new listing with the embedded <co> -->
164 <xsl:variable name="newlisting">
165 <xsl:apply-templates select="programlisting|screen" mode="build.listing.co">
166 <xsl:with-param name="listing" select="programlisting|screen"/>
167 <xsl:with-param name="areas" select="areaspec//area"/>
168 </xsl:apply-templates>
169 </xsl:variable>
170 <xsl:apply-templates select="exsl:node-set($newlisting)/*">
171 <xsl:with-param name="rnode" select="calloutlist"/>
172 </xsl:apply-templates>
173 <xsl:apply-templates select="calloutlist">
174 <xsl:with-param name="rnode" select="exsl:node-set($newlisting)"/>
175 </xsl:apply-templates>
176 </xsl:template>
177
178 <xsl:template match="areaspec|areaset|area"/>
179
180
181 <!-- Process the external files referenced in a programlistingco or screenco
182 environment. Since in XSLT 1.0 you cannot directly include text files, the
183 workaround is to load the data from a listings database file. -->
184
185 <xsl:template match="textdata|
186 imagedata[@format='linespecific']|
187 inlinegraphic[@format='linespecific']"
188 mode="latex.programlisting">
189
190 <xsl:variable name="name" select="name(.)"/>
191 <xsl:variable name="lst.doc" select="document($listings.xml)"/>
192 <xsl:variable name="lst.id">
193 <xsl:apply-templates select="." mode="lstid"/>
194 </xsl:variable>
195 <xsl:variable name="lst.ext"
196 select="$lst.doc/listings/listing[@type=$name][@lstid=$lst.id]"/>
197
198 <xsl:if test="$output.quietly = 0">
199 <xsl:message><xsl:text>Load external file </xsl:text>
200 <xsl:value-of select="$name"/>
201 <xsl:text>[</xsl:text>
202 <xsl:value-of select="$lst.id"/>
203 <xsl:text>]</xsl:text>
204 <xsl:if test="not($lst.ext)">
205 <xsl:text>(failed)</xsl:text>
206 </xsl:if>
207 </xsl:message>
208 </xsl:if>
209
210 <xsl:apply-templates mode="latex.programlisting" select="$lst.ext"/>
211 </xsl:template>
212
213 <!-- The intermediate elements in external file handling just apply
214 the templates in the same mode. -->
215 <xsl:template match="textobject|listing"
216 mode="latex.programlisting">
217 <xsl:apply-templates mode="latex.programlisting"/>
218 </xsl:template>
219
220
221 <xsl:template match="textdata|
222 imagedata[@format='linespecific']|
223 inlinegraphic[@format='linespecific']" mode="lstid">
224 <xsl:number from="/"
225 level="any"
226 format="1"/>
227 </xsl:template>
228
229 </xsl:stylesheet>