Debian Codegraph @

xslt/docbook/html/db2html-math.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:db="http://docbook.org/ns/docbook"
19 xmlns:mml="http://www.w3.org/1998/Math/MathML"
20 xmlns:xlink="http://www.w3.org/1999/xlink"
21 xmlns="http://www.w3.org/1999/xhtml"
22 exclude-result-prefixes="db mml xlink"
23 version="1.0">
24
25 <!--!!==========================================================================
26 DocBook to HTML - MathML
27 Handle MathML in DocBook documents.
28 @revision[version=3.8 date=2012-11-13 status=final]
29
30 This stylesheet matches embedded MathML and processes it in {db2html.math.mode}.
31 The matched templates for the `mml:math` element automatically set the `display`
32 attribute based on whether the element is in block or inline context.
33 -->
34
35
36 <!--**==========================================================================
37 db2html.math.div
38 Output an HTML `div` element and block-level MathML.
39 @revision[version=3.8 date=2012-11-13 status=final]
40
41 [xsl:params]
42 $node: The `mml:math` element to render.
43
44 This template creates an HTML `div` element for a MathML `mml:math` element,
45 then outputs MathML content. It sets the `display` attribute on the output to
46 `"block"` and applies {db2html.math.mode} to the child content.
47 -->
48 <xsl:template name="db2html.math.div">
49 <xsl:param name="node" select="."/>
50 <div>
51 <xsl:call-template name="html.class.attr">
52 <xsl:with-param name="node" select="$node"/>
53 <xsl:with-param name="class" select="'math'"/>
54 </xsl:call-template>
55 <xsl:call-template name="html.lang.attrs">
56 <xsl:with-param name="node" select="$node"/>
57 </xsl:call-template>
58 <xsl:element name="math" namespace="{$html.mathml.namespace}">
59 <xsl:for-each select="$node/@*[name(.) != 'display']">
60 <xsl:copy-of select="."/>
61 </xsl:for-each>
62 <xsl:attribute name="display">
63 <xsl:value-of select="'block'"/>
64 </xsl:attribute>
65 <xsl:apply-templates mode="db2html.math.mode" select="$node/node()"/>
66 </xsl:element>
67 </div>
68 </xsl:template>
69
70
71 <!--**==========================================================================
72 db2html.math.span
73 Output an HTML `span` element and inline MathML.
74 @revision[version=3.8 date=2012-11-13 status=final]
75
76 [xsl:params]
77 $node: The `mml:math` element to render.
78
79 This template creates an HTML `span` element for a MathML `mml:math` element,
80 then outputs MathML content. It sets the `display` attribute on the output to
81 `"inline"` and applies {db2html.math.mode} to the child content.
82 -->
83 <xsl:template name="db2html.math.span">
84 <xsl:param name="node" select="."/>
85 <span class="math">
86 <xsl:call-template name="html.lang.attrs">
87 <xsl:with-param name="node" select="$node"/>
88 </xsl:call-template>
89 <xsl:element name="math" namespace="{$html.mathml.namespace}">
90 <xsl:for-each select="$node/@*[name(.) != 'display']">
91 <xsl:copy-of select="."/>
92 </xsl:for-each>
93 <xsl:attribute name="display">
94 <xsl:value-of select="'inline'"/>
95 </xsl:attribute>
96 <xsl:apply-templates mode="db2html.math.mode" select="$node/node()"/>
97 </xsl:element>
98 </span>
99 </xsl:template>
100
101
102 <!--%%==========================================================================
103 db2html.math.mode
104 Output MathML and handle Mallard extension.
105 @revision[version=3.8 date=2012-11-13 status=final]
106
107 This mode is used for processing MathML embedded into DocBook documents. For
108 most types of MathML content, it simply copies the input directly, except it
109 outputs the MathML in a way that allows the namespace to stripped for non-XML
110 output. It converts `xlink:href` attributes from MathML 2 to `href` attributes
111 for MathML 3.
112 -->
113 <xsl:template mode="db2html.math.mode" match="mml:*">
114 <xsl:element name="{local-name(.)}" namespace="{$html.mathml.namespace}">
115 <xsl:for-each select="@*[name(.) != 'href']">
116 <xsl:copy-of select="."/>
117 </xsl:for-each>
118 <xsl:choose>
119 <xsl:when test="@href">
120 <xsl:copy-of select="@href"/>
121 </xsl:when>
122 <xsl:when test="@xlink:href">
123 <xsl:attribute name="href">
124 <xsl:value-of select="@xlink:href"/>
125 </xsl:attribute>
126 </xsl:when>
127 </xsl:choose>
128 <xsl:apply-templates mode="db2html.math.mode"/>
129 </xsl:element>
130 </xsl:template>
131
132 <xsl:template mode="db2html.math.mode" match="text()">
133 <xsl:value-of select="."/>
134 </xsl:template>
135
136 <xsl:template mode="db2html.math.mode" match="*"/>
137
138
139 <!-- == Matched Templates == -->
140
141 <xsl:template match="equation/mml:math | informalequation/mml:math |
142 db:equation/mml:math | db:informalequation/mml:math">
143 <xsl:call-template name="db2html.math.div"/>
144 </xsl:template>
145
146 <xsl:template match="inlineequation/mml:math | db:inlineequation/mml:math">
147 <xsl:call-template name="db2html.math.span"/>
148 </xsl:template>
149
150 <xsl:template match="db:imagedata[@format='mathml']/mml:math">
151 <xsl:variable name="media" select="(ancestor::db:mediaobject[1] |
152 ancestor::db:inlinemediaobject[1]
153 )[last()]"/>
154 <xsl:choose>
155 <xsl:when test="local-name($media) = 'inlinemediaobject'">
156 <xsl:call-template name="db2html.math.span"/>
157 </xsl:when>
158 <xsl:otherwise>
159 <xsl:call-template name="db2html.math.div"/>
160 </xsl:otherwise>
161 </xsl:choose>
162 </xsl:template>
163
164 </xsl:stylesheet>