toMathML.js 7.9 KB

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