From 9f407ca532da67d30fa5c7c9fb7761ebf0b07279 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:07:10 -0400 Subject: [PATCH] updated documentation --- .../break-on-boundaries.ts | 42 +++++++++++-------- .../tests/break-on-boundaries.test.ts | 4 +- 2 files changed, 26 insertions(+), 20 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 848fa3a1..a3b48077 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, 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,6 +41,26 @@ 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 ( @@ -59,35 +79,21 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { anyEnvironment(node) && divisions.map((x) => x.mappedEnviron).includes(node.env) ) { + console.log("repeat"); return; } - // if it's a group, push a warning message - if (match.group(node)) { - 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 - else if (match.group(node)) { - return node.content; - } }); - console.log(messagesLst); - return messagesLst; } @@ -109,7 +115,7 @@ function breakUp(content: Ast.Node[], depth: number): Ast.Node[] { createEnvironments(splits, divisions[depth].mappedEnviron); - // rebuild this part of the ast + // rebuild this part of the AST return unsplitOnMacro(splits); } 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 52d20513..1558681e 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 @@ -111,7 +111,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + String.raw`{ group: {\subsection{Intro}description.\subsubsection{body}more text.{\subparagraph{Conclusion}Conclusion.}} }`, String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + - String.raw`{ group: {\subparagraph{Conclusion}Conclusion.} }`, + String.raw`{ group: {\subparagraph{Conclusion}Conclusion.} }`, // ** Doesn't keep nested group ], }); @@ -146,7 +146,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( - String.raw`\begin{_subsubsection}[first]subsection 1` + + String.raw`\begin{_subsubsection}[first]subsection 1 ` + String.raw`\begin{_paragraph}[body]This is paragraph` + String.raw`\end{_paragraph}\end{_subsubsection}` );