--- loncom/html/adm/jsMath/plugins/tex2math.js 2006/05/17 22:19:38 1.3 +++ loncom/html/adm/jsMath/plugins/tex2math.js 2007/10/09 21:29:28 1.4 @@ -3,14 +3,14 @@ * * Part of the jsMath package for mathematics on the web. * - * This file is a plugin that searches text wthin a web page + * This file is a plugin that searches text within a web page * for \(...\), \[...\], $...$ and $$...$$ and converts them to * the appropriate ... or *
...
tags. * * --------------------------------------------------------------------- * - * Copyright 2004-2006 by Davide P. Cervone + * Copyright 2004-2007 by Davide P. Cervone * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,6 +44,7 @@ jsMath.Add(jsMath.tex2math,{ this.Convert(element,{ processSingleDollars: 1, processDoubleDollars: 1, processSlashParens: 1, processSlashBrackets: 1, + processLaTeXenvironments: 0, custom: 0, fixEscapedDollars: 1 }); }, @@ -52,6 +53,7 @@ jsMath.Add(jsMath.tex2math,{ this.Convert(element,{ processSingleDollars: 0, processDoubleDollars: 1, processSlashParens: 1, processSlashBrackets: 1, + processLaTeXenvironments: 0, custom: 0, fixEscapedDollars: 0 }); }, @@ -60,6 +62,7 @@ jsMath.Add(jsMath.tex2math,{ this.Convert(element,{ processSingleDollars: 0, processDoubleDollars: 0, processSlashParens: 1, processSlashBrackets: 1, + processLaTeXenvironments: 1, custom: 0, fixEscapedDollars: 0 }); }, @@ -87,7 +90,7 @@ jsMath.Add(jsMath.tex2math,{ }, patternQuote: function (s) { - s = s.replace(/([\^(){}+*?\-|\[\]\:\\])/g,'\\$1'); + s = s.replace(/([\^$(){}+*?\-|\[\]\:\\])/g,'\\$1'); return s; }, @@ -133,7 +136,7 @@ jsMath.Add(jsMath.tex2math,{ } if (this.processDoubleDollars || this.processSingleDollars || this.processSlashParens || this.processSlashBrackets || - this.custom) this.ScanElement(element); + this.processLaTeXenvironments || this.custom) this.ScanElement(element); } }, @@ -145,14 +148,16 @@ jsMath.Add(jsMath.tex2math,{ if (!element) {element = jsMath.document.body} if (typeof(element) == 'string') {element = jsMath.document.getElementById(element)} while (element) { - if (element.className == null) {element.className = ''} if (element.nodeName == '#text') { if (!ignore) {element = this.ScanText(element)} - } else if (element.firstChild && element.className != 'math') { - var off = ignore || element.className == 'tex2math_ignore' || - (element.tagName && element.tagName.match(/^(script|noscript|style|textarea|pre)$/i)); - off = off && element.className != 'tex2math_process'; - this.ScanElement(element.firstChild,off); + } else { + if (element.className == null) {element.className = ''} + if (element.firstChild && element.className != 'math') { + var off = ignore || element.className == 'tex2math_ignore' || + (element.tagName && element.tagName.match(/^(script|noscript|style|textarea|pre)$/i)); + off = off && element.className != 'tex2math_process'; + this.ScanElement(element.firstChild,off); + } } if (element) {element = element.nextSibling} } @@ -192,23 +197,23 @@ jsMath.Add(jsMath.tex2math,{ stdProcessMatch: function (match,index,element) { if (match == this.search.end) { this.search.close = element; - this.search.clength = match.length; this.search.cpos = this.pattern.lastIndex; + this.search.clength = (match.substr(0,4) == '\\end' ? 0 : match.length); element = this.EncloseMath(element); } else { switch (match) { case '\\(': - if (this.search.end == null || - (this.search.end != '$' && this.search.end != '$$') && + if ((this.search.end == null || + (this.search.end != '$' && this.search.end != '$$')) && this.processSlashParens) { this.ScanMark('span',element,'\\)'); } break; case '\\[': - if (this.search.end == null || - (this.search.end != '$' && this.search.end != '$$') && - this.processSlashBrackets) { + if ((this.search.end == null || + (this.search.end != '$' && this.search.end != '$$')) && + this.processSlashBrackets) { this.ScanMark('div',element,'\\]'); } break; @@ -230,6 +235,15 @@ jsMath.Add(jsMath.tex2math,{ if (this.search.end == null && this.fixEscapedDollars) { element.nodeValue = element.nodeValue.substr(0,index) + element.nodeValue.substr(index+1); + this.pattern.lastIndex--; + } + break; + + default: + if (match.substr(0,6) == '\\begin' && this.search.end == null && + this.processLaTeXenvironments) { + this.ScanMark('span',element,'\\end'+match.substr(6)); + this.search.olength = 0; } break; } @@ -247,7 +261,9 @@ jsMath.Add(jsMath.tex2math,{ this.search.close = element; this.search.clength = match.length; this.search.cpos = this.pattern.lastIndex; - this.search.matched = 1; + if (match == this.inLineOpen || match == this.displayOpen) { + element = this.EncloseMath(element); + } else {this.search.matched = 1} } else if (match == this.inLineOpen) { if (this.search.matched) {element = this.EncloseMath(element)} this.ScanMark('span',element,this.inLineClose); @@ -277,8 +293,7 @@ jsMath.Add(jsMath.tex2math,{ * SPAN or DIV element marked as CLASS="math". */ EncloseMath: function (element) { - if (this.callback) {if (!this.callback()) {return null}} - var search = this.search; + var search = this.search; search.end = null; var close = search.close; if (search.cpos == close.length) {close = close.nextSibling} else {close = close.splitText(search.cpos)} @@ -335,14 +350,8 @@ jsMath.Add(jsMath.tex2math,{ tag.className = "math"; text = text.replace(//g,'>'); if (type == 'div') { - tag.className = ""; - tag.style.width = "100%"; tag.style.margin = jsMath.tex2math.margin; - tag.style.display = "inline-block"; + tag.className = "tex2math_div"; text = '\\displaystyle{'+text+'}'; - if (jsMath.tex2math.center) { - tag.style.textAlign = "center"; - text = ''+text+'' - } } tag.innerHTML = text; return tag; @@ -351,28 +360,18 @@ jsMath.Add(jsMath.tex2math,{ /*******************************************************************/ Init: function () { - - if (this.inited || !jsMath.browser) return + if (!jsMath.browser && document.all && !window.opera) { + jsMath.browser = 'MSIE'; + jsMath.platform = (navigator.platform.match(/Mac/) ? "mac" : + navigator.platform.match(/Win/) ? "pc" : "unix"); + } + if (this.inited || !jsMath.browser) return; /* * MSIE can't handle the DIV's properly, so we need to do it by - * hand. Look up the style for typeset math to see if the user - * has changed it, and get whether it is centered or indented - * so we can mirror that using a SPAN + * hand. Use an extra SPAN that uses CSS to act like a DIV. */ - if (jsMath.browser == 'MSIE' && navigator.platform == 'Win32') { - this.createMathTag = this.MSIEcreateMathTag; - this.margin = ""; this.center = 0; - for (var i = 0; i < jsMath.document.styleSheets.length; i++) { - var rules = jsMath.document.styleSheets[i].cssRules; - if (!rules) {rules = jsMath.document.styleSheets[i].rules} - for (var j = 0; j < rules.length; j++) { - if (rules[j].selectorText.toLowerCase() == 'div.typeset') { - if (rules[j].style.margin != "") {this.margin = rules[j].style.margin} - this.center = (rules[j].style.textAlign == 'center'); - } - } - } - } + if (jsMath.browser == 'MSIE' && jsMath.platform == 'pc') + {this.createMathTag = this.MSIEcreateMathTag} this.inited = 1; }, @@ -394,6 +393,6 @@ jsMath.Add(jsMath.tex2math,{ if (jsMath.Controls.cookie.tex2math == null) {jsMath.Controls.cookie.tex2math = 1} if (jsMath.tex2math.allowDisableTag == null) {jsMath.tex2math.allowDisableTag = 1} jsMath.tex2math.TestPatterns(); -jsMath.tex2math.createPattern('stdPattern',/(\\[\(\)\[\]$]|\$\$|\$)/g); +jsMath.tex2math.createPattern('stdPattern',/(\\[\(\)\[\]$]|\$\$|\$|\\(begin|end)\{[^}]+\})/g); }