diff --git a/unpacked/jax/input/AsciiMath/jax.js b/unpacked/jax/input/AsciiMath/jax.js index 46b69c052..e62225e80 100644 --- a/unpacked/jax/input/AsciiMath/jax.js +++ b/unpacked/jax/input/AsciiMath/jax.js @@ -11,8 +11,11 @@ * Originally adapted for MathJax by David Lippman. * Additional work done by Davide P. Cervone. * + * The current development repository for AsciiMathML is + * https://github.com/mathjax/asciimathml + * * A portion of this file is taken from - * ASCIIMathML.js Version 1.4.7 Aug 30, 2005, (c) Peter Jipsen http://www.chapman.edu/~jipsen + * ASCIIMathML.js Version 2.2 Mar 3, 2014, (c) Peter Jipsen http://www.chapman.edu/~jipsen * and is used by permission of Peter Jipsen, who has agreed to allow us to * release it under the Apache2 license (see below). That portion is indicated * via comments. @@ -81,6 +84,9 @@ old.nextSibling = old.parent = null; return old; }, + hasChildNodes: function (node) { + return (this.childNodes.length>0); + }, toString: function () {return "{"+this.childNodes.join("")+"}"} }); @@ -144,6 +150,9 @@ old.nextSibling = old.parent = null; return old; }, + hasChildNodes: function (node) { + return (this.childNodes.length>0); + }, setAttribute: function (name,value) {this[name] = value} }); }; @@ -175,133 +184,228 @@ /****************************************************************** * - * The following section is ASCIIMathML.js Version 1.4.7 + * The following section is ASCIIMathML.js Version 2.2 * (c) Peter Jipsen, used with permission. * * Some sections are commented out to save space in the * minified version (but that is not strictly necessary). - * A few items are commented out and marked with DPVC comments - * in order to keep the minifier from complaining about the - * coding practices in ASCIIMathML.js * - * Two sections are modified to include changes from version 2.0.1 of - * ASCIIMathML.js and are marked with comments to that effect. This - * makes this version effectively the same as version 2.0.1, but - * without the overhead of the LaTeX-processing code. - * ******************************************************************/ /* ASCIIMathML.js ============== This file contains JavaScript functions to convert ASCII math notation -to Presentation MathML. The conversion is done while the (X)HTML page -loads, and should work with Firefox/Mozilla/Netscape 7+ and Internet -Explorer 6+MathPlayer (http://www.dessci.com/en/products/mathplayer/). -Just add the next line to your (X)HTML page with this file in the same folder: - -This is a convenient and inexpensive solution for authoring MathML. +and (some) LaTeX to Presentation MathML. The conversion is done while the +HTML page loads, and should work with Firefox and other browsers that can +render MathML. -Version 1.4.7 Aug 30, 2005, (c) Peter Jipsen http://www.chapman.edu/~jipsen -Latest version at http://www.chapman.edu/~jipsen/mathml/ASCIIMathML.js -For changes see http://www.chapman.edu/~jipsen/mathml/asciimathchanges.txt +Just add the next line to your HTML page with this file in the same folder: + + + +Version 2.2 Mar 3, 2014. +Latest version at https://github.com/mathjax/asciimathml If you use it on a webpage, please send the URL to jipsen@chapman.edu -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or (at -your option) any later version. +Copyright (c) 2014 Peter Jipsen and other ASCIIMathML.js contributors -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License (at http://www.gnu.org/copyleft/gpl.html) -for more details. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. */ +//var asciimath = {}; -//var checkForMathML = true; // check if browser can display MathML -//var notifyIfNoMathML = true; // display note if no MathML capability -//var alertIfNoMathML = true; // show alert box if no MathML capability -var mathcolor = "red"; // change it to "" (to inherit) or any other color -var mathfontfamily = "serif"; // change to "" to inherit (works in IE) - // or another family (e.g. "arial") +//(function(){ +var mathcolor = "blue"; // change it to "" (to inherit) or another color +var mathfontsize = "1em"; // change to e.g. 1.2em for larger math +var mathfontfamily = "serif"; // change to "" to inherit (works in IE) + // or another family (e.g. "arial") +//var automathrecognize = false; // writing "amath" on page makes this true +//var checkForMathML = true; // check if browser can display MathML +//var notifyIfNoMathML = true; // display note at top if no MathML capability +//var alertIfNoMathML = false; // show alert box if no MathML capability +//var translateOnLoad = true; // set to false to do call translators from js +//var translateASCIIMath = true; // false to preserve `..` var displaystyle = true; // puts limits above and below large operators var showasciiformulaonhover = true; // helps students learn ASCIIMath var decimalsign = "."; // change to "," if you like, beware of `(1,2)`! //var AMdelimiter1 = "`", AMescape1 = "\\\\`"; // can use other characters -//var AMdelimiter2 = "$", AMescape2 = "\\\\\\$", AMdelimiter2regexp = "\\$"; -//var doubleblankmathdelimiter = false; // if true, x+1 is equal to `x+1` - // for IE this works only in -//var separatetokens;// has been removed (email me if this is a problem) -var isIE = document.createElementNS==null; +//var AMdocumentId = "wikitext" // PmWiki element containing math (default=body) +var fixphi = true; //false to return to legacy phi/varphi mapping -/* - * if (document.getElementById==null) - * alert("This webpage requires a recent browser such as\ - * \nMozilla/Netscape 7+ or Internet Explorer 6+MathPlayer") - */ +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -// all further global variables start with "AM" +var isIE = (navigator.appName.slice(0,9)=="Microsoft"); +var noMathML = false, translated = false; -function AMcreateElementXHTML(t) { +/* +if (isIE) { // add MathPlayer info to IE webpages + document.write(""); + document.write(""); +} + +// Add a stylesheet, replacing any previous custom stylesheet (adapted from TW) +function setStylesheet(s) { + var id = "AMMLcustomStyleSheet"; + var n = document.getElementById(id); + if(document.createStyleSheet) { + // Test for IE's non-standard createStyleSheet method + if(n) + n.parentNode.removeChild(n); + // This failed without the   + document.getElementsByTagName("head")[0].insertAdjacentHTML("beforeEnd"," "); + } else { + if(n) { + n.replaceChild(document.createTextNode(s),n.firstChild); + } else { + n = document.createElement("style"); + n.type = "text/css"; + n.id = id; + n.appendChild(document.createTextNode(s)); + document.getElementsByTagName("head")[0].appendChild(n); + } + } +} + +setStylesheet("#AMMLcloseDiv \{font-size:0.8em; padding-top:1em; color:#014\}\n#AMMLwarningBox \{position:absolute; width:100%; top:0; left:0; z-index:200; text-align:center; font-size:1em; font-weight:bold; padding:0.5em 0 0.5em 0; color:#ffc; background:#c30\}"); + +function init(){ + var msg, warnings = new Array(); + if (document.getElementById==null){ + alert("This webpage requires a recent browser such as Mozilla Firefox"); + return null; + } + if (checkForMathML && (msg = checkMathML())) warnings.push(msg); + if (warnings.length>0) displayWarnings(warnings); + if (!noMathML) initSymbols(); + return true; +} + +function checkMathML(){ + if (navigator.appName.slice(0,8)=="Netscape") + if (navigator.appVersion.slice(0,1)>="5") noMathML = null; + else noMathML = true; + else if (navigator.appName.slice(0,9)=="Microsoft") + try { + var ActiveX = new ActiveXObject("MathPlayer.Factory.1"); + noMathML = null; + } catch (e) { + noMathML = true; + } + else if (navigator.appName.slice(0,5)=="Opera") + if (navigator.appVersion.slice(0,3)>="9.5") noMathML = null; + else noMathML = true; +//noMathML = true; //uncomment to check + if (noMathML && notifyIfNoMathML) { + var msg = "To view the ASCIIMathML notation use Internet Explorer + MathPlayer or Mozilla Firefox 2.0 or later."; + if (alertIfNoMathML) + alert(msg); + else return msg; + } +} + +function hideWarning(){ + var body = document.getElementsByTagName("body")[0]; + body.removeChild(document.getElementById('AMMLwarningBox')); + body.onclick = null; +} + +function displayWarnings(warnings) { + var i, frag, nd = createElementXHTML("div"); + var body = document.getElementsByTagName("body")[0]; + body.onclick=hideWarning; + nd.id = 'AMMLwarningBox'; + for (i=0; i="5") return null; - * else return AMnoMathMLNote(); - * else if (navigator.appName.slice(0,9)=="Microsoft") - * try { - * var ActiveX = new ActiveXObject("MathPlayer.Factory.1"); - * return null; - * } catch (e) { - * return AMnoMathMLNote(); - * } - * else return AMnoMathMLNote(); - * } - */ +var AMmathml = "http://www.w3.org/1998/Math/MathML"; + +function AMcreateElementMathML(t) { + if (isIE) return document.createElement("m:"+t); + else return document.createElementNS(AMmathml,t); +} + +function createMmlNode(t,frag) { + var node; + if (isIE) node = document.createElement("m:"+t); + else node = document.createElementNS(AMmathml,t); + if (frag) node.appendChild(frag); + return node; +} + +function newcommand(oldstr,newstr) { + AMsymbols = AMsymbols.concat([{input:oldstr, tag:"mo", output:newstr, + tex:null, ttype:DEFINITION}]); + // #### Added from Version 2.0.1 #### // + AMsymbols.sort(compareNames); + for (i=0; i", tex:null, ttype:CONST}, {input:">=", tag:"mo", output:"\u2265", tex:"ge", ttype:CONST}, -{input:"geq", tag:"mo", output:"\u2265", tex:null, ttype:CONST}, +{input:"gt=", tag:"mo", output:"\u2265", tex:"geq", ttype:CONST}, {input:"-<", tag:"mo", output:"\u227A", tex:"prec", ttype:CONST}, {input:"-lt", tag:"mo", output:"\u227A", tex:null, ttype:CONST}, {input:">-", tag:"mo", output:"\u227B", tex:"succ", ttype:CONST}, +{input:"-<=", tag:"mo", output:"\u2AAF", tex:"preceq", ttype:CONST}, +{input:">-=", tag:"mo", output:"\u2AB0", tex:"succeq", ttype:CONST}, {input:"in", tag:"mo", output:"\u2208", tex:null, ttype:CONST}, {input:"!in", tag:"mo", output:"\u2209", tex:"notin", ttype:CONST}, {input:"sub", tag:"mo", output:"\u2282", tex:"subset", ttype:CONST}, @@ -433,6 +545,9 @@ var AMsymbols = [ {input:"...", tag:"mo", output:"...", tex:"ldots", ttype:CONST}, {input:":.", tag:"mo", output:"\u2234", tex:"therefore", ttype:CONST}, {input:"/_", tag:"mo", output:"\u2220", tex:"angle", ttype:CONST}, +{input:"/_\\", tag:"mo", output:"\u25B3", tex:"triangle", ttype:CONST}, +{input:"'", tag:"mo", output:"\u2032", tex:"prime", ttype:CONST}, +{input:"tilde", tag:"mover", output:"~", tex:null, ttype:UNARY, acc:true}, {input:"\\ ", tag:"mo", output:"\u00A0", tex:null, ttype:CONST}, {input:"quad", tag:"mo", output:"\u00A0\u00A0", tex:null, ttype:CONST}, {input:"qquad", tag:"mo", output:"\u00A0\u00A0\u00A0\u00A0", tex:null, ttype:CONST}, @@ -465,6 +580,17 @@ var AMsymbols = [ {input:"cot", tag:"mo", output:"cot", tex:null, ttype:UNARY, func:true}, {input:"sec", tag:"mo", output:"sec", tex:null, ttype:UNARY, func:true}, {input:"csc", tag:"mo", output:"csc", tex:null, ttype:UNARY, func:true}, +{input:"arcsin", tag:"mo", output:"arcsin", tex:null, ttype:UNARY, func:true}, +{input:"arccos", tag:"mo", output:"arccos", tex:null, ttype:UNARY, func:true}, +{input:"arctan", tag:"mo", output:"arctan", tex:null, ttype:UNARY, func:true}, +{input:"coth", tag:"mo", output:"coth", tex:null, ttype:UNARY, func:true}, +{input:"sech", tag:"mo", output:"sech", tex:null, ttype:UNARY, func:true}, +{input:"csch", tag:"mo", output:"csch", tex:null, ttype:UNARY, func:true}, +{input:"exp", tag:"mo", output:"exp", tex:null, ttype:UNARY, func:true}, +{input:"abs", tag:"mo", output:"abs", tex:null, ttype:UNARY, rewriteleftright:["|","|"]}, +{input:"norm", tag:"mo", output:"norm", tex:null, ttype:UNARY, rewriteleftright:["\u2225","\u2225"]}, +{input:"floor", tag:"mo", output:"floor", tex:null, ttype:UNARY, rewriteleftright:["\u230A","\u230B"]}, +{input:"ceil", tag:"mo", output:"ceil", tex:null, ttype:UNARY, rewriteleftright:["\u2308","\u2309"]}, {input:"log", tag:"mo", output:"log", tex:null, ttype:UNARY, func:true}, {input:"ln", tag:"mo", output:"ln", tex:null, ttype:UNARY, func:true}, {input:"det", tag:"mo", output:"det", tex:null, ttype:UNARY, func:true}, @@ -482,32 +608,46 @@ var AMsymbols = [ {input:"darr", tag:"mo", output:"\u2193", tex:"downarrow", ttype:CONST}, {input:"rarr", tag:"mo", output:"\u2192", tex:"rightarrow", ttype:CONST}, {input:"->", tag:"mo", output:"\u2192", tex:"to", ttype:CONST}, +{input:">->", tag:"mo", output:"\u21A3", tex:"rightarrowtail", ttype:CONST}, +{input:"->>", tag:"mo", output:"\u21A0", tex:"twoheadrightarrow", ttype:CONST}, +{input:">->>", tag:"mo", output:"\u2916", tex:"twoheadrightarrowtail", ttype:CONST}, {input:"|->", tag:"mo", output:"\u21A6", tex:"mapsto", ttype:CONST}, {input:"larr", tag:"mo", output:"\u2190", tex:"leftarrow", ttype:CONST}, {input:"harr", tag:"mo", output:"\u2194", tex:"leftrightarrow", ttype:CONST}, {input:"rArr", tag:"mo", output:"\u21D2", tex:"Rightarrow", ttype:CONST}, {input:"lArr", tag:"mo", output:"\u21D0", tex:"Leftarrow", ttype:CONST}, {input:"hArr", tag:"mo", output:"\u21D4", tex:"Leftrightarrow", ttype:CONST}, - //commands with argument -AMsqrt, AMroot, AMfrac, AMdiv, AMover, AMsub, AMsup, +{input:"sqrt", tag:"msqrt", output:"sqrt", tex:null, ttype:UNARY}, +{input:"root", tag:"mroot", output:"root", tex:null, ttype:BINARY}, +{input:"frac", tag:"mfrac", output:"/", tex:null, ttype:BINARY}, +{input:"/", tag:"mfrac", output:"/", tex:null, ttype:INFIX}, +{input:"stackrel", tag:"mover", output:"stackrel", tex:null, ttype:BINARY}, +{input:"_", tag:"msub", output:"_", tex:null, ttype:INFIX}, +{input:"^", tag:"msup", output:"^", tex:null, ttype:INFIX}, {input:"hat", tag:"mover", output:"\u005E", tex:null, ttype:UNARY, acc:true}, {input:"bar", tag:"mover", output:"\u00AF", tex:"overline", ttype:UNARY, acc:true}, {input:"vec", tag:"mover", output:"\u2192", tex:null, ttype:UNARY, acc:true}, {input:"dot", tag:"mover", output:".", tex:null, ttype:UNARY, acc:true}, {input:"ddot", tag:"mover", output:"..", tex:null, ttype:UNARY, acc:true}, {input:"ul", tag:"munder", output:"\u0332", tex:"underline", ttype:UNARY, acc:true}, -AMtext, AMmbox, AMquote, -{input:"bb", tag:"mstyle", atname:"fontweight", atval:"bold", output:"bb", tex:null, ttype:UNARY}, -{input:"mathbf", tag:"mstyle", atname:"fontweight", atval:"bold", output:"mathbf", tex:null, ttype:UNARY}, -{input:"sf", tag:"mstyle", atname:"fontfamily", atval:"sans-serif", output:"sf", tex:null, ttype:UNARY}, -{input:"mathsf", tag:"mstyle", atname:"fontfamily", atval:"sans-serif", output:"mathsf", tex:null, ttype:UNARY}, +{input:"ubrace", tag:"munder", output:"\u23DF", tex:"underbrace", ttype:UNARYUNDEROVER, acc:true}, +{input:"obrace", tag:"mover", output:"\u23DE", tex:"overbrace", ttype:UNARYUNDEROVER, acc:true}, +{input:"text", tag:"mtext", output:"text", tex:null, ttype:TEXT}, +{input:"mbox", tag:"mtext", output:"mbox", tex:null, ttype:TEXT}, +{input:"color", tag:"mstyle", ttype:BINARY}, +{input:"cancel", tag:"menclose", output:"cancel", tex:null, ttype:UNARY}, +AMquote, +{input:"bb", tag:"mstyle", atname:"mathvariant", atval:"bold", output:"bb", tex:null, ttype:UNARY}, +{input:"mathbf", tag:"mstyle", atname:"mathvariant", atval:"bold", output:"mathbf", tex:null, ttype:UNARY}, +{input:"sf", tag:"mstyle", atname:"mathvariant", atval:"sans-serif", output:"sf", tex:null, ttype:UNARY}, +{input:"mathsf", tag:"mstyle", atname:"mathvariant", atval:"sans-serif", output:"mathsf", tex:null, ttype:UNARY}, {input:"bbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"bbb", tex:null, ttype:UNARY, codes:AMbbb}, {input:"mathbb", tag:"mstyle", atname:"mathvariant", atval:"double-struck", output:"mathbb", tex:null, ttype:UNARY, codes:AMbbb}, {input:"cc", tag:"mstyle", atname:"mathvariant", atval:"script", output:"cc", tex:null, ttype:UNARY, codes:AMcal}, {input:"mathcal", tag:"mstyle", atname:"mathvariant", atval:"script", output:"mathcal", tex:null, ttype:UNARY, codes:AMcal}, -{input:"tt", tag:"mstyle", atname:"fontfamily", atval:"monospace", output:"tt", tex:null, ttype:UNARY}, -{input:"mathtt", tag:"mstyle", atname:"fontfamily", atval:"monospace", output:"mathtt", tex:null, ttype:UNARY}, +{input:"tt", tag:"mstyle", atname:"mathvariant", atval:"monospace", output:"tt", tex:null, ttype:UNARY}, +{input:"mathtt", tag:"mstyle", atname:"mathvariant", atval:"monospace", output:"mathtt", tex:null, ttype:UNARY}, {input:"fr", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"fr", tex:null, ttype:UNARY, codes:AMfrk}, {input:"mathfrak", tag:"mstyle", atname:"mathvariant", atval:"fraktur", output:"mathfrak", tex:null, ttype:UNARY, codes:AMfrk} ]; @@ -519,39 +659,28 @@ function compareNames(s1,s2) { var AMnames = []; //list of input symbols -function AMinitSymbols() { +function initSymbols() { var texsymbols = [], i; for (i=0; i=n where str appears or would be inserted // assumes arr is sorted if (n==0) { @@ -594,7 +723,7 @@ function AMgetSymbol(str) { for (var i=1; i<=str.length && more; i++) { st = str.slice(0,i); //initial substring of length i j = k; - k = AMposition(AMnames, st, j); + k = position(AMnames, st, j); if (kst || st>"Z") && ("a">st || st>"z")?"mo":"mi"); } - // #### Replaced by lines from Version 2.0.1 #### // if (st=="-" && AMpreviousSymbol==INFIX) { AMcurrentSymbol = INFIX; //trick "/" into recognizing "-" on second parse return {input:st, tag:tagst, output:st, ttype:UNARY, func:true}; } - // #### End of Replacement #### // return {input:st, tag:tagst, output:st, ttype:CONST}; } function AMremoveBrackets(node) { var st; - if (node.nodeName=="mrow") { + if (!node.hasChildNodes()) { return; } + if (node.firstChild.hasChildNodes() && (node.nodeName=="mrow" || node.nodeName=="M:MROW")) { st = node.firstChild.firstChild.nodeValue; if (st=="(" || st=="[" || st=="{") node.removeChild(node.firstChild); } - if (node.nodeName=="mrow") { + if (node.lastChild.hasChildNodes() && (node.nodeName=="mrow" || node.nodeName=="M:MROW")) { st = node.lastChild.firstChild.nodeValue; if (st==")" || st=="]" || st=="}") node.removeChild(node.lastChild); } @@ -681,11 +809,10 @@ function AMparseSexpr(str) { //parses str and returns [node,tailstr] str = symbol.output+AMremoveCharsAndBlanks(str,symbol.input.length); symbol = AMgetSymbol(str); } - switch (symbol.ttype) { - case UNDEROVER: + switch (symbol.ttype) { case UNDEROVER: case CONST: str = AMremoveCharsAndBlanks(str,symbol.input.length); - return [AMcreateMmlNode(symbol.tag, //its a constant + return [createMmlNode(symbol.tag, //its a constant document.createTextNode(symbol.output)),str]; case LEFTBRACKET: //read (expr+) AMnestingDepth++; @@ -693,10 +820,10 @@ function AMparseSexpr(str) { //parses str and returns [node,tailstr] result = AMparseExpr(str,true); AMnestingDepth--; if (typeof symbol.invisible == "boolean" && symbol.invisible) - node = AMcreateMmlNode("mrow",result[0]); + node = createMmlNode("mrow",result[0]); else { - node = AMcreateMmlNode("mo",document.createTextNode(symbol.output)); - node = AMcreateMmlNode("mrow",node); + node = createMmlNode("mo",document.createTextNode(symbol.output)); + node = createMmlNode("mrow",node); node.appendChild(result[0]); } return [node,result[1]]; @@ -710,42 +837,53 @@ function AMparseSexpr(str) { //parses str and returns [node,tailstr] if (i==-1) i = str.length; st = str.slice(1,i); if (st.charAt(0) == " ") { - node = AMcreateElementMathML("mspace"); + node = createMmlNode("mspace"); node.setAttribute("width","1ex"); newFrag.appendChild(node); } newFrag.appendChild( - AMcreateMmlNode(symbol.tag,document.createTextNode(st))); + createMmlNode(symbol.tag,document.createTextNode(st))); if (st.charAt(st.length-1) == " ") { - node = AMcreateElementMathML("mspace"); + node = createMmlNode("mspace"); node.setAttribute("width","1ex"); newFrag.appendChild(node); } str = AMremoveCharsAndBlanks(str,i+1); - return [AMcreateMmlNode("mrow",newFrag),str]; + return [createMmlNode("mrow",newFrag),str]; + case UNARYUNDEROVER: case UNARY: str = AMremoveCharsAndBlanks(str,symbol.input.length); result = AMparseSexpr(str); - if (result[0]==null) return [AMcreateMmlNode(symbol.tag, + if (result[0]==null) return [createMmlNode(symbol.tag, document.createTextNode(symbol.output)),str]; if (typeof symbol.func == "boolean" && symbol.func) { // functions hack st = str.charAt(0); - if (st=="^" || st=="_" || st=="/" || st=="|" || st==",") { - return [AMcreateMmlNode(symbol.tag, + if (st=="^" || st=="_" || st=="/" || st=="|" || st=="," || + (symbol.input.length==1 && symbol.input.match(/\w/) && st!="(")) { + return [createMmlNode(symbol.tag, document.createTextNode(symbol.output)),str]; } else { - node = AMcreateMmlNode("mrow", - AMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output))); + node = createMmlNode("mrow", + createMmlNode(symbol.tag,document.createTextNode(symbol.output))); node.appendChild(result[0]); return [node,result[1]]; } } AMremoveBrackets(result[0]); if (symbol.input == "sqrt") { // sqrt - return [AMcreateMmlNode(symbol.tag,result[0]),result[1]]; + return [createMmlNode(symbol.tag,result[0]),result[1]]; + } else if (typeof symbol.rewriteleftright != "undefined") { // abs, floor, ceil + node = createMmlNode("mrow", createMmlNode("mo",document.createTextNode(symbol.rewriteleftright[0]))); + node.appendChild(result[0]); + node.appendChild(createMmlNode("mo",document.createTextNode(symbol.rewriteleftright[1]))); + return [node,result[1]]; + } else if (symbol.input == "cancel") { // cancel + node = createMmlNode(symbol.tag,result[0]); + node.setAttribute("notation","updiagonalstrike"); + return [node,result[1]]; } else if (typeof symbol.acc == "boolean" && symbol.acc) { // accent - node = AMcreateMmlNode(symbol.tag,result[0]); - node.appendChild(AMcreateMmlNode("mo",document.createTextNode(symbol.output))); + node = createMmlNode(symbol.tag,result[0]); + node.appendChild(createMmlNode("mo",document.createTextNode(symbol.output))); return [node,result[1]]; } else { // font change command if (!isIE && typeof symbol.codes != "undefined") { @@ -755,73 +893,84 @@ function AMparseSexpr(str) { //parses str and returns [node,tailstr] result[0].childNodes[i].firstChild.nodeValue); var newst = []; for (var j=0; j64 && st.charCodeAt(j)<91) newst = newst + - String.fromCharCode(symbol.codes[st.charCodeAt(j)-65]); + if (st.charCodeAt(j)>64 && st.charCodeAt(j)<91) + newst = newst + symbol.codes[st.charCodeAt(j)-65]; + else if (st.charCodeAt(j)>96 && st.charCodeAt(j)<123) + newst = newst + symbol.codes[st.charCodeAt(j)-71]; else newst = newst + st.charAt(j); if (result[0].nodeName=="mi") - result[0]=AMcreateElementMathML("mo"). + result[0]=createMmlNode("mo"). appendChild(document.createTextNode(newst)); - else result[0].replaceChild(AMcreateElementMathML("mo"). - appendChild(document.createTextNode(newst)),result[0].childNodes[i]); + else result[0].replaceChild(createMmlNode("mo"). + appendChild(document.createTextNode(newst)), + result[0].childNodes[i]); } } - node = AMcreateMmlNode(symbol.tag,result[0]); + node = createMmlNode(symbol.tag,result[0]); node.setAttribute(symbol.atname,symbol.atval); return [node,result[1]]; } case BINARY: str = AMremoveCharsAndBlanks(str,symbol.input.length); result = AMparseSexpr(str); - if (result[0]==null) return [AMcreateMmlNode("mo", + if (result[0]==null) return [createMmlNode("mo", document.createTextNode(symbol.input)),str]; AMremoveBrackets(result[0]); var result2 = AMparseSexpr(result[1]); - if (result2[0]==null) return [AMcreateMmlNode("mo", + if (result2[0]==null) return [createMmlNode("mo", document.createTextNode(symbol.input)),str]; AMremoveBrackets(result2[0]); + if (symbol.input=="color") { + if (str.charAt(0)=="{") i=str.indexOf("}"); + else if (str.charAt(0)=="(") i=str.indexOf(")"); + else if (str.charAt(0)=="[") i=str.indexOf("]"); + st = str.slice(1,i); + node = createMmlNode(symbol.tag,result2[0]); + node.setAttribute("mathcolor",st); + return [node,result2[1]]; + } if (symbol.input=="root" || symbol.input=="stackrel") newFrag.appendChild(result2[0]); newFrag.appendChild(result[0]); if (symbol.input=="frac") newFrag.appendChild(result2[0]); - return [AMcreateMmlNode(symbol.tag,newFrag),result2[1]]; + return [createMmlNode(symbol.tag,newFrag),result2[1]]; case INFIX: str = AMremoveCharsAndBlanks(str,symbol.input.length); - return [AMcreateMmlNode("mo",document.createTextNode(symbol.output)),str]; + return [createMmlNode("mo",document.createTextNode(symbol.output)),str]; case SPACE: str = AMremoveCharsAndBlanks(str,symbol.input.length); - node = AMcreateElementMathML("mspace"); + node = createMmlNode("mspace"); node.setAttribute("width","1ex"); newFrag.appendChild(node); newFrag.appendChild( - AMcreateMmlNode(symbol.tag,document.createTextNode(symbol.output))); - node = AMcreateElementMathML("mspace"); + createMmlNode(symbol.tag,document.createTextNode(symbol.output))); + node = createMmlNode("mspace"); node.setAttribute("width","1ex"); newFrag.appendChild(node); - return [AMcreateMmlNode("mrow",newFrag),str]; + return [createMmlNode("mrow",newFrag),str]; case LEFTRIGHT: // if (rightvert) return [null,str]; else rightvert = true; AMnestingDepth++; str = AMremoveCharsAndBlanks(str,symbol.input.length); result = AMparseExpr(str,false); AMnestingDepth--; - /*var*//*DPVC*/ st = ""; + st = ""; if (result[0].lastChild!=null) st = result[0].lastChild.firstChild.nodeValue; -//alert(result[0].lastChild+"***"+st); if (st == "|") { // its an absolute value subterm - node = AMcreateMmlNode("mo",document.createTextNode(symbol.output)); - node = AMcreateMmlNode("mrow",node); + node = createMmlNode("mo",document.createTextNode(symbol.output)); + node = createMmlNode("mrow",node); node.appendChild(result[0]); return [node,result[1]]; - } else { // the "|" is a \mid - node = AMcreateMmlNode("mo",document.createTextNode(symbol.output)); - node = AMcreateMmlNode("mrow",node); + } else { // the "|" is a \mid so use unicode 2223 (divides) for spacing + node = createMmlNode("mo",document.createTextNode("\u2223")); + node = createMmlNode("mrow",node); return [node,str]; } default: //alert("default"); str = AMremoveCharsAndBlanks(str,symbol.input.length); - return [AMcreateMmlNode(symbol.tag, //its a constant + return [createMmlNode(symbol.tag, //its a constant document.createTextNode(symbol.output)),str]; } } @@ -839,36 +988,48 @@ function AMparseIexpr(str) { // if (symbol.input == "/") result = AMparseIexpr(str); else ... result = AMparseSexpr(str); if (result[0] == null) // show box in place of missing argument - result[0] = AMcreateMmlNode("mo",document.createTextNode("\u25A1")); + result[0] = createMmlNode("mo",document.createTextNode("\u25A1")); else AMremoveBrackets(result[0]); str = result[1]; // if (symbol.input == "/") AMremoveBrackets(node); + underover = (sym1.ttype == UNDEROVER || sym1.ttype == UNARYUNDEROVER); if (symbol.input == "_") { sym2 = AMgetSymbol(str); - underover = (sym1.ttype == UNDEROVER); if (sym2.input == "^") { str = AMremoveCharsAndBlanks(str,sym2.input.length); var res2 = AMparseSexpr(str); AMremoveBrackets(res2[0]); str = res2[1]; - node = AMcreateMmlNode((underover?"munderover":"msubsup"),node); + node = createMmlNode((underover?"munderover":"msubsup"),node); node.appendChild(result[0]); node.appendChild(res2[0]); - node = AMcreateMmlNode("mrow",node); // so sum does not stretch + node = createMmlNode("mrow",node); // so sum does not stretch } else { - node = AMcreateMmlNode((underover?"munder":"msub"),node); + node = createMmlNode((underover?"munder":"msub"),node); node.appendChild(result[0]); } + } else if (symbol.input == "^" && underover) { + node = createMmlNode("mover",node); + node.appendChild(result[0]); } else { - node = AMcreateMmlNode(symbol.tag,node); + node = createMmlNode(symbol.tag,node); node.appendChild(result[0]); } + if (typeof sym1.func != 'undefined' && sym1.func) { + sym2 = AMgetSymbol(str); + if (sym2.ttype != INFIX && sym2.ttype != RIGHTBRACKET) { + result = AMparseIexpr(str); + node = createMmlNode("mrow",node); + node.appendChild(result[0]); + str = result[1]; + } + } } return [node,str]; } function AMparseExpr(str,rightbracket) { - var symbol, node, result, i, /*nodeList = [],*//*DPVC*/ + var symbol, node, result, i, newFrag = document.createDocumentFragment(); do { str = AMremoveCharsAndBlanks(str,0); @@ -880,11 +1041,11 @@ function AMparseExpr(str,rightbracket) { str = AMremoveCharsAndBlanks(str,symbol.input.length); result = AMparseIexpr(str); if (result[0] == null) // show box in place of missing argument - result[0] = AMcreateMmlNode("mo",document.createTextNode("\u25A1")); + result[0] = createMmlNode("mo",document.createTextNode("\u25A1")); else AMremoveBrackets(result[0]); str = result[1]; AMremoveBrackets(node); - node = AMcreateMmlNode(symbol.tag,node); + node = createMmlNode(symbol.tag,node); node.appendChild(result[0]); newFrag.appendChild(node); symbol = AMgetSymbol(str); @@ -896,9 +1057,10 @@ function AMparseExpr(str,rightbracket) { if (symbol.ttype == RIGHTBRACKET || symbol.ttype == LEFTRIGHT) { // if (AMnestingDepth > 0) AMnestingDepth--; var len = newFrag.childNodes.length; - if (len>0 && newFrag.childNodes[len-1].nodeName == "mrow" && len>1 && - newFrag.childNodes[len-2].nodeName == "mo" && - newFrag.childNodes[len-2].firstChild.nodeValue == ",") { //matrix + if (len>0 && newFrag.childNodes[len-1].nodeName == "mrow" ) { //matrix + //removed to allow row vectors: //&& len>1 && + //newFrag.childNodes[len-2].nodeName == "mo" && + //newFrag.childNodes[len-2].firstChild.nodeValue == "," var right = newFrag.childNodes[len-1].lastChild.firstChild.nodeValue; if (right==")" || right=="]") { var left = newFrag.childNodes[len-1].firstChild.firstChild.nodeValue; @@ -921,6 +1083,7 @@ function AMparseExpr(str,rightbracket) { pos[i][pos[i].length]=j; if (matrix && i>1) matrix = pos[i].length == pos[i-2].length; } + matrix = matrix && (pos.length>1 || pos[0].length>0); if (matrix) { var row, frag, n, k, table = document.createDocumentFragment(); for (i=0; i2) { newFrag.removeChild(newFrag.firstChild); //remove ) newFrag.removeChild(newFrag.firstChild); //remove , } - table.appendChild(AMcreateMmlNode("mtr",row)); + table.appendChild(createMmlNode("mtr",row)); } - node = AMcreateMmlNode("mtable",table); + node = createMmlNode("mtable",table); if (typeof symbol.invisible == "boolean" && symbol.invisible) node.setAttribute("columnalign","left"); newFrag.replaceChild(node,newFrag.firstChild); } @@ -953,198 +1116,208 @@ function AMparseExpr(str,rightbracket) { } str = AMremoveCharsAndBlanks(str,symbol.input.length); if (typeof symbol.invisible != "boolean" || !symbol.invisible) { - node = AMcreateMmlNode("mo",document.createTextNode(symbol.output)); + node = createMmlNode("mo",document.createTextNode(symbol.output)); newFrag.appendChild(node); } } return [newFrag,str]; } -function AMparseMath(str) { - var /*result,*//*DPVC*/ node = AMcreateElementMathML("mstyle"); - if (mathcolor != "") node.setAttribute("mathcolor",mathcolor); - if (displaystyle) node.setAttribute("displaystyle","true"); - if (mathfontfamily != "") node.setAttribute("fontfamily",mathfontfamily); +function parseMath(str,latex) { + var frag, node; AMnestingDepth = 0; - node.appendChild(AMparseExpr(str.replace(/^\s+/g,""),false)[0]); - node = AMcreateMmlNode("math",node); + //some basic cleanup for dealing with stuff editors like TinyMCE adds + str = str.replace(/ /g,""); + str = str.replace(/>/g,">"); + str = str.replace(/</g,"<"); + str = str.replace(/(Sin|Cos|Tan|Arcsin|Arccos|Arctan|Sinh|Cosh|Tanh|Cot|Sec|Csc|Log|Ln|Abs)/g, function(v) { return v.toLowerCase(); }); + frag = AMparseExpr(str.replace(/^\s+/g,""),false)[0]; + node = createMmlNode("mstyle",frag); + if (mathcolor != "") node.setAttribute("mathcolor",mathcolor); + if (mathfontfamily != "") node.setAttribute("fontfamily",mathfontfamily); + if (displaystyle) node.setAttribute("displaystyle","true"); + node = createMmlNode("math",node); if (showasciiformulaonhover) //fixed by djhsu so newline node.setAttribute("title",str.replace(/\s+/g," "));//does not show in Gecko - if (mathfontfamily != "" && (isIE || mathfontfamily != "serif")) { - var fnode = AMcreateElementXHTML("font"); - fnode.setAttribute("face",mathfontfamily); - fnode.appendChild(node); - return fnode; - } return node; } -/* - * function AMstrarr2docFrag(arr, linebreaks) { - * var newFrag=document.createDocumentFragment(); - * var expr = false; - * for (var i=0; i1 || mtch) { - * if (checkForMathML) { - * checkForMathML = false; - * var nd = AMisMathMLavailable(); - * AMnoMathML = nd != null; - * if (AMnoMathML && notifyIfNoMathML) - * if (alertIfNoMathML) - * alert("To view the ASCIIMathML notation use Internet Explorer 6 +\nMathPlayer (free from www.dessci.com)\n\ - * or Firefox/Mozilla/Netscape"); - * else AMbody.insertBefore(nd,AMbody.childNodes[0]); - * } - * if (!AMnoMathML) { - * frg = AMstrarr2docFrag(arr,n.nodeType==8); - * var len = frg.childNodes.length; - * n.parentNode.replaceChild(frg,n); - * return len-1; - * } else return 0; - * } - * } - * } else return 0; - * } else if (n.nodeName!="math") { - * for (i=0; i"); - * document.write(""); - * } - * - * // GO1.1 Generic onload by Brothercake - * // http://www.brothercake.com/ - * //onload function (replaces the onload="translate()" in the tag) - * function generic() - * { - * translate(); - * }; - * //setup onload function - * if(typeof window.addEventListener != 'undefined') - * { - * //.. gecko, safari, konqueror and standard - * window.addEventListener('load', generic, false); - * } - * else if(typeof document.addEventListener != 'undefined') - * { - * //.. opera 7 - * document.addEventListener('load', generic, false); - * } - * else if(typeof window.attachEvent != 'undefined') - * { - * //.. win/ie - * window.attachEvent('onload', generic); - * } - * //** remove this condition to degrade older browsers - * else - * { - * //.. mac/ie5 and anything else that gets this far - * //if there's an existing onload function - * if(typeof window.onload == 'function') - * { - * //store it - * var existing = onload; - * //add new onload handler - * window.onload = function() - * { - * //call existing onload function - * existing(); - * //call generic onload function - * generic(); - * }; - * } - * else - * { - * //setup onload function - * window.onload = generic; - * } - * } - */ +/* +function strarr2docFrag(arr, linebreaks, latex) { + var newFrag=document.createDocumentFragment(); + var expr = false; + for (var i=0; i,\\|!:;'~]|\\.(?!(?:\x20|$))|"+ambigAMtoken+englishAMtoken+simpleAMtoken; + var re = new RegExp("(^|\\s)((("+token+")\\s?)(("+token+secondenglishAMtoken+")\\s?)+)([,.?]?(?=\\s|$))","g"); + str = str.replace(re," `$2`$7"); + var arr = str.split(AMdelimiter1); + var re1 = new RegExp("(^|\\s)([b-zB-HJ-Z+*<>]|"+texcommand+ambigAMtoken+simpleAMtoken+")(\\s|\\n|$)","g"); + var re2 = new RegExp("(^|\\s)([a-z]|"+texcommand+ambigAMtoken+simpleAMtoken+")([,.])","g"); // removed |\d+ for now + for (i=0; i1 || mtch) { + if (!noMathML) { + frg = strarr2docFrag(arr,n.nodeType==8,latex); + var len = frg.childNodes.length; + n.parentNode.replaceChild(frg,n); + return len-1; + } else return 0; + } + } + } else return 0; + } else if (n.nodeName!="math") { + for (i=0; i", tex:null, ttype:CONST}, - {input:"gt=", tag:"mo", output:"\u2265", tex:"geq", ttype:CONST}, - {input:"-<=", tag:"mo", output:"\u2AAF", tex:"preceq", ttype:CONST}, - {input:">-=", tag:"mo", output:"\u2AB0", tex:"succeq", ttype:CONST}, - {input:"'", tag:"mo", output:"\u2032", tex:"prime", ttype:CONST}, - {input:"arcsin", tag:"mi", output:"arcsin", tex:null, ttype:UNARY, func:true}, - {input:"arccos", tag:"mi", output:"arccos", tex:null, ttype:UNARY, func:true}, - {input:"arctan", tag:"mi", output:"arctan", tex:null, ttype:UNARY, func:true}, - {input:"coth", tag:"mi", output:"coth", tex:null, ttype:UNARY, func:true}, - {input:"sech", tag:"mi", output:"sech", tex:null, ttype:UNARY, func:true}, - {input:"csch", tag:"mi", output:"csch", tex:null, ttype:UNARY, func:true}, - {input:"abs", tag:"mi", output:"abs", tex:null, ttype:UNARY, func:true}, - {input:"exp", tag:"mi", output:"exp", tex:null, ttype:UNARY, func:true}, - {input:"tilde", tag:"mover", output:"~", tex:null, ttype:UNARY, acc:true} -) - // // Access to AsciiMath functions and values // @@ -1196,75 +1347,76 @@ ASCIIMATH.Augment({ // Old versions use the "decimal" option, so take it into account if it // is defined by the user. See issue 384. decimalsign = (ASCIIMATH.config.decimal || ASCIIMATH.config.decimalsign); - // fix phi and varphi, if requested - if (ASCIIMATH.config.fixphi) { + // unfix phi and varphi, if requested + if (!ASCIIMATH.config.fixphi) { for (var i = 0, m = AMsymbols.length; i < m; i++) { - if (AMsymbols[i].input === "phi") {AMsymbols[i].output = "\u03D5"} - if (AMsymbols[i].input === "varphi") {AMsymbols[i].output = "\u03C6"; i = m} + if (AMsymbols[i].input === "phi") {AMsymbols[i].output = "\u03C6"} + if (AMsymbols[i].input === "varphi") {AMsymbols[i].output = "\u03D5"; i = m} } } + INITASCIIMATH(); - AMinitSymbols(); + initSymbols(); }, Augment: function (def) { for (var id in def) {if (def.hasOwnProperty(id)) { switch (id) { case "displaystyle": displaystyle = def[id]; break; case "decimal": decimal = def[id]; break; - case "parseMath": AMparseMath = def[id]; break; + case "parseMath": parseMath = def[id]; break; case "parseExpr": AMparseExpr = def[id]; break; case "parseIexpr": AMparseIexpr = def[id]; break; case "parseSexpr": AMparseSexpr = def[id]; break; case "removeBrackets": AMremoveBrackets = def[id]; break; case "getSymbol": AMgetSymbol = def[id]; break; - case "position": AMposition = def[id]; break; + case "position": position = def[id]; break; case "removeCharsAndBlanks": AMremoveCharsAndBlanks = def[id]; break; - case "createMmlNode": AMcreateMmlNode = def[id]; break; + case "createMmlNode": createMmlNode = def[id]; break; case "createElementMathML": AMcreateElementMathML = def[id]; break; - case "createElementXHTML": AMcreateElementXHTML = def[id]; break; - case "initSymbols": AMinitSymbols = def[id]; break; - case "compareNames": comareNames = def[id]; break; + case "createElementXHTML": createElementXHTML = def[id]; break; + case "initSymbols": initSymbols = def[id]; break; + case "refreshSymbols": refreshSymbols = def[id]; break; + case "compareNames": compareNames = def[id]; break; }; this[id] = def[id]; }}; }, - parseMath: AMparseMath, + parseMath: parseMath, parseExpr: AMparseExpr, parseIexpr: AMparseIexpr, parseSexr: AMparseSexpr, removeBrackets: AMremoveBrackets, getSymbol: AMgetSymbol, - position: AMposition, + position: position, removeCharsAndBlanks: AMremoveCharsAndBlanks, - createMmlNode: AMcreateMmlNode, + createMmlNode: createMmlNode, createElementMathML: AMcreateElementMathML, - createElementXHTML: AMcreateElementXHTML, - initSymbols: AMinitSymbols, + createElementXHTML: createElementXHTML, + initSymbols: initSymbols, + refreshSymbols: refreshSymbols, compareNames: compareNames, createDocumentFragment: DOCFRAG, document: document, - define: newcommand, + define: define, + newcommand: newcommand, symbols: AMsymbols, names: AMnames, TOKEN: { CONST:CONST, UNARY:UNARY, BINARY:BINARY, INFIX:INFIX, LEFTBRACKET:LEFTBRACKET, RIGHTBRACKET:RIGHTBRACKET, SPACE:SPACE, - UNDEROVER:UNDEROVER, DEFINITION:DEFINITION, LEFTRIGHT:LEFTRIGHT, TEXT:TEXT + UNDEROVER:UNDEROVER, DEFINITION:DEFINITION, LEFTRIGHT:LEFTRIGHT, TEXT:TEXT, + UNARYUNDEROVER:UNARYUNDEROVER } } }); // // Make minimizer think these have been used -var junk = [ - window, navigator //, -// checkForMathML, notifyIfNoMathML, alertIfNoMathML, AMdelimiter1, AMescape1, -// AMdelimiter2, AMescape2, AMdelimiter2regexp, doubleblankmathdelimiter -]; -junk = null; +// +var junk = [window, navigator]; junk = null; })(MathJax.InputJax.AsciiMath); @@ -1292,7 +1444,7 @@ junk = null; if (!err.asciimathError) {throw err} mml = this.formatError(err,math); } - data.math = MML(mml); + data.math = MML(mml); this.postfilterHooks.Execute(data); return this.postfilterHooks.Execute(data) || data.math; }, formatError: function (err,math,script) {