asciimath2jax.js 5.6 KB

123456789101112131415161718192021222324252627282930
  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/asciimath2jax.js
  6. *
  7. * Implements the AsciiMath to Jax preprocessor that locates AsciiMath
  8. * code within the text of a document and replaces it with SCRIPT tags for
  9. * processing by MathJax.
  10. *
  11. * Modified by David Lippman, based on tex2jax.js.
  12. * Additional work by Davide P. Cervone.
  13. *
  14. * ---------------------------------------------------------------------
  15. *
  16. * Copyright (c) 2012-2013 The MathJax Consortium
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */MathJax.Extension.asciimath2jax={version:"2.2",config:{delimiters:[["`","`"]],skipTags:"script noscript style textarea pre code".split(" "),ignoreClass:"asciimath2jax_ignore",processClass:"asciimath2jax_process",preview:"AsciiMath"},PreProcess:function(a){this.configured||(this.config=MathJax.Hub.CombineConfig("asciimath2jax",this.config),this.config.Augment&&MathJax.Hub.Insert(this,this.config.Augment),this.configured=!0);"string"===typeof a&&(a=document.getElementById(a));a||(a=document.body);this.createPatterns()&&this.scanElement(a,a.nextSibling)},createPatterns:function(){var a=[],b,d,c=this.config;this.match={};if(0===c.delimiters.length)return!1;b=0;for(d=c.delimiters.length;b<d;b++)a.push(this.patternQuote(c.delimiters[b][0])),this.match[c.delimiters[b][0]]={mode:"",end:c.delimiters[b][1],pattern:this.endPattern(c.delimiters[b][1])};this.start=new RegExp(a.sort(this.sortLength).join("|"),"g");this.skipTags=new RegExp("^("+c.skipTags.join("|")+")$","i");a=[];MathJax.Hub.config.preRemoveClass&&a.push(MathJax.Hub.config.preRemoveClass);c.ignoreClass&&a.push(c.ignoreClass);this.ignoreClass=a.length?new RegExp("(^| )("+a.join("|")+")( |$)"):/^$/;this.processClass=new RegExp("(^| )("+c.processClass+")( |$)");return!0},patternQuote:function(a){return a.replace(/([\^$(){}+*?\-|\[\]\:\\])/g,"\\$1")},endPattern:function(a){return new RegExp(this.patternQuote(a)+"|\\\\.","g")},sortLength:function(a,b){return a.length!==b.length?b.length-a.length:a==b?0:a<b?-1:1},scanElement:function(a,b,d){for(var c,f,e;a&&a!=b;)"#text"===a.nodeName.toLowerCase()?d||(a=this.scanText(a)):(c="undefined"===typeof a.className?"":a.className,f="undefined"===typeof a.tagName?"":a.tagName,"string"!==typeof c&&(c=String(c)),e=this.processClass.exec(c),!a.firstChild||c.match(/(^| )MathJax/)||!e&&this.skipTags.exec(f)||(c=(d||this.ignoreClass.exec(c))&&!e,this.scanElement(a.firstChild,b,c))),a&&(a=a.nextSibling)},scanText:function(a){if(""==a.nodeValue.replace(/\s+/,""))return a;var b,d;this.search={start:!0};for(this.pattern=this.start;a;){for(this.pattern.lastIndex=0;a&&"#text"===a.nodeName.toLowerCase()&&(b=this.pattern.exec(a.nodeValue));)a=this.search.start?this.startMatch(b,a):this.endMatch(b,a);this.search.matched&&(a=this.encloseMath(a));if(a){do d=a,a=a.nextSibling;while(a&&("br"===a.nodeName.toLowerCase()||"#comment"===a.nodeName.toLowerCase()));if(!a||"#text"!==a.nodeName)return d}}return a},startMatch:function(a,b){var d=this.match[a[0]];null!=d&&(this.search={end:d.end,mode:d.mode,open:b,olen:a[0].length,opos:this.pattern.lastIndex-a[0].length},this.switchPattern(d.pattern));return b},endMatch:function(a,b){a[0]==this.search.end&&(this.search.close=b,this.search.cpos=this.pattern.lastIndex,this.search.clen=this.search.isBeginEnd?0:a[0].length,this.search.matched=!0,b=this.encloseMath(b),this.switchPattern(this.start));return b},switchPattern:function(a){a.lastIndex=this.pattern.lastIndex;this.pattern=a;this.search.start=a===this.start},encloseMath:function(a){a=this.search;var b=a.close,d,c;(b=a.cpos===b.length?b.nextSibling:b.splitText(a.cpos))||(d=b=MathJax.HTML.addText(a.close.parentNode,""));a.close=b;for(c=a.opos?a.open.splitText(a.opos):a.open;c.nextSibling&&c.nextSibling!==b;)c.nodeValue=null!==c.nextSibling.nodeValue?"#comment"===c.nextSibling.nodeName?c.nodeValue+c.nextSibling.nodeValue.replace(/^\[CDATA\[((.|\n|\r)*)\]\]$/,"$1"):c.nodeValue+c.nextSibling.nodeValue:this.msieNewlineBug?c.nodeValue+("br"===c.nextSibling.nodeName.toLowerCase()?"\n":" "):c.nodeValue+" ",c.parentNode.removeChild(c.nextSibling);b=c.nodeValue.substr(a.olen,c.nodeValue.length-a.olen-a.clen);c.parentNode.removeChild(c);"none"!==this.config.preview&&this.createPreview(a.mode,b);c=this.createMathTag(a.mode,b);this.search={};this.pattern.lastIndex=0;d&&d.parentNode.removeChild(d);return c},insertNode:function(a){var b=this.search;b.close.parentNode.insertBefore(a,b.close)},createPreview:function(a,b){var d=this.config.preview;"none"!==d&&("AsciiMath"===d&&(d=[this.filterPreview(b)]),d&&(d=MathJax.HTML.Element("span",{className:MathJax.Hub.config.preRemoveClass},d),this.insertNode(d)))},createMathTag:function(a,b){var d=document.createElement("script");d.type="math/asciimath"+a;MathJax.HTML.setScript(d,b);this.insertNode(d);return d},filterPreview:function(a){return a},msieNewlineBug:MathJax.Hub.Browser.isMSIE&&9>(document.documentMode||0)};MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.asciimath2jax]);MathJax.Ajax.loadComplete("[MathJax]/extensions/asciimath2jax.js");