version 1.1, 2005/12/07 18:57:49
|
version 1.2, 2006/03/27 19:32:29
|
Line 12
|
Line 12
|
* jsMath.Autoload.findMathElements |
* jsMath.Autoload.findMathElements |
* jsMath.Autoload.findTeXstrings |
* jsMath.Autoload.findTeXstrings |
* jsMath.Autoload.findLaTeXstrings |
* jsMath.Autoload.findLaTeXstrings |
|
* jsMath.Autoload.findCustomStrings |
|
* jsMath.Autoload.findCustomSettings |
* |
* |
* which control whether to look for SPAN and DIV elements of class |
* which control whether to look for SPAN and DIV elements of class |
* "math", TeX strings that will be converted by jsMath.ConvertTeX(), or |
* "math", TeX strings that will be converted by jsMath.ConvertTeX(), or |
* LaTeX strings that will be converted by jsMath.ConvertLaTeX(). By |
* LaTeX strings that will be converted by jsMath.ConvertLaTeX(). By |
* default, the first is true and the last two are false. |
* default, the first is true and the second and third are false. The |
|
* findCustomStrings can be used to specify your own delimiters for |
|
* in-line and display mathematics, e.g |
|
* |
|
* jsMath.Autoload.findCustomStrings = [ |
|
* '[math],'[/math]', // start and end in-line math |
|
* '[display]','[/display]' // start and end display math |
|
* ]; |
|
* |
|
* Finally, findCustomSettings can be set to an object reference whose |
|
* name:value pairs control the individual search settings for tex2math. |
|
* (See the plugins/tex2math.js file for more details). |
* |
* |
* If any math strings are found, jsMath.js will be loaded automatically, |
* If any math strings are found, jsMath.js will be loaded automatically, |
* but not loaded otherwise. If any of the last two are true and TeX math |
* but not loaded otherwise. If any of the last four are set and TeX math |
* strings are found, then plugins/tex2ath.js will be loaded |
* strings are found, then plugins/tex2ath.js will be loaded |
* automatically. jsMath.Autoload.needsJsMath will be set to true or |
* automatically. jsMath.Autoload.needsJsMath will be set to true or |
* false depending on whether jsMath needs to be loaded. |
* false depending on whether jsMath needed to be loaded. |
* |
* |
* The value of jsMath.Autoload.element controls the element to be |
* The value of jsMath.Autoload.element controls the element to be |
* searched by the autoload plug-in. If unset, the complete document will |
* searched by the autoload plug-in. If unset, the complete document will |
Line 41
|
Line 54
|
* will be run. Relative URL's are loaded based from the URL containing |
* will be run. Relative URL's are loaded based from the URL containing |
* jsMath.js. |
* jsMath.js. |
* |
* |
* --------------------------------------------------------------------- |
* The autoload plugin can be loaded in the document HEAD or in the BODY. |
|
* If it is loaded in the HEAD, you will need to call jsMath.Autoload.Check() |
|
* at the end of the BODY (say in the window.onload handler) in order to |
|
* get it to check the page for math that needs to be tagged, otherwise load |
|
* the file at the bottom of the BODY and it will run the check automatically. |
* |
* |
* jsMath is free software; you can redistribute it and/or modify |
* You can call jsMath.Autoload.Run() after the check has been performed |
* it under the terms of the GNU General Public License as published by |
* in order to call the appropriate tex2math routines for the given Autoload |
* the Free Software Foundation; either version 2 of the License, or |
* settings. You can call jsMath.Autoload.Run() even when jsMath isn't loaded. |
* (at your option) any later version. |
* |
* |
* --------------------------------------------------------------------- |
* jsMath is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
* |
* You should have received a copy of the GNU General Public License |
* Copyright 2004-2006 by Davide P. Cervone |
* along with jsMath; if not, write to the Free Software |
* |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
* you may not use this file except in compliance with the License. |
|
* You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
*/ |
*/ |
|
|
/*************************************************************************/ |
/*************************************************************************/ |
Line 63
|
Line 86
|
/* |
/* |
* Make sure jsMath.Autoload is available |
* Make sure jsMath.Autoload is available |
*/ |
*/ |
if (jsMath == null) {var jsMath = {}} |
if (jsMath == null) {jsMath = {}} |
if (jsMath.Autoload == null) {jsMath.Autoload = {}} |
if (jsMath.Autoload == null) {jsMath.Autoload = {}} |
|
jsMath.Add = function (dst,src) {for (var id in src) {dst[id] = src[id]}}, |
|
jsMath.document = document; // tex2math needs this |
|
|
/* |
jsMath.Add(jsMath.Autoload,{ |
* Look to see if there are SPAN or DIV elements of class "math". |
|
*/ |
Script: { |
jsMath.Autoload.areMathElements = function (obj) { |
|
if (!obj) {obj = document} |
iframe: null, // the hidden iframe |
if (typeof(obj) == 'string') {obj = document.getElementById(obj)} |
|
if (!obj.getElementsByTagName) {return false} |
/* |
var math = obj.getElementsByTagName('DIV'); |
* Load an external JavaScript file asynchronously |
for (var k = 0; k < math.length; k++) |
*/ |
{if (math[k].className == 'math') {return true}} |
Load: function (url) { |
math = obj.getElementsByTagName('SPAN'); |
this.iframe = document.createElement('iframe'); |
for (var k = 0; k < math.length; k++) |
this.iframe.style.visibility = 'hidden'; |
{if (math[k].className == 'math') {return true}} |
this.iframe.style.position = 'absolute'; |
return false; |
this.iframe.style.width = '0px'; |
}; |
this.iframe.style.height = '0px'; |
|
if (document.body.firstChild) { |
|
document.body.insertBefore(this.iframe,document.body.firstChild); |
|
} else { |
|
document.body.appendChild(this.iframe); |
|
} |
|
this.url = url; setTimeout('jsMath.Autoload.Script.setURL()',100); |
|
}, |
|
endLoad: function () {setTimeout('jsMath.Autoload.Script.AfterLoad()',1)}, |
|
|
|
/* |
|
* Use location.replace() to avoid browsers placing the file in |
|
* the history (and messing up the BACK button action). Not |
|
* completely effective in Firefox 1.0.x. Safari won't handle |
|
* replace() if the document is local (not sure if that's really |
|
* the issue, but that's the only time I see it). |
|
*/ |
|
setURL: function () { |
|
var url = jsMath.Autoload.root+"jsMath-autoload.html"; |
|
var doc = this.iframe.contentDocument; |
|
if (!doc && this.iframe.contentWindow) {doc = this.iframe.contentWindow.document} |
|
if (navigator.vendor == "Apple Computer, Inc." && |
|
document.location.protocol == 'file:') {doc = null} |
|
if (doc) {doc.location.replace(url)} else {this.iframe.src = url} |
|
}, |
|
|
|
/* |
|
* Queue items that need to be postponed until jsMath has run |
|
*/ |
|
queue: [], |
|
Push: function (name,data) {this.queue[this.queue.length] = [name,data]}, |
|
RunStack: function () { |
|
if (this.tex2math) {jsMath.Autoload.Check2(); return} |
|
for (var i = 0; i < this.queue.length; i++) { |
|
var name = this.queue[i][0]; |
|
var data = this.queue[i][1]; |
|
if (data.length == 1) {jsMath[name](data[0])} |
|
else {jsMath[name](data[0],data[1],data[2],data[3])} |
|
} |
|
}, |
|
|
|
AfterLoad: function () {jsMath.Autoload.Script.RunStack()}, |
|
|
|
/* |
|
* Look up the jsMath root directory, if it is not already supplied |
|
*/ |
|
Root: function () { |
|
if (jsMath.Autoload.root) return; |
|
var script = document.getElementsByTagName('script'); |
|
if (script) { |
|
for (var i = 0; i < script.length; i++) { |
|
var src = script[i].src; |
|
if (src && src.match('(^|/)plugins/autoload.js$')) { |
|
jsMath.Autoload.root = src.replace(/plugins\/autoload.js$/,''); |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
|
/* |
}, |
* The patterns used for searching for TeX and LaTeX math strings |
|
* (taken from plugins/tex2math.js). |
/**************************************************************/ |
*/ |
|
jsMath.Autoload.pattern = { |
/* |
tex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\]|(\$\$?)(([^$\\]|\\.)*)\9)/, |
* Load tex2math first (so we can call its search functions |
latex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\])/ |
* to look to see if anything needs to be turned into math) |
}; |
* if it is needed, otherwise go on to the second check. |
|
*/ |
|
Check: function () { |
|
if (this.checked) return; this.checked = 1; |
|
if (this.findTeXstrings || this.findLaTeXstrings || |
|
this.findCustomStrings || this.findCustomSettings) { |
|
this.Script.tex2math = 1; |
|
this.Script.Load('plugins/tex2math.js'); |
|
} else { |
|
if (!jsMath.tex2math) {jsMath.tex2math = {}} |
|
this.Check2(); |
|
} |
|
}, |
|
|
/* |
/* |
* Recursively search for text elements, and check them for |
* Once tex2math is loaded, use it to check for math that |
* TeX or LaTeX strings that would be recognized by tex2math. |
* needs to be tagged for jsMath, and load jsMath if it is needed |
*/ |
*/ |
jsMath.Autoload.FindPattern = function (method,element,recurse) { |
Check2: function () { |
if (!element) { |
this.Script.tex2math = 0; |
if (recurse) {return false}; |
this.needsJsMath = 0; |
element = document.body; |
if (this.findMathElements) { |
} |
this.needsJsMath = this.areMathElements(this.checkElement); |
if (typeof(element) == 'string') {element = document.getElementById(element)} |
|
|
|
var pattern = jsMath.Autoload.pattern[method]; |
|
while (element) { |
|
if (element.nodeName == '#text') { |
|
if (pattern.exec(element.nodeValue.replace(/\n/g,' '))) {return true} |
|
} else if (!element.tagName || |
|
!element.tagName.match(/^(SCRIPT|NOSCRIPT|STYLE|TEXTAREA|PRE)$/i)) { |
|
if (this.FindPattern(method,element.firstChild,1)) {return true}; |
|
} |
} |
element = element.nextSibling; |
jsMath.tex2math.callback = this.tex2mathCallback; |
} |
if (this.findTeXstrings && !this.needsJsMath) { |
return false; |
jsMath.tex2math.ConvertTeX(this.checkElement); |
}; |
} |
|
if (this.findLaTeXstrings && !this.needsJsMath) { |
|
jsMath.tex2math.ConvertLaTeX(this.checkElement); |
|
} |
|
if (this.findCustomSettings && !this.needsJsMath) { |
|
jsMath.tex2math.Convert(this.checkElement,this.findCustomSettings); |
|
} |
|
if (this.findCustomStrings && !this.needsJsMath) { |
|
var s = this.findCustomStrings; |
|
jsMath.tex2math.CustomSearch(s[0],s[1],s[2],s[3]); |
|
jsMath.tex2math.ConvertCustom(this.checkElement); |
|
} |
|
jsMath.tex2math.callback = null; |
|
|
jsMath.Autoload.areTeXstrings = function (obj) {return this.FindPattern('tex',obj,0)} |
if (this.needsJsMath) { |
jsMath.Autoload.areLaTeXstrings = function (obj) {return this.FindPattern('latex',obj,0)} |
this.LoadJsMath(); |
|
} else { |
|
jsMath.Autoload.Script = null; |
|
jsMath.Process = function () {}; |
|
jsMath.ProcessBeforeShowing = function () {}; |
|
jsMath.Synchronize = function () {}; |
|
jsMath.ConvertTeX = function () {}; |
|
jsMath.ConvertTeX2 = function () {}; |
|
jsMath.ConvertLaTeX = function () {}; |
|
jsMath.ConvertCustom = function () {}; |
|
jsMath.CustomSearch = function () {}; |
|
jsMath.Macro = function () {}; |
|
jsMath.Autoload.Run = function () {}; |
|
} |
|
}, |
|
|
/* |
/* |
* When math tags are found, load the jsMath.js file. If we looked |
* A callback used in the tex2math searches to signal that |
* for TeX and LaTeX strings, load the tex2math as well. |
* some math has been found. |
*/ |
*/ |
jsMath.Autoload.LoadJsMath = function () { |
tex2mathCallback: function () { |
if (!jsMath.Autoload.root) { |
jsMath.Autoload.needsJsMath = 1; |
var script = document.getElementsByTagName('SCRIPT'); |
return false; |
if (script) { |
}, |
for (var i = 0; i < script.length; i++) { |
|
var src = script[i].src; |
/* |
if (src && src.match('(^|/)plugins/autoload.js$')) { |
* jsMath.Autoload.Run() can be called to perform the |
jsMath.Autoload.root = src.replace(/plugins\/autoload.js$/,''); |
* tex2math calls given by the Autoload parameters. |
break; |
*/ |
} |
Run: function (data) {this.Script.Push('Autorun',[data])}, |
} |
|
|
Autorun: function () { |
|
if (this.findTeXstrings) {jsMath.ConvertTeX(this.checkElement)} |
|
if (this.findLaTeXstrings) {jsMath.ConvertLaTeX(this.checkElement)} |
|
if (this.findCustomSettings) { |
|
jsMath.Synchronize(function () { |
|
jsMath.tex2math.Convert(jsMath.Autoload.checkElement, |
|
jsMath.Autoload.findCustomSettings); |
|
}); |
} |
} |
} |
if (this.findCustomStrings) { |
if (jsMath.Autoload.root) { |
var s = this.findCustomStrings; |
document.write('<SCRIPT SRC="'+jsMath.Autoload.root+'jsMath.js"></'+'SCRIPT>'); |
jsMath.CustomSearch(s[0],s[1],s[2],s[3]); |
if (jsMath.Autoload.findTeXstrings || jsMath.Autoload.findLaTeXstrings) |
jsMath.ConvertCustom(this.checkElement); |
{document.write('<SCRIPT SRC="'+jsMath.Autoload.root+'plugins/tex2math.js"></'+'SCRIPT>')} |
} |
} else { |
}, |
alert("Can't determine URL for jsMath.js"); |
|
} |
|
} |
|
|
|
/* |
/* |
* Load any specified fonts |
* Look to see if there are SPAN or DIV elements of class "math". |
*/ |
*/ |
jsMath.Autoload.LoadExtraFonts = function () { |
areMathElements: function (obj) { |
var fonts = jsMath.Autoload.loadFonts; |
if (!obj) {obj = document} |
if (typeof(fonts) != 'object') {fonts = [fonts]} |
if (typeof(obj) == 'string') {obj = document.getElementById(obj)} |
for (var i in fonts) { |
if (!obj.getElementsByTagName) {return false} |
document.writeln('<SCRIPT>jsMath.Font.Load("'+fonts[i]+'")</'+'SCRIPT>'); |
var math = obj.getElementsByTagName('div'); |
|
for (var k = 0; k < math.length; k++) |
|
{if (math[k].className == 'math') {return true}} |
|
math = obj.getElementsByTagName('span'); |
|
for (var k = 0; k < math.length; k++) |
|
{if (math[k].className == 'math') {return true}} |
|
return false; |
|
}, |
|
|
|
/* |
|
* When math tags are found, load the jsMath.js file, |
|
* and afterward, load any auxiliary files or fonts, |
|
* and then do any pending commands. |
|
*/ |
|
LoadJsMath: function () { |
|
if (this.root) { |
|
this.setMessage('Loading jsMath...'); |
|
this.Script.AfterLoad = this.afterLoad; |
|
this.Script.Load('jsMath.js'); |
|
} else { |
|
alert("Can't determine URL for jsMath.js"); |
|
} |
|
}, |
|
afterLoad: function () { |
|
if (jsMath.tex2math.window) {jsMath.tex2math.window.jsMath = jsMath} |
|
// |
|
// Handle MSIE bug where jsMath.window both is and is not the actual window |
|
// |
|
if (jsMath.browser == 'MSIE') {window.onscroll = jsMath.window.onscroll}; |
|
var fonts = jsMath.Autoload.loadFonts; |
|
if (fonts) { |
|
if (typeof(fonts) != 'object') {fonts = [fonts]} |
|
for (var i in fonts) {jsMath.Font.Load(fonts[i])} |
|
} |
|
var files = jsMath.Autoload.loadFiles; |
|
if (files) { |
|
if (typeof(files) != 'object') {files = [files]} |
|
for (var i in files) {jsMath.Setup.Script(files[i])} |
|
} |
|
jsMath.Synchronize(function () {jsMath.Autoload.Script.RunStack()}); |
|
jsMath.Autoload.setMessage(); |
|
}, |
|
|
|
/* |
|
* Display a message in a small box at the bottom of the screen |
|
*/ |
|
setMessage: function (message) { |
|
if (message) { |
|
this.div = document.createElement('div'); |
|
if (!document.body.hasChildNodes) {document.body.appendChild(this.div)} |
|
else {document.body.insertBefore(this.div,document.body.firstChild)} |
|
var style = { |
|
position:'absolute', bottom:'1px', left:'2px', |
|
backgroundColor:'#E6E6E6', border:'solid 1px #959595', |
|
margin:'0px', padding:'1px 8px', zIndex:102, |
|
color:'black', fontSize:'75%', width:'auto' |
|
}; |
|
for (var id in style) {this.div.style[id] = style[id]} |
|
this.div.appendChild(jsMath.document.createTextNode(message)); |
|
} else { |
|
this.div.firstChild.nodeValue = ""; |
|
this.div.style.visibility = 'hidden'; |
|
} |
} |
} |
} |
|
|
}); |
|
|
/* |
/* |
* Load any specified files |
* Queue these so we can do them after jsMath has been loaded |
*/ |
*/ |
jsMath.Autoload.LoadExtraFiles = function () { |
jsMath.Add(jsMath,{ |
var files = jsMath.Autoload.loadFiles; |
Process: function (data) {jsMath.Autoload.Script.Push('Process',[data])}, |
if (typeof(files) != 'object') {files = [files]} |
ProcessBeforeShowing: function (data) {jsMath.Autoload.Script.Push('ProcessBeforeShowing',[data])}, |
for (var i in files) { |
ConvertTeX: function (data) {jsMath.Autoload.Script.Push('ConvertTeX',[data])}, |
document.writeln('<SCRIPT>jsMath.Setup.Script("'+files[i]+'")<'+'/SCRIPT>'); |
ConvertTeX2: function (data) {jsMath.Autoload.Script.Push('ConvertTeX2',[data])}, |
} |
ConvertLaTeX: function (data) {jsMath.Autoload.Script.Push('ConvertLaTeX',[data])}, |
} |
ConvertCustom: function (data) {jsMath.Autoload.Script.Push('ConvertCustom',[data])}, |
|
CustomSearch: function (d1,d2,d3,d4) {jsMath.Autoload.Script.Push('CustomSearch',[d1,d2,d3,d4])}, |
|
Synchronize: function (data) {jsMath.Autoload.Script.Push('Synchronize',[data])}, |
|
Macro: function (cs,def,params) {jsMath.Autoload.Script.Push('Macro',[cs,def,params])}, |
|
Autorun: function () {jsMath.Autoload.Autorun()} |
|
}); |
|
|
/* |
/* |
* The default settings |
* Initialize |
*/ |
*/ |
|
|
if (jsMath.Autoload.findMathElements == null) {jsMath.Autoload.findMathElements = 1} |
if (jsMath.Autoload.findMathElements == null) {jsMath.Autoload.findMathElements = 1} |
if (jsMath.Autoload.findTeXstrings == null) {jsMath.Autoload.findTeXstrings = 0} |
if (jsMath.Autoload.findTeXstrings == null) {jsMath.Autoload.findTeXstrings = 0} |
if (jsMath.Autoload.findLaTeXstrings == null) {jsMath.Autoload.findLaTeXstrings = 0} |
if (jsMath.Autoload.findLaTeXstrings == null) {jsMath.Autoload.findLaTeXstrings = 0} |
|
|
/* |
jsMath.Autoload.Script.Root(); |
* Look for the requested math strings (stop after the first is found) |
if (document.body) {jsMath.Autoload.Check()} |
*/ |
|
jsMath.Autoload.needsJsMath = 0; |
|
if (jsMath.Autoload.findMathElements) { |
|
jsMath.Autoload.needsJsMath = |
|
jsMath.Autoload.areMathElements(jsMath.Autoload.checkElement); |
|
} |
|
if (jsMath.Autoload.findTeXstrings && !jsMath.Autoload.needsJsMath) { |
|
jsMath.Autoload.needsJsMath = |
|
jsMath.Autoload.areTeXstrings(jsMath.Autoload.checkElement); |
|
} |
|
if (jsMath.Autoload.findLaTeXstrings && !jsMath.Autoload.needsJsMath) { |
|
jsMath.Autoload.needsJsMath = |
|
jsMath.Autoload.areLaTeXstrings(jsMath.Autoload.checkElement); |
|
} |
|
|
|
/* |
|
* If math was found, load jsMath.js and tex2math.js (if needed). |
|
* Otherwise, define the routines that are normally called |
|
* to process the page, so the author doesn't have to do a |
|
* check before calling these. |
|
*/ |
|
if (jsMath.Autoload.needsJsMath) { |
|
jsMath.Autoload.LoadJsMath(); |
|
jsMath.Autoload.LoadExtraFonts(); |
|
jsMath.Autoload.LoadExtraFiles(); |
|
} else { |
|
jsMath.Process = function () {}; |
|
jsMath.ProcessBeforeShowing = function () {}; |
|
jsMath.ConvertTeX = function () {}; |
|
jsMath.ConvertLaTeX = function () {}; |
|
} |
|