tex2jax.js 7.0 KB

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