Debian Codegraph @

unpacked/extensions/asciimath2jax.js

raw
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3
4 /*************************************************************
5 *
6 * MathJax/extensions/asciimath2jax.js
7 *
8 * Implements the AsciiMath to Jax preprocessor that locates AsciiMath
9 * code within the text of a document and replaces it with SCRIPT tags for
10 * processing by MathJax.
11 *
12 * Modified by David Lippman, based on tex2jax.js.
13 * Additional work by Davide P. Cervone.
14 *
15 * ---------------------------------------------------------------------
16 *
17 * Copyright (c) 2012-2020 The MathJax Consortium
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License");
20 * you may not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS,
27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 */
31
32 MathJax.Extension.asciimath2jax = {
33 version: "2.7.9",
34 config: {
35 delimiters: [['`','`']], // The star/stop delimiter pairs for asciimath code
36
37 skipTags: ["script","noscript","style","textarea","pre","code","annotation","annotation-xml"],
38 // The names of the tags whose contents will not be
39 // scanned for math delimiters
40
41 ignoreClass: "asciimath2jax_ignore", // the class name of elements whose contents should
42 // NOT be processed by asciimath2jax. Note that this
43 // is a regular expression, so be sure to quote any
44 // regexp special characters
45
46 processClass: "asciimath2jax_process", // the class name of elements whose contents SHOULD
47 // be processed when they appear inside ones that
48 // are ignored. Note that this is a regular expression,
49 // so be sure to quote any regexp special characters
50
51 preview: "AsciiMath" // set to "none" to not insert MathJax_Preview spans
52 // or set to an array specifying an HTML snippet
53 // to use the same preview for every equation.
54
55 },
56
57 //
58 // Tags to ignore when searching for AsciiMath in the page
59 //
60 ignoreTags: {
61 br: (MathJax.Hub.Browser.isMSIE && document.documentMode < 9 ? "\n" : " "),
62 wbr: "",
63 "#comment": ""
64 },
65
66 PreProcess: function (element) {
67 if (!this.configured) {
68 this.config = MathJax.Hub.CombineConfig("asciimath2jax",this.config);
69 if (this.config.Augment) {MathJax.Hub.Insert(this,this.config.Augment)}
70 this.configured = true;
71 }
72 if (typeof(element) === "string") {element = document.getElementById(element)}
73 if (!element) {element = document.body}
74 if (this.createPatterns()) {this.scanElement(element,element.nextSibling)}
75 },
76
77 createPatterns: function () {
78 var starts = [], i, m, config = this.config; this.match = {};
79 if (config.delimiters.length === 0) {return false}
80 for (i = 0, m = config.delimiters.length; i < m; i++) {
81 starts.push(this.patternQuote(config.delimiters[i][0]));
82 this.match[config.delimiters[i][0]] = {
83 mode: "",
84 end: config.delimiters[i][1],
85 pattern: this.endPattern(config.delimiters[i][1])
86 };
87 }
88 this.start = new RegExp(starts.sort(this.sortLength).join("|"),"g");
89 this.skipTags = new RegExp("^("+config.skipTags.join("|")+")$","i");
90 var ignore = [];
91 if (MathJax.Hub.config.preRemoveClass) {ignore.push(MathJax.Hub.config.preRemoveClass)}
92 if (config.ignoreClass) {ignore.push(config.ignoreClass)}
93 this.ignoreClass = (ignore.length ? new RegExp("(^| )("+ignore.join("|")+")( |$)") : /^$/);
94 this.processClass = new RegExp("(^| )("+config.processClass+")( |$)");
95 return true;
96 },
97
98 patternQuote: function (s) {return s.replace(/([\^$(){}+*?\-|\[\]\:\\])/g,'\\$1')},
99
100 endPattern: function (end) {
101 return new RegExp(this.patternQuote(end)+"|\\\\.","g");
102 },
103
104 sortLength: function (a,b) {
105 if (a.length !== b.length) {return b.length - a.length}
106 return (a == b ? 0 : (a < b ? -1 : 1));
107 },
108
109 scanElement: function (element,stop,ignore) {
110 var cname, tname, ignoreChild, process;
111 while (element && element != stop) {
112 if (element.nodeName.toLowerCase() === '#text') {
113 if (!ignore) {element = this.scanText(element)}
114 } else {
115 cname = (typeof(element.className) === "undefined" ? "" : element.className);
116 tname = (typeof(element.tagName) === "undefined" ? "" : element.tagName);
117 if (typeof(cname) !== "string") {cname = String(cname)} // jsxgraph uses non-string class names!
118 process = this.processClass.exec(cname);
119 if (element.firstChild && !cname.match(/(^| )MathJax/) &&
120 (process || !this.skipTags.exec(tname))) {
121 ignoreChild = (ignore || this.ignoreClass.exec(cname)) && !process;
122 this.scanElement(element.firstChild,stop,ignoreChild);
123 }
124 }
125 if (element) {element = element.nextSibling}
126 }
127 },
128
129 scanText: function (element) {
130 if (element.nodeValue.replace(/\s+/,'') == '') {return element}
131 var match, prev, pos = 0, rescan;
132 this.search = {start: true};
133 this.pattern = this.start;
134 while (element) {
135 rescan = null;
136 this.pattern.lastIndex = pos || 0; pos = 0;
137 while (element && element.nodeName.toLowerCase() === '#text' &&
138 (match = this.pattern.exec(element.nodeValue))) {
139 if (this.search.start) {element = this.startMatch(match,element)}
140 else {element = this.endMatch(match,element)}
141 }
142 if (this.search.matched) element = this.encloseMath(element);
143 else if (!this.search.start) rescan = this.search;
144 if (element) {
145 do {prev = element; element = element.nextSibling}
146 while (element && this.ignoreTags[element.nodeName.toLowerCase()] != null);
147 if (!element || element.nodeName !== '#text') {
148 if (!rescan) return prev;
149 element = rescan.open;
150 pos = rescan.opos + rescan.olen;
151 this.search = {start: true};
152 this.pattern = this.start;
153 }
154 }
155 }
156 return element;
157 },
158
159 startMatch: function (match,element) {
160 var delim = this.match[match[0]];
161 if (delim != null) {
162 this.search = {
163 end: delim.end, mode: delim.mode,
164 open: element, olen: match[0].length,
165 opos: this.pattern.lastIndex - match[0].length
166 };
167 this.switchPattern(delim.pattern);
168 }
169 return element;
170 },
171
172 endMatch: function (match,element) {
173 if (match[0] == this.search.end) {
174 this.search.close = element;
175 this.search.cpos = this.pattern.lastIndex;
176 this.search.clen = (this.search.isBeginEnd ? 0 : match[0].length);
177 this.search.matched = true;
178 element = this.encloseMath(element);
179 this.switchPattern(this.start);
180 }
181 return element;
182 },
183
184 switchPattern: function (pattern) {
185 pattern.lastIndex = this.pattern.lastIndex;
186 this.pattern = pattern;
187 this.search.start = (pattern === this.start);
188 },
189
190 encloseMath: function (element) {
191 var search = this.search, close = search.close, CLOSE, math, next;
192 if (search.cpos === close.length) {close = close.nextSibling}
193 else {close = close.splitText(search.cpos)}
194 if (!close) {CLOSE = close = MathJax.HTML.addText(search.close.parentNode,"")}
195 search.close = close;
196 math = (search.opos ? search.open.splitText(search.opos) : search.open);
197 while ((next = math.nextSibling) && next !== close) {
198 if (next.nodeValue !== null) {
199 if (next.nodeName === "#comment") {
200 math.nodeValue += next.nodeValue.replace(/^\[CDATA\[((.|\n|\r)*)\]\]$/,"$1");
201 } else {
202 math.nodeValue += math.nextSibling.nodeValue;
203 }
204 } else {
205 var ignore = this.ignoreTags[next.nodeName.toLowerCase()];
206 math.nodeValue += (ignore == null ? " " : ignore);
207 }
208 math.parentNode.removeChild(next);
209 }
210 var AM = math.nodeValue.substr(search.olen,math.nodeValue.length-search.olen-search.clen);
211 math.parentNode.removeChild(math);
212 if (this.config.preview !== "none") {this.createPreview(search.mode,AM)}
213 math = this.createMathTag(search.mode,AM);
214 this.search = {}; this.pattern.lastIndex = 0;
215 if (CLOSE) {CLOSE.parentNode.removeChild(CLOSE)}
216 return math;
217 },
218
219 insertNode: function (node) {
220 var search = this.search;
221 search.close.parentNode.insertBefore(node,search.close);
222 },
223
224 createPreview: function (mode,asciimath) {
225 var previewClass = MathJax.Hub.config.preRemoveClass;
226 var preview = this.config.preview;
227 if (preview === "none") return;
228 if ((this.search.close.previousSibling||{}).className === previewClass) return;
229 if (preview === "AsciiMath") {preview = [this.filterPreview(asciimath)]}
230 if (preview) {
231 preview = MathJax.HTML.Element("span",{className:previewClass},preview);
232 this.insertNode(preview);
233 }
234 },
235
236 createMathTag: function (mode,asciimath) {
237 var script = document.createElement("script");
238 script.type = "math/asciimath" + mode;
239 MathJax.HTML.setScript(script,asciimath);
240 this.insertNode(script);
241 return script;
242 },
243
244 filterPreview: function (asciimath) {return asciimath}
245
246 };
247
248 // We register the preprocessors with the following priorities:
249 // - mml2jax.js: 5
250 // - jsMath2jax.js: 8
251 // - asciimath2jax.js, tex2jax.js: 10 (default)
252 // See issues 18 and 484 and the other *2jax.js files.
253 MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.asciimath2jax]);
254 MathJax.Ajax.loadComplete("[MathJax]/extensions/asciimath2jax.js");