Skip to content

Commit

Permalink
add convertTree()
Browse files Browse the repository at this point in the history
  • Loading branch information
qwinsi committed Sep 10, 2024
1 parent df8eac9 commit 68d032c
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 189 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseTex } from "./parser";
import { Tex2TypstOptions } from "./types";
import { TypstWriter } from "./writer";
import { convertTree, TypstWriter } from "./writer";
import { symbolMap } from "./map";


Expand All @@ -21,9 +21,10 @@ export function tex2typst(tex: string, options?: Tex2TypstOptions): string {
opt.customTexMacros = options.customTexMacros;
}
}
const t = parseTex(tex, opt.customTexMacros!);
const texTree = parseTex(tex, opt.customTexMacros!);
const typstTree = convertTree(texTree);
const writer = new TypstWriter(opt.nonStrict!, opt.preferTypstIntrinsic!);
writer.append(t);
writer.append(typstTree);
return writer.finalize();
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export class LatexParser {
if (num_prime > 0) {
res.sup = { type: 'ordgroup', content: '', args: [] };
for (let i = 0; i < num_prime; i++) {
res.sup.args!.push({ type: 'symbol', content: '\\prime' });
res.sup.args!.push({ type: 'element', content: "'" });
}
if (sup) {
res.sup.args!.push(sup);
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ export interface TexNode {
data?: TexSqrtData | TexSupsubData | TexArrayData;
}

export interface TypstSupsubData {
base: TypstNode;
sup?: TypstNode;
sub?: TypstNode;
}

export type TypstArrayData = TypstNode[][];

export interface TypstNode {
type: 'atom' | 'symbol' | 'text' | 'softSpace' | 'comment' | 'newline';
type: 'atom' | 'symbol' | 'text' | 'softSpace' | 'comment' | 'newline'
| 'empty' | 'group' | 'supsub' | 'unaryFunc' | 'binaryFunc' | 'align' | 'matrix' | 'unknown';
content: string;
args?: TypstNode[];
data?: TypstSupsubData | TypstArrayData;
}

export interface Tex2TypstOptions {
Expand Down
Loading

0 comments on commit 68d032c

Please sign in to comment.