xmmath 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/xmmath.es.js +288 -284
- package/dist/xmmath.es.js.map +1 -1
- package/dist/xmmath.umd.js +2 -2
- package/dist/xmmath.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/main.ts +74 -59
- package/test/text.html +0 -32
package/package.json
CHANGED
package/src/main.ts
CHANGED
|
@@ -7,39 +7,13 @@ function createEl(tagname: string) {
|
|
|
7
7
|
return document.createElement(tagname);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
| "mprescripts"
|
|
18
|
-
| "mn"
|
|
19
|
-
| "mo"
|
|
20
|
-
| "mover"
|
|
21
|
-
| "mpadded"
|
|
22
|
-
| "mphantom"
|
|
23
|
-
| "mroot"
|
|
24
|
-
| "mrow"
|
|
25
|
-
| "ms"
|
|
26
|
-
| "mspace"
|
|
27
|
-
| "msqrt"
|
|
28
|
-
| "mstyle"
|
|
29
|
-
| "msub"
|
|
30
|
-
| "msubsup"
|
|
31
|
-
| "msup"
|
|
32
|
-
| "mtable"
|
|
33
|
-
| "mtd"
|
|
34
|
-
| "mtext"
|
|
35
|
-
| "mtr"
|
|
36
|
-
| "munder"
|
|
37
|
-
| "munderover"
|
|
38
|
-
| "semantics";
|
|
39
|
-
|
|
40
|
-
function createMath(tagname: mathtag, innerText?: string, attr?: { [name: string]: string }) {
|
|
41
|
-
let el = document.createElement(tagname);
|
|
42
|
-
if (innerText) el.innerText = innerText;
|
|
10
|
+
function createMath<K extends keyof MathMLElementTagNameMap>(
|
|
11
|
+
tagname: K,
|
|
12
|
+
innerText?: string,
|
|
13
|
+
attr?: { [name: string]: string }
|
|
14
|
+
) {
|
|
15
|
+
let el = document.createElementNS("http://www.w3.org/1998/Math/MathML", tagname);
|
|
16
|
+
if (innerText) el.textContent = innerText;
|
|
43
17
|
if (attr) {
|
|
44
18
|
for (let i in attr) {
|
|
45
19
|
el.setAttribute(i, attr[i]);
|
|
@@ -169,16 +143,16 @@ function ast(str: string) {
|
|
|
169
143
|
} else {
|
|
170
144
|
p_tree.push({ tree: now_tree, close: false });
|
|
171
145
|
now_tree.push({ type: "group", value: tmp_str, children: [] });
|
|
172
|
-
now_tree = now_tree
|
|
146
|
+
now_tree = now_tree.at(-1).children;
|
|
173
147
|
tmp_str = "";
|
|
174
148
|
continue;
|
|
175
149
|
}
|
|
176
150
|
}
|
|
177
151
|
if (t == ")") {
|
|
178
|
-
if (p_tree
|
|
179
|
-
if (p_tree
|
|
180
|
-
p_tree
|
|
181
|
-
now_tree = p_tree
|
|
152
|
+
if (p_tree.at(-1)) {
|
|
153
|
+
if (p_tree.at(-1).close == false) {
|
|
154
|
+
p_tree.at(-1).close = true;
|
|
155
|
+
now_tree = p_tree.at(-1).tree;
|
|
182
156
|
p_tree.pop();
|
|
183
157
|
continue;
|
|
184
158
|
}
|
|
@@ -289,8 +263,7 @@ let f = {
|
|
|
289
263
|
attach: (attr: tree[], dic: fdic) => {
|
|
290
264
|
let base = createMath("mrow");
|
|
291
265
|
base.append(render(attr[0]));
|
|
292
|
-
let el
|
|
293
|
-
el.append(base);
|
|
266
|
+
let el: MathMLElement;
|
|
294
267
|
let tl = createMath("mrow");
|
|
295
268
|
if (dic.tl) tl.append(render(dic.tl));
|
|
296
269
|
let bl = createMath("mrow");
|
|
@@ -300,23 +273,33 @@ let f = {
|
|
|
300
273
|
let br = createMath("mrow");
|
|
301
274
|
if (dic.br) br.append(render(dic.br));
|
|
302
275
|
if (dic.tl || dic.bl || dic.tr || dic.br) {
|
|
303
|
-
|
|
276
|
+
if (dic.tl || dic.bl) {
|
|
277
|
+
el = createMath("mmultiscripts");
|
|
278
|
+
el.append(base, br, tr, createMath("mprescripts"), bl, tl);
|
|
279
|
+
} else if (dic.tr && dic.br) {
|
|
280
|
+
el = createMath("msubsup");
|
|
281
|
+
el.append(base, br, tr);
|
|
282
|
+
} else if (dic.tr) {
|
|
283
|
+
el = createMath("msup");
|
|
284
|
+
el.append(base, tr);
|
|
285
|
+
} else {
|
|
286
|
+
el = createMath("msub");
|
|
287
|
+
el.append(base, br);
|
|
288
|
+
}
|
|
304
289
|
}
|
|
305
290
|
if (dic.t || dic.b) {
|
|
306
|
-
let
|
|
307
|
-
if (el
|
|
308
|
-
|
|
309
|
-
base.append(render(attr[0]));
|
|
310
|
-
ud.append(base);
|
|
291
|
+
let uo = createMath("munderover");
|
|
292
|
+
if (!el) {
|
|
293
|
+
uo.append(base);
|
|
311
294
|
} else {
|
|
312
|
-
|
|
295
|
+
uo.append(el);
|
|
313
296
|
}
|
|
314
297
|
let t = createMath("mrow");
|
|
315
298
|
if (dic.t) t.append(render(dic.t));
|
|
316
299
|
let b = createMath("mrow");
|
|
317
300
|
if (dic.b) b.append(render(dic.b));
|
|
318
|
-
|
|
319
|
-
el =
|
|
301
|
+
uo.append(b, t);
|
|
302
|
+
el = uo;
|
|
320
303
|
}
|
|
321
304
|
return el;
|
|
322
305
|
},
|
|
@@ -505,7 +488,30 @@ let f = {
|
|
|
505
488
|
let t = createMath("mtable");
|
|
506
489
|
for (let i of attr) {
|
|
507
490
|
let r = createMath("mtr");
|
|
508
|
-
|
|
491
|
+
|
|
492
|
+
// 按&拆分
|
|
493
|
+
let result: tree[] = [];
|
|
494
|
+
let tmp_group: tree = [];
|
|
495
|
+
for (let n = 0; n < i.length; n++) {
|
|
496
|
+
if (!i[n].esc && i[n].value == "&") {
|
|
497
|
+
if (tmp_group.length > 0) {
|
|
498
|
+
result.push(tmp_group);
|
|
499
|
+
tmp_group = [];
|
|
500
|
+
}
|
|
501
|
+
} else {
|
|
502
|
+
tmp_group.push(i[n]);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (tmp_group.length > 0) {
|
|
506
|
+
result.push(tmp_group);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
for (let i of result) {
|
|
510
|
+
let d = createMath("mtd");
|
|
511
|
+
r.append(d);
|
|
512
|
+
d.append(render(i));
|
|
513
|
+
}
|
|
514
|
+
|
|
509
515
|
t.append(r);
|
|
510
516
|
}
|
|
511
517
|
return t;
|
|
@@ -644,10 +650,10 @@ function underover_f(type: "under" | "over", tree: tree, x: string, str?: tree)
|
|
|
644
650
|
function kh(tree: tree) {
|
|
645
651
|
let f = createMath("mrow");
|
|
646
652
|
let l = createMath("mo");
|
|
647
|
-
l.
|
|
653
|
+
l.textContent = "(";
|
|
648
654
|
let c = render(tree);
|
|
649
655
|
let r = createMath("mo");
|
|
650
|
-
r.
|
|
656
|
+
r.textContent = ")";
|
|
651
657
|
f.append(l, c, r);
|
|
652
658
|
return f;
|
|
653
659
|
}
|
|
@@ -716,6 +722,9 @@ function dic_to_ast(dic: { [id: string]: tree }) {
|
|
|
716
722
|
for (let i in dic) {
|
|
717
723
|
l.push({ type: "f", value: i });
|
|
718
724
|
l.push({ type: "v", value: ":" });
|
|
725
|
+
for (let x of dic[i]) {
|
|
726
|
+
if (x.type == "v" && x.value == ",") x.esc = true;
|
|
727
|
+
}
|
|
719
728
|
l.push(...dic[i]);
|
|
720
729
|
if (Number(i) + 1 != Object.keys(dic).length) {
|
|
721
730
|
l.push(dh);
|
|
@@ -782,11 +791,11 @@ function ast2(tree: tree) {
|
|
|
782
791
|
nn++;
|
|
783
792
|
}
|
|
784
793
|
if (l.length) {
|
|
785
|
-
let nx: tree[0] = { type: "f", value: shorthand[l
|
|
794
|
+
let nx: tree[0] = { type: "f", value: shorthand[l.at(-1)] };
|
|
786
795
|
t.push(nx);
|
|
787
796
|
|
|
788
797
|
// 不处理已经后面连起来的字符
|
|
789
|
-
continue_c = l
|
|
798
|
+
continue_c = l.at(-1).length - 1;
|
|
790
799
|
} else {
|
|
791
800
|
t.push(x);
|
|
792
801
|
}
|
|
@@ -1134,7 +1143,7 @@ function render(tree: tree) {
|
|
|
1134
1143
|
if (is_f_mark(t, ",")) {
|
|
1135
1144
|
if (type == "dic") {
|
|
1136
1145
|
dicl.push(l);
|
|
1137
|
-
|
|
1146
|
+
type = "array";
|
|
1138
1147
|
} else {
|
|
1139
1148
|
attr.push(l);
|
|
1140
1149
|
}
|
|
@@ -1184,7 +1193,13 @@ function render(tree: tree) {
|
|
|
1184
1193
|
|
|
1185
1194
|
if (x.type == "f" && !x.children) {
|
|
1186
1195
|
if (ss[x.value]) {
|
|
1187
|
-
let
|
|
1196
|
+
let tag: keyof MathMLElementTagNameMap;
|
|
1197
|
+
if (x.value.match(/[a-zA-Z\u0391-\u03C9]/)) {
|
|
1198
|
+
tag = "mi";
|
|
1199
|
+
} else {
|
|
1200
|
+
tag = "mo";
|
|
1201
|
+
}
|
|
1202
|
+
let el = createMath(tag, ss[x.value]);
|
|
1188
1203
|
fragment.append(el);
|
|
1189
1204
|
} else {
|
|
1190
1205
|
if (f[x.value]) {
|
|
@@ -1200,10 +1215,10 @@ function render(tree: tree) {
|
|
|
1200
1215
|
}
|
|
1201
1216
|
|
|
1202
1217
|
if (x.type == "v") {
|
|
1203
|
-
let tag:
|
|
1218
|
+
let tag: keyof MathMLElementTagNameMap;
|
|
1204
1219
|
if (x.value.match(/[0-9]+/)) {
|
|
1205
1220
|
tag = "mn";
|
|
1206
|
-
} else if (x.value.match(/[a-zA-Z]/)) {
|
|
1221
|
+
} else if (x.value.match(/[a-zA-Z\u0391-\u03C9]/)) {
|
|
1207
1222
|
tag = "mi";
|
|
1208
1223
|
} else {
|
|
1209
1224
|
tag = "mo";
|
|
@@ -1224,7 +1239,7 @@ function toMML(str: string) {
|
|
|
1224
1239
|
let obj = ast(str);
|
|
1225
1240
|
console.log(obj);
|
|
1226
1241
|
|
|
1227
|
-
let mathEl =
|
|
1242
|
+
let mathEl = createMath("math");
|
|
1228
1243
|
mathEl.setAttribute("display", "block");
|
|
1229
1244
|
let f = render(obj);
|
|
1230
1245
|
mathEl.append(f);
|
package/test/text.html
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
<html lang="">
|
|
2
|
-
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<title></title>
|
|
6
|
-
</head>
|
|
7
|
-
|
|
8
|
-
<body>
|
|
9
|
-
|
|
10
|
-
<textarea name="" id="" cols="30" rows="10"></textarea>
|
|
11
|
-
|
|
12
|
-
<script src="../dist/xmmath.umd.js"></script>
|
|
13
|
-
<script>
|
|
14
|
-
let text = document.querySelector('textarea')
|
|
15
|
-
let div = document.createElement('div')
|
|
16
|
-
document.body.append(div)
|
|
17
|
-
text.oninput = () => {
|
|
18
|
-
div.innerHTML = xmmath.toMMLHTML(text.value)
|
|
19
|
-
}
|
|
20
|
-
let l = ['x^2y^2', 'attach(F,bl:2,br:3)', "(x+y^2)/(k+1)", "x+y^(2/(k+1))", 'a/(b\\/2)', "a_0+1/(a_1+1/(a_2+1/(a_3+1/a_4)))", "attach(Pi, t: alpha, b: beta,tl: 1, tr: 2+3, bl: 4+5, br: 6,)", `binom(n,k\\/2)`, `binom(p,2)x^2y^(p-2)-1/(1-x) 1/(1-x^2)`, `sum_(0<=i<=m\\ 0<j<n) P(i,j)`, `x^(2y)`, `sum^p_(i=1)sum^q_(j=1)sum^r_(k=1) a_(i j)b_(j k)c_(k i)`, 'sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+sqrt(1+x)))))))', '(diff^2/(diff x^2)+diff^2/(diff y^2))abs(phi (x+"i" y))^2=0', '2^2^2^x', 'integral^x_1(d t)/t', 'integral integral_D d x d y', 'f(x)=cases(1 \\/ 3 "if" 0<= x <=1";",2\\/3 "if"3<=x<=4";")', 'overbrace(x+dots+x,k"times")', 'sum_(p"prime")f(p)=integral_(t>1)f(t)d Pi(t)', '{underbrace(overbrace(a\\,dots\\,a),overbrace(b\\,dots\\,b))}', 'mat(mat(a,b;c,d),mat(e,f;g,h);0,mat(i,j;k,l))', 'det mat(delim:"|",c_0,c_1,c_2,dots,c_n;c_1)>0', 'y_x_2', 'x^31415_92+Pi', 'x^(z^c_d)_(y^a_b)', "y^(''')_3", 'lim_(n->+oo)sqrt(2Pi n)/(n!)(n/e)^n=1', 'det(A)=sum_(sigma in S_n)epsilon.alt(sigma)product^n_(i=1)a_(i,sigma_i)']
|
|
21
|
-
for (let i of l) {
|
|
22
|
-
let div = document.createElement('div')
|
|
23
|
-
let p = document.createElement('pre')
|
|
24
|
-
p.innerText = i
|
|
25
|
-
div.append(p)
|
|
26
|
-
div.insertAdjacentHTML('beforeend', xmmath.toMMLHTML(i))
|
|
27
|
-
document.body.append(div)
|
|
28
|
-
}
|
|
29
|
-
</script>
|
|
30
|
-
</body>
|
|
31
|
-
|
|
32
|
-
</html>
|