toMathML.js 5.2 KB

1234567891011121314151617181920212223242526
  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. */MathJax.Hub.Register.LoadHook("[MathJax]/jax/element/mml/jax.js",function(){var d=MathJax.ElementJax.mml;SETTINGS=MathJax.Hub.config.menuSettings;d.mbase.Augment({toMathML:function(a){var b=this.inferred&&this.parent.inferRow;null==a&&(a="");var e=this.type,f=this.toMathMLattributes();if("mspace"===e)return a+"\x3c"+e+f+" /\x3e";for(var c=[],d=this.isToken?"":a+(b?"":" "),g=0,k=this.data.length;g<k;g++)this.data[g]?c.push(this.data[g].toMathML(d)):this.isToken||c.push(d+"\x3cmrow /\x3e");return this.isToken?a+"\x3c"+e+f+"\x3e"+c.join("")+"\x3c/"+e+"\x3e":b?c.join("\n"):0===c.length||1===c.length&&""===c[0]?a+"\x3c"+e+f+" /\x3e":a+"\x3c"+e+f+"\x3e\n"+c.join("\n")+"\n"+a+"\x3c/"+e+"\x3e"},toMathMLattributes:function(){var a=[],b=this.defaults,e=this.attrNames||d.copyAttributeNames,f=d.skipAttributes;"math"!==this.type||this.attr&&this.attr.xmlns||a.push('xmlns\x3d"http://www.w3.org/1998/Math/MathML"');if(!this.attrNames){"mstyle"===this.type&&(b=d.math.prototype.defaults);for(var c in b)if(!f[c]&&b.hasOwnProperty(c)){var h="open"===c||"close"===c;if(null!=this[c]&&(h||this[c]!==b[c])){var g=this[c];delete this[c];(h||this.Get(c)!==g)&&a.push(c+'\x3d"'+this.toMathMLattribute(g)+'"');this[c]=g}}}b=0;for(f=e.length;b<f;b++)"class"!==e[b]&&(g=(this.attr||{})[e[b]],null==g&&(g=this[e[b]]),null!=g&&a.push(e[b]+'\x3d"'+this.toMathMLquote(g)+'"'));this.toMathMLclass(a);return a.length?" "+a.join(" "):""},toMathMLclass:function(a){var b=[];this["class"]&&b.push(this["class"]);if(this.isa(d.TeXAtom)&&SETTINGS.texHints){var e="ORD OP BIN REL OPEN CLOSE PUNCT INNER VCENTER".split(" ")[this.texClass];e&&b.push("MJX-TeXAtom-"+e)}this.mathvariant&&this.toMathMLvariants[this.mathvariant]&&b.push("MJX"+this.mathvariant);this.arrow&&b.push("MJX-arrow");this.variantForm&&b.push("MJX-variant");b.length&&a.unshift('class\x3d"'+b.join(" ")+'"')},toMathMLattribute:function(a){return"string"===typeof a&&a.replace(/ /g,"").match(/^(([-+])?(\d+(\.\d*)?|\.\d+))mu$/)?RegExp.$2+(1/18*RegExp.$3).toFixed(3).replace(/\.?0+$/,"")+"em":this.toMathMLvariants[a]?this.toMathMLvariants[a]:this.toMathMLquote(a)},toMathMLvariants:{"-tex-caligraphic":d.VARIANT.SCRIPT,"-tex-caligraphic-bold":d.VARIANT.BOLDSCRIPT,"-tex-oldstyle":d.VARIANT.NORMAL,"-tex-oldstyle-bold":d.VARIANT.BOLD,"-tex-mathit":d.VARIANT.ITALIC},toMathMLquote:function(a){a=String(a).split("");for(var b=0,e=a.length;b<e;b++){var f=a[b].charCodeAt(0);if(55295>=f||57344<=f)32>f||126<f?a[b]="\x26#x"+f.toString(16).toUpperCase()+";":(f={"\x26":"\x26amp;","\x3c":"\x26lt;","\x3e":"\x26gt;",'"':"\x26quot;"}[a[b]])&&(a[b]=f);else if(b+1<e){var c=a[b+1].charCodeAt(0);a[b]="\x26#x"+((f-55296<<10)+(c-56320)+65536).toString(16).toUpperCase()+";";a[b+1]="";b++}else a[b]=""}return a.join("")}});d.msubsup.Augment({toMathML:function(a){var b=this.type;null==this.data[this.sup]&&(b="msub");null==this.data[this.sub]&&(b="msup");var e=this.toMathMLattributes();delete this.data[0].inferred;for(var f=[],c=0,d=this.data.length;c<d;c++)this.data[c]&&f.push(this.data[c].toMathML(a+" "));return a+"\x3c"+b+e+"\x3e\n"+f.join("\n")+"\n"+a+"\x3c/"+b+"\x3e"}});d.munderover.Augment({toMathML:function(a){var b=this.type;null==this.data[this.under]&&(b="mover");null==this.data[this.over]&&(b="munder");var e=this.toMathMLattributes();delete this.data[0].inferred;for(var f=[],c=0,d=this.data.length;c<d;c++)this.data[c]&&f.push(this.data[c].toMathML(a+" "));return a+"\x3c"+b+e+"\x3e\n"+f.join("\n")+"\n"+a+"\x3c/"+b+"\x3e"}});d.TeXAtom.Augment({toMathML:function(a){var b=this.toMathMLattributes();return b||1!==this.data[0].data.length?a+"\x3cmrow"+b+"\x3e\n"+this.data[0].toMathML(a+" ")+"\n"+a+"\x3c/mrow\x3e":a.substr(2)+this.data[0].toMathML(a)}});d.chars.Augment({toMathML:function(a){return(a||"")+this.toMathMLquote(this.toString())}});d.entity.Augment({toMathML:function(a){return(a||"")+"\x26"+this.data[0]+";\x3c!-- "+this.toString()+" --\x3e"}});d.xml.Augment({toMathML:function(a){return(a||"")+this.toString()}});MathJax.Hub.Register.StartupHook("TeX mathchoice Ready",function(){d.TeXmathchoice.Augment({toMathML:function(a){return this.Core().toMathML(a)}})});MathJax.Hub.Startup.signal.Post("toMathML Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/toMathML.js");