Diff for /loncom/html/adm/jsMath/plugins/tex2math.js between versions 1.3 and 1.4

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

Removed from v.1.3  
changed lines
  Added in v.1.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>