Debian Codegraph @

xsl/common/mklistings.xsl

raw
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version='1.0'>
5
6 <!-- ********************************************************************
7 $Id
8 ********************************************************************
9
10 This file is part of the XSL DocBook Stylesheet distribution.
11 See ../README or http://docbook.sf.net/release/xsl/current/ for
12 copyright and other information.
13
14 ******************************************************************** -->
15
16 <xsl:param name="textdata.default.encoding">iso-8859-1</xsl:param>
17 <xsl:param name="current.dir">.</xsl:param>
18
19 <!-- * This stylesheet is derivated from the insertfile.xsl stylesheet from
20 * the DocBook Project (thanks Michael). It makes a listing tree of the
21 * external text files referenced in the document, with each reference
22 * replaced with corresponding Xinclude instance.
23 *
24 * <textobject><textdata fileref="foo.txt">
25 * <imagedata format="linespecific" fileref="foo.txt">
26 * <inlinegraphic format="linespecific" fileref="foo.txt">
27 *
28 * Those become in the result tree:
29 *
30 * <listing type="textdata">
31 * <xi:include href="foo.txt" parse="text"/></listing>
32 * <listing type="imagedata">
33 * <xi:include href="foo.txt" parse="text"/></listing>
34 * <listing type="inlinegraphic">
35 * <xi:include href="foo.txt" parse="text"/></listing>
36 *
37 * It also works as expected with entityref in place of fileref,
38 * and copies over the value of the <textdata> encoding attribute (if
39 * found). It is basically intended as an alternative to using the
40 * DocBook XSLT Java insertfile() extension.
41 -->
42
43 <!-- ==================================================================== -->
44
45 <xsl:template name="string-replace" >
46 <xsl:param name="string"/>
47 <xsl:param name="from"/>
48 <xsl:param name="to"/>
49 <xsl:choose>
50 <xsl:when test="contains($string,$from)">
51 <xsl:value-of select="substring-before($string,$from)"/>
52 <xsl:value-of select="$to"/>
53 <xsl:call-template name="string-replace">
54 <xsl:with-param name="string" select="substring-after($string,$from)"/>
55 <xsl:with-param name="from" select="$from"/>
56 <xsl:with-param name="to" select="$to"/>
57 </xsl:call-template>
58 </xsl:when>
59 <xsl:otherwise>
60 <xsl:value-of select="$string"/>
61 </xsl:otherwise>
62 </xsl:choose>
63 </xsl:template>
64
65
66 <xsl:template name="get.external.filename">
67 <xsl:variable name="filename">
68 <xsl:choose>
69 <xsl:when test="@entityref">
70 <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
71 </xsl:when>
72 <xsl:otherwise>
73 <xsl:value-of select="@fileref"/>
74 </xsl:otherwise>
75 </xsl:choose>
76 </xsl:variable>
77 <xsl:variable name="absfilename">
78 <xsl:choose>
79 <xsl:when test="starts-with($filename, '/') or
80 contains($filename, ':')">
81 <!-- it has absolute path or a uri scheme so it is an absolute uri -->
82 <xsl:value-of select="$filename"/>
83 </xsl:when>
84 <xsl:otherwise>
85 <xsl:value-of select="$current.dir"/>
86 <xsl:text>/</xsl:text>
87 <xsl:value-of select="$filename"/>
88 </xsl:otherwise>
89 </xsl:choose>
90 </xsl:variable>
91 <!-- get a valid URI -->
92 <xsl:call-template name="string-replace">
93 <xsl:with-param name="string" select="$absfilename"/>
94 <xsl:with-param name="from" select="' '"/>
95 <xsl:with-param name="to" select="'%20'"/>
96 </xsl:call-template>
97 </xsl:template>
98
99 <!-- ==================================================================== -->
100
101 <xsl:template match="textdata|
102 imagedata[@format='linespecific']|
103 inlinegraphic[@format='linespecific']" mode="lstid">
104 <xsl:number from="/"
105 level="any"
106 format="1"/>
107 </xsl:template>
108
109 <!-- ==================================================================== -->
110
111 <xsl:template match="textobject[child::textdata[@entityref|@fileref]]">
112 <xsl:apply-templates select="textdata"/>
113 </xsl:template>
114
115 <xsl:template match="textdata[@entityref|@fileref]">
116 <xsl:variable name="filename">
117 <xsl:call-template name="get.external.filename"/>
118 </xsl:variable>
119 <xsl:variable name="encoding">
120 <xsl:choose>
121 <xsl:when test="@encoding">
122 <xsl:value-of select="@encoding"/>
123 </xsl:when>
124 <xsl:otherwise>
125 <xsl:value-of select="$textdata.default.encoding"/>
126 </xsl:otherwise>
127 </xsl:choose>
128 </xsl:variable>
129 <xsl:variable name="lstid">
130 <xsl:apply-templates select="." mode="lstid"/>
131 </xsl:variable>
132 <listing type="textdata" lstid="{$lstid}">
133 <xsl:element name="xi:include">
134 <xsl:attribute name="href">
135 <xsl:value-of select="$filename"/>
136 </xsl:attribute>
137 <xsl:attribute name="parse">text</xsl:attribute>
138 <xsl:attribute name="encoding">
139 <xsl:value-of select="$encoding"/>
140 </xsl:attribute>
141 </xsl:element>
142 </listing>
143 </xsl:template>
144
145 <!-- ==================================================================== -->
146
147 <xsl:template
148 match="inlinemediaobject
149 [child::imageobject
150 [child::imagedata
151 [@format = 'linespecific' and
152 (@entityref|@fileref)]]]">
153 <xsl:apply-templates select="imageobject/imagedata"/>
154 </xsl:template>
155
156 <xsl:template match="imagedata
157 [@format = 'linespecific' and
158 (@entityref|@fileref)]">
159 <xsl:variable name="filename">
160 <xsl:call-template name="get.external.filename"/>
161 </xsl:variable>
162 <xsl:variable name="lstid">
163 <xsl:apply-templates select="." mode="lstid"/>
164 </xsl:variable>
165 <listing type="imagedata" lstid="{$lstid}">
166 <xsl:element name="xi:include">
167 <xsl:attribute name="href">
168 <xsl:value-of select="$filename"/>
169 </xsl:attribute>
170 <xsl:attribute name="parse">text</xsl:attribute>
171 <xsl:attribute name="encoding">
172 <xsl:value-of select="$textdata.default.encoding"/>
173 </xsl:attribute>
174 </xsl:element>
175 </listing>
176 </xsl:template>
177
178 <!-- ==================================================================== -->
179
180 <xsl:template match="inlinegraphic
181 [@format = 'linespecific' and
182 (@entityref|@fileref)]">
183 <xsl:variable name="filename">
184 <xsl:call-template name="get.external.filename"/>
185 </xsl:variable>
186 <xsl:variable name="lstid">
187 <xsl:apply-templates select="." mode="lstid"/>
188 </xsl:variable>
189 <listing type="inlinegraphic" lstid="{$lstid}">
190 <xsl:element name="xi:include">
191 <xsl:attribute name="href">
192 <xsl:value-of select="$filename"/>
193 </xsl:attribute>
194 <xsl:attribute name="parse">text</xsl:attribute>
195 <xsl:attribute name="encoding">
196 <xsl:value-of select="$textdata.default.encoding"/>
197 </xsl:attribute>
198 </xsl:element>
199 </listing>
200 </xsl:template>
201
202 <!-- ==================================================================== -->
203
204 <!-- browse the tree -->
205 <xsl:template match="node() | @*">
206 <xsl:apply-templates select="@* | node()"/>
207 </xsl:template>
208
209 <xsl:template match="/">
210 <listings xmlns:xi="http://www.w3.org/2001/XInclude">
211 <xsl:apply-templates/>
212 </listings>
213 </xsl:template>
214
215 </xsl:stylesheet>