tex2typst 0.2.12 → 0.2.15
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/index.js +307 -245
- package/dist/tex-parser.d.ts +28 -0
- package/dist/tex2typst.min.js +1 -1
- package/dist/types.d.ts +47 -6
- package/dist/writer.d.ts +7 -7
- package/package.json +2 -2
- package/src/index.ts +4 -3
- package/src/{parser.ts → tex-parser.ts} +119 -111
- package/src/types.ts +99 -10
- package/src/writer.ts +255 -197
- package/dist/parser.d.ts +0 -28
package/src/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export enum
|
|
1
|
+
export enum TexTokenType {
|
|
2
2
|
ELEMENT,
|
|
3
3
|
COMMAND,
|
|
4
4
|
TEXT,
|
|
5
5
|
COMMENT,
|
|
6
|
-
|
|
6
|
+
SPACE,
|
|
7
7
|
NEWLINE,
|
|
8
8
|
CONTROL,
|
|
9
9
|
UNKNOWN,
|
|
@@ -21,16 +21,83 @@ export type TexSqrtData = TexNode;
|
|
|
21
21
|
|
|
22
22
|
export type TexArrayData = TexNode[][];
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
/**
|
|
25
|
+
* element: 0-9, a-z, A-Z, punctuations such as +-/*,:; etc.
|
|
26
|
+
* symbol: LaTeX macro with no parameter. e.g. \sin \cos \int \sum
|
|
27
|
+
* unaryFunc: LaTeX macro with 1 parameter. e.g. \sqrt{3} \log{x} \exp{x}
|
|
28
|
+
* binaryFunc: LaTeX macro with 2 parameters. e.g. \frac{1}{2}
|
|
29
|
+
* text: text enclosed by braces. e.g. \text{hello world}
|
|
30
|
+
* empty: special type when something is empty. e.g. the base of _{a} or ^{a}
|
|
31
|
+
* whitespace: space, tab, newline
|
|
32
|
+
*/
|
|
33
|
+
type TexNodeType = 'element' | 'text' | 'comment' | 'whitespace' | 'control' | 'ordgroup' | 'supsub'
|
|
26
34
|
| 'unaryFunc' | 'binaryFunc' | 'leftright' | 'beginend' | 'symbol' | 'empty' | 'unknownMacro';
|
|
35
|
+
|
|
36
|
+
export class TexNode {
|
|
37
|
+
type: TexNodeType;
|
|
27
38
|
content: string;
|
|
28
39
|
args?: TexNode[];
|
|
29
|
-
// position?: Position;
|
|
30
40
|
// For type="sqrt", it's additional argument wrapped square bracket. e.g. 3 in \sqrt[3]{x}
|
|
31
41
|
// For type="supsub", it's base, sup, and sub.
|
|
32
|
-
// For type="
|
|
42
|
+
// For type="beginend", it's the 2-dimensional matrix.
|
|
33
43
|
data?: TexSqrtData | TexSupsubData | TexArrayData;
|
|
44
|
+
|
|
45
|
+
constructor(type: TexNodeType, content: string, args?: TexNode[],
|
|
46
|
+
data?: TexSqrtData | TexSupsubData | TexArrayData) {
|
|
47
|
+
this.type = type;
|
|
48
|
+
this.content = content;
|
|
49
|
+
this.args = args;
|
|
50
|
+
this.data = data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public eq_shallow(other: TexNode): boolean {
|
|
54
|
+
return this.type === other.type && this.content === other.content;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public toString(): string {
|
|
58
|
+
switch (this.type) {
|
|
59
|
+
case 'text':
|
|
60
|
+
return `\\text{${this.content}}`;
|
|
61
|
+
default:
|
|
62
|
+
throw new Error(`toString() is not implemented for type ${this.type}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export enum TypstTokenType {
|
|
68
|
+
SYMBOL,
|
|
69
|
+
ATOM,
|
|
70
|
+
TEXT,
|
|
71
|
+
COMMENT,
|
|
72
|
+
SPACE,
|
|
73
|
+
SOFT_SPACE,
|
|
74
|
+
CONTROL,
|
|
75
|
+
NEWLINE,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class TypstToken {
|
|
79
|
+
type: TypstTokenType;
|
|
80
|
+
content: string;
|
|
81
|
+
|
|
82
|
+
constructor(type: TypstTokenType, content: string) {
|
|
83
|
+
this.type = type;
|
|
84
|
+
this.content = content;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
eq(other: TypstToken): boolean {
|
|
88
|
+
return this.type === other.type && this.content === other.content;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
isOneOf(tokens: TypstToken[]): boolean {
|
|
92
|
+
let found = false;
|
|
93
|
+
for (const token of tokens) {
|
|
94
|
+
if (this.eq(token)) {
|
|
95
|
+
found = true;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
34
101
|
}
|
|
35
102
|
|
|
36
103
|
export interface TypstSupsubData {
|
|
@@ -41,17 +108,39 @@ export interface TypstSupsubData {
|
|
|
41
108
|
|
|
42
109
|
export type TypstArrayData = TypstNode[][];
|
|
43
110
|
|
|
44
|
-
|
|
45
|
-
type: 'atom' | 'symbol' | 'text' | 'softSpace' | 'comment' | 'newline'
|
|
111
|
+
type TypstNodeType = 'atom' | 'symbol' | 'text' | 'softSpace' | 'comment' | 'whitespace'
|
|
46
112
|
| 'empty' | 'group' | 'supsub' | 'unaryFunc' | 'binaryFunc' | 'align' | 'matrix' | 'unknown';
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
export class TypstNode {
|
|
116
|
+
type: TypstNodeType;
|
|
47
117
|
content: string;
|
|
48
118
|
args?: TypstNode[];
|
|
49
119
|
data?: TypstSupsubData | TypstArrayData;
|
|
120
|
+
// Some Typst functions accept additional options. e.g. mat() has option "delim", op() has option "limits"
|
|
121
|
+
options?: { [key: string]: string };
|
|
122
|
+
|
|
123
|
+
constructor(type: TypstNodeType, content: string, args?: TypstNode[],
|
|
124
|
+
data?: TypstSupsubData | TypstArrayData) {
|
|
125
|
+
this.type = type;
|
|
126
|
+
this.content = content;
|
|
127
|
+
this.args = args;
|
|
128
|
+
this.data = data;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public setOptions(options: { [key: string]: string }) {
|
|
132
|
+
this.options = options;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public eq_shallow(other: TypstNode): boolean {
|
|
136
|
+
return this.type === other.type && this.content === other.content;
|
|
137
|
+
}
|
|
50
138
|
}
|
|
51
139
|
|
|
52
140
|
export interface Tex2TypstOptions {
|
|
53
|
-
nonStrict?: boolean; // default is
|
|
54
|
-
preferTypstIntrinsic?: boolean; // default is
|
|
141
|
+
nonStrict?: boolean; // default is true
|
|
142
|
+
preferTypstIntrinsic?: boolean; // default is true,
|
|
143
|
+
keepSpaces?: boolean; // default is false
|
|
55
144
|
customTexMacros?: { [key: string]: string };
|
|
56
145
|
// TODO: custom typst functions
|
|
57
146
|
}
|