Debian Codegraph @

xslt/mallard/html/mal2html-media.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:tt="http://www.w3.org/ns/ttml"
20 xmlns:ttp="http://www.w3.org/ns/ttml#parameter"
21 xmlns="http://www.w3.org/1999/xhtml"
22 exclude-result-prefixes="mal tt ttp"
23 version="1.0">
24
25 <!--!!==========================================================================
26 Mallard to HTML - Media Elements
27 Handle Mallard media elements.
28 @revision[version=3.8 date=2012-11-05 status=final]
29
30 This stylesheet contains templates for handling Mallard `media` elements.
31 It also handles TTML inside block audio and video `media` elements.
32 -->
33
34
35 <!--**==========================================================================
36 mal2html.media.image
37 Output an `img` element for an image.
38 @revision[version=3.8 date=2012-11-05 status=final]
39
40 [xsl:params]
41 $node: The Mallard `media` element.
42 $inline: Whether $node is inline.
43
44 This template outputs an HTML `img` element for a Mallard `media` element
45 with the `type` attribute set to `"image"` (or with no `type` attribute).
46 If $node has fallback content, it is used for the `alt` attribute.
47 -->
48 <xsl:template name="mal2html.media.image">
49 <xsl:param name="node" select="."/>
50 <xsl:param name="inline" select="false()"/>
51 <img src="{$node/@src}">
52 <xsl:copy-of select="$node/@height"/>
53 <xsl:copy-of select="$node/@width"/>
54 <xsl:attribute name="class">
55 <xsl:text>media </xsl:text>
56 <xsl:choose>
57 <xsl:when test="$inline">
58 <xsl:text>media-inline</xsl:text>
59 </xsl:when>
60 <xsl:otherwise>
61 <xsl:text>media-block</xsl:text>
62 </xsl:otherwise>
63 </xsl:choose>
64 </xsl:attribute>
65 <xsl:attribute name="alt">
66 <xsl:choose>
67 <xsl:when test="$inline">
68 <xsl:variable name="alt">
69 <xsl:apply-templates mode="mal2html.inline.mode" select="$node/node()"/>
70 </xsl:variable>
71 <xsl:value-of select="normalize-space($alt)"/>
72 </xsl:when>
73 <xsl:otherwise>
74 <!-- FIXME: This is not ideal. Nested block container elements
75 will introduce lots of garbage whitespace. But then, XML
76 processors are supposed to normalize whitespace in attribute
77 values anyway. Ideally, we'd have a set of modes for text
78 conversion. That'd probably be best handled in a set of
79 mal2text stylesheets.
80 -->
81 <xsl:for-each select="$node/mal:*">
82 <xsl:if test="position() &gt; 1">
83 <xsl:text>&#x000A;</xsl:text>
84 </xsl:if>
85 <xsl:value-of select="string(.)"/>
86 </xsl:for-each>
87 </xsl:otherwise>
88 </xsl:choose>
89 </xsl:attribute>
90 </img>
91 </xsl:template>
92
93
94 <!--**==========================================================================
95 mal2html.media.video
96 Output a `video` element for a video.
97 @revision[version=3.8 date=2012-11-05 status=final]
98
99 [xsl:params]
100 $node: The Mallard `media` element.
101 $inline: Whether $node is inline.
102
103 This template outputs an HTML `video` element for a Mallard `media` element
104 with the `type` attribute set to `"video"`. It converts any fallback content
105 in the source to the `video` element's fallback content. If $inline is
106 `false`, this template will process TTML child content.
107
108 If $node has a child image `media` element with the `style` attribute set
109 to `"poster"`, that image will be used for the `poster` attribute on the
110 HTML `video` element.
111 -->
112 <xsl:template name="mal2html.media.video">
113 <xsl:param name="node" select="."/>
114 <xsl:param name="inline" select="false()"/>
115 <video src="{$node/@src}" preload="auto">
116 <xsl:attribute name="class">
117 <xsl:text>media </xsl:text>
118 <xsl:choose>
119 <xsl:when test="$inline">
120 <xsl:text>media-inline</xsl:text>
121 </xsl:when>
122 <xsl:otherwise>
123 <xsl:text>media-block</xsl:text>
124 </xsl:otherwise>
125 </xsl:choose>
126 </xsl:attribute>
127 <xsl:copy-of select="$node/@height"/>
128 <xsl:copy-of select="$node/@width"/>
129 <xsl:variable name="poster"
130 select="$node/mal:media[not(@type) or @type = 'image'][@style = 'poster']"/>
131 <xsl:if test="$poster">
132 <xsl:attribute name="poster">
133 <xsl:value-of select="$poster[1]/@src"/>
134 </xsl:attribute>
135 </xsl:if>
136 <xsl:choose>
137 <xsl:when test="$inline">
138 <xsl:apply-templates mode="mal2html.inline.mode" select="$node/node()"/>
139 </xsl:when>
140 <xsl:otherwise>
141 <xsl:apply-templates mode="mal2html.block.mode" select="$node/node()"/>
142 </xsl:otherwise>
143 </xsl:choose>
144 </video>
145 <xsl:call-template name="html.media.controls">
146 <xsl:with-param name="type" select="'video'"/>
147 <xsl:with-param name="ttml" select="not($inline) and tt:tt"/>
148 </xsl:call-template>
149 <xsl:if test="not($inline)">
150 <xsl:apply-templates mode="mal2html.ttml.mode" select="tt:tt[1]"/>
151 </xsl:if>
152 </xsl:template>
153
154
155 <!--**==========================================================================
156 mal2html.media.audio
157 Output an `audio` element for an audio object.
158 @revision[version=3.8 date=2012-11-05 status=final]
159
160 [xsl:params]
161 $node: The Mallard `media` element.
162 $inline: Whether $node is inline.
163
164 This template outputs an HTML `audio` element for a Mallard `media` element
165 with the `type` attribute set to `"audio"`. It converts any fallback content
166 in the source to the `audio` element's fallback content. If $inline is
167 `false`, this template will process TTML child content.
168 -->
169 <xsl:template name="mal2html.media.audio">
170 <xsl:param name="node" select="."/>
171 <xsl:param name="inline" select="false()"/>
172 <audio src="{$node/@src}" preload="auto">
173 <xsl:attribute name="class">
174 <xsl:text>media </xsl:text>
175 <xsl:choose>
176 <xsl:when test="$inline">
177 <xsl:text>media-inline</xsl:text>
178 </xsl:when>
179 <xsl:otherwise>
180 <xsl:text>media-block</xsl:text>
181 </xsl:otherwise>
182 </xsl:choose>
183 </xsl:attribute>
184 <xsl:choose>
185 <xsl:when test="$inline">
186 <xsl:apply-templates mode="mal2html.inline.mode" select="$node/node()"/>
187 </xsl:when>
188 <xsl:otherwise>
189 <xsl:apply-templates mode="mal2html.block.mode" select="$node/node()"/>
190 </xsl:otherwise>
191 </xsl:choose>
192 </audio>
193 <xsl:call-template name="html.media.controls">
194 <xsl:with-param name="type" select="'audio'"/>
195 <xsl:with-param name="ttml" select="not($inline) and tt:tt"/>
196 </xsl:call-template>
197 <xsl:if test="not($inline)">
198 <xsl:apply-templates mode="mal2html.ttml.mode" select="tt:tt[1]"/>
199 </xsl:if>
200 </xsl:template>
201
202
203 <!-- == TTML == -->
204
205 <xsl:template mode="mal2html.block.mode" match="tt:*"/>
206
207 <!--%%==========================================================================
208 mal2html.ttml.mode
209 Process TTML subtitles in a Mallard `media` element.
210 @revision[version=3.8 date=2012-11-05 status=final]
211
212 This mode is applied to TTML elements inside block audio and video elements.
213 It outputs HTML elements that are hidden by default and shown dynamically as
214 the parent media element is played.
215 -->
216 <xsl:template mode="mal2html.ttml.mode" match="tt:tt">
217 <xsl:variable name="profile">
218 <xsl:choose>
219 <xsl:when test="tt:head/ttp:profile">
220 <xsl:for-each select="tt:head/ttp:profile">
221 <xsl:call-template name="ttml.profile"/>
222 </xsl:for-each>
223 </xsl:when>
224 <xsl:when test="@profile">
225 <xsl:call-template name="ttml.profile.attr"/>
226 </xsl:when>
227 </xsl:choose>
228 </xsl:variable>
229 <xsl:choose>
230 <xsl:when test="not(contains($profile, 'false'))">
231 <xsl:variable name="if">
232 <xsl:call-template name="mal.if.test"/>
233 </xsl:variable>
234 <xsl:choose>
235 <xsl:when test="$if = 'true'">
236 <xsl:apply-templates mode="mal2html.ttml.mode" select="tt:body"/>
237 </xsl:when>
238 <xsl:when test="$if != ''">
239 <div class="if-if {$if}">
240 <xsl:apply-templates mode="mal2html.ttml.mode" select="tt:body"/>
241 </div>
242 </xsl:when>
243 </xsl:choose>
244 </xsl:when>
245 <xsl:otherwise>
246 <xsl:apply-templates mode="mal2html.ttml.mode"
247 select="following-sibling::tt:tt[1]"/>
248 </xsl:otherwise>
249 </xsl:choose>
250 </xsl:template>
251
252 <xsl:template mode="mal2html.ttml.mode" match="tt:body">
253 <div>
254 <xsl:call-template name="html.class.attr">
255 <xsl:with-param name="class">
256 <xsl:text>media-ttml</xsl:text>
257 <xsl:choose>
258 <xsl:when test="@xml:space">
259 <xsl:if test="@xml:space='preserve'">
260 <xsl:text> media-ttml-pre</xsl:text>
261 </xsl:if>
262 <xsl:if test="@xml:space='default'">
263 <xsl:text> media-ttml-nopre</xsl:text>
264 </xsl:if>
265 </xsl:when>
266 <xsl:otherwise>
267 <xsl:if test="../@xml:space='preserve'">
268 <xsl:text> media-ttml-pre</xsl:text>
269 </xsl:if>
270 <xsl:if test="../@xml:space='default'">
271 <xsl:text> media-ttml-nopre</xsl:text>
272 </xsl:if>
273 </xsl:otherwise>
274 </xsl:choose>
275 </xsl:with-param>
276 </xsl:call-template>
277 <xsl:call-template name="html.lang.attrs">
278 <xsl:with-param name="parent" select="../self::tt:tt"/>
279 </xsl:call-template>
280 <xsl:apply-templates mode="mal2html.ttml.mode" select="tt:div">
281 <xsl:with-param name="range">
282 <xsl:call-template name="ttml.time.range"/>
283 </xsl:with-param>
284 </xsl:apply-templates>
285 </div>
286 </xsl:template>
287
288 <xsl:template mode="mal2html.ttml.mode" match="tt:div">
289 <xsl:param name="range"/>
290 <xsl:variable name="beginend">
291 <xsl:call-template name="ttml.time.range">
292 <xsl:with-param name="range" select="$range"/>
293 </xsl:call-template>
294 </xsl:variable>
295 <div>
296 <xsl:call-template name="html.class.attr">
297 <xsl:with-param name="class">
298 <xsl:text>media-ttml-node media-ttml-div</xsl:text>
299 <xsl:if test="@xml:space='preserve'">
300 <xsl:text> media-ttml-pre</xsl:text>
301 </xsl:if>
302 <xsl:if test="@xml:space='default'">
303 <xsl:text> media-ttml-nopre</xsl:text>
304 </xsl:if>
305 </xsl:with-param>
306 </xsl:call-template>
307 <xsl:call-template name="html.lang.attrs"/>
308 <xsl:copy-of select="@xml:space"/>
309 <xsl:attribute name="data-ttml-begin">
310 <xsl:value-of select="substring-before($beginend, ',')"/>
311 </xsl:attribute>
312 <xsl:variable name="end" select="substring-after($beginend, ',')"/>
313 <xsl:if test="$end != '∞'">
314 <xsl:attribute name="data-ttml-end">
315 <xsl:value-of select="$end"/>
316 </xsl:attribute>
317 </xsl:if>
318 <xsl:for-each select="*">
319 <xsl:choose>
320 <xsl:when test="self::tt:*">
321 <xsl:apply-templates mode="mal2html.ttml.mode" select=".">
322 <xsl:with-param name="range" select="$beginend"/>
323 </xsl:apply-templates>
324 </xsl:when>
325 <xsl:otherwise>
326 <xsl:apply-templates mode="mal2html.block.mode" select="."/>
327 </xsl:otherwise>
328 </xsl:choose>
329 </xsl:for-each>
330 </div>
331 </xsl:template>
332
333 <xsl:template mode="mal2html.ttml.mode" match="tt:p">
334 <xsl:param name="range"/>
335 <xsl:variable name="beginend">
336 <xsl:call-template name="ttml.time.range">
337 <xsl:with-param name="range" select="$range"/>
338 </xsl:call-template>
339 </xsl:variable>
340 <div>
341 <xsl:call-template name="html.class.attr">
342 <xsl:with-param name="class">
343 <xsl:text>media-ttml-node media-ttml-p</xsl:text>
344 <xsl:if test="@xml:space='preserve'">
345 <xsl:text> media-ttml-pre</xsl:text>
346 </xsl:if>
347 <xsl:if test="@xml:space='default'">
348 <xsl:text> media-ttml-nopre</xsl:text>
349 </xsl:if>
350 </xsl:with-param>
351 </xsl:call-template>
352 <xsl:call-template name="html.lang.attrs"/>
353 <xsl:attribute name="data-ttml-begin">
354 <xsl:value-of select="substring-before($beginend, ',')"/>
355 </xsl:attribute>
356 <xsl:variable name="end" select="substring-after($beginend, ',')"/>
357 <xsl:if test="$end != '∞'">
358 <xsl:attribute name="data-ttml-end">
359 <xsl:value-of select="$end"/>
360 </xsl:attribute>
361 </xsl:if>
362 <xsl:apply-templates mode="mal2html.inline.mode">
363 <xsl:with-param name="range" select="$beginend"/>
364 </xsl:apply-templates>
365 </div>
366 </xsl:template>
367
368 <xsl:template mode="mal2html.inline.mode" match="tt:span">
369 <xsl:param name="range"/>
370 <xsl:variable name="beginend">
371 <xsl:call-template name="ttml.time.range">
372 <xsl:with-param name="range" select="$range"/>
373 </xsl:call-template>
374 </xsl:variable>
375 <span>
376 <xsl:call-template name="html.class.attr">
377 <xsl:with-param name="class">
378 <xsl:text>media-ttml-node media-ttml-span</xsl:text>
379 <xsl:if test="@xml:space='preserve'">
380 <xsl:text> media-ttml-pre</xsl:text>
381 </xsl:if>
382 <xsl:if test="@xml:space='default'">
383 <xsl:text> media-ttml-nopre</xsl:text>
384 </xsl:if>
385 </xsl:with-param>
386 </xsl:call-template>
387 <xsl:call-template name="html.lang.attrs"/>
388 <xsl:attribute name="data-ttml-begin">
389 <xsl:value-of select="substring-before($beginend, ',')"/>
390 </xsl:attribute>
391 <xsl:variable name="end" select="substring-after($beginend, ',')"/>
392 <xsl:if test="$end != '∞'">
393 <xsl:attribute name="data-ttml-end">
394 <xsl:value-of select="substring-after($beginend, ',')"/>
395 </xsl:attribute>
396 </xsl:if>
397 <xsl:apply-templates mode="mal2html.inline.mode"/>
398 </span>
399 </xsl:template>
400
401 <xsl:template mode="mal2html.inline.mode" match="tt:br">
402 <br>
403 <xsl:call-template name="html.class.attr">
404 <xsl:with-param name="class" select="'media-ttml-br'"/>
405 </xsl:call-template>
406 </br>
407 </xsl:template>
408
409
410
411 <!-- == Matched Templates == -->
412
413 <!-- = mal2html.block.mode % media = -->
414 <xsl:template mode="mal2html.block.mode" match="mal:media">
415 <xsl:variable name="if">
416 <xsl:call-template name="mal.if.test"/>
417 </xsl:variable>
418 <xsl:variable name="style" select="concat(' ', @style, ' ')"/>
419 <xsl:variable name="class">
420 <xsl:choose>
421 <xsl:when test="contains($style, ' floatstart ')">
422 <xsl:text> floatstart</xsl:text>
423 </xsl:when>
424 <xsl:when test="contains($style, ' floatend ')">
425 <xsl:text> floatend</xsl:text>
426 </xsl:when>
427 <xsl:when test="contains($style, ' floatleft ')">
428 <xsl:text> floatleft</xsl:text>
429 </xsl:when>
430 <xsl:when test="contains($style, ' floatright ')">
431 <xsl:text> floatright</xsl:text>
432 </xsl:when>
433 </xsl:choose>
434 </xsl:variable>
435 <xsl:choose>
436 <xsl:when test="$if = ''"/>
437 <xsl:when test="@type = 'image' or not(@type)">
438 <div>
439 <xsl:call-template name="html.class.attr">
440 <xsl:with-param name="class">
441 <xsl:text>media media-image</xsl:text>
442 <xsl:value-of select="$class"/>
443 <xsl:if test="$if != 'true'">
444 <xsl:text> if-if </xsl:text>
445 <xsl:value-of select="$if"/>
446 </xsl:if>
447 </xsl:with-param>
448 </xsl:call-template>
449 <div class="inner">
450 <xsl:call-template name="mal2html.media.image"/>
451 </div>
452 </div>
453 </xsl:when>
454 <xsl:when test="@type = 'video'">
455 <div>
456 <xsl:call-template name="html.class.attr">
457 <xsl:with-param name="class">
458 <xsl:text>media media-video</xsl:text>
459 <xsl:value-of select="$class"/>
460 <xsl:if test="$if != 'true'">
461 <xsl:text> if-if </xsl:text>
462 <xsl:value-of select="$if"/>
463 </xsl:if>
464 </xsl:with-param>
465 </xsl:call-template>
466 <div class="inner">
467 <xsl:call-template name="mal2html.media.video"/>
468 </div>
469 </div>
470 </xsl:when>
471 <xsl:when test="@type = 'audio'">
472 <div>
473 <xsl:call-template name="html.class.attr">
474 <xsl:with-param name="class">
475 <xsl:text>media media-audio</xsl:text>
476 <xsl:value-of select="$class"/>
477 <xsl:if test="$if != 'true'">
478 <xsl:text> if-if </xsl:text>
479 <xsl:value-of select="$if"/>
480 </xsl:if>
481 </xsl:with-param>
482 </xsl:call-template>
483 <div class="inner">
484 <xsl:call-template name="mal2html.media.audio"/>
485 </div>
486 </div>
487 </xsl:when>
488 <xsl:otherwise>
489 <xsl:for-each select="mal:*">
490 <xsl:apply-templates mode="mal2html.block.mode" select="."/>
491 </xsl:for-each>
492 </xsl:otherwise>
493 </xsl:choose>
494 </xsl:template>
495
496 <!-- = mal2html.inline.mode % media = -->
497 <xsl:template mode="mal2html.inline.mode" match="mal:media">
498 <xsl:call-template name="mal2html.span"/>
499 </xsl:template>
500
501 <xsl:template mode="mal2html.inline.content.mode" match="mal:media">
502 <xsl:choose>
503 <xsl:when test="@type = 'image' or not(@type)">
504 <span class="media media-image">
505 <xsl:call-template name="mal2html.media.image">
506 <xsl:with-param name="inline" select="true()"/>
507 </xsl:call-template>
508 </span>
509 </xsl:when>
510 <xsl:when test="@type = 'video'">
511 <span class="media media-video">
512 <xsl:call-template name="mal2html.media.video">
513 <xsl:with-param name="inline" select="true()"/>
514 </xsl:call-template>
515 </span>
516 </xsl:when>
517 <xsl:when test="@type = 'audio'">
518 <span class="media media-audio">
519 <xsl:call-template name="mal2html.media.audio">
520 <xsl:with-param name="inline" select="true()"/>
521 </xsl:call-template>
522 </span>
523 </xsl:when>
524 <xsl:otherwise>
525 <xsl:apply-templates mode="mal2html.inline.mode"/>
526 </xsl:otherwise>
527 </xsl:choose>
528 </xsl:template>
529
530 </xsl:stylesheet>