Debian Codegraph @

unpacked/extensions/tex2jax.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/tex2jax.js
7 *
8 * Implements the TeX to Jax preprocessor that locates TeX code
9 * within the text of a document and replaces it with SCRIPT tags
10 * for processing by MathJax.
11 *
12 * ---------------------------------------------------------------------
13 *
14 * Copyright (c) 2009-2020 The MathJax Consortium
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29 MathJax.Extension.tex2jax = {
30 version: "2.7.9",
31 config: {
32 inlineMath: [ // The start/stop pairs for in-line math
33 // ['$','$'], // (comment out any you don't want, or add your own, but
34 ['\\(','\\)'] // be sure that you don't have an extra comma at the end)
35 ],
36
37 displayMath: [ // The start/stop pairs for display math
38 ['$$','$$'], // (comment out any you don't want, or add your own, but
39 ['\\[','\\]'] // be sure that you don't have an extra comma at the end)
40 ],
41
42 skipTags: ["script","noscript","style","textarea","pre","code","annotation","annotation-xml"],
43 // The names of the tags whose contents will not be
44 // scanned for math delimiters
45
46 ignoreClass: "tex2jax_ignore", // the class name of elements whose contents should
47 // NOT be processed by tex2jax. Note that this
48 // is a regular expression, so be sure to quote any
49 // regexp special characters
50
51 processClass: "tex2jax_process", // the class name of elements whose contents SHOULD
52 // be processed when they appear inside ones that
53 // are ignored. Note that this is a regular expression,
54 // so be sure to quote any regexp special characters
55
56 processEscapes: false, // set to true to allow \$ to produce a dollar without
57 // starting in-line math mode
58
59 processEnvironments: true, // set to true to process \begin{xxx}...\end{xxx} outside
60 // of math mode, false to prevent that
61
62 processRefs: true, // set to true to process \ref{...} outside of math mode
63
64
65 preview: "TeX" // set to "none" to not insert MathJax_Preview spans
66 // or set to an array specifying an HTML snippet
67 // to use the same preview for every equation.
68
69 },
70
71 //
72 // Tags to ignore when searching for TeX in the page
73 //
74 ignoreTags: {
75 br: (MathJax.Hub.Browser.isMSIE && document.documentMode < 9 ? "\n" : " "),
76 wbr: "",
77 "#comment": ""
78 },
79
80 PreProcess: function (element) {
81 if (!this.configured) {
82 this.config = MathJax.Hub.CombineConfig("tex2jax",this.config);
83 if (this.config.Augment) {MathJax.Hub.Insert(this,this.config.Augment)}
84 if (typeof(this.config.previewTeX) !== "undefined" && !this.config.previewTeX)
85 {this.config.preview = "none"} // backward compatibility for previewTeX parameter
86 this.configured = true;
87 }
88 if (typeof(element) === "string") {element = document.getElementById(element)}
89 if (!element) {element = document.body}
90 if (this.createPatterns()) {this.scanElement(element,element.nextSibling)}
91 },
92
93 createPatterns: function () {
94 var starts = [], parts = [], i, m, config = this.config;
95 this.match = {};
96 for (i = 0, m = config.inlineMath.length; i < m; i++) {
97 starts.push(this.patternQuote(config.inlineMath[i][0]));
98 this.match[config.inlineMath[i][0]] = {
99 mode: "",
100 end: config.inlineMath[i][1],
101 pattern: this.endPattern(config.inlineMath[i][1])
102 };
103 }
104 for (i = 0, m = config.displayMath.length; i < m; i++) {
105 starts.push(this.patternQuote(config.displayMath[i][0]));
106 this.match[config.displayMath[i][0]] = {
107 mode: "; mode=display",
108 end: config.displayMath[i][1],
109 pattern: this.endPattern(config.displayMath[i][1])
110 };
111 }
112 if (starts.length) {parts.push(starts.sort(this.sortLength).join("|"))}
113 if (config.processEnvironments) {parts.push("\\\\begin\\{([^}]*)\\}")}
114 if (config.processEscapes) {parts.push("\\\\*\\\\\\\$")}
115 if (config.processRefs) {parts.push("\\\\(eq)?ref\\{[^}]*\\}")}
116 this.start = new RegExp(parts.join("|"),"g");
117 this.skipTags = new RegExp("^("+config.skipTags.join("|")+")$","i");
118 var ignore = [];
119 if (MathJax.Hub.config.preRemoveClass) {ignore.push(MathJax.Hub.config.preRemoveClass)};
120 if (config.ignoreClass) {ignore.push(config.ignoreClass)}
121 this.ignoreClass = (ignore.length ? new RegExp("(^| )("+ignore.join("|")+")( |$)") : /^$/);
122 this.processClass = new RegExp("(^| )("+config.processClass+")( |$)");
123 return (parts.length > 0);
124 },
125
126 patternQuote: function (s) {return s.replace(/([\^$(){}+*?\-|\[\]\:\\])/g,'\\$1')},
127
128 endPattern: function (end) {
129 return new RegExp(this.patternQuote(end)+"|\\\\.|[{}]","g");
130 },
131
132 sortLength: function (a,b) {
133 if (a.length !== b.length) {return b.length - a.length}
134 return (a == b ? 0 : (a < b ? -1 : 1));
135 },
136
137 scanElement: function (element,stop,ignore) {
138 var cname, tname, ignoreChild, process;
139 while (element && element != stop) {
140 if (element.nodeName.toLowerCase() === '#text') {
141 if (!ignore) {element = this.scanText(element)}
142 } else {
143 cname = (typeof(element.className) === "undefined" ? "" : element.className);
144 tname = (typeof(element.tagName) === "undefined" ? "" : element.tagName);
145 if (typeof(cname) !== "string") {cname = String(cname)} // jsxgraph uses non-string class names!
146 process = this.processClass.exec(cname);
147 if (element.firstChild && !cname.match(/(^| )MathJax/) &&
148 (process || !this.skipTags.exec(tname))) {
149 ignoreChild = (ignore || this.ignoreClass.exec(cname)) && !process;
150 this.scanElement(element.firstChild,stop,ignoreChild);
151 }
152 }
153 if (element) {element = element.nextSibling}
154 }
155 },
156
157 scanText: function (element) {
158 if (element.nodeValue.replace(/\s+/,'') == '') {return element}
159 var match, prev, pos = 0, rescan;
160 this.search = {start: true};
161 this.pattern = this.start;
162 while (element) {
163 rescan = null;
164 this.pattern.lastIndex = pos; pos = 0;
165 while (element && element.nodeName.toLowerCase() === '#text' &&
166 (match = this.pattern.exec(element.nodeValue))) {
167 if (this.search.start) {element = this.startMatch(match,element)}
168 else {element = this.endMatch(match,element)}
169 }
170 if (this.search.matched) element = this.encloseMath(element);
171 else if (!this.search.start && !this.search.close) rescan = this.search;
172 if (element) {
173 do {prev = element; element = element.nextSibling}
174 while (element && this.ignoreTags[element.nodeName.toLowerCase()] != null);
175 if (!element || element.nodeName !== '#text') {
176 if (!rescan) return (this.search.close ? this.prevEndMatch() : prev);
177 element = rescan.open;
178 pos = rescan.opos + rescan.olen + (rescan.blen || 0);
179 this.search = {start: true};
180 this.pattern = this.start;
181 }
182 }
183 }
184 return element;
185 },
186
187 startMatch: function (match,element) {
188 var delim = this.match[match[0]];
189 if (delim != null) { // a start delimiter
190 this.search = {
191 end: delim.end, mode: delim.mode, pcount: 0,
192 open: element, olen: match[0].length, opos: this.pattern.lastIndex - match[0].length
193 };
194 this.switchPattern(delim.pattern);
195 } else if (match[0].substr(0,6) === "\\begin") { // \begin{...}
196 this.search = {
197 end: "\\end{"+match[1]+"}", mode: "; mode=display", pcount: 0,
198 open: element, olen: 0, opos: this.pattern.lastIndex - match[0].length,
199 blen: match[1].length + 3, isBeginEnd: true
200 };
201 this.switchPattern(this.endPattern(this.search.end));
202 } else if (match[0].substr(0,4) === "\\ref" || match[0].substr(0,6) === "\\eqref") {
203 this.search = {
204 mode: "", end: "", open: element, pcount: 0,
205 olen: 0, opos: this.pattern.lastIndex - match[0].length
206 }
207 return this.endMatch([""],element);
208 } else { // escaped dollar signs
209 // put $ in a span so it doesn't get processed again
210 // split off backslashes so they don't get removed later
211 var slashes = match[0].substr(0,match[0].length-1), n, span;
212 if (slashes.length % 2 === 0) {span = [slashes.replace(/\\\\/g,"\\")]; n = 1}
213 else {span = [slashes.substr(1).replace(/\\\\/g,"\\"),"$"]; n = 0}
214 span = MathJax.HTML.Element("span",null,span);
215 var text = MathJax.HTML.TextNode(element.nodeValue.substr(0,match.index));
216 element.nodeValue = element.nodeValue.substr(match.index + match[0].length - n);
217 element.parentNode.insertBefore(span,element);
218 element.parentNode.insertBefore(text,span);
219 this.pattern.lastIndex = n;
220 }
221 return element;
222 },
223
224 endMatch: function (match,element) {
225 var search = this.search;
226 if (match[0] == search.end) {
227 if (!search.close || search.pcount === 0) {
228 search.close = element;
229 search.cpos = this.pattern.lastIndex;
230 search.clen = (search.isBeginEnd ? 0 : match[0].length);
231 }
232 if (search.pcount === 0) {
233 search.matched = true;
234 element = this.encloseMath(element);
235 this.switchPattern(this.start);
236 }
237 }
238 if (match[0] === "{") {search.pcount++}
239 else if (match[0] === "}" && search.pcount) {search.pcount--}
240 return element;
241 },
242 prevEndMatch: function () {
243 this.search.matched = true;
244 var element = this.encloseMath(this.search.close);
245 this.switchPattern(this.start);
246 return element;
247 },
248
249 switchPattern: function (pattern) {
250 pattern.lastIndex = this.pattern.lastIndex;
251 this.pattern = pattern;
252 this.search.start = (pattern === this.start);
253 },
254
255 encloseMath: function (element) {
256 var search = this.search, close = search.close, CLOSE, math, next;
257 if (search.cpos === close.length) {close = close.nextSibling}
258 else {close = close.splitText(search.cpos)}
259 if (!close) {CLOSE = close = MathJax.HTML.addText(search.close.parentNode,"")}
260 search.close = close;
261 math = (search.opos ? search.open.splitText(search.opos) : search.open);
262 while ((next = math.nextSibling) && next !== close) {
263 if (next.nodeValue !== null) {
264 if (next.nodeName === "#comment") {
265 math.nodeValue += next.nodeValue.replace(/^\[CDATA\[((.|\n|\r)*)\]\]$/,"$1");
266 } else {
267 math.nodeValue += next.nodeValue;
268 }
269 } else {
270 var ignore = this.ignoreTags[next.nodeName.toLowerCase()];
271 math.nodeValue += (ignore == null ? " " : ignore);
272 }
273 math.parentNode.removeChild(next);
274 }
275 var TeX = math.nodeValue.substr(search.olen,math.nodeValue.length-search.olen-search.clen);
276 math.parentNode.removeChild(math);
277 if (this.config.preview !== "none") {this.createPreview(search.mode,TeX)}
278 math = this.createMathTag(search.mode,TeX);
279 this.search = {}; this.pattern.lastIndex = 0;
280 if (CLOSE) {CLOSE.parentNode.removeChild(CLOSE)}
281 return math;
282 },
283
284 insertNode: function (node) {
285 var search = this.search;
286 search.close.parentNode.insertBefore(node,search.close);
287 },
288
289 createPreview: function (mode,tex) {
290 var previewClass = MathJax.Hub.config.preRemoveClass;
291 var preview = this.config.preview;
292 if (preview === "none") return;
293 if ((this.search.close.previousSibling||{}).className === previewClass) return;
294 if (preview === "TeX") {preview = [this.filterPreview(tex)]}
295 if (preview) {
296 preview = MathJax.HTML.Element("span",{className:previewClass},preview);
297 this.insertNode(preview);
298 }
299 },
300
301 createMathTag: function (mode,tex) {
302 var script = document.createElement("script");
303 script.type = "math/tex" + mode;
304 MathJax.HTML.setScript(script,tex);
305 this.insertNode(script);
306 return script;
307 },
308
309 filterPreview: function (tex) {return tex}
310
311 };
312
313 // We register the preprocessors with the following priorities:
314 // - mml2jax.js: 5
315 // - jsMath2jax.js: 8
316 // - asciimath2jax.js, tex2jax.js: 10 (default)
317 // See issues 18 and 484 and the other *2jax.js files.
318 MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.tex2jax]);
319 MathJax.Ajax.loadComplete("[MathJax]/extensions/tex2jax.js");