tex2typst 0.0.19 → 0.2.0
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/README.md +6 -4
- package/dist/index.js +598 -179
- package/dist/parser.d.ts +18 -5
- package/dist/tex2typst.min.js +1 -1
- package/dist/types.d.ts +11 -6
- package/package.json +1 -1
- package/src/map.ts +4 -0
- package/src/parser.ts +702 -207
- package/src/types.ts +12 -6
- package/src/writer.ts +21 -5
- package/tsconfig.json +1 -1
package/src/types.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface LatexParseNode {
|
|
2
2
|
type: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
content?: string;
|
|
4
|
+
arg1?: LatexParseNode;
|
|
5
|
+
arg2?: LatexParseNode;
|
|
6
|
+
args?: LatexParseNode[];
|
|
7
|
+
base?: LatexParseNode;
|
|
8
|
+
sub?: LatexParseNode;
|
|
9
|
+
sup?: LatexParseNode;
|
|
10
|
+
exponent?: LatexParseNode;
|
|
11
|
+
body?: LatexParseNode | LatexParseNode[] | LatexParseNode[][];
|
|
7
12
|
}
|
|
8
13
|
|
|
14
|
+
|
|
9
15
|
export interface TexSupsubData {
|
|
10
16
|
base: TexNode;
|
|
11
17
|
sup?: TexNode;
|
|
@@ -28,7 +34,7 @@ export interface TexNode {
|
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
export interface TypstNode {
|
|
31
|
-
type: 'atom' | 'symbol' | 'text' | 'softSpace'
|
|
37
|
+
type: 'atom' | 'symbol' | 'text' | 'softSpace' | 'comment' | 'newline',
|
|
32
38
|
content: string;
|
|
33
39
|
args?: TypstNode[];
|
|
34
40
|
}
|
package/src/writer.ts
CHANGED
|
@@ -55,6 +55,8 @@ export class TypstWriter {
|
|
|
55
55
|
no_need_space ||= /[0-9]$/.test(this.buffer) && /^[0-9]/.test(str);
|
|
56
56
|
// leading sign
|
|
57
57
|
no_need_space ||= /[\(\[{]\s*(-|\+)$/.test(this.buffer) || this.buffer === "-" || this.buffer === "+";
|
|
58
|
+
// new line
|
|
59
|
+
no_need_space ||= str.startsWith('\n');
|
|
58
60
|
// buffer is empty
|
|
59
61
|
no_need_space ||= this.buffer === "";
|
|
60
62
|
// other cases
|
|
@@ -101,7 +103,7 @@ export class TypstWriter {
|
|
|
101
103
|
|
|
102
104
|
}
|
|
103
105
|
|
|
104
|
-
if (
|
|
106
|
+
if (base.type === 'empty') {
|
|
105
107
|
this.queue.push({ type: 'text', content: '' });
|
|
106
108
|
} else {
|
|
107
109
|
this.appendWithBracketsIfNeeded(base);
|
|
@@ -208,9 +210,6 @@ export class TypstWriter {
|
|
|
208
210
|
if (this.preferTypstIntrinsic && TYPST_INTRINSIC_SYMBOLS.includes(text)) {
|
|
209
211
|
// e.g. we prefer just sech over op("sech")
|
|
210
212
|
this.queue.push({ type: 'symbol', content: text});
|
|
211
|
-
} else if (text.startsWith('SyMb01-')) {
|
|
212
|
-
// special hacks made in parseTex()
|
|
213
|
-
this.queue.push({ type: 'symbol', content: '\\' + text.substring(7)});
|
|
214
213
|
} else {
|
|
215
214
|
this.queue.push({ type: 'symbol', content: 'op' });
|
|
216
215
|
this.queue.push({ type: 'atom', content: '('});
|
|
@@ -226,6 +225,9 @@ export class TypstWriter {
|
|
|
226
225
|
this.append(arg0);
|
|
227
226
|
this.queue.push({ type: 'atom', content: ')'});
|
|
228
227
|
this.insideFunctionDepth --;
|
|
228
|
+
} else if (node.type === 'newline') {
|
|
229
|
+
this.queue.push({ type: 'newline', content: '\n'});
|
|
230
|
+
return;
|
|
229
231
|
} else if (node.type === 'align') {
|
|
230
232
|
const matrix = node.irregularData as TexNode[][];
|
|
231
233
|
matrix.forEach((row, i) => {
|
|
@@ -249,8 +251,12 @@ export class TypstWriter {
|
|
|
249
251
|
row.forEach((cell, j) => {
|
|
250
252
|
// There is a leading & in row
|
|
251
253
|
if (cell.type === 'ordgroup' && cell.args!.length === 0) {
|
|
254
|
+
this.queue.push({ type: 'atom', content: ',' });
|
|
252
255
|
return;
|
|
253
256
|
}
|
|
257
|
+
// if (j == 0 && cell.type === 'newline' && cell.content === '\n') {
|
|
258
|
+
// return;
|
|
259
|
+
// }
|
|
254
260
|
this.append(cell);
|
|
255
261
|
// cell.args!.forEach((n) => this.append(n));
|
|
256
262
|
if (j < row.length - 1) {
|
|
@@ -270,6 +276,8 @@ export class TypstWriter {
|
|
|
270
276
|
} else {
|
|
271
277
|
throw new TypstWriterError(`Unknown macro: ${node.content}`, node);
|
|
272
278
|
}
|
|
279
|
+
} else if (node.type === 'comment') {
|
|
280
|
+
this.queue.push({ type: 'comment', content: node.content });
|
|
273
281
|
} else {
|
|
274
282
|
throw new TypstWriterError(`Unimplemented node type to append: ${node.type}`, node);
|
|
275
283
|
}
|
|
@@ -292,6 +300,12 @@ export class TypstWriter {
|
|
|
292
300
|
this.needSpaceAfterSingleItemScript = true;
|
|
293
301
|
str = '';
|
|
294
302
|
break;
|
|
303
|
+
case 'comment':
|
|
304
|
+
str = `//${node.content}`;
|
|
305
|
+
break;
|
|
306
|
+
case 'newline':
|
|
307
|
+
str = '\n';
|
|
308
|
+
break;
|
|
295
309
|
default:
|
|
296
310
|
throw new TypstWriterError(`Unexpected node type to stringify: ${node.type}`, node)
|
|
297
311
|
}
|
|
@@ -350,7 +364,9 @@ function convertToken(token: string): string {
|
|
|
350
364
|
if (/^[a-zA-Z0-9]$/.test(token)) {
|
|
351
365
|
return token;
|
|
352
366
|
} else if (token === '\\\\') {
|
|
353
|
-
return '
|
|
367
|
+
return '\\';
|
|
368
|
+
} else if (token == '/') {
|
|
369
|
+
return '\\/';
|
|
354
370
|
} else if (['\\$', '\\#', '\\&', '\\_'].includes(token)) {
|
|
355
371
|
return token;
|
|
356
372
|
} else if (token.startsWith('\\')) {
|
package/tsconfig.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
12
|
|
|
13
13
|
/* Language and Environment */
|
|
14
|
-
"target": "
|
|
14
|
+
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
15
|
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
16
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
17
|
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|