Skip to content

Commit

Permalink
fixed all test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
renee-k committed Jul 4, 2024
1 parent 9f407ca commit 3d43101
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[] = [
Expand All @@ -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 (
Expand All @@ -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;
Expand Down Expand Up @@ -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[] },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down

0 comments on commit 3d43101

Please sign in to comment.