Debian Codegraph @

xslt/mallard/common/mal-if.xsl

raw
1 <?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
2 <!--
3 This program is free software; you can redistribute it and/or modify it under
4 the terms of the GNU Lesser General Public License as published by the Free
5 Software Foundation; either version 2 of the License, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 details.
12
13 You should have received a copy of the GNU Lesser General Public License
14 along with this program; see the file COPYING.LGPL. If not, see <http://www.gnu.org/licenses/>.
15 -->
16
17 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
18 xmlns:mal="http://projectmallard.org/1.0/"
19 xmlns:if="http://projectmallard.org/if/1.0/"
20 xmlns:str="http://exslt.org/strings"
21 exclude-result-prefixes="mal if str"
22 version="1.0">
23
24 <!--!!==========================================================================
25 Mallard Conditionals
26 Support for run-time conditional processing.
27 @revision[version=3.8 date=2012-11-05 status=final]
28
29 This stylesheet contains utilities for handling conditional processing
30 in Mallard documents.
31 -->
32
33
34 <!--@@==========================================================================
35 mal.if.target
36 The list of supported target tokens.
37 @revision[version=3.8 date=2012-11-05 status=final]
38
39 This parameter takes a space-separated list of tokens to enable for conditional
40 processing. It is used by the template {mal.if.test}. This parameter is meant
41 to hold tokens starting with `target:`. It should usually be set by the primary
42 importing stylesheet. The default in this stylesheet is just `''`, but it is
43 set to appropriate values by the importing HTML and XHTML stylesheets.
44 -->
45 <xsl:param name="mal.if.target" select="''"/>
46
47
48 <!--@@==========================================================================
49 mal.if.platform
50 The list of supported platform tokens.
51 @revision[version=3.8 date=2012-11-05 status=final]
52
53 This parameter takes a space-separated list of tokens to enable for conditional
54 processing. It is used by the template {mal.if.test}. This parameter is meant
55 to hold tokens starting with `platform:`. It should usually be set by hand or
56 by a customization stylesheet.
57 -->
58 <xsl:param name="mal.if.platform" select="''"/>
59
60
61 <!--@@==========================================================================
62 mal.if.features
63 The list of supported feature tokens.
64 @revision[version=3.8 date=2012-11-05 status=final]
65
66 This parameter takes a space-separated list of tokens to enable for conditional
67 processing. It is used by the template {mal.if.test}. This parameter is meant
68 to hold tokens that specify the capabilities of these stylesheets. It should
69 usually be set by the primary importing stylesheet. The default in this
70 stylesheet is just `'mallard:1.0'`, but it is set to a more complete set of
71 features by the importing HTML and XHTML stylesheets.
72 -->
73 <xsl:param name="mal.if.features" select="'
74 mallard:1.0
75 '"/>
76
77
78 <!--@@==========================================================================
79 mal.if.custom
80 A custom list of supported tokens.
81 @revision[version=3.8 date=2012-11-05 status=final]
82
83 This parameter takes a space-separated list of tokens to enable for conditional
84 processing. It is used by the template {mal.if.test}. This parameter is meant
85 to hold extra values enabled by hand or by a customization stylesheet.
86 -->
87 <xsl:param name="mal.if.custom" select="''"/>
88
89
90 <!--@@==========================================================================
91 mal.if.maybe
92 A list of tokens that may be true.
93 @revision[version=3.8 date=2012-11-05 status=final]
94
95 This parameter takes a space-separated list of tokens that may be true. The
96 template {mal.if.test} returns special flags when a condition may be true,
97 allowing conditional processing to be deferred (for example, to CSS media
98 selectors). This parameter should usually be set by the primary importing
99 stylesheet.
100 -->
101 <xsl:param name="mal.if.maybe" select="''"/>
102
103
104 <xsl:variable name="_mal.if.tokens" select="concat(' ', $mal.if.target,
105 ' ', $mal.if.platform,
106 ' ', $mal.if.features,
107 ' ', $mal.if.custom,
108 ' ')"/>
109 <xsl:variable name="_mal.if.maybe" select="concat(' ', $mal.if.maybe, ' ')"/>
110
111
112 <!--**==========================================================================
113 mal.if.test
114 Test if a condition is true.
115 @revision[version=3.8 date=2012-11-05 status=final]
116
117 [xsl:params]
118 $node: The element to check the condition for.
119 $test: The test expression.
120
121 This template evaluates the test expression $test, which is taken automatically
122 from the `test` or `if:test` attribute of $node. It splits the expression on
123 commas into subexpressions, then splits each subexpression on spaces into tokens.
124 A token is taken to be true if it's in one of the space-separated lists from
125 {mal.if.target}, {mal.if.platform}, {mal.if.features}, or {mal.if.custom}.
126 If the token starts with an exclamation point, the exclamation point is stripped
127 and the resulting truth value is negated.
128
129 A subexpression is true if all its tokens is true. The full test expression is
130 true if any subexpression is true. If the test expression is true, the literal
131 string `'true'` is returned. If the test expression is false, the empty
132 string is returned.
133
134 This template can handle "maybe" values: tokens that may or may not be true,
135 and whose truth values are deferred to post-transform time. A token is maybe
136 if it appears in the space-separated list {mal.if.maybe}. If a subexpression
137 contains a maybe value and does not contain any false tokens, its truth value
138 is a special string constructed from the maybe tokens and starting with the
139 string `if__`. If any subexpressions are maybe and none of the subexpressions
140 are false, the return value is a space-separated list of the maybe strings.
141
142 Maybe tokens usually must be handled specifically by the importing stylesheet.
143 It's usually not sufficient to just add a token to {mal.if.maybe}. This
144 template will handle any maybe token, but it does not handle the actual logic
145 of dynamically showing or hiding content based on those tokens.
146 -->
147 <xsl:template name="mal.if.test">
148 <xsl:param name="node" select="."/>
149 <xsl:param name="test" select="($node/self::if:if/@test |
150 $node/self::if:when/@test |
151 $node/@if:test)[1]"/>
152 <xsl:choose>
153 <xsl:when test="$test != ''">
154 <xsl:variable name="ret">
155 <xsl:for-each select="str:split($test, ',')">
156 <xsl:text> </xsl:text>
157 <xsl:variable name="subexpr">
158 <xsl:for-each select="str:tokenize(., ' ')">
159 <xsl:text> </xsl:text>
160 <xsl:choose>
161 <xsl:when test="starts-with(., '!')">
162 <xsl:variable name="tmp">
163 <xsl:call-template name="_mal.if.test.check_token">
164 <xsl:with-param name="node" select="$node"/>
165 <xsl:with-param name="token" select="substring(., 2)"/>
166 </xsl:call-template>
167 </xsl:variable>
168 <xsl:choose>
169 <xsl:when test="$tmp = '1'">
170 <xsl:text>0</xsl:text>
171 </xsl:when>
172 <xsl:when test="$tmp = '0'">
173 <xsl:text>1</xsl:text>
174 </xsl:when>
175 <xsl:otherwise>
176 <xsl:value-of select="concat('not-', $tmp)"/>
177 </xsl:otherwise>
178 </xsl:choose>
179 </xsl:when>
180 <xsl:otherwise>
181 <xsl:call-template name="_mal.if.test.check_token">
182 <xsl:with-param name="node" select="$node"/>
183 <xsl:with-param name="token" select="."/>
184 </xsl:call-template>
185 </xsl:otherwise>
186 </xsl:choose>
187 <xsl:text> </xsl:text>
188 </xsl:for-each>
189 </xsl:variable>
190 <xsl:choose>
191 <xsl:when test="contains($subexpr, ' 0 ')">
192 <xsl:text></xsl:text>
193 </xsl:when>
194 <xsl:otherwise>
195 <xsl:variable name="subcond">
196 <xsl:for-each select="str:tokenize($subexpr, ' ')[.!='1']">
197 <xsl:if test="position() != 1">
198 <xsl:text>__</xsl:text>
199 </xsl:if>
200 <xsl:value-of select="."/>
201 </xsl:for-each>
202 </xsl:variable>
203 <xsl:choose>
204 <xsl:when test="$subcond != ''">
205 <xsl:text>if__</xsl:text>
206 <xsl:value-of select="$subcond"/>
207 </xsl:when>
208 <xsl:otherwise>
209 <xsl:text>true</xsl:text>
210 </xsl:otherwise>
211 </xsl:choose>
212 </xsl:otherwise>
213 </xsl:choose>
214 </xsl:for-each>
215 <xsl:text> </xsl:text>
216 </xsl:variable>
217 <xsl:choose>
218 <xsl:when test="contains($ret, ' true ')">
219 <xsl:text>true</xsl:text>
220 </xsl:when>
221 <xsl:otherwise>
222 <xsl:for-each select="str:split($ret, ' ')[. != 'true']">
223 <xsl:value-of select="."/>
224 <xsl:text> </xsl:text>
225 </xsl:for-each>
226 </xsl:otherwise>
227 </xsl:choose>
228 </xsl:when>
229 <xsl:otherwise>
230 <xsl:text>true</xsl:text>
231 </xsl:otherwise>
232 </xsl:choose>
233 </xsl:template>
234
235 <!--#* _mal.if.test.check_token -->
236 <xsl:template name="_mal.if.test.check_token">
237 <xsl:param name="node"/>
238 <xsl:param name="token"/>
239 <xsl:choose>
240 <xsl:when test="$token = 'lang:C' or $token = 'lang:c'">
241 <xsl:choose>
242 <xsl:when test="not($node/ancestor-or-self::*/@xml:lang)">
243 <xsl:text>1</xsl:text>
244 </xsl:when>
245 <xsl:otherwise>
246 <xsl:text>0</xsl:text>
247 </xsl:otherwise>
248 </xsl:choose>
249 </xsl:when>
250 <xsl:when test="starts-with($token, 'lang:')">
251 <xsl:for-each select="$node">
252 <xsl:choose>
253 <xsl:when test="lang(substring($token, 6))">
254 <xsl:text>1</xsl:text>
255 </xsl:when>
256 <xsl:otherwise>
257 <xsl:text>0</xsl:text>
258 </xsl:otherwise>
259 </xsl:choose>
260 </xsl:for-each>
261 </xsl:when>
262 <xsl:when test="contains($_mal.if.tokens, concat(' ', $token, ' '))">
263 <xsl:text>1</xsl:text>
264 </xsl:when>
265 <xsl:when test="contains($_mal.if.maybe, concat(' ', $token, ' '))">
266 <xsl:call-template name="_mal.if.test.flatten_token">
267 <xsl:with-param name="token" select="$token"/>
268 </xsl:call-template>
269 </xsl:when>
270 <xsl:otherwise>
271 <xsl:text>0</xsl:text>
272 </xsl:otherwise>
273 </xsl:choose>
274 </xsl:template>
275
276 <!--#* _mal.if.test.flatten_token -->
277 <xsl:template name="_mal.if.test.flatten_token">
278 <xsl:param name="token"/>
279 <xsl:for-each select="str:split($token, '')">
280 <xsl:choose>
281 <xsl:when test="contains('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', .)">
282 <xsl:value-of select="."/>
283 </xsl:when>
284 <xsl:otherwise>
285 <xsl:text>-</xsl:text>
286 </xsl:otherwise>
287 </xsl:choose>
288 </xsl:for-each>
289 </xsl:template>
290
291 </xsl:stylesheet>