--- loncom/html/adm/jsMath/plugins/autoload.js 2005/12/07 18:57:49 1.1
+++ loncom/html/adm/jsMath/plugins/autoload.js 2007/10/09 21:29:28 1.4
@@ -9,20 +9,31 @@
*
* You can control the items to look for via the variables
*
- * jsMath.Autoload.findMathElements
* jsMath.Autoload.findTeXstrings
* jsMath.Autoload.findLaTeXstrings
+ * jsMath.Autoload.findCustomStrings
+ * jsMath.Autoload.findCustomSettings
*
- * which control whether to look for SPAN and DIV elements of class
- * "math", TeX strings that will be converted by jsMath.ConvertTeX(), or
- * LaTeX strings that will be converted by jsMath.ConvertLaTeX(). By
- * default, the first is true and the last two are false.
+ * which control whether to look for TeX strings that will be converted
+ * by jsMath.ConvertTeX(), or LaTeX strings that will be converted by
+ * jsMath.ConvertLaTeX(). By 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,
- * 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
* 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
* searched by the autoload plug-in. If unset, the complete document will
@@ -41,21 +52,31 @@
* will be run. Relative URL's are loaded based from the URL containing
* 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
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (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 can call jsMath.Autoload.Run() after the check has been performed
+ * in order to call the appropriate tex2math routines for the given Autoload
+ * settings. You can call jsMath.Autoload.Run() even when jsMath isn't loaded.
+ *
+ * ---------------------------------------------------------------------
*
- * You should have received a copy of the GNU General Public License
- * along with jsMath; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Copyright 2004-2006 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.
+ * 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.
*/
/*************************************************************************/
@@ -63,146 +84,317 @@
/*
* Make sure jsMath.Autoload is available
*/
-if (jsMath == null) {var jsMath = {}}
+if (!window.jsMath) {window.jsMath = {}}
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
-/*
- * Look to see if there are SPAN or DIV elements of class "math".
- */
-jsMath.Autoload.areMathElements = function (obj) {
- if (!obj) {obj = document}
- if (typeof(obj) == 'string') {obj = document.getElementById(obj)}
- if (!obj.getElementsByTagName) {return false}
- 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;
-};
-
-/*
- * The patterns used for searching for TeX and LaTeX math strings
- * (taken from plugins/tex2math.js).
- */
-jsMath.Autoload.pattern = {
- tex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\]|(\$\$?)(([^$\\]|\\.)*)\9)/,
- latex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\])/
-};
-
-/*
- * Recursively search for text elements, and check them for
- * TeX or LaTeX strings that would be recognized by tex2math.
- */
-jsMath.Autoload.FindPattern = function (method,element,recurse) {
- if (!element) {
- if (recurse) {return false};
- element = document.body;
- }
- 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;
- }
- return false;
-};
+jsMath.Add(jsMath.Autoload,{
+
+ Script: {
+
+ request: null, // XMLHttpRequest object (if we can get it)
+ iframe: null, // the hidden iframe (if not)
+
+ /*
+ * Get XMLHttpRequest object, if possible, and look up the URL root
+ * (MSIE can't use xmlReuest to load local files, so avoid that)
+ */
+ Init: function () {
+ if (!(document.URL && document.URL.match(/^file:\/\/.*\\/))) {
+ if (window.XMLHttpRequest) {try {this.request = new XMLHttpRequest} catch (err) {}}
+ if (!this.request && window.ActiveXObject) {
+ var xml = ["MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0",
+ "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
+ for (var i = 0; i < xml.length && !this.request; i++) {
+ try {this.request = new ActiveXObject(xml[i])} catch (err) {}
+ }
+ }
+ }
+ this.Root();
+ },
-jsMath.Autoload.areTeXstrings = function (obj) {return this.FindPattern('tex',obj,0)}
-jsMath.Autoload.areLaTeXstrings = function (obj) {return this.FindPattern('latex',obj,0)}
+ /*
+ * Load an external JavaScript file
+ */
+ Load: function (url) {
+ if (this.request) {
+ setTimeout(function () {jsMath.Autoload.Script.xmlLoad(url)},1);
+ } else {
+ this.startLoad(url);
+ }
+ },
-/*
- * When math tags are found, load the jsMath.js file. If we looked
- * for TeX and LaTeX strings, load the tex2math as well.
- */
-jsMath.Autoload.LoadJsMath = function () {
- if (!jsMath.Autoload.root) {
- 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;
+ /*
+ * Load an external JavaScript file via XMLHttpRequest
+ */
+ xmlLoad: function (url) {
+ try {
+ this.request.open("GET",jsMath.Autoload.root+url,false);
+ this.request.send(null);
+ } catch (err) {
+ throw "autoload: can't load the file '"+url+"'\n"
+ + "Message: "+err.message;
+ }
+ if (this.request.status && this.request.status >= 400) {
+ throw "autoload: can't load the file '"+url+"'\n"
+ + "Error status: "+this.request.status;
+ }
+ window.eval(this.request.responseText);
+ this.endLoad();
+ },
+
+ /*
+ * Load an external JavaScript file via jsMath-autoload.html
+ */
+ startLoad: function (url) {
+ this.iframe = document.createElement('iframe');
+ this.iframe.style.visibility = 'hidden';
+ this.iframe.style.position = 'absolute';
+ 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])}
+ }
+ this.queue = [];
+ },
+
+ 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;
+ }
}
}
}
- }
- if (jsMath.Autoload.root) {
- document.write('