From 7fba2d792209d24b1505af6b39f060c8ccf77b96 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:51:36 -0400 Subject: [PATCH] self-documented divisions --- .../break-on-boundaries.ts | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts index c770e6b3..848fa3a1 100644 --- a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts @@ -18,14 +18,14 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; * All the divisions, where each item is [division macro, environment] * Note that this is ordered from the "largest" division to the "smallest" division. */ -const divisions: [string, string][] = [ - ["part", "_part"], - ["chapter", "_chapter"], - ["section", "_section"], - ["subsection", "_subsection"], - ["subsubsection", "_subsubsection"], - ["paragraph", "_paragraph"], - ["subparagraph", "_subparagraph"], +const divisions: { division: string; mappedEnviron: string }[] = [ + { division: "part", mappedEnviron: "_part" }, + { division: "chapter", mappedEnviron: "_chapter" }, + { division: "section", mappedEnviron: "_section" }, + { division: "subsection", mappedEnviron: "_subsection" }, + { division: "subsubsection", mappedEnviron: "_subsubsection" }, + { division: "paragraph", mappedEnviron: "_paragraph" }, + { division: "subparagraph", mappedEnviron: "_subparagraph" }, ]; /** @@ -37,7 +37,9 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { const messagesLst: { messages: string[] } = { messages: [] }; // check if a macro is a division macro - const isDivision = match.createMacroMatcher(divisions.map((x) => x[0])); + const isDivision = match.createMacroMatcher( + divisions.map((x) => x.division) + ); visit(ast, (node, info) => { // needs to be an environment, root, or group node @@ -55,7 +57,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { // if it's an environment, make sure it isn't a newly created one else if ( anyEnvironment(node) && - divisions.map((x) => x[1]).includes(node.env) + divisions.map((x) => x.mappedEnviron).includes(node.env) ) { return; } @@ -70,7 +72,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { } // now break up the divisions, starting at part - node.content = breakUp(node.content, divisions, 0); + node.content = breakUp(node.content, 0); }); replaceNode(ast, (node) => { @@ -92,24 +94,20 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { /** * Recursively breaks up the AST at the division macros. */ -function breakUp( - content: Ast.Node[], - divisions: [string, string][], - depth: number -): Ast.Node[] { +function breakUp(content: Ast.Node[], depth: number): Ast.Node[] { // broke up all divisions if (depth > 6) { return content; } - const splits = splitOnMacro(content, divisions[depth][0]); + const splits = splitOnMacro(content, divisions[depth].division); // go through each segment to recursively break for (let i = 0; i < splits.segments.length; i++) { - splits.segments[i] = breakUp(splits.segments[i], divisions, depth + 1); + splits.segments[i] = breakUp(splits.segments[i], depth + 1); } - createEnvironments(splits, divisions[depth][1]); + createEnvironments(splits, divisions[depth].mappedEnviron); // rebuild this part of the ast return unsplitOnMacro(splits);