From 3d43101495cc989e6e08502e29d211cc3358e250 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 3 Jul 2024 20:03:33 -0400 Subject: [PATCH] fixed all test cases --- .../break-on-boundaries.ts | 51 ++++++++++--------- .../tests/break-on-boundaries.test.ts | 22 ++++++++ 2 files changed, 49 insertions(+), 24 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 a3b48077..5825fac9 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 @@ -15,7 +15,7 @@ import { import { visit } from "@unified-latex/unified-latex-util-visit"; /** - * All the divisions, where each item is {division macro, mapped environment} + * All the divisions, where each item is {division macro, mapped environment}. * Note that this is ordered from the "largest" division to the "smallest" division. */ const divisions: { division: string; mappedEnviron: string }[] = [ @@ -41,26 +41,6 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { divisions.map((x) => x.division) ); - // check if a node is a mapped environment - const isMappedEnviron = match.createMacroMatcher( - // *** not working - divisions.map((x) => x.mappedEnviron) - ); - - // first remove groups that contain a division as an immediate child - replaceNode(ast, (node) => { - if (match.group(node) && isDivision(node.content[0])) { - // push a warning message - messagesLst.messages.push( - `Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw( - node - )} }` - ); - - return node.content; - } - }); - visit(ast, (node, info) => { // needs to be an environment, root, or group node if ( @@ -79,19 +59,42 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { anyEnvironment(node) && divisions.map((x) => x.mappedEnviron).includes(node.env) ) { - console.log("repeat"); return; } + // add message for groups to be removed that contain a division as an immediate child + if (match.group(node) && isDivision(node.content[0])) { + // push a warning message + messagesLst.messages.push( + `Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw( + node + )} }` + ); + } + // now break up the divisions, starting at part node.content = breakUp(node.content, 0); }); - // remove all old division nodes replaceNode(ast, (node) => { + // remove all old division nodes if (anyMacro(node) && isDivision(node)) { return null; } + + // remove groups in messages + if (match.group(node)) { + // remove if it contains a division or new environment as an immediate child + if ( + isDivision(node.content[0]) || + (anyEnvironment(node.content[0]) && + divisions + .map((x) => x.mappedEnviron) + .includes(node.content[0].env)) + ) { + return node.content; + } + } }); return messagesLst; @@ -120,7 +123,7 @@ function breakUp(content: Ast.Node[], depth: number): Ast.Node[] { } /** - * Create the new environments that replace the division macros + * Create the new environments that replace the division macros. */ function createEnvironments( splits: { segments: Ast.Node[][]; macros: Ast.Macro[] }, diff --git a/packages/unified-latex-to-pretext/tests/break-on-boundaries.test.ts b/packages/unified-latex-to-pretext/tests/break-on-boundaries.test.ts index 1558681e..0f71e227 100644 --- a/packages/unified-latex-to-pretext/tests/break-on-boundaries.test.ts +++ b/packages/unified-latex-to-pretext/tests/break-on-boundaries.test.ts @@ -122,6 +122,28 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { ); }); + it("doesn't break on groups without a division as an immediate child", () => { + value = + String.raw`\part{part1}{not immediate\subsection{Intro}` + + String.raw`\subsubsection{body}{$\mathbb{N}$\subparagraph{Conclusion}Conclusion.}}{\paragraph{immediate} words}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(breakOnBoundaries(ast)).toEqual({ + messages: [ + String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + + String.raw`{ group: {\paragraph{immediate} words} }`, + ], + }); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_part}[part1]{not immediate\begin{_subsection}[Intro]\begin{_subsubsection}[body]` + + String.raw`{$\mathbb{N}$\begin{_subparagraph}[Conclusion]Conclusion.\end{_subparagraph}}` + + String.raw`\end{_subsubsection}\end{_subsection}}\begin{_paragraph}[immediate] words\end{_paragraph}\end{_part}` + ); + }); + it("can break on divisions with latex in their titles", () => { value = String.raw`\chapter{$x = \frac{1}{2}$}Chapter 1\subsection{\"name\_1\" \$}This is subsection`;