Annotation of loncom/html/adm/jsMath/plugins/autoload.js, revision 1.1
1.1 ! albertel 1: /*
! 2: * autoload.js
! 3: *
! 4: * Part of the jsMath package for mathematics on the web.
! 5: *
! 6: * This file is a plugin that checks if a page contains any math
! 7: * that must be processed by jsMath, and only loads jsMath.js
! 8: * when there is.
! 9: *
! 10: * You can control the items to look for via the variables
! 11: *
! 12: * jsMath.Autoload.findMathElements
! 13: * jsMath.Autoload.findTeXstrings
! 14: * jsMath.Autoload.findLaTeXstrings
! 15: *
! 16: * which control whether to look for SPAN and DIV elements of class
! 17: * "math", TeX strings that will be converted by jsMath.ConvertTeX(), or
! 18: * LaTeX strings that will be converted by jsMath.ConvertLaTeX(). By
! 19: * default, the first is true and the last two are false.
! 20: *
! 21: * If any math strings are found, jsMath.js will be loaded automatically,
! 22: * but not loaded otherwise. If any of the last two are true and TeX math
! 23: * strings are found, then plugins/tex2ath.js will be loaded
! 24: * automatically. jsMath.Autoload.needsJsMath will be set to true or
! 25: * false depending on whether jsMath needs to be loaded.
! 26: *
! 27: * The value of jsMath.Autoload.element controls the element to be
! 28: * searched by the autoload plug-in. If unset, the complete document will
! 29: * be searched. If set to a string, the element with that name will be
! 30: * searched. If set to a DOM object, that object and its children will
! 31: * be searched.
! 32: *
! 33: * Finally, there are two additional parameters that control files to
! 34: * be loaded after jsMath.js, should it be needed. These are
! 35: *
! 36: * jsMath.Autoload.loadFonts
! 37: * jsMath.Autoload.loadFiles
! 38: *
! 39: * If jsMath.js is loaded, the fonts contained in the loadFonts array
! 40: * will be loaded, and the JavaScript files listed in the loadFiles array
! 41: * will be run. Relative URL's are loaded based from the URL containing
! 42: * jsMath.js.
! 43: *
! 44: * ---------------------------------------------------------------------
! 45: *
! 46: * jsMath is free software; you can redistribute it and/or modify
! 47: * it under the terms of the GNU General Public License as published by
! 48: * the Free Software Foundation; either version 2 of the License, or
! 49: * (at your option) any later version.
! 50: *
! 51: * jsMath is distributed in the hope that it will be useful,
! 52: * but WITHOUT ANY WARRANTY; without even the implied warranty of
! 53: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 54: * GNU General Public License for more details.
! 55: *
! 56: * You should have received a copy of the GNU General Public License
! 57: * along with jsMath; if not, write to the Free Software
! 58: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
! 59: */
! 60:
! 61: /*************************************************************************/
! 62:
! 63: /*
! 64: * Make sure jsMath.Autoload is available
! 65: */
! 66: if (jsMath == null) {var jsMath = {}}
! 67: if (jsMath.Autoload == null) {jsMath.Autoload = {}}
! 68:
! 69: /*
! 70: * Look to see if there are SPAN or DIV elements of class "math".
! 71: */
! 72: jsMath.Autoload.areMathElements = function (obj) {
! 73: if (!obj) {obj = document}
! 74: if (typeof(obj) == 'string') {obj = document.getElementById(obj)}
! 75: if (!obj.getElementsByTagName) {return false}
! 76: var math = obj.getElementsByTagName('DIV');
! 77: for (var k = 0; k < math.length; k++)
! 78: {if (math[k].className == 'math') {return true}}
! 79: math = obj.getElementsByTagName('SPAN');
! 80: for (var k = 0; k < math.length; k++)
! 81: {if (math[k].className == 'math') {return true}}
! 82: return false;
! 83: };
! 84:
! 85: /*
! 86: * The patterns used for searching for TeX and LaTeX math strings
! 87: * (taken from plugins/tex2math.js).
! 88: */
! 89: jsMath.Autoload.pattern = {
! 90: tex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\]|(\$\$?)(([^$\\]|\\.)*)\9)/,
! 91: latex: /((^|[^\\])(\\[^\[\(])*)(\\\((([^\\]|\\[^\)])*)\\\)|\\\[(([^\\]|\\[^\]])*)\\\])/
! 92: };
! 93:
! 94: /*
! 95: * Recursively search for text elements, and check them for
! 96: * TeX or LaTeX strings that would be recognized by tex2math.
! 97: */
! 98: jsMath.Autoload.FindPattern = function (method,element,recurse) {
! 99: if (!element) {
! 100: if (recurse) {return false};
! 101: element = document.body;
! 102: }
! 103: if (typeof(element) == 'string') {element = document.getElementById(element)}
! 104:
! 105: var pattern = jsMath.Autoload.pattern[method];
! 106: while (element) {
! 107: if (element.nodeName == '#text') {
! 108: if (pattern.exec(element.nodeValue.replace(/\n/g,' '))) {return true}
! 109: } else if (!element.tagName ||
! 110: !element.tagName.match(/^(SCRIPT|NOSCRIPT|STYLE|TEXTAREA|PRE)$/i)) {
! 111: if (this.FindPattern(method,element.firstChild,1)) {return true};
! 112: }
! 113: element = element.nextSibling;
! 114: }
! 115: return false;
! 116: };
! 117:
! 118: jsMath.Autoload.areTeXstrings = function (obj) {return this.FindPattern('tex',obj,0)}
! 119: jsMath.Autoload.areLaTeXstrings = function (obj) {return this.FindPattern('latex',obj,0)}
! 120:
! 121: /*
! 122: * When math tags are found, load the jsMath.js file. If we looked
! 123: * for TeX and LaTeX strings, load the tex2math as well.
! 124: */
! 125: jsMath.Autoload.LoadJsMath = function () {
! 126: if (!jsMath.Autoload.root) {
! 127: var script = document.getElementsByTagName('SCRIPT');
! 128: if (script) {
! 129: for (var i = 0; i < script.length; i++) {
! 130: var src = script[i].src;
! 131: if (src && src.match('(^|/)plugins/autoload.js$')) {
! 132: jsMath.Autoload.root = src.replace(/plugins\/autoload.js$/,'');
! 133: break;
! 134: }
! 135: }
! 136: }
! 137: }
! 138: if (jsMath.Autoload.root) {
! 139: document.write('<SCRIPT SRC="'+jsMath.Autoload.root+'jsMath.js"></'+'SCRIPT>');
! 140: if (jsMath.Autoload.findTeXstrings || jsMath.Autoload.findLaTeXstrings)
! 141: {document.write('<SCRIPT SRC="'+jsMath.Autoload.root+'plugins/tex2math.js"></'+'SCRIPT>')}
! 142: } else {
! 143: alert("Can't determine URL for jsMath.js");
! 144: }
! 145: }
! 146:
! 147: /*
! 148: * Load any specified fonts
! 149: */
! 150: jsMath.Autoload.LoadExtraFonts = function () {
! 151: var fonts = jsMath.Autoload.loadFonts;
! 152: if (typeof(fonts) != 'object') {fonts = [fonts]}
! 153: for (var i in fonts) {
! 154: document.writeln('<SCRIPT>jsMath.Font.Load("'+fonts[i]+'")</'+'SCRIPT>');
! 155: }
! 156: }
! 157:
! 158: /*
! 159: * Load any specified files
! 160: */
! 161: jsMath.Autoload.LoadExtraFiles = function () {
! 162: var files = jsMath.Autoload.loadFiles;
! 163: if (typeof(files) != 'object') {files = [files]}
! 164: for (var i in files) {
! 165: document.writeln('<SCRIPT>jsMath.Setup.Script("'+files[i]+'")<'+'/SCRIPT>');
! 166: }
! 167: }
! 168:
! 169: /*
! 170: * The default settings
! 171: */
! 172: if (jsMath.Autoload.findMathElements == null) {jsMath.Autoload.findMathElements = 1}
! 173: if (jsMath.Autoload.findTeXstrings == null) {jsMath.Autoload.findTeXstrings = 0}
! 174: if (jsMath.Autoload.findLaTeXstrings == null) {jsMath.Autoload.findLaTeXstrings = 0}
! 175:
! 176: /*
! 177: * Look for the requested math strings (stop after the first is found)
! 178: */
! 179: jsMath.Autoload.needsJsMath = 0;
! 180: if (jsMath.Autoload.findMathElements) {
! 181: jsMath.Autoload.needsJsMath =
! 182: jsMath.Autoload.areMathElements(jsMath.Autoload.checkElement);
! 183: }
! 184: if (jsMath.Autoload.findTeXstrings && !jsMath.Autoload.needsJsMath) {
! 185: jsMath.Autoload.needsJsMath =
! 186: jsMath.Autoload.areTeXstrings(jsMath.Autoload.checkElement);
! 187: }
! 188: if (jsMath.Autoload.findLaTeXstrings && !jsMath.Autoload.needsJsMath) {
! 189: jsMath.Autoload.needsJsMath =
! 190: jsMath.Autoload.areLaTeXstrings(jsMath.Autoload.checkElement);
! 191: }
! 192:
! 193: /*
! 194: * If math was found, load jsMath.js and tex2math.js (if needed).
! 195: * Otherwise, define the routines that are normally called
! 196: * to process the page, so the author doesn't have to do a
! 197: * check before calling these.
! 198: */
! 199: if (jsMath.Autoload.needsJsMath) {
! 200: jsMath.Autoload.LoadJsMath();
! 201: jsMath.Autoload.LoadExtraFonts();
! 202: jsMath.Autoload.LoadExtraFiles();
! 203: } else {
! 204: jsMath.Process = function () {};
! 205: jsMath.ProcessBeforeShowing = function () {};
! 206: jsMath.ConvertTeX = function () {};
! 207: jsMath.ConvertLaTeX = function () {};
! 208: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>