1 |
|
2 | import {defineFunctionBuilders} from "../defineFunction";
|
3 | import buildCommon from "../buildCommon";
|
4 | import mathMLTree from "../mathMLTree";
|
5 |
|
6 | import * as mml from "../buildMathML";
|
7 |
|
8 | import type {ParseNode} from "../parseNode";
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const defaultVariant: {[string]: string} = {
|
14 | "mi": "italic",
|
15 | "mn": "normal",
|
16 | "mtext": "normal",
|
17 | };
|
18 |
|
19 | defineFunctionBuilders({
|
20 | type: "mathord",
|
21 | htmlBuilder(group, options) {
|
22 | return buildCommon.makeOrd(group, options, "mathord");
|
23 | },
|
24 | mathmlBuilder(group: ParseNode<"mathord">, options) {
|
25 | const node = new mathMLTree.MathNode(
|
26 | "mi",
|
27 | [mml.makeText(group.text, group.mode, options)]);
|
28 |
|
29 | const variant = mml.getVariant(group, options) || "italic";
|
30 | if (variant !== defaultVariant[node.type]) {
|
31 | node.setAttribute("mathvariant", variant);
|
32 | }
|
33 | return node;
|
34 | },
|
35 | });
|
36 |
|
37 | defineFunctionBuilders({
|
38 | type: "textord",
|
39 | htmlBuilder(group, options) {
|
40 | return buildCommon.makeOrd(group, options, "textord");
|
41 | },
|
42 | mathmlBuilder(group: ParseNode<"textord">, options) {
|
43 | const text = mml.makeText(group.text, group.mode, options);
|
44 | const variant = mml.getVariant(group, options) || "normal";
|
45 |
|
46 | let node;
|
47 | if (group.mode === 'text') {
|
48 | node = new mathMLTree.MathNode("mtext", [text]);
|
49 | } else if (/[0-9]/.test(group.text)) {
|
50 |
|
51 |
|
52 | node = new mathMLTree.MathNode("mn", [text]);
|
53 | } else if (group.text === "\\prime") {
|
54 | node = new mathMLTree.MathNode("mo", [text]);
|
55 | } else {
|
56 | node = new mathMLTree.MathNode("mi", [text]);
|
57 | }
|
58 | if (variant !== defaultVariant[node.type]) {
|
59 | node.setAttribute("mathvariant", variant);
|
60 | }
|
61 |
|
62 | return node;
|
63 | },
|
64 | });
|