forked from asciimath/asciimathml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
1234 lines (1142 loc) · 71.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ASCIIMathML.ts
==============
lightly modified by Tom Berend - original copyright below...
convert to a TS function that accepts AsciiMath and returns MathML.
not a page translator. Don't touch my document, I'll do that.
and don't worry about IE.
got rid of the onLoad function (just use ASCIIMathML.js for that)
/*
ASCIIMathML.js
==============
This file contains JavaScript functions to convert ASCII math notation
and (some) LaTeX to Presentation MathML. The conversion is done while the
HTML page loads, and should work with Firefox and other browsers that can
render MathML.
Just add the next line to your HTML page with this file in the same folder:
<script type="text/javascript" src="ASCIIMathML.js"></script>
Version 2.2 Mar 3, 2014.
Latest version at https://github.com/mathjax/asciimathml
If you use it on a webpage, please send the URL to jipsen@chapman.edu
Copyright (c) 2014 Peter Jipsen and other ASCIIMathML.js contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// or another family (e.g. "arial")
let automathrecognize = false; // writing "amath" on page makes this true
let checkForMathML = true; // check if browser can display MathML
let notifyIfNoMathML = true; // display note at top if no MathML capability
let alertIfNoMathML = false; // show alert box if no MathML capability
let translateOnLoad = true; // set to false to do call translators from js
let translateASCIIMath = true; // false to preserve `..`
let displaystyle = true; // puts limits above and below large operators
let showasciiformulaonhover = true; // helps students learn ASCIIMath
let decimalsign = "."; // if "," then when writing lists or matrices put
//a space after the "," like `(1, 2)` not `(1,2)`
let fixphi = true; //false to return to legacy phi/varphi mapping
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
type AMSymbol = {
input: string
tag: 'mi' | 'mo' | 'mn' | 'mroot' | 'mfrac' | 'msup' | 'msub' | 'mover' | 'mtext' | 'msqrt' | 'munder' | 'mstyle' | 'menclose' | 'mrow'
output: string
tex: string | null
ttype: number //tokenType
invisible?: boolean // all these other unreliable elements ?!?!
func?: boolean
acc?: boolean
rewriteleftright?: string[] // always two
notexcopy?: boolean
atname?: "mathvariant",
atval?: "bold" | "sans-serif" | "double-struck" | "script" | "fraktur" | "monospace"
codes?: string[] // AMcal | AMfrk | AMbbb |
}
type Tag = 'div' | 'p' | 'span' | 'body' | 'a'
// character lists for Mozilla/Netscape fonts
let AMcal = ["\uD835\uDC9C", "\u212C", "\uD835\uDC9E", "\uD835\uDC9F", "\u2130", "\u2131", "\uD835\uDCA2", "\u210B", "\u2110", "\uD835\uDCA5", "\uD835\uDCA6", "\u2112", "\u2133", "\uD835\uDCA9", "\uD835\uDCAA", "\uD835\uDCAB", "\uD835\uDCAC", "\u211B", "\uD835\uDCAE", "\uD835\uDCAF", "\uD835\uDCB0", "\uD835\uDCB1", "\uD835\uDCB2", "\uD835\uDCB3", "\uD835\uDCB4", "\uD835\uDCB5", "\uD835\uDCB6", "\uD835\uDCB7", "\uD835\uDCB8", "\uD835\uDCB9", "\u212F", "\uD835\uDCBB", "\u210A", "\uD835\uDCBD", "\uD835\uDCBE", "\uD835\uDCBF", "\uD835\uDCC0", "\uD835\uDCC1", "\uD835\uDCC2", "\uD835\uDCC3", "\u2134", "\uD835\uDCC5", "\uD835\uDCC6", "\uD835\uDCC7", "\uD835\uDCC8", "\uD835\uDCC9", "\uD835\uDCCA", "\uD835\uDCCB", "\uD835\uDCCC", "\uD835\uDCCD", "\uD835\uDCCE", "\uD835\uDCCF"];
let AMfrk = ["\uD835\uDD04", "\uD835\uDD05", "\u212D", "\uD835\uDD07", "\uD835\uDD08", "\uD835\uDD09", "\uD835\uDD0A", "\u210C", "\u2111", "\uD835\uDD0D", "\uD835\uDD0E", "\uD835\uDD0F", "\uD835\uDD10", "\uD835\uDD11", "\uD835\uDD12", "\uD835\uDD13", "\uD835\uDD14", "\u211C", "\uD835\uDD16", "\uD835\uDD17", "\uD835\uDD18", "\uD835\uDD19", "\uD835\uDD1A", "\uD835\uDD1B", "\uD835\uDD1C", "\u2128", "\uD835\uDD1E", "\uD835\uDD1F", "\uD835\uDD20", "\uD835\uDD21", "\uD835\uDD22", "\uD835\uDD23", "\uD835\uDD24", "\uD835\uDD25", "\uD835\uDD26", "\uD835\uDD27", "\uD835\uDD28", "\uD835\uDD29", "\uD835\uDD2A", "\uD835\uDD2B", "\uD835\uDD2C", "\uD835\uDD2D", "\uD835\uDD2E", "\uD835\uDD2F", "\uD835\uDD30", "\uD835\uDD31", "\uD835\uDD32", "\uD835\uDD33", "\uD835\uDD34", "\uD835\uDD35", "\uD835\uDD36", "\uD835\uDD37"];
let AMbbb = ["\uD835\uDD38", "\uD835\uDD39", "\u2102", "\uD835\uDD3B", "\uD835\uDD3C", "\uD835\uDD3D", "\uD835\uDD3E", "\u210D", "\uD835\uDD40", "\uD835\uDD41", "\uD835\uDD42", "\uD835\uDD43", "\uD835\uDD44", "\u2115", "\uD835\uDD46", "\u2119", "\u211A", "\u211D", "\uD835\uDD4A", "\uD835\uDD4B", "\uD835\uDD4C", "\uD835\uDD4D", "\uD835\uDD4E", "\uD835\uDD4F", "\uD835\uDD50", "\u2124", "\uD835\uDD52", "\uD835\uDD53", "\uD835\uDD54", "\uD835\uDD55", "\uD835\uDD56", "\uD835\uDD57", "\uD835\uDD58", "\uD835\uDD59", "\uD835\uDD5A", "\uD835\uDD5B", "\uD835\uDD5C", "\uD835\uDD5D", "\uD835\uDD5E", "\uD835\uDD5F", "\uD835\uDD60", "\uD835\uDD61", "\uD835\uDD62", "\uD835\uDD63", "\uD835\uDD64", "\uD835\uDD65", "\uD835\uDD66", "\uD835\uDD67", "\uD835\uDD68", "\uD835\uDD69", "\uD835\uDD6A", "\uD835\uDD6B"];
/*let AMcal = [0xEF35,0x212C,0xEF36,0xEF37,0x2130,0x2131,0xEF38,0x210B,0x2110,0xEF39,0xEF3A,0x2112,0x2133,0xEF3B,0xEF3C,0xEF3D,0xEF3E,0x211B,0xEF3F,0xEF40,0xEF41,0xEF42,0xEF43,0xEF44,0xEF45,0xEF46];
let AMfrk = [0xEF5D,0xEF5E,0x212D,0xEF5F,0xEF60,0xEF61,0xEF62,0x210C,0x2111,0xEF63,0xEF64,0xEF65,0xEF66,0xEF67,0xEF68,0xEF69,0xEF6A,0x211C,0xEF6B,0xEF6C,0xEF6D,0xEF6E,0xEF6F,0xEF70,0xEF71,0x2128];
let AMbbb = [0xEF8C,0xEF8D,0x2102,0xEF8E,0xEF8F,0xEF90,0xEF91,0x210D,0xEF92,0xEF93,0xEF94,0xEF95,0xEF96,0x2115,0xEF97,0x2119,0x211A,0x211D,0xEF98,0xEF99,0xEF9A,0xEF9B,0xEF9C,0xEF9D,0xEF9E,0x2124];*/
let CONST = 0, UNARY = 1, BINARY = 2, INFIX = 3, LEFTBRACKET = 4,
RIGHTBRACKET = 5, SPACE = 6, UNDEROVER = 7, DEFINITION = 8,
LEFTRIGHT = 9, TEXT = 10, BIG = 11, LONG = 12, STRETCHY = 13,
MATRIX = 14, UNARYUNDEROVER = 15; // token types
let AMquote: AMSymbol = { input: "\"", tag: "mtext", output: "mbox", tex: null, ttype: TEXT };
/** convert an AsciiMath statement to MathML */
export class AsciiMath {
noMathML = false
translated = false
latex = false
AMnames: string[] = []; //list of input symbols
AMmathml = "http://www.w3.org/1998/Math/MathML";
AMnestingDepth: number
AMpreviousSymbol: number // one of the
AMcurrentSymbol: number
mathcolor = "blue"; // change it to "" (to inherit) or another color
mathfontsize = "1em"; // change to e.g. 1.2em for larger math
mathfontfamily = "serif"; // change to "" to inherit (works in IE)
AMdelimiter1 = "`" // when hunting through a doc looking for math to translate
AMescape1 = "\\\\`" // can use other characters
AMSymbols: AMSymbol[]
constructor() {
this.setStylesheet("#AMMLcloseDiv \{font-size:0.8em padding-top:1em color:#014\}\n#AMMLwarningBox \{position:absolute width:100% top:0 left:0 z-index:200 text-align:center font-size:1em font-weight:bold padding:0.5em 0 0.5em 0 color:#ffc background:#c30\}")
this.initSymbols()
this.init()
}
/** Add a stylesheet, replacing any previous custom stylesheet (adapted from TW) */
setStylesheet(s: string) {
let id = "AMMLcustomStyleSheet";
let n = document.getElementById(id);
// if (document.createStyleSheet) { // tbtb
// // Test for IE's non-standard createStyleSheet method
// if (n)
// n.parentNode.removeChild(n);
// // This failed without the
// document.getElementsByTagName("head")[0].insertAdjacentHTML("beforeend", " <style id='" + id + "'>" + s + "</style>"); // tbtb not 'beforeEnd'
// } else {
if (n) {
n.replaceChild(document.createTextNode(s), n.firstChild);
} else {
n = document.createElement("style");
// n.type = "text/css"; //tbtb
n.id = id;
n.appendChild(document.createTextNode(s));
document.getElementsByTagName("head")[0].appendChild(n);
}
// }
}
init() {
let msg, warnings = new Array();
if (document.getElementById == null) {
alert("This webpage requires a recent browser such as Mozilla Firefox");
return null;
}
if (checkForMathML && (msg = this.checkMathML())) warnings.push(msg);
if (warnings.length > 0) this.displayWarnings(warnings);
if (!this.noMathML) this.initSymbols();
return true;
}
checkMathML() {
if (navigator.appName.slice(0, 8) == "Netscape")
if (navigator.appVersion.slice(0, 1) >= "5") this.noMathML = null;
else this.noMathML = true;
// else if (navigator.appName.slice(0, 9) == "Microsoft")
// try {
// let ActiveX = new ActiveXObject("MathPlayer.Factory.1");
// noMathML = null;
// } catch (e) {
// noMathML = true;
// }
else if (navigator.appName.slice(0, 5) == "Opera")
if (navigator.appVersion.slice(0, 3) >= "9.5") this.noMathML = null;
else this.noMathML = true;
//noMathML = true; //uncomment to check
if (this.noMathML && notifyIfNoMathML) {
let msg = "To view the ASCIIMathML notation use Internet Explorer + MathPlayer or Mozilla Firefox 2.0 or later.";
if (alertIfNoMathML)
alert(msg);
else return msg;
}
}
hideWarning() {
let body = document.getElementsByTagName("body")[0];
body.removeChild(document.getElementById('AMMLwarningBox'));
body.onclick = null;
}
displayWarnings(warnings: string[]) {
let i, frag, nd = this.createElementXHTML("div");
let body = document.getElementsByTagName("body")[0];
body.onclick = this.hideWarning;
nd.id = 'AMMLwarningBox';
for (i = 0; i < warnings.length; i++) {
frag = this.createElementXHTML("div");
frag.appendChild(document.createTextNode(warnings[i]));
frag.style.paddingBottom = "1.0em";
nd.appendChild(frag);
}
nd.appendChild(this.createElementXHTML("p"));
nd.appendChild(document.createTextNode("For instructions see the "));
let an = this.createElementXHTML("a");
an.appendChild(document.createTextNode("ASCIIMathML"));
an.setAttribute("href", "http://asciimath.org");
nd.appendChild(an);
nd.appendChild(document.createTextNode(" homepage"));
an = this.createElementXHTML("div");
an.id = 'AMMLcloseDiv';
an.appendChild(document.createTextNode('(click anywhere to close this warning)'));
nd.appendChild(an);
body.insertBefore(nd, body.childNodes[0]);
}
/** Find and translate all math on a page. if spanclassAM is provided then it
* is the tag to look for. Perhaps 'span' is a good value. If it is NOT
* provided, then we will look for AMDelimiter1 (by default a backtick)
*/
translate(spanclassAM?: string) {
if (!this.translated) { // run this only once
this.translated = true;
let body = document.getElementsByTagName("body")[0];
this.AMprocessNode(body, false, spanclassAM);
}
}
createElementXHTML(t: Tag) {
return document.createElementNS("http://www.w3.org/1999/xhtml", t);
}
AMcreateElementMathML(t: Tag) {
return document.createElementNS(this.AMmathml, t);
}
createMmlNode(t: string, frag?: any): any { // too many things in frag to type properly
let node;
node = document.createElementNS(this.AMmathml, t) as HTMLElement
if (frag) node.appendChild(frag);
return node;
}
newcommand(oldstr: string, newstr: string) {
this.AMSymbols.push({ input: oldstr, tag: "mo", output: newstr, tex: null, ttype: DEFINITION });
this.refreshSymbols();
}
newsymbol(symbolobj: AMSymbol) {
this.AMSymbols.push(symbolobj);
this.refreshSymbols();
}
compareNames(s1: AMSymbol, s2: AMSymbol): number {
if (s1.input > s2.input) return 1
else return -1;
}
initSymbols() {
let i;
this.loadAMSymbols()
let symlen = this.AMSymbols.length;
for (i = 0; i < symlen; i++) {
if (this.AMSymbols[i].tex) {
this.AMSymbols.push({
input: this.AMSymbols[i].tex,
tex: null,
tag: this.AMSymbols[i].tag, output: this.AMSymbols[i].output, ttype: this.AMSymbols[i].ttype,
acc: (this.AMSymbols[i].acc || false)
});
}
}
this.refreshSymbols();
}
refreshSymbols() {
this.AMSymbols.sort(this.compareNames);
for (let i = 0; i < this.AMSymbols.length; i++) this.AMnames[i] = this.AMSymbols[i].input;
}
define(oldstr: string, newstr: string) {
this.AMSymbols.push({ input: oldstr, tag: "mo", output: newstr, tex: null, ttype: DEFINITION });
this.refreshSymbols(); // this may be a problem if many symbols are defined!
}
AMremoveCharsAndBlanks(str: string, n: number) {
//remove n characters and any following blanks
let st;
if (str.charAt(n) == "\\" && str.charAt(n + 1) != "\\" && str.charAt(n + 1) != " ")
st = str.slice(n + 1);
else st = str.slice(n);
let i // tbtb must NOT be defined in the for loop, goes out of scope
for (i = 0; i < st.length && st.charCodeAt(i) <= 32; i = i + 1) { }
return st.slice(i); // tbtb ?? this isn't valid TS, not sure what it means in JS
}
position(arr: string[], str: string, n: number) {
// return position >=n where str appears or would be inserted
// assumes arr is sorted
if (n == 0) {
let h, m;
n = -1;
h = arr.length;
while (n + 1 < h) {
m = (n + h) >> 1;
if (arr[m] < str) n = m; else h = m;
}
return h;
} else {
let i
for (i = n; i < arr.length && arr[i] < str; i++) { } // stops when it stops
return i; // i=arr.length || arr[i]>=str
}
}
AMgetSymbol(str: string): AMSymbol {
//return maximal initial substring of str that appears in names
//return null if there is none
let k = 0; //new pos
let j = 0; //old pos
let mk; //match pos
let st: string;
let tagst: 'mn' | 'mo' | 'mi'
let match = "";
let more = true;
for (let i = 1; i <= str.length && more; i++) {
st = str.slice(0, i); //initial substring of length i
j = k;
k = this.position(this.AMnames, st, j);
if (k < this.AMnames.length && str.slice(0, this.AMnames[k].length) == this.AMnames[k]) {
match = this.AMnames[k];
mk = k;
i = match.length;
}
more = k < this.AMnames.length && str.slice(0, this.AMnames[k].length) >= this.AMnames[k];
}
this.AMpreviousSymbol = this.AMcurrentSymbol;
if (match != "") {
this.AMcurrentSymbol = this.AMSymbols[mk].ttype;
return this.AMSymbols[mk];
}
// if str[0] is a digit or - return maxsubstring of digits.digits
this.AMcurrentSymbol = CONST;
k = 1;
st = str.slice(0, 1);
let integ = true;
while ("0" <= st && st <= "9" && k <= str.length) {
st = str.slice(k, k + 1);
k++;
}
if (st == decimalsign) {
st = str.slice(k, k + 1);
if ("0" <= st && st <= "9") {
integ = false;
k++;
while ("0" <= st && st <= "9" && k <= str.length) {
st = str.slice(k, k + 1);
k++;
}
}
}
if ((integ && k > 1) || k > 2) {
st = str.slice(0, k - 1);
tagst = "mn";
} else {
k = 2;
st = str.slice(0, 1); //take 1 character
tagst = (("A" > st || st > "Z") && ("a" > st || st > "z") ? "mo" : "mi");
}
if (st == "-" && str.charAt(1) !== ' ' && this.AMpreviousSymbol == INFIX) {
this.AMcurrentSymbol = INFIX; //trick "/" into recognizing "-" on second parse
return { input: st, tag: tagst, output: st, tex: null, ttype: UNARY, func: true };
}
return { input: st, tag: tagst, output: st, tex: null, ttype: CONST };
}
AMremoveBrackets(node: Node) {
let st;
if (!node.hasChildNodes()) { return; }
if (node.firstChild.hasChildNodes() && (node.nodeName == "mrow" || node.nodeName == "M:MROW")) {
if (node.firstChild.nextSibling && node.firstChild.nextSibling.nodeName == "mtable") { return; }
st = node.firstChild.firstChild.nodeValue;
if (st == "(" || st == "[" || st == "{") node.removeChild(node.firstChild);
}
if (node.lastChild.hasChildNodes() && (node.nodeName == "mrow" || node.nodeName == "M:MROW")) {
st = node.lastChild.firstChild.nodeValue;
if (st == ")" || st == "]" || st == "}") node.removeChild(node.lastChild);
}
}
/*Parsing ASCII math expressions with the following grammar
v ::= [A-Za-z] | greek letters | numbers | other constant symbols
u ::= sqrt | text | bb | other unary symbols for font commands
b ::= frac | root | stackrel binary symbols
l ::= ( | [ | { | (: | {: left brackets
r ::= ) | ] | } | :) | :} right brackets
S ::= v | lEr | uS | bSS Simple expression
I ::= S_S | S^S | S_S^S | S Intermediate expression
E ::= IE | I/I Expression
Each terminal symbol is translated into a corresponding mathml node.*/
AMparseSexpr(str: string): [Node, string] { //parses str and returns [node,tailstr]
let symbol, node, result, i, st,// rightvert = false,
newFrag = document.createDocumentFragment();
str = this.AMremoveCharsAndBlanks(str, 0);
symbol = this.AMgetSymbol(str); //either a token or a bracket or empty
if (symbol == null || symbol.ttype == RIGHTBRACKET && this.AMnestingDepth > 0) {
return [null, str];
}
if (symbol.ttype == DEFINITION) {
str = symbol.output + this.AMremoveCharsAndBlanks(str, symbol.input.length);
symbol = this.AMgetSymbol(str);
}
switch (symbol.ttype) {
case UNDEROVER:
case CONST:
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
return [this.createMmlNode(symbol.tag, //its a constant
document.createTextNode(symbol.output)), str];
case LEFTBRACKET: //read (expr+)
this.AMnestingDepth++;
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
result = this.AMparseExpr(str, true);
this.AMnestingDepth--;
if (typeof symbol.invisible == "boolean" && symbol.invisible)
node = this.createMmlNode("mrow", result[0]);
else {
node = this.createMmlNode("mo", document.createTextNode(symbol.output));
node = this.createMmlNode("mrow", node);
node.appendChild(result[0]);
}
return [node, result[1]];
case TEXT:
if (symbol != AMquote) str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
if (str.charAt(0) == "{") i = str.indexOf("}");
else if (str.charAt(0) == "(") i = str.indexOf(")");
else if (str.charAt(0) == "[") i = str.indexOf("]");
else if (symbol == AMquote) i = str.slice(1).indexOf("\"") + 1;
else i = 0;
if (i == -1) i = str.length;
st = str.slice(1, i);
if (st.charAt(0) == " ") {
node = this.createMmlNode("mspace");
node.setAttribute("width", "1ex");
newFrag.appendChild(node);
}
newFrag.appendChild(
this.createMmlNode(symbol.tag, document.createTextNode(st)));
if (st.charAt(st.length - 1) == " ") {
node = this.createMmlNode("mspace");
node.setAttribute("width", "1ex");
newFrag.appendChild(node);
}
str = this.AMremoveCharsAndBlanks(str, i + 1);
return [this.createMmlNode("mrow", newFrag), str];
case UNARYUNDEROVER:
case UNARY:
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
result = this.AMparseSexpr(str);
if (result[0] == null) {
if (symbol.tag == "mi" || symbol.tag == "mo") {
return [this.createMmlNode(symbol.tag,
document.createTextNode(symbol.output)), str];
} else {
result[0] = this.createMmlNode("mi", "");
}
}
if (typeof symbol.func == "boolean" && symbol.func) { // functions hack
st = str.charAt(0);
if (st == "^" || st == "_" || st == "/" || st == "|" || st == "," ||
(symbol.input.length == 1 && symbol.input.match(/\w/) && st != "(")) {
return [this.createMmlNode(symbol.tag,
document.createTextNode(symbol.output)), str];
} else {
node = this.createMmlNode("mrow",
this.createMmlNode(symbol.tag, document.createTextNode(symbol.output)));
node.appendChild(result[0]);
return [node, result[1]];
}
}
this.AMremoveBrackets(result[0]);
if (symbol.input == "sqrt") { // sqrt
return [this.createMmlNode(symbol.tag, result[0]), result[1]];
} else if (typeof symbol.rewriteleftright != "undefined") { // abs, floor, ceil
node = this.createMmlNode("mrow", this.createMmlNode("mo", document.createTextNode(symbol.rewriteleftright[0])));
node.appendChild(result[0]);
node.appendChild(this.createMmlNode("mo", document.createTextNode(symbol.rewriteleftright[1])));
return [node, result[1]];
} else if (symbol.input == "cancel") { // cancel
node = this.createMmlNode(symbol.tag, result[0]);
node.setAttribute("notation", "updiagonalstrike");
return [node, result[1]];
} else if (typeof symbol.acc == "boolean" && symbol.acc) { // accent
node = this.createMmlNode(symbol.tag, result[0]);
let accnode = this.createMmlNode("mo", document.createTextNode(symbol.output));
if (symbol.input == "vec" && (
(result[0].nodeName == "mrow" && result[0].childNodes.length == 1
&& result[0].firstChild.firstChild.nodeValue !== null
&& result[0].firstChild.firstChild.nodeValue.length == 1) ||
(result[0].firstChild.nodeValue !== null
&& result[0].firstChild.nodeValue.length == 1))) {
accnode.setAttribute("stretchy", false); //tbtb
}
node.appendChild(accnode);
return [node, result[1]];
} else { // font change command
if (typeof symbol.codes != "undefined") {
for (i = 0; i < result[0].childNodes.length; i++)
if (result[0].childNodes[i].nodeName == "mi" || result[0].nodeName == "mi") {
st = (result[0].nodeName == "mi" ? result[0].firstChild.nodeValue :
result[0].childNodes[i].firstChild.nodeValue);
let newst = ''; // tbtb should be string
for (let j = 0; j < st.length; j++)
if (st.charCodeAt(j) > 64 && st.charCodeAt(j) < 91)
newst = newst + symbol.codes[st.charCodeAt(j) - 65]; // tbtb newst coerced to string here
else if (st.charCodeAt(j) > 96 && st.charCodeAt(j) < 123)
newst = newst + symbol.codes[st.charCodeAt(j) - 71];
else newst = newst + st.charAt(j);
if (result[0].nodeName == "mi")
result[0] = this.createMmlNode("mo").
appendChild(document.createTextNode(newst));
else result[0].replaceChild(this.createMmlNode("mo").
appendChild(document.createTextNode(newst)),
result[0].childNodes[i]);
}
}
node = this.createMmlNode(symbol.tag, result[0]);
node.setAttribute(symbol.atname, symbol.atval);
return [node, result[1]];
}
case BINARY:
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
result = this.AMparseSexpr(str);
if (result[0] == null) return [this.createMmlNode("mo",
document.createTextNode(symbol.input)), str];
this.AMremoveBrackets(result[0]);
let result2 = this.AMparseSexpr(result[1]);
if (result2[0] == null) return [this.createMmlNode("mo",
document.createTextNode(symbol.input)), str];
this.AMremoveBrackets(result2[0]);
if (['color', 'class', 'id'].indexOf(symbol.input) >= 0) {
// Get the second argument
if (str.charAt(0) == "{") i = str.indexOf("}");
else if (str.charAt(0) == "(") i = str.indexOf(")");
else if (str.charAt(0) == "[") i = str.indexOf("]");
st = str.slice(1, i);
// Make a mathml node
node = this.createMmlNode(symbol.tag, result2[0]);
// Set the correct attribute
if (symbol.input === "color") node.setAttribute("mathcolor", st)
else if (symbol.input === "class") node.setAttribute("class", st)
else if (symbol.input === "id") node.setAttribute("id", st)
return [node, result2[1]];
}
if (symbol.input == "root" || symbol.output == "stackrel")
newFrag.appendChild(result2[0]);
newFrag.appendChild(result[0]);
if (symbol.input == "frac") newFrag.appendChild(result2[0]);
return [this.createMmlNode(symbol.tag, newFrag), result2[1]];
case INFIX:
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
return [this.createMmlNode("mo", document.createTextNode(symbol.output)), str];
case SPACE:
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
node = this.createMmlNode("mspace");
node.setAttribute("width", "1ex");
newFrag.appendChild(node);
newFrag.appendChild(
this.createMmlNode(symbol.tag, document.createTextNode(symbol.output)));
node = this.createMmlNode("mspace");
node.setAttribute("width", "1ex");
newFrag.appendChild(node);
return [this.createMmlNode("mrow", newFrag), str];
case LEFTRIGHT:
// if (rightvert) return [null,str]; else rightvert = true;
this.AMnestingDepth++;
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
result = this.AMparseExpr(str, false);
this.AMnestingDepth--;
st = "";
if (result[0].lastChild != null)
st = result[0].lastChild.firstChild.nodeValue;
if (st == "|" && str.charAt(0) !== ",") { // its an absolute value subterm
node = this.createMmlNode("mo", document.createTextNode(symbol.output));
node = this.createMmlNode("mrow", node);
node.appendChild(result[0]);
return [node, result[1]];
} else { // the "|" is a \mid so use unicode 2223 (divides) for spacing
node = this.createMmlNode("mo", document.createTextNode("\u2223"));
node = this.createMmlNode("mrow", node);
return [node, str];
}
default:
//alert("default");
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
return [this.createMmlNode(symbol.tag, //its a constant
document.createTextNode(symbol.output)), str];
}
}
AMparseIexpr(str: string): [Node, string] {
let symbol, sym1, sym2, node, result, underover;
str = this.AMremoveCharsAndBlanks(str, 0);
sym1 = this.AMgetSymbol(str);
result = this.AMparseSexpr(str);
node = result[0];
str = result[1];
symbol = this.AMgetSymbol(str);
if (symbol.ttype == INFIX && symbol.input != "/") {
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
// if (symbol.input == "/") result = AMparseIexpr(str); else ...
result = this.AMparseSexpr(str);
if (result[0] == null) // show box in place of missing argument
result[0] = this.createMmlNode("mo", document.createTextNode("\u25A1"));
else this.AMremoveBrackets(result[0]);
str = result[1];
// if (symbol.input == "/") AMremoveBrackets(node);
underover = (sym1.ttype == UNDEROVER || sym1.ttype == UNARYUNDEROVER);
if (symbol.input == "_") {
sym2 = this.AMgetSymbol(str);
if (sym2.input == "^") {
str = this.AMremoveCharsAndBlanks(str, sym2.input.length);
let res2 = this.AMparseSexpr(str);
this.AMremoveBrackets(res2[0]);
str = res2[1];
node = this.createMmlNode((underover ? "munderover" : "msubsup"), node);
node.appendChild(result[0]);
node.appendChild(res2[0]);
node = this.createMmlNode("mrow", node); // so sum does not stretch
} else {
node = this.createMmlNode((underover ? "munder" : "msub"), node);
node.appendChild(result[0]);
}
} else if (symbol.input == "^" && underover) {
node = this.createMmlNode("mover", node);
node.appendChild(result[0]);
} else {
node = this.createMmlNode(symbol.tag, node);
node.appendChild(result[0]);
}
if (typeof sym1.func != 'undefined' && sym1.func) {
sym2 = this.AMgetSymbol(str);
if (sym2.ttype != INFIX && sym2.ttype != RIGHTBRACKET &&
(sym1.input.length > 1 || sym2.ttype == LEFTBRACKET)) {
result = this.AMparseIexpr(str);
node = this.createMmlNode("mrow", node);
node.appendChild(result[0]);
str = result[1];
}
}
}
return [node, str];
}
AMparseExpr(str: string, rightbracket: boolean) {
let symbol, node, result, i,
newFrag = document.createDocumentFragment();
do {
str = this.AMremoveCharsAndBlanks(str, 0);
result = this.AMparseIexpr(str);
node = result[0];
str = result[1];
symbol = this.AMgetSymbol(str);
if (symbol.ttype == INFIX && symbol.input == "/") {
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
result = this.AMparseIexpr(str);
if (result[0] == null) // show box in place of missing argument
result[0] = this.createMmlNode("mo", document.createTextNode("\u25A1"));
else this.AMremoveBrackets(result[0]);
str = result[1];
this.AMremoveBrackets(node);
node = this.createMmlNode(symbol.tag, node);
node.appendChild(result[0]);
newFrag.appendChild(node);
symbol = this.AMgetSymbol(str);
}
else if (node != undefined) newFrag.appendChild(node);
} while ((symbol.ttype != RIGHTBRACKET &&
(symbol.ttype != LEFTRIGHT || rightbracket)
|| this.AMnestingDepth == 0) && symbol != null && symbol.output != "");
if (symbol.ttype == RIGHTBRACKET || symbol.ttype == LEFTRIGHT) {
// if (AMnestingDepth > 0) AMnestingDepth--;
let len = newFrag.childNodes.length;
if (len > 0 && newFrag.childNodes[len - 1].nodeName == "mrow"
&& newFrag.childNodes[len - 1].lastChild
&& newFrag.childNodes[len - 1].lastChild.firstChild) { //matrix
//removed to allow row vectors: //&& len>1 &&
//newFrag.childNodes[len-2].nodeName == "mo" &&
//newFrag.childNodes[len-2].firstChild.nodeValue == ","
let right = newFrag.childNodes[len - 1].lastChild.firstChild.nodeValue;
if (right == ")" || right == "]") {
let left = newFrag.childNodes[len - 1].firstChild.firstChild.nodeValue;
if (left == "(" && right == ")" && symbol.output != "}" ||
left == "[" && right == "]") {
let pos = []; // positions of commas
let matrix = true;
let m = newFrag.childNodes.length;
for (i = 0; matrix && i < m; i = i + 2) {
pos[i] = [];
node = newFrag.childNodes[i];
if (matrix) matrix = node.nodeName == "mrow" &&
(i == m - 1 || node.nextSibling.nodeName == "mo" &&
node.nextSibling.firstChild.nodeValue == ",") &&
node.firstChild.firstChild &&
node.firstChild.firstChild.nodeValue == left &&
node.lastChild.firstChild &&
node.lastChild.firstChild.nodeValue == right;
if (matrix)
for (let j = 0; j < node.childNodes.length; j++)
if (node.childNodes[j].firstChild.nodeValue == ",")
pos[i][pos[i].length] = j;
if (matrix && i > 1) matrix = pos[i].length == pos[i - 2].length;
}
matrix = matrix && (pos.length > 1 || pos[0].length > 0);
let columnlines = [];
if (matrix) {
let row, frag, n, k, table = document.createDocumentFragment();
for (i = 0; i < m; i = i + 2) {
row = document.createDocumentFragment();
frag = document.createDocumentFragment();
node = newFrag.firstChild; // <mrow>(-,-,...,-,-)</mrow>
n = node.childNodes.length;
k = 0;
node.removeChild(node.firstChild); //remove (
for (let j = 1; j < n - 1; j++) {
if (typeof pos[i][k] != "undefined" && j == pos[i][k]) {
node.removeChild(node.firstChild); //remove ,
if (node.firstChild.nodeName == "mrow" && node.firstChild.childNodes.length == 1 &&
node.firstChild.firstChild.firstChild.nodeValue == "\u2223") {
//is columnline marker - skip it
if (i == 0) { columnlines.push("solid"); }
node.removeChild(node.firstChild); //remove mrow
node.removeChild(node.firstChild); //remove ,
j += 2;
k++;
} else if (i == 0) { columnlines.push("none"); }
row.appendChild(this.createMmlNode("mtd", frag));
k++;
} else frag.appendChild(node.firstChild);
}
row.appendChild(this.createMmlNode("mtd", frag));
if (i == 0) { columnlines.push("none"); }
if (newFrag.childNodes.length > 2) {
newFrag.removeChild(newFrag.firstChild); //remove <mrow>)</mrow>
newFrag.removeChild(newFrag.firstChild); //remove <mo>,</mo>
}
table.appendChild(this.createMmlNode("mtr", row));
}
node = this.createMmlNode("mtable", table);
node.setAttribute("columnlines", columnlines.join(" "));
if (typeof symbol.invisible == "boolean" && symbol.invisible) node.setAttribute("columnalign", "left");
newFrag.replaceChild(node, newFrag.firstChild);
}
}
}
}
str = this.AMremoveCharsAndBlanks(str, symbol.input.length);
if (typeof symbol.invisible != "boolean" || !symbol.invisible) {
node = this.createMmlNode("mo", document.createTextNode(symbol.output));
newFrag.appendChild(node);
}
}
return [newFrag, str];
}
/** Convert a single string to an HTML Element ready for insertion.
* let a = new AsciiMath()
* let eqn = 'sum_(i=1)^n i^3=((n(n+1))/2)^2'
* document.getElementById('insertMathHere').appendChild(a.parseMath(eqn))
*/
parseMath(str: string): Element {
this.AMnestingDepth = 0;
//some basic cleanup for dealing with stuff editors like TinyMCE adds
str = str.replace(/ /g, "");
str = str.replace(/>/g, ">");
str = str.replace(/</g, "<");
let frag = this.AMparseExpr(str.replace(/^\s+/g, ""), false)[0];
let node = this.createMmlNode("mstyle", frag);
if (this.mathcolor != "") node.setAttribute("mathcolor", this.mathcolor);
if (this.mathfontsize != "") {
node.setAttribute("fontsize", this.mathfontsize);
node.setAttribute("mathsize", this.mathfontsize);
}
if (this.mathfontfamily != "") {
node.setAttribute("fontfamily", this.mathfontfamily);
node.setAttribute("mathvariant", this.mathfontfamily);
}
if (displaystyle) node.setAttribute("displaystyle", "true");
node = this.createMmlNode("math", node);
if (showasciiformulaonhover) //fixed by djhsu so newline
node.setAttribute("title", str.replace(/\s+/g, " "));//does not show in Gecko
return node;
}
strarr2docFrag(arr: string[], linebreaks: boolean): DocumentFragment {
let newFrag = document.createDocumentFragment();
let expr = false;
for (let i = 0; i < arr.length; i++) {
if (expr) newFrag.appendChild(this.parseMath(arr[i]));
else {
let arri = (linebreaks ? arr[i].split("\n\n") : [arr[i]]);
newFrag.appendChild(this.createElementXHTML("span").
appendChild(document.createTextNode(arri[0])));
for (let j = 1; j < arri.length; j++) {
newFrag.appendChild(this.createElementXHTML("p"));
newFrag.appendChild(this.createElementXHTML("span").
appendChild(document.createTextNode(arri[j])));
}
}
expr = !expr;
}
return newFrag;
}
AMautomathrec(str: string): string {
//formula is a space (or start of str) followed by a maximal sequence of *two* or more tokens, possibly separated by runs of digits and/or space.
//tokens are single letters (except a, A, I) and ASCIIMathML tokens
let texcommand = "\\\\[a-zA-Z]+|\\\\\\s|";
let ambigAMtoken = "\\b(?:oo|lim|ln|int|oint|del|grad|aleph|prod|prop|sinh|cosh|tanh|cos|sec|pi|tt|fr|sf|sube|supe|sub|sup|det|mod|gcd|lcm|min|max|vec|ddot|ul|chi|eta|nu|mu)(?![a-z])|";
let englishAMtoken = "\\b(?:sum|ox|log|sin|tan|dim|hat|bar|dot)(?![a-z])|";
let secondenglishAMtoken = "|\\bI\\b|\\bin\\b|\\btext\\b"; // took if and or not out
let simpleAMtoken = "NN|ZZ|QQ|RR|CC|TT|AA|EE|sqrt|dx|dy|dz|dt|xx|vv|uu|nn|bb|cc|csc|cot|alpha|beta|delta|Delta|epsilon|gamma|Gamma|kappa|lambda|Lambda|omega|phi|Phi|Pi|psi|Psi|rho|sigma|Sigma|tau|theta|Theta|xi|Xi|zeta"; // uuu nnn?
let letter = "[a-zA-HJ-Z](?=(?:[^a-zA-Z]|$|" + ambigAMtoken + englishAMtoken + simpleAMtoken + "))|";
let token = letter + texcommand + "\\d+|[-()[\\]{}+=*&^_%\\\@/<>,\\|!:;'~]|\\.(?!(?:\x20|$))|" + ambigAMtoken + englishAMtoken + simpleAMtoken;
let re = new RegExp("(^|\\s)(((" + token + ")\\s?)((" + token + secondenglishAMtoken + ")\\s?)+)([,.?]?(?=\\s|$))", "g");
str = str.replace(re, " `$2`$7");
let arr = str.split(this.AMdelimiter1);
let re1 = new RegExp("(^|\\s)([b-zB-HJ-Z+*<>]|" + texcommand + ambigAMtoken + simpleAMtoken + ")(\\s|\\n|$)", "g");
let re2 = new RegExp("(^|\\s)([a-z]|" + texcommand + ambigAMtoken + simpleAMtoken + ")([,.])", "g"); // removed |\d+ for now
let i
for (i = 0; i < arr.length; i++) //single nonenglish tokens
if (i % 2 == 0) {
arr[i] = arr[i].replace(re1, " `$2`$3");
arr[i] = arr[i].replace(re2, " `$2`$3");
arr[i] = arr[i].replace(/([{}[\]])/, "`$1`");
}
str = arr.join(this.AMdelimiter1);
str = str.replace(/((^|\s)\([a-zA-Z]{2,}.*?)\)`/g, "$1`)"); //fix parentheses
str = str.replace(/`(\((a\s|in\s))(.*?[a-zA-Z]{2,}\))/g, "$1`$3"); //fix parentheses
str = str.replace(/\sin`/g, "` in");
str = str.replace(/`(\(\w\)[,.]?(\s|\n|$))/g, "$1`");
str = str.replace(/`([0-9.]+|e.g|i.e)`(\.?)/gi, "$1$2");
str = str.replace(/`([0-9.]+:)`/g, "$1");
return str;
}
processNodeR(n: Node, linebreaks: boolean): number {
let mtch, str, arr, frg, i;
if (n.childNodes.length == 0) {
if ((n.nodeType != 8 || linebreaks) &&
n.parentNode.nodeName != "form" && n.parentNode.nodeName != "FORM" &&
n.parentNode.nodeName != "textarea" && n.parentNode.nodeName != "TEXTAREA"
/*&& n.parentNode.nodeName!="pre" && n.parentNode.nodeName!="PRE"*/) {
str = n.nodeValue;
if (!(str == null)) {
str = str.replace(/\r\n\r\n/g, "\n\n");
str = str.replace(/\x20+/g, " ");
str = str.replace(/\s*\r\n/g, " ");
if (this.latex) {
// DELIMITERS:
mtch = (str.indexOf("\$") == -1 ? false : true);
str = str.replace(/([^\\])\$/g, "$1 \$");
str = str.replace(/^\$/, " \$"); // in case \$ at start of string
arr = str.split(" \$");
for (i = 0; i < arr.length; i++)
arr[i] = arr[i].replace(/\\\$/g, "\$");
} else {
mtch = false;
str = str.replace(new RegExp(this.AMescape1, "g"),
function () { mtch = true; return "AMescape1" });
str = str.replace(/\\?end{?a?math}?/i,
function () { automathrecognize = false; mtch = true; return "" });
str = str.replace(/amath\b|\\begin{a?math}/i,
function () { automathrecognize = true; mtch = true; return "" });
arr = str.split(this.AMdelimiter1);
if (automathrecognize)
for (i = 0; i < arr.length; i++)
if (i % 2 == 0) arr[i] = this.AMautomathrec(arr[i]);
str = arr.join(this.AMdelimiter1);
arr = str.split(this.AMdelimiter1);
for (i = 0; i < arr.length; i++) // this is a problem ************
arr[i] = arr[i].replace(/AMescape1/g, this.AMdelimiter1);
}
if (arr.length > 1 || mtch) {
if (!this.noMathML) {
frg = this.strarr2docFrag(arr, n.nodeType == 8);
let len = frg.childNodes.length;
n.parentNode.replaceChild(frg, n);
return len - 1;
} else return 0;
}
}
} else return 0;
} else if (n.nodeName != "math") {
for (i = 0; i < n.childNodes.length; i++)
i += this.processNodeR(n.childNodes[i], linebreaks);
}
return 0;
}
/** hunt through a document and translate every math element.
* if spanclassAM is provided, then it is the <tag> that holds math (perhaps 'span'?)
* otherwise we go looking for AMdelimiter
*/
AMprocessNode(n: HTMLElement, linebreaks: boolean, spanclassAM?: string) {
if (spanclassAM != null) {
let frag = document.getElementsByTagName(spanclassAM)
for (let i = 0; i < frag.length; i++)
if (frag[i].className == "AM")
this.processNodeR(frag[i], linebreaks);
} else {
let st: string
try {
st = n.innerHTML; // look for AMdelimiter on page
} catch (err) { }
//alert(st)
if (st == null || /amath\b|\\begin{a?math}/i.test(st) ||
st.indexOf(this.AMdelimiter1 + " ") != -1 || st.slice(-1) == this.AMdelimiter1 ||
st.indexOf(this.AMdelimiter1 + "<") != -1 || st.indexOf(this.AMdelimiter1 + "\n") != -1) {
this.processNodeR(n, linebreaks);
}
}
}
/** load the parsing table. Needs to be reloaded when fixPHI is changed. */
loadAMSymbols() {
this.AMSymbols = [
//some greek symbols
{ input: "alpha", tag: "mi", output: "\u03B1", tex: null, ttype: CONST },
{ input: "beta", tag: "mi", output: "\u03B2", tex: null, ttype: CONST },
{ input: "chi", tag: "mi", output: "\u03C7", tex: null, ttype: CONST },
{ input: "delta", tag: "mi", output: "\u03B4", tex: null, ttype: CONST },
{ input: "Delta", tag: "mo", output: "\u0394", tex: null, ttype: CONST },
{ input: "epsi", tag: "mi", output: "\u03B5", tex: "epsilon", ttype: CONST },
{ input: "varepsilon", tag: "mi", output: "\u025B", tex: null, ttype: CONST },
{ input: "eta", tag: "mi", output: "\u03B7", tex: null, ttype: CONST },
{ input: "gamma", tag: "mi", output: "\u03B3", tex: null, ttype: CONST },
{ input: "Gamma", tag: "mo", output: "\u0393", tex: null, ttype: CONST },
{ input: "iota", tag: "mi", output: "\u03B9", tex: null, ttype: CONST },
{ input: "kappa", tag: "mi", output: "\u03BA", tex: null, ttype: CONST },
{ input: "lambda", tag: "mi", output: "\u03BB", tex: null, ttype: CONST },
{ input: "Lambda", tag: "mo", output: "\u039B", tex: null, ttype: CONST },
{ input: "lamda", tag: "mi", output: "\u03BB", tex: null, ttype: CONST },
{ input: "Lamda", tag: "mo", output: "\u039B", tex: null, ttype: CONST },
{ input: "mu", tag: "mi", output: "\u03BC", tex: null, ttype: CONST },
{ input: "nu", tag: "mi", output: "\u03BD", tex: null, ttype: CONST },
{ input: "omega", tag: "mi", output: "\u03C9", tex: null, ttype: CONST },
{ input: "Omega", tag: "mo", output: "\u03A9", tex: null, ttype: CONST },
{ input: "phi", tag: "mi", output: fixphi ? "\u03D5" : "\u03C6", tex: null, ttype: CONST },
{ input: "varphi", tag: "mi", output: fixphi ? "\u03C6" : "\u03D5", tex: null, ttype: CONST },
{ input: "Phi", tag: "mo", output: "\u03A6", tex: null, ttype: CONST },
{ input: "pi", tag: "mi", output: "\u03C0", tex: null, ttype: CONST },
{ input: "Pi", tag: "mo", output: "\u03A0", tex: null, ttype: CONST },
{ input: "psi", tag: "mi", output: "\u03C8", tex: null, ttype: CONST },
{ input: "Psi", tag: "mi", output: "\u03A8", tex: null, ttype: CONST },
{ input: "rho", tag: "mi", output: "\u03C1", tex: null, ttype: CONST },
{ input: "sigma", tag: "mi", output: "\u03C3", tex: null, ttype: CONST },
{ input: "Sigma", tag: "mo", output: "\u03A3", tex: null, ttype: CONST },
{ input: "tau", tag: "mi", output: "\u03C4", tex: null, ttype: CONST },
{ input: "theta", tag: "mi", output: "\u03B8", tex: null, ttype: CONST },
{ input: "vartheta", tag: "mi", output: "\u03D1", tex: null, ttype: CONST },
{ input: "Theta", tag: "mo", output: "\u0398", tex: null, ttype: CONST },
{ input: "upsilon", tag: "mi", output: "\u03C5", tex: null, ttype: CONST },
{ input: "xi", tag: "mi", output: "\u03BE", tex: null, ttype: CONST },
{ input: "Xi", tag: "mo", output: "\u039E", tex: null, ttype: CONST },
{ input: "zeta", tag: "mi", output: "\u03B6", tex: null, ttype: CONST },
//binary operation symbols
//{input:"-", tag:"mo", output:"\u0096", tex:null, ttype:CONST},
{ input: "*", tag: "mo", output: "\u22C5", tex: "cdot", ttype: CONST },
{ input: "**", tag: "mo", output: "\u2217", tex: "ast", ttype: CONST },
{ input: "***", tag: "mo", output: "\u22C6", tex: "star", ttype: CONST },
{ input: "//", tag: "mo", output: "/", tex: null, ttype: CONST },
{ input: "\\\\", tag: "mo", output: "\\", tex: "backslash", ttype: CONST },
{ input: "setminus", tag: "mo", output: "\\", tex: null, ttype: CONST },
{ input: "xx", tag: "mo", output: "\u00D7", tex: "times", ttype: CONST },
{ input: "|><", tag: "mo", output: "\u22C9", tex: "ltimes", ttype: CONST },
{ input: "><|", tag: "mo", output: "\u22CA", tex: "rtimes", ttype: CONST },