jsMath2jax.js (4242B)
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 4 /************************************************************* 5 * 6 * MathJax/extensions/jsMath2jax.js 7 * 8 * Implements a jsMath to Jax preprocessor that locates jsMath-style 9 * <SPAN CLASS="math">...</SPAN> and <DIV CLASS="math">...</DIV> tags 10 * and replaces them with SCRIPT tags for processing by MathJax. 11 * (Note: use the tex2jax preprocessor to convert TeX delimiters or 12 * custom delimiters to MathJax SCRIPT tags. This preprocessor is 13 * only for the SPAN and DIV form of jsMath delimiters). 14 * 15 * To use this preprocessor, include "jsMath2jax.js" in the extensions 16 * array in your config/MathJax.js file, or the MathJax.Hub.Config() call 17 * in your HTML document. 18 * 19 * --------------------------------------------------------------------- 20 * 21 * Copyright (c) 2010-2015 The MathJax Consortium 22 * 23 * Licensed under the Apache License, Version 2.0 (the "License"); 24 * you may not use this file except in compliance with the License. 25 * You may obtain a copy of the License at 26 * 27 * http://www.apache.org/licenses/LICENSE-2.0 28 * 29 * Unless required by applicable law or agreed to in writing, software 30 * distributed under the License is distributed on an "AS IS" BASIS, 31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 * See the License for the specific language governing permissions and 33 * limitations under the License. 34 */ 35 36 MathJax.Extension.jsMath2jax = { 37 version: "2.6.0", 38 39 config: { 40 preview: "TeX" // Set to "none" to prevent preview strings from being inserted 41 // or to an array that specifies an HTML snippet to use for 42 // the preview. 43 }, 44 45 PreProcess: function (element) { 46 if (!this.configured) { 47 this.config = MathJax.Hub.CombineConfig("jsMath2jax",this.config); 48 if (this.config.Augment) {MathJax.Hub.Insert(this,this.config.Augment)} 49 if (typeof(this.config.previewTeX) !== "undefined" && !this.config.previewTeX) 50 {this.config.preview = "none"} // backward compatibility for previewTeX parameter 51 this.previewClass = MathJax.Hub.config.preRemoveClass; 52 this.configured = true; 53 } 54 if (typeof(element) === "string") {element = document.getElementById(element)} 55 if (!element) {element = document.body} 56 var span = element.getElementsByTagName("span"), i; 57 for (i = span.length-1; i >= 0; i--) 58 {if (String(span[i].className).match(/(^| )math( |$)/)) {this.ConvertMath(span[i],"")}} 59 var div = element.getElementsByTagName("div"); 60 for (i = div.length-1; i >= 0; i--) 61 {if (String(div[i].className).match(/(^| )math( |$)/)) {this.ConvertMath(div[i],"; mode=display")}} 62 }, 63 64 ConvertMath: function (node,mode) { 65 if (node.getElementsByTagName("script").length === 0) { 66 var parent = node.parentNode, 67 script = this.createMathTag(mode,node.innerHTML); 68 if (node.nextSibling) {parent.insertBefore(script,node.nextSibling)} 69 else {parent.appendChild(script)} 70 if (this.config.preview !== "none") {this.createPreview(node)} 71 parent.removeChild(node); 72 } 73 }, 74 75 createPreview: function (node) { 76 var preview = this.config.preview; 77 if (preview === "TeX") {preview = [this.filterPreview(node.innerHTML)]} 78 if (preview) { 79 preview = MathJax.HTML.Element("span",{className: MathJax.Hub.config.preRemoveClass},preview); 80 node.parentNode.insertBefore(preview,node); 81 } 82 }, 83 84 createMathTag: function (mode,tex) { 85 tex = tex.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"); 86 var script = document.createElement("script"); 87 script.type = "math/tex" + mode; 88 MathJax.HTML.setScript(script,tex); 89 return script; 90 }, 91 92 filterPreview: function (tex) {return tex} 93 94 }; 95 96 // We register the preprocessors with the following priorities: 97 // - mml2jax.js: 5 98 // - jsMath2jax.js: 8 99 // - asciimath2jax.js, tex2jax.js: 10 (default) 100 // See issues 18 and 484 and the other *2jax.js files. 101 MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.jsMath2jax],8); 102 MathJax.Ajax.loadComplete("[MathJax]/extensions/jsMath2jax.js");