1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
// @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0
/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/*************************************************************
*
* MathJax/extensions/toMathML.js
*
* Implements a toMathML() method for the mml Element Jax that returns
* a MathML string from a given math expression.
*
* ---------------------------------------------------------------------
*
* Copyright (c) 2010-2020 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
MathJax.Hub.Register.LoadHook("[MathJax]/jax/element/mml/jax.js",function () {
var VERSION = "2.7.9";
var MML = MathJax.ElementJax.mml,
SETTINGS = MathJax.Hub.config.menuSettings;
MML.mbase.Augment({
toMathML: function (space) {
var inferred = (this.inferred && this.parent.inferRow);
if (space == null) {space = ""}
var tag = this.type, attr = this.toMathMLattributes();
if (tag === "mspace") {return space + "<"+tag+attr+" />"}
var data = [], SPACE = (this.isToken ? "" : space+(inferred ? "" : " "));
for (var i = 0, m = this.data.length; i < m; i++) {
if (this.data[i]) {data.push(this.data[i].toMathML(SPACE))}
else if (!this.isToken && !this.isChars) {data.push(SPACE+"<mrow />")}
}
if (this.isToken || this.isChars) {return space + "<"+tag+attr+">"+data.join("")+"</"+tag+">"}
if (inferred) {return data.join("\n")}
if (data.length === 0 || (data.length === 1 && data[0] === ""))
{return space + "<"+tag+attr+" />"}
return space + "<"+tag+attr+">\n"+data.join("\n")+"\n"+ space +"</"+tag+">";
},
toMathMLattributes: function () {
var defaults = (this.type === "mstyle" ? MML.math.prototype.defaults : this.defaults);
var names = (this.attrNames||MML.copyAttributeNames),
skip = MML.skipAttributes, copy = MML.copyAttributes;
var attr = [];
if (this.type === "math" && (!this.attr || !('xmlns' in this.attr)))
{attr.push('xmlns="http://www.w3.org/1998/Math/MathML"')}
if (!this.attrNames) {
for (var id in defaults) {if (!skip[id] && !copy[id] && defaults.hasOwnProperty(id)) {
if (this[id] != null && this[id] !== defaults[id]) {
if (this.Get(id,null,1) !== this[id])
attr.push(id+'="'+this.toMathMLattribute(this[id])+'"');
}
}}
}
for (var i = 0, m = names.length; i < m; i++) {
if (copy[names[i]] === 1 && !defaults.hasOwnProperty(names[i])) continue;
value = (this.attr||{})[names[i]]; if (value == null) {value = this[names[i]]}
if (value != null) {attr.push(names[i]+'="'+this.toMathMLquote(value)+'"')}
}
this.toMathMLclass(attr);
if (attr.length) {return " "+attr.join(" ")} else {return ""}
},
toMathMLclass: function (attr) {
var CLASS = []; if (this["class"]) {CLASS.push(this["class"])}
if (this.isa(MML.TeXAtom) && SETTINGS.texHints) {
var TEXCLASS = ["ORD","OP","BIN","REL","OPEN","CLOSE","PUNCT","INNER","VCENTER"][this.texClass];
if (TEXCLASS) {
CLASS.push("MJX-TeXAtom-"+TEXCLASS)
if (TEXCLASS === "OP" && !this.movablelimits) CLASS.push("MJX-fixedlimits");
}
}
if (this.mathvariant && this.toMathMLvariants[this.mathvariant])
{CLASS.push("MJX"+this.mathvariant)}
if (this.variantForm) {CLASS.push("MJX-variant")}
if (CLASS.length) {attr.unshift('class="'+this.toMathMLquote(CLASS.join(" "))+'"')}
},
toMathMLattribute: function (value) {
if (typeof(value) === "string" &&
value.replace(/ /g,"").match(/^(([-+])?(\d+(\.\d*)?|\.\d+))mu$/)) {
// FIXME: should take scriptlevel into account
return (RegExp.$2||"")+((1/18)*RegExp.$3).toFixed(3).replace(/\.?0+$/,"")+"em";
}
else if (this.toMathMLvariants[value]) {return this.toMathMLvariants[value]}
return this.toMathMLquote(value);
},
toMathMLvariants: {
"-tex-caligraphic": MML.VARIANT.SCRIPT,
"-tex-caligraphic-bold": MML.VARIANT.BOLDSCRIPT,
"-tex-oldstyle": MML.VARIANT.NORMAL,
"-tex-oldstyle-bold": MML.VARIANT.BOLD,
"-tex-mathit": MML.VARIANT.ITALIC
},
toMathMLquote: function (string) {
string = String(string).split("");
for (var i = 0, m = string.length; i < m; i++) {
var n = string[i].charCodeAt(0);
if (n <= 0xD7FF || 0xE000 <= n) {
// Code points U+0000 to U+D7FF and U+E000 to U+FFFF.
// They are directly represented by n.
if (n > 0x7E || (n < 0x20 && n !== 0x0A && n !== 0x0D && n !== 0x09)) {
string[i] = "&#x"+n.toString(16).toUpperCase()+";";
} else {
var c =
{'&':'&', '<':'<', '>':'>', '"':'"'}[string[i]];
if (c) {string[i] = c}
}
} else if (i+1 < m) {
// Code points U+10000 to U+10FFFF.
// n is the lead surrogate, let's read the trail surrogate.
var trailSurrogate = string[i+1].charCodeAt(0);
var codePoint = (((n-0xD800)<<10)+(trailSurrogate-0xDC00)+0x10000);
string[i] = "&#x"+codePoint.toString(16).toUpperCase()+";";
string[i+1] = "";
i++;
} else {
// n is a lead surrogate without corresponding trail surrogate:
// remove that character.
string[i] = "";
}
}
return string.join("");
}
});
//
// Override math.toMathML in order to add semantics tag
// for the input format, if the user requests that in the
// Show As menu.
//
MML.math.Augment({
toMathML: function (space,jax) {
var annotation;
if (space == null) {space = ""}
if (jax && jax.originalText && SETTINGS.semantics)
{annotation = MathJax.InputJax[jax.inputJax].annotationEncoding}
var nested = (this.data[0] && this.data[0].data.length > 1);
var tag = this.type, attr = this.toMathMLattributes();
var data = [], SPACE = space + (annotation ? " " + (nested ? " " : "") : "") + " ";
for (var i = 0, m = this.data.length; i < m; i++) {
if (this.data[i]) {data.push(this.data[i].toMathML(SPACE))}
else {data.push(SPACE+"<mrow />")}
}
if (data.length === 0 || (data.length === 1 && data[0] === "")) {
if (!annotation) {return "<"+tag+attr+" />"}
data.push(SPACE+"<mrow />");
}
if (annotation) {
if (nested) {data.unshift(space+" <mrow>"); data.push(space+" </mrow>")}
data.unshift(space+" <semantics>");
var xmlEscapedTex = jax.originalText.replace(/[&<>]/g, function(item) {
return { '>': '>', '<': '<','&': '&' }[item]
});
data.push(space+' <annotation encoding="'+this.toMathMLquote(annotation)+'">'+xmlEscapedTex+"</annotation>");
data.push(space+" </semantics>");
}
return space+"<"+tag+attr+">\n"+data.join("\n")+"\n"+space+"</"+tag+">";
}
});
MML.msubsup.Augment({
toMathML: function (space) {
var tag = this.type;
if (this.data[this.sup] == null) {tag = "msub"}
if (this.data[this.sub] == null) {tag = "msup"}
var attr = this.toMathMLattributes();
delete this.data[0].inferred;
var data = [];
for (var i = 0, m = this.data.length; i < m; i++)
{if (this.data[i]) {data.push(this.data[i].toMathML(space+" "))}}
return space + "<"+tag+attr+">\n" + data.join("\n") + "\n" + space + "</"+tag+">";
}
});
MML.munderover.Augment({
toMathML: function (space) {
var tag = this.type;
var base = this.data[this.base];
if (base && base.isa(MML.TeXAtom) && base.movablelimits && !base.Get("displaystyle")) {
type = "msubsup";
if (this.data[this.under] == null) {tag = "msup"}
if (this.data[this.over] == null) {tag = "msub"}
} else {
if (this.data[this.under] == null) {tag = "mover"}
if (this.data[this.over] == null) {tag = "munder"}
}
var attr = this.toMathMLattributes();
delete this.data[0].inferred;
var data = [];
for (var i = 0, m = this.data.length; i < m; i++)
{if (this.data[i]) {data.push(this.data[i].toMathML(space+" "))}}
return space + "<"+tag+attr+">\n" + data.join("\n") + "\n" + space + "</"+tag+">";
}
});
MML.TeXAtom.Augment({
toMathML: function (space) {
// FIXME: Handle spacing using mpadded?
var attr = this.toMathMLattributes();
if (!attr && this.data[0].data.length === 1) {return space.substr(2) + this.data[0].toMathML(space)}
return space+"<mrow"+attr+">\n" + this.data[0].toMathML(space+" ")+"\n"+space+"</mrow>";
}
});
MML.chars.Augment({
toMathML: function (space) {return (space||"") + this.toMathMLquote(this.toString())}
});
MML.entity.Augment({
toMathML: function (space) {return (space||"") + "&"+this.toMathMLquote(this.data[0])+";<!-- "+this.toString()+" -->"}
});
MML.xml.Augment({
toMathML: function (space) {return (space||"") + this.toString()}
});
MathJax.Hub.Register.StartupHook("TeX mathchoice Ready",function () {
MML.TeXmathchoice.Augment({
toMathML: function (space) {return this.Core().toMathML(space)}
});
});
MathJax.Hub.Startup.signal.Post("toMathML Ready");
});
MathJax.Ajax.loadComplete("[MathJax]/extensions/toMathML.js");
// @license-end
|