From 3a85c9887c4a892266f29228fb5edb3457e852a7 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Sat, 25 May 2024 00:16:46 -0400 Subject: [PATCH 01/40] added math mode macro report function and test file --- .../libs/report-math-mode-macros.ts | 31 +++++++++++++++++++ .../tests/unified-latex-report-macro.test.ts | 24 ++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts create mode 100644 packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts diff --git a/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts b/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts new file mode 100644 index 00000000..8357f9dd --- /dev/null +++ b/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts @@ -0,0 +1,31 @@ +import * as Ast from "@unified-latex/unified-latex-types"; +import {createMacroExpander, expandMacros } from "@unified-latex/unified-latex-util-macros" +import { anyMacro } from "@unified-latex/unified-latex-util-match"; +import { visit } from "@unified-latex/unified-latex-util-visit"; +import KATEX_SUPPORT_LIST from "./katex-support.json" + +// return list of unsupported macros? +export function parse_macros(ast: Ast.Ast): string[] { + let unsupported: string[]; + + // visit all nodes + visit(ast, (node, info) => { + // macro in math mode (need both?) + if (anyMacro(node) && (info.context.hasMathModeAncestor || info.context.inMathMode)) { + // check if user-defined + // *see if there are more user-defined commands + if (node.content === "newcommand" || node.content === "renewcommand") { + return; // For now + } + + // check if supported from katex + + } + }); +} + +// might not need this depending on macros package capabilities +export function expand_user_macros(node: Ast.Macro): { + +} + diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts new file mode 100644 index 00000000..2a61bba2 --- /dev/null +++ b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts @@ -0,0 +1,24 @@ +import { describe, it, expect } from "vitest"; +import Prettier from "prettier"; +import util from "util"; +import { getParser } from "@unified-latex/unified-latex-util-parse"; +import { parse_macros } from "../libs/report-math-mode-macros"; + +// Make console.log pretty-print by default +const origLog = console.log; +console.log = (...args) => { + origLog(...args.map((x) => util.inspect(x, false, 10, true))); +}; + +describe("unified-latex-to-pretext:unified-latex-report-macro", () => { + let value: string | undefined; + + it("can reported unsupported macros", () => { + value = String.raw`$\\mathbb{R} \\fakemacro{X}$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(parse_macros(ast)).toEqual(["fakemacro"]); + }); +}); From 0c1971a4ea200599937a31ce33d5e0df29c40d15 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Sun, 26 May 2024 00:19:08 -0400 Subject: [PATCH 02/40] updated report unsupported katex and expand command files --- .../libs/report-and-expand-macros.ts | 34 +++++++++++++++++++ .../libs/report-math-mode-macros.ts | 31 ----------------- .../tests/unified-latex-report-macro.test.ts | 10 +++--- 3 files changed, 39 insertions(+), 36 deletions(-) create mode 100644 packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts delete mode 100644 packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts new file mode 100644 index 00000000..8efcf58c --- /dev/null +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -0,0 +1,34 @@ +import * as Ast from "@unified-latex/unified-latex-types"; +import { newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros/libs/newcommand"; +import { newcommandMatcher } from "@unified-latex/unified-latex-util-macros/libs/list-newcommands" +import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; +import { visit } from "@unified-latex/unified-latex-util-visit"; +import { katexSpecificMacroReplacements } from "./pre-conversion-subs/katex-subs"; + +// return list of unsupported macros? +export function report_macros(ast: Ast.Ast): string[] { + let unsupported: string[] = []; + + // match a macro supported by Katex + const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); + + // visit all nodes + visit(ast, (node, info) => { + // macro in math mode + if (anyMacro(node) && info.context.hasMathModeAncestor) { + const macro_name = node.content; + + // check if not supported by katex + if (!isKatexMacro(node)) { + unsupported.push(macro_name); + } + } + }); + + return unsupported; +} + +export function expand_user_macros(node: Ast.Macro): Ast.Node[] { + return newcommandMacroToSubstitutionAst(node); +} + diff --git a/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts b/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts deleted file mode 100644 index 8357f9dd..00000000 --- a/packages/unified-latex-to-pretext/libs/report-math-mode-macros.ts +++ /dev/null @@ -1,31 +0,0 @@ -import * as Ast from "@unified-latex/unified-latex-types"; -import {createMacroExpander, expandMacros } from "@unified-latex/unified-latex-util-macros" -import { anyMacro } from "@unified-latex/unified-latex-util-match"; -import { visit } from "@unified-latex/unified-latex-util-visit"; -import KATEX_SUPPORT_LIST from "./katex-support.json" - -// return list of unsupported macros? -export function parse_macros(ast: Ast.Ast): string[] { - let unsupported: string[]; - - // visit all nodes - visit(ast, (node, info) => { - // macro in math mode (need both?) - if (anyMacro(node) && (info.context.hasMathModeAncestor || info.context.inMathMode)) { - // check if user-defined - // *see if there are more user-defined commands - if (node.content === "newcommand" || node.content === "renewcommand") { - return; // For now - } - - // check if supported from katex - - } - }); -} - -// might not need this depending on macros package capabilities -export function expand_user_macros(node: Ast.Macro): { - -} - diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts index 2a61bba2..71e67711 100644 --- a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts +++ b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest"; import Prettier from "prettier"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; -import { parse_macros } from "../libs/report-math-mode-macros"; +import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; // Make console.log pretty-print by default const origLog = console.log; @@ -11,14 +11,14 @@ console.log = (...args) => { }; describe("unified-latex-to-pretext:unified-latex-report-macro", () => { - let value: string | undefined; + let value: string; it("can reported unsupported macros", () => { value = String.raw`$\\mathbb{R} \\fakemacro{X}$`; const parser = getParser(); const ast = parser.parse(value); - - expect(parse_macros(ast)).toEqual(["fakemacro"]); + + expect(report_macros(ast)).toEqual(["fakemacro"]); }); -}); +}); \ No newline at end of file From a140ea36b55246041438e55913e1c3985802ea7c Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Sun, 26 May 2024 19:30:00 -0400 Subject: [PATCH 03/40] added more tests to be implemented --- .../libs/report-and-expand-macros.ts | 2 +- .../tests/unified-latex-report-macro.test.ts | 24 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts index 8efcf58c..5972c203 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -5,7 +5,7 @@ import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; import { katexSpecificMacroReplacements } from "./pre-conversion-subs/katex-subs"; -// return list of unsupported macros? +// return list of unsupported macros export function report_macros(ast: Ast.Ast): string[] { let unsupported: string[] = []; diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts index 71e67711..e9d0ba69 100644 --- a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts +++ b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts @@ -10,7 +10,7 @@ console.log = (...args) => { origLog(...args.map((x) => util.inspect(x, false, 10, true))); }; -describe("unified-latex-to-pretext:unified-latex-report-macro", () => { +describe("unified-latex-to-pretext:report-and-expand-macro", () => { let value: string; it("can reported unsupported macros", () => { @@ -21,4 +21,26 @@ describe("unified-latex-to-pretext:unified-latex-report-macro", () => { expect(report_macros(ast)).toEqual(["fakemacro"]); }); + + it("can report no unsupported macros", () => { + value = String.raw`$\\mathbb{R} \\frac{1}{2} \\cup$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(report_macros(ast)).toEqual([]); + }); + + it("can expand newcommand", () => { + // value = String.raw`\\newcommand{\\foo}{\\bar{#1}}`; + + // const parser = getParser(); + // const ast = parser.parse(value); + + // expect(expand_user_macros(ast)).toEqual([]); // want \bar{#1} + }); + + it("can expand renewcommand", () => { + + }); }); \ No newline at end of file From 575141def030003ea2a04fa6d96112057b52bf74 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Tue, 28 May 2024 00:04:41 -0400 Subject: [PATCH 04/40] implemented expand macros tests and function --- .../libs/report-and-expand-macros.ts | 30 ++++++-- .../tests/unified-latex-macro.test.ts | 76 +++++++++++++++++++ .../tests/unified-latex-report-macro.test.ts | 46 ----------- 3 files changed, 98 insertions(+), 54 deletions(-) create mode 100644 packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts delete mode 100644 packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts index 5972c203..a5d83b88 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -1,25 +1,32 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros/libs/newcommand"; -import { newcommandMatcher } from "@unified-latex/unified-latex-util-macros/libs/list-newcommands" +import { newcommandMatcher,listNewcommands, newcommandMacroToSubstitutionAst, expandMacrosExcludingDefinitions, expandMacros } from "@unified-latex/unified-latex-util-macros"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; -import { katexSpecificMacroReplacements } from "./pre-conversion-subs/katex-subs"; +import { attachNeededRenderInfo, katexSpecificMacroReplacements, KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; +import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; +import { replaceNode } from "@unified-latex/unified-latex-util-replace"; -// return list of unsupported macros +/** + * Return list of macros unsupported by Katex + */ export function report_macros(ast: Ast.Ast): string[] { let unsupported: string[] = []; + //attachNeededRenderInfo(ast); // match a macro supported by Katex - const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); + // const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); // visit all nodes visit(ast, (node, info) => { // macro in math mode if (anyMacro(node) && info.context.hasMathModeAncestor) { const macro_name = node.content; + //console.log(node); + //console.log(katexSpecificMacroReplacements[node.content]); // check if not supported by katex - if (!isKatexMacro(node)) { + // if (!isKatexMacro(node)) { + if (!KATEX_SUPPORT.macros.includes(macro_name)) { unsupported.push(macro_name); } } @@ -28,7 +35,14 @@ export function report_macros(ast: Ast.Ast): string[] { return unsupported; } -export function expand_user_macros(node: Ast.Macro): Ast.Node[] { - return newcommandMacroToSubstitutionAst(node); +/** + * Expands user-defined macros + */ +export function expand_user_macros(ast: Ast.Ast): void { + replaceNode(ast, (node) => { + if (anyMacro(node) && newcommandMatcher(node)) { + return newcommandMacroToSubstitutionAst(node); + } + }); } diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts new file mode 100644 index 00000000..e4d9461c --- /dev/null +++ b/packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts @@ -0,0 +1,76 @@ +import { describe, it, expect } from "vitest"; +import Prettier from "prettier"; +import util from "util"; +import { getParser } from "@unified-latex/unified-latex-util-parse"; +import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; +import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; + +// Make console.log pretty-print by default +const origLog = console.log; +console.log = (...args) => { + origLog(...args.map((x) => util.inspect(x, false, 10, true))); +}; + +describe("unified-latex-to-pretext:report-and-expand-macro", () => { + let value: string; + + it("can reported unsupported macros", () => { + value = String.raw`$\mathbb{R} \fakemacro{X}$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(report_macros(ast)).toEqual(["fakemacro"]); + }); + + it("can report no unsupported macros in mathmode", () => { + value = String.raw`$\mathbb{R} \frac{1}{2} \cup$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(report_macros(ast)).toEqual([]); + }); + + it("can report no unsupported macros not in mathmode", () => { + value = String.raw`\underline{text} \textbf{bold} \subsection{section}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(report_macros(ast)).toEqual([]); + }); + + it("can expand newcommand", () => { + value = String.raw`\newcommand{\foo}{\bar{#1}}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expand_user_macros(ast) + + expect(printRaw(ast)).toEqual("\\bar{#1}"); + }); + + it("can expand renewcommand", () => { + value = String.raw`\renewcommand{\mathbb{N}}{\N}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expand_user_macros(ast) + + expect(printRaw(ast)).toEqual("\\N"); + }); + + it("can expand multiple user-defined commands", () => { + value = String.raw`\newcommand{\join}{\vee} \renewcommand{\vee}{\foo}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expand_user_macros(ast) + + expect(printRaw(ast)).toEqual("\\vee \\foo"); + }); +}); \ No newline at end of file diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts b/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts deleted file mode 100644 index e9d0ba69..00000000 --- a/packages/unified-latex-to-pretext/tests/unified-latex-report-macro.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { describe, it, expect } from "vitest"; -import Prettier from "prettier"; -import util from "util"; -import { getParser } from "@unified-latex/unified-latex-util-parse"; -import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; - -// Make console.log pretty-print by default -const origLog = console.log; -console.log = (...args) => { - origLog(...args.map((x) => util.inspect(x, false, 10, true))); -}; - -describe("unified-latex-to-pretext:report-and-expand-macro", () => { - let value: string; - - it("can reported unsupported macros", () => { - value = String.raw`$\\mathbb{R} \\fakemacro{X}$`; - - const parser = getParser(); - const ast = parser.parse(value); - - expect(report_macros(ast)).toEqual(["fakemacro"]); - }); - - it("can report no unsupported macros", () => { - value = String.raw`$\\mathbb{R} \\frac{1}{2} \\cup$`; - - const parser = getParser(); - const ast = parser.parse(value); - - expect(report_macros(ast)).toEqual([]); - }); - - it("can expand newcommand", () => { - // value = String.raw`\\newcommand{\\foo}{\\bar{#1}}`; - - // const parser = getParser(); - // const ast = parser.parse(value); - - // expect(expand_user_macros(ast)).toEqual([]); // want \bar{#1} - }); - - it("can expand renewcommand", () => { - - }); -}); \ No newline at end of file From 463f366fab351758df7ade006e4737a342b5032f Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 00:17:23 -0400 Subject: [PATCH 05/40] added files for breaking source on boundaries --- .../libs/break-on-boundaries.ts | 1 + .../tests/break-on-boundaries-test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 packages/unified-latex-to-pretext/libs/break-on-boundaries.ts create mode 100644 packages/unified-latex-to-pretext/tests/break-on-boundaries-test.ts diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts new file mode 100644 index 00000000..f05abc6d --- /dev/null +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -0,0 +1 @@ +import * as Ast from "@unified-latex/unified-latex-types"; \ No newline at end of file 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 new file mode 100644 index 00000000..a8b1d4e8 --- /dev/null +++ b/packages/unified-latex-to-pretext/tests/break-on-boundaries-test.ts @@ -0,0 +1,15 @@ +import { describe, it, expect } from "vitest"; +import util from "util"; +import { getParser } from "@unified-latex/unified-latex-util-parse"; +import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; +import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; + +// Make console.log pretty-print by default +const origLog = console.log; +console.log = (...args) => { + origLog(...args.map((x) => util.inspect(x, false, 10, true))); +}; + +describe("unified-latex-to-pretext:break-on-boundaries", () => { + +}); \ No newline at end of file From 26c09acf6f7c68ea4250562e34f9fda1e5c8008f Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 00:28:24 -0400 Subject: [PATCH 06/40] added some test cases --- .../tests/break-on-boundaries-test.ts | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) 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 a8b1d4e8..ed96b325 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 @@ -11,5 +11,53 @@ console.log = (...args) => { }; describe("unified-latex-to-pretext:break-on-boundaries", () => { - + let value: string; + + it("can break on sections", () => { + value = String.raw`\section{Foo} + Hi, this is a section + + \section{Bar} + This is another section`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw`\begin{_section} + \title{Foo} + Hi, this is a section + \end{_section} + \begin{_section} + \title{Bar} + This is another section + \end{_section}`]); + }); + it("can break on a subsection", () => { + value = String.raw`\subsection{subsection} + Hi, this is a subsection`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw``]); + }); + it("can break on subsection between subsections properly", () => { + value = String.raw`\section{First Section} + Hi, this is a section + \subsection{Inner Subsection} + This is a subsection + \section{Second Subsection} + This is another section`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw``]); + }); }); \ No newline at end of file From 2e5fdbd1c1f0e612a300fe8ae0be02b9ff854275 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 13:37:39 -0400 Subject: [PATCH 07/40] updated code --- .../libs/report-and-expand-macros.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts index a5d83b88..f4b7f47d 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -1,9 +1,8 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { newcommandMatcher,listNewcommands, newcommandMacroToSubstitutionAst, expandMacrosExcludingDefinitions, expandMacros } from "@unified-latex/unified-latex-util-macros"; +import { newcommandMatcher, newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; -import { attachNeededRenderInfo, katexSpecificMacroReplacements, KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; -import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; +import { KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; import { replaceNode } from "@unified-latex/unified-latex-util-replace"; /** @@ -11,22 +10,18 @@ import { replaceNode } from "@unified-latex/unified-latex-util-replace"; */ export function report_macros(ast: Ast.Ast): string[] { let unsupported: string[] = []; - //attachNeededRenderInfo(ast); // match a macro supported by Katex - // const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); + const isSupported = match.createMacroMatcher(KATEX_SUPPORT.macros); // visit all nodes visit(ast, (node, info) => { // macro in math mode if (anyMacro(node) && info.context.hasMathModeAncestor) { const macro_name = node.content; - //console.log(node); - //console.log(katexSpecificMacroReplacements[node.content]); // check if not supported by katex - // if (!isKatexMacro(node)) { - if (!KATEX_SUPPORT.macros.includes(macro_name)) { + if (!isSupported(node)) { unsupported.push(macro_name); } } @@ -38,7 +33,7 @@ export function report_macros(ast: Ast.Ast): string[] { /** * Expands user-defined macros */ -export function expand_user_macros(ast: Ast.Ast): void { +export function expand_user_macros(ast: Ast.Ast): void { replaceNode(ast, (node) => { if (anyMacro(node) && newcommandMatcher(node)) { return newcommandMacroToSubstitutionAst(node); From 27117651914aafacd8d66fb2ffa3c4fdd7ecc8ce Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 13:40:59 -0400 Subject: [PATCH 08/40] formatted code --- .../libs/report-and-expand-macros.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts index a5d83b88..958cf16d 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -1,9 +1,8 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { newcommandMatcher,listNewcommands, newcommandMacroToSubstitutionAst, expandMacrosExcludingDefinitions, expandMacros } from "@unified-latex/unified-latex-util-macros"; +import { newcommandMatcher, newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; -import { attachNeededRenderInfo, katexSpecificMacroReplacements, KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; -import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; +import { KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; import { replaceNode } from "@unified-latex/unified-latex-util-replace"; /** @@ -11,22 +10,18 @@ import { replaceNode } from "@unified-latex/unified-latex-util-replace"; */ export function report_macros(ast: Ast.Ast): string[] { let unsupported: string[] = []; - //attachNeededRenderInfo(ast); // match a macro supported by Katex - // const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); + const isSupported = match.createMacroMatcher(KATEX_SUPPORT.macros); // visit all nodes visit(ast, (node, info) => { // macro in math mode if (anyMacro(node) && info.context.hasMathModeAncestor) { const macro_name = node.content; - //console.log(node); - //console.log(katexSpecificMacroReplacements[node.content]); // check if not supported by katex - // if (!isKatexMacro(node)) { - if (!KATEX_SUPPORT.macros.includes(macro_name)) { + if (!isSupported(node)) { unsupported.push(macro_name); } } @@ -38,11 +33,10 @@ export function report_macros(ast: Ast.Ast): string[] { /** * Expands user-defined macros */ -export function expand_user_macros(ast: Ast.Ast): void { +export function expand_user_macros(ast: Ast.Ast): void { replaceNode(ast, (node) => { if (anyMacro(node) && newcommandMatcher(node)) { return newcommandMacroToSubstitutionAst(node); } }); -} - +} \ No newline at end of file From ac2333409bb3975996df4647b0800bbe3033adb5 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 13:54:32 -0400 Subject: [PATCH 09/40] Revert "added some test cases" This reverts commit 26c09acf6f7c68ea4250562e34f9fda1e5c8008f. --- .../tests/break-on-boundaries-test.ts | 50 +------------------ 1 file changed, 1 insertion(+), 49 deletions(-) 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 ed96b325..a8b1d4e8 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 @@ -11,53 +11,5 @@ console.log = (...args) => { }; describe("unified-latex-to-pretext:break-on-boundaries", () => { - let value: string; - - it("can break on sections", () => { - value = String.raw`\section{Foo} - Hi, this is a section - - \section{Bar} - This is another section`; - - const parser = getParser(); - const ast = parser.parse(value); - - // break-on-boundaries work done here - - expect(printRaw(ast)).toEqual([String.raw`\begin{_section} - \title{Foo} - Hi, this is a section - \end{_section} - \begin{_section} - \title{Bar} - This is another section - \end{_section}`]); - }); - it("can break on a subsection", () => { - value = String.raw`\subsection{subsection} - Hi, this is a subsection`; - - const parser = getParser(); - const ast = parser.parse(value); - - // break-on-boundaries work done here - - expect(printRaw(ast)).toEqual([String.raw``]); - }); - it("can break on subsection between subsections properly", () => { - value = String.raw`\section{First Section} - Hi, this is a section - \subsection{Inner Subsection} - This is a subsection - \section{Second Subsection} - This is another section`; - - const parser = getParser(); - const ast = parser.parse(value); - - // break-on-boundaries work done here - - expect(printRaw(ast)).toEqual([String.raw``]); - }); + }); \ No newline at end of file From 30bac19646dfa9b22d45d1390eb1ed6f0bb33588 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 13:55:34 -0400 Subject: [PATCH 10/40] Revert "updated code" This reverts commit 2e5fdbd1c1f0e612a300fe8ae0be02b9ff854275. --- .../libs/report-and-expand-macros.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts index f4b7f47d..a5d83b88 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts @@ -1,8 +1,9 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { newcommandMatcher, newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros"; +import { newcommandMatcher,listNewcommands, newcommandMacroToSubstitutionAst, expandMacrosExcludingDefinitions, expandMacros } from "@unified-latex/unified-latex-util-macros"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; -import { KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; +import { attachNeededRenderInfo, katexSpecificMacroReplacements, KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; +import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; import { replaceNode } from "@unified-latex/unified-latex-util-replace"; /** @@ -10,18 +11,22 @@ import { replaceNode } from "@unified-latex/unified-latex-util-replace"; */ export function report_macros(ast: Ast.Ast): string[] { let unsupported: string[] = []; + //attachNeededRenderInfo(ast); // match a macro supported by Katex - const isSupported = match.createMacroMatcher(KATEX_SUPPORT.macros); + // const isKatexMacro = match.createMacroMatcher(katexSpecificMacroReplacements); // visit all nodes visit(ast, (node, info) => { // macro in math mode if (anyMacro(node) && info.context.hasMathModeAncestor) { const macro_name = node.content; + //console.log(node); + //console.log(katexSpecificMacroReplacements[node.content]); // check if not supported by katex - if (!isSupported(node)) { + // if (!isKatexMacro(node)) { + if (!KATEX_SUPPORT.macros.includes(macro_name)) { unsupported.push(macro_name); } } @@ -33,7 +38,7 @@ export function report_macros(ast: Ast.Ast): string[] { /** * Expands user-defined macros */ -export function expand_user_macros(ast: Ast.Ast): void { +export function expand_user_macros(ast: Ast.Ast): void { replaceNode(ast, (node) => { if (anyMacro(node) && newcommandMatcher(node)) { return newcommandMacroToSubstitutionAst(node); From 292de9f6492818f6dc4f0ca25de6466442973da5 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 14:41:37 -0400 Subject: [PATCH 11/40] fixed test file --- .../tests/break-on-boundaries-test.ts | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) 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 a8b1d4e8..4c8d0272 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 @@ -1,7 +1,6 @@ import { describe, it, expect } from "vitest"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; -import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; // Make console.log pretty-print by default @@ -12,4 +11,53 @@ console.log = (...args) => { describe("unified-latex-to-pretext:break-on-boundaries", () => { + let value: string; + + it("can break on sections", () => { + value = String.raw`\section{Foo} + Hi, this is a section + + \section{Bar} + This is another section`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw`\begin{_section} + \title{Foo} + Hi, this is a section + \end{_section} + \begin{_section} + \title{Bar} + This is another section + \end{_section}`]); + }); + it("can break on a subsection", () => { + value = String.raw`\subsection{subsection} + Hi, this is a subsection`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw``]); + }); + it("can break on subsection between subsections properly", () => { + value = String.raw`\section{First Section} + Hi, this is a section + \subsection{Inner Subsection} + This is a subsection + \section{Second Subsection} + This is another section`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + + expect(printRaw(ast)).toEqual([String.raw``]); + }); }); \ No newline at end of file From a450ac12434367e4eb58045d94b3a89f82d4a0ba Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 23:41:22 -0400 Subject: [PATCH 12/40] addressed some katex related comments --- .../libs/expand-user-defined-macros.ts | 18 ++++++++ ...s.ts => report-unsupported-macro-katex.ts} | 17 +------- ....ts => expand-user-defined-macros.test.ts} | 38 +++-------------- .../report-unsupported-macro-katex.test.ts | 42 +++++++++++++++++++ 4 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts rename packages/unified-latex-to-pretext/libs/{report-and-expand-macros.ts => report-unsupported-macro-katex.ts} (60%) rename packages/unified-latex-to-pretext/tests/{unified-latex-macro.test.ts => expand-user-defined-macros.test.ts} (54%) create mode 100644 packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts new file mode 100644 index 00000000..33209cd6 --- /dev/null +++ b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts @@ -0,0 +1,18 @@ +import * as Ast from "@unified-latex/unified-latex-types"; +import { anyMacro } from "@unified-latex/unified-latex-util-match"; +import { replaceNode } from "@unified-latex/unified-latex-util-replace"; +import { + newcommandMatcher, + newcommandMacroToSubstitutionAst, +} from "@unified-latex/unified-latex-util-macros"; + +/** + * Expands user-defined macros + */ +export function ExpandUserDefinedMacros(ast: Ast.Ast): void { + replaceNode(ast, (node) => { + if (anyMacro(node) && newcommandMatcher(node)) { + return newcommandMacroToSubstitutionAst(node); + } + }); +} diff --git a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts b/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts similarity index 60% rename from packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts rename to packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts index 958cf16d..9db351c7 100644 --- a/packages/unified-latex-to-pretext/libs/report-and-expand-macros.ts +++ b/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts @@ -1,15 +1,13 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { newcommandMatcher, newcommandMacroToSubstitutionAst } from "@unified-latex/unified-latex-util-macros"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; import { KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; -import { replaceNode } from "@unified-latex/unified-latex-util-replace"; /** * Return list of macros unsupported by Katex */ -export function report_macros(ast: Ast.Ast): string[] { - let unsupported: string[] = []; +export function reportMacrosUnsupportedByKatex(ast: Ast.Ast): string[] { + const unsupported: string[] = []; // match a macro supported by Katex const isSupported = match.createMacroMatcher(KATEX_SUPPORT.macros); @@ -29,14 +27,3 @@ export function report_macros(ast: Ast.Ast): string[] { return unsupported; } - -/** - * Expands user-defined macros - */ -export function expand_user_macros(ast: Ast.Ast): void { - replaceNode(ast, (node) => { - if (anyMacro(node) && newcommandMatcher(node)) { - return newcommandMacroToSubstitutionAst(node); - } - }); -} \ No newline at end of file diff --git a/packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts similarity index 54% rename from packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts rename to packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index e4d9461c..dbe4411e 100644 --- a/packages/unified-latex-to-pretext/tests/unified-latex-macro.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -1,9 +1,8 @@ import { describe, it, expect } from "vitest"; -import Prettier from "prettier"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; -import { report_macros, expand_user_macros } from "../libs/report-and-expand-macros"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; +import { ExpandUserDefinedMacros } from "@unified-latex/unified-latex-to-pretext/libs/expand-user-defined-macros"; // Make console.log pretty-print by default const origLog = console.log; @@ -14,40 +13,13 @@ console.log = (...args) => { describe("unified-latex-to-pretext:report-and-expand-macro", () => { let value: string; - it("can reported unsupported macros", () => { - value = String.raw`$\mathbb{R} \fakemacro{X}$`; - - const parser = getParser(); - const ast = parser.parse(value); - - expect(report_macros(ast)).toEqual(["fakemacro"]); - }); - - it("can report no unsupported macros in mathmode", () => { - value = String.raw`$\mathbb{R} \frac{1}{2} \cup$`; - - const parser = getParser(); - const ast = parser.parse(value); - - expect(report_macros(ast)).toEqual([]); - }); - - it("can report no unsupported macros not in mathmode", () => { - value = String.raw`\underline{text} \textbf{bold} \subsection{section}`; - - const parser = getParser(); - const ast = parser.parse(value); - - expect(report_macros(ast)).toEqual([]); - }); - it("can expand newcommand", () => { value = String.raw`\newcommand{\foo}{\bar{#1}}`; const parser = getParser(); const ast = parser.parse(value); - expand_user_macros(ast) + ExpandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual("\\bar{#1}"); }); @@ -58,7 +30,7 @@ describe("unified-latex-to-pretext:report-and-expand-macro", () => { const parser = getParser(); const ast = parser.parse(value); - expand_user_macros(ast) + ExpandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual("\\N"); }); @@ -69,8 +41,8 @@ describe("unified-latex-to-pretext:report-and-expand-macro", () => { const parser = getParser(); const ast = parser.parse(value); - expand_user_macros(ast) + ExpandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual("\\vee \\foo"); }); -}); \ No newline at end of file +}); diff --git a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts new file mode 100644 index 00000000..d49a4f4e --- /dev/null +++ b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts @@ -0,0 +1,42 @@ +import { describe, it, expect } from "vitest"; +import util from "util"; +import { getParser } from "@unified-latex/unified-latex-util-parse"; +import { reportMacrosUnsupportedByKatex } from "@unified-latex/unified-latex-to-pretext/libs/report-unsupported-macro-katex"; + +// Make console.log pretty-print by default +const origLog = console.log; +console.log = (...args) => { + origLog(...args.map((x) => util.inspect(x, false, 10, true))); +}; + +describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { + let value: string; + + it("can reported unsupported macros", () => { + value = String.raw`$\mathbb{R} \fakemacro{X}$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["fakemacro"]); + }); + + it("can report no unsupported macros in mathmode", () => { + value = String.raw`$\mathbb{R} \frac{1}{2} \cup$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); + }); + + // change to Unsupported macros outside of math mode. + it("can report no unsupported macros not in mathmode", () => { + value = String.raw`\underline{text} \textbf{bold} \subsection{section}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); + }); +}); From bd2f4d5346b60fced6f0bd5e960f96c01a59cbe6 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Fri, 31 May 2024 00:44:16 -0400 Subject: [PATCH 13/40] added more tests --- .../libs/expand-user-defined-macros.ts | 21 ++++++++++-------- .../tests/expand-user-defined-macros.test.ts | 22 +++++++++++++------ .../report-unsupported-macro-katex.test.ts | 14 +++++++++--- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts index 33209cd6..f95ac028 100644 --- a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts +++ b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts @@ -1,18 +1,21 @@ import * as Ast from "@unified-latex/unified-latex-types"; -import { anyMacro } from "@unified-latex/unified-latex-util-match"; -import { replaceNode } from "@unified-latex/unified-latex-util-replace"; import { - newcommandMatcher, - newcommandMacroToSubstitutionAst, + expandMacrosExcludingDefinitions, + listNewcommands, } from "@unified-latex/unified-latex-util-macros"; +import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; /** * Expands user-defined macros */ export function ExpandUserDefinedMacros(ast: Ast.Ast): void { - replaceNode(ast, (node) => { - if (anyMacro(node) && newcommandMatcher(node)) { - return newcommandMacroToSubstitutionAst(node); - } - }); + const newcommands = listNewcommands(ast); + + const macroInfo = Object.fromEntries( + newcommands.map((m) => [m.name, { signature: m.signature }]) + ); + + // attach the arguments to each macro before prcoessing it + attachMacroArgs(ast, macroInfo); + expandMacrosExcludingDefinitions(ast, newcommands); } diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index dbe4411e..35ff3dae 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -10,39 +10,47 @@ console.log = (...args) => { origLog(...args.map((x) => util.inspect(x, false, 10, true))); }; -describe("unified-latex-to-pretext:report-and-expand-macro", () => { +describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { let value: string; it("can expand newcommand", () => { - value = String.raw`\newcommand{\foo}{\bar{#1}}`; + value = String.raw`\newcommand{\foo}{\bar} \foo`; const parser = getParser(); const ast = parser.parse(value); ExpandUserDefinedMacros(ast); - expect(printRaw(ast)).toEqual("\\bar{#1}"); + expect(printRaw(ast)).toEqual(String.raw`\newcommand{\foo}{\bar} \bar`); }); it("can expand renewcommand", () => { - value = String.raw`\renewcommand{\mathbb{N}}{\N}`; + value = String.raw`\renewcommand{\N}{\mathbb{N}} \mathbb{N}`; // not subbing at all const parser = getParser(); const ast = parser.parse(value); ExpandUserDefinedMacros(ast); - expect(printRaw(ast)).toEqual("\\N"); + expect(printRaw(ast)).toEqual( + String.raw`\renewcommand{\N}{\mathbb{N}} \N` + ); }); it("can expand multiple user-defined commands", () => { - value = String.raw`\newcommand{\join}{\vee} \renewcommand{\vee}{\foo}`; + value = String.raw`\newcommand{\join}{\vee} + \join + \renewcommand{\vee}{\foo} + \join`; const parser = getParser(); const ast = parser.parse(value); ExpandUserDefinedMacros(ast); - expect(printRaw(ast)).toEqual("\\vee \\foo"); + expect(printRaw(ast)).toEqual(String.raw`\newcommand{\join}{\vee} + \vee + \renewcommand{\vee}{\foo} + \foo`); }); }); diff --git a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts index d49a4f4e..d4bcc545 100644 --- a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts +++ b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts @@ -30,13 +30,21 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); }); - // change to Unsupported macros outside of math mode. - it("can report no unsupported macros not in mathmode", () => { - value = String.raw`\underline{text} \textbf{bold} \subsection{section}`; + it("can not report any unsupported macros outside of math mode", () => { + value = String.raw`\fakemacro`; const parser = getParser(); const ast = parser.parse(value); expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); }); + + it("can report unsupported macros in text mode with a math anscestor", () => { + value = String.raw`$\frac{1}{\text{ hi \unsupported}}$`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["unsupported"]); + }); }); From 3f08772fb4c64cdbfbd3738620da2c6a04acd558 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Sun, 2 Jun 2024 16:55:25 -0400 Subject: [PATCH 14/40] started implementing break-on-boundaries --- .../libs/break-on-boundaries.ts | 39 ++++++++++++++++++- ...es-test.ts => break-on-boundaries.test.ts} | 9 +++-- 2 files changed, 44 insertions(+), 4 deletions(-) rename packages/unified-latex-to-pretext/tests/{break-on-boundaries-test.ts => break-on-boundaries.test.ts} (87%) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts index f05abc6d..559f9875 100644 --- a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -1 +1,38 @@ -import * as Ast from "@unified-latex/unified-latex-types"; \ No newline at end of file +import * as Ast from "@unified-latex/unified-latex-types"; +import { match } from "@unified-latex/unified-latex-util-match"; +import { splitOnMacro } from "@unified-latex/unified-latex-util-split"; +import { EXIT, visit } from "@unified-latex/unified-latex-util-visit"; + +export function breakOnBoundaries(ast: Ast.Ast): void { + const divisions = [ + "part", + "chapter", + "section", + "subsection", + "subsubsection", + "paragraph", + "subparagraph", + ]; + + // const splits = splitOnMacro(ast, divisions); + // console.log(ast); + + // get list of nodes + const content = (ast as Ast.Root).content; + + // split by parts first (probably turn this to a function, for each division since want to keep segments seperated?) + const splits = splitOnMacro(content, "section"); // probably use splitOnCond when doing for groups + console.log(splits) + + // check if there are any parts + if (splits.macros.length != 0) { + + } + + // maybe do groups seperately after, since they stay contained in one segment + +} + +function breakOnChapters(): void { + +} 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 similarity index 87% rename from packages/unified-latex-to-pretext/tests/break-on-boundaries-test.ts rename to packages/unified-latex-to-pretext/tests/break-on-boundaries.test.ts index 4c8d0272..6d764db0 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 @@ -1,7 +1,8 @@ import { describe, it, expect } from "vitest"; import util from "util"; -import { getParser } from "@unified-latex/unified-latex-util-parse"; +import { getParser, parseMath } from "@unified-latex/unified-latex-util-parse"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; +import { breakOnBoundaries } from "../libs/break-on-boundaries"; // Make console.log pretty-print by default const origLog = console.log; @@ -14,8 +15,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { let value: string; it("can break on sections", () => { - value = String.raw`\section{Foo} - Hi, this is a section + value = String.raw`{\section{Foo} + Hi, this is a section} \section{Bar} This is another section`; @@ -24,6 +25,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const ast = parser.parse(value); // break-on-boundaries work done here + breakOnBoundaries(ast); + // console.log(ast); expect(printRaw(ast)).toEqual([String.raw`\begin{_section} \title{Foo} From 3e1a7e3d73a6db6973e308d46ba458e39f09d35d Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:09:49 -0400 Subject: [PATCH 15/40] successfully splits on parts --- .../libs/break-on-boundaries.ts | 33 +++++++++++++++++-- .../tests/break-on-boundaries.test.ts | 26 +++++---------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts index 559f9875..0a9a97ee 100644 --- a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -1,6 +1,9 @@ +import { env, m } from "@unified-latex/unified-latex-builder"; import * as Ast from "@unified-latex/unified-latex-types"; +import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments"; import { match } from "@unified-latex/unified-latex-util-match"; -import { splitOnMacro } from "@unified-latex/unified-latex-util-split"; +import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; +import { splitOnMacro, unsplitOnMacro } from "@unified-latex/unified-latex-util-split"; import { EXIT, visit } from "@unified-latex/unified-latex-util-visit"; export function breakOnBoundaries(ast: Ast.Ast): void { @@ -21,14 +24,38 @@ export function breakOnBoundaries(ast: Ast.Ast): void { const content = (ast as Ast.Root).content; // split by parts first (probably turn this to a function, for each division since want to keep segments seperated?) - const splits = splitOnMacro(content, "section"); // probably use splitOnCond when doing for groups - console.log(splits) + const splits = splitOnMacro(content, "part"); // probably use splitOnCond when doing for groups + // console.log(splits.macros[0]._renderInfo?.namedArguments) // check if there are any parts if (splits.macros.length != 0) { + // could replace first part with \begin{_part} + // but need to at \end + // use env to create env node and pass in segment as body + // loop through segments (skipping first segment) + for (let i = 1; i < splits.segments.length; i++) { + // create the title + const title = getNamedArgsContent(splits.macros[i-1] || [])["title"]; + + // add title to the front of the segment + if (title) { + splits.segments[i].unshift(m("title", (title[0] as Ast.Macro).content)); + } + + // wrap segment around an environment + console.log(env("_part", splits.segments[i])); + splits.segments[i] = [env("_part", splits.segments[i])]; + + // remove the part macro + splits.macros[i-1] = m("") // didn't replace, appended + } + + (ast as Ast.Root).content = unsplitOnMacro(splits); } + // remove all empty nodes with replaceNode return null + // maybe do groups seperately after, since they stay contained in one segment } 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 6d764db0..ad407e4c 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 @@ -11,15 +11,10 @@ console.log = (...args) => { }; describe("unified-latex-to-pretext:break-on-boundaries", () => { - let value: string; - it("can break on sections", () => { - value = String.raw`{\section{Foo} - Hi, this is a section} - - \section{Bar} - This is another section`; + it("can break on parts", () => { + value = String.raw`\part{Foo}Hi, this is a part\part{Bar}This is another part`; const parser = getParser(); const ast = parser.parse(value); @@ -28,14 +23,9 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); // console.log(ast); - expect(printRaw(ast)).toEqual([String.raw`\begin{_section} - \title{Foo} - Hi, this is a section - \end{_section} - \begin{_section} - \title{Bar} - This is another section - \end{_section}`]); + expect(printRaw(ast)).toEqual( + String.raw`\begin{_part}\title{Foo}Hi, this is a part\end{_part}\begin{_part}\title{Bar}This is another part\end{_part}` + ); }); it("can break on a subsection", () => { value = String.raw`\subsection{subsection} @@ -46,7 +36,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { // break-on-boundaries work done here - expect(printRaw(ast)).toEqual([String.raw``]); + expect(printRaw(ast)).toEqual(String.raw``); }); it("can break on subsection between subsections properly", () => { value = String.raw`\section{First Section} @@ -61,6 +51,6 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { // break-on-boundaries work done here - expect(printRaw(ast)).toEqual([String.raw``]); + expect(printRaw(ast)).toEqual(String.raw``); }); -}); \ No newline at end of file +}); From 76352c5eb258193daff906fa82e480815ef7c4a9 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:25:18 -0400 Subject: [PATCH 16/40] removed uneeded variable --- .../libs/expand-user-defined-macros.ts | 2 +- .../libs/report-unsupported-macro-katex.ts | 4 +--- .../tests/expand-user-defined-macros.test.ts | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts index f95ac028..307befc5 100644 --- a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts +++ b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts @@ -15,7 +15,7 @@ export function ExpandUserDefinedMacros(ast: Ast.Ast): void { newcommands.map((m) => [m.name, { signature: m.signature }]) ); - // attach the arguments to each macro before prcoessing it + // attach the arguments to each macro before processing it attachMacroArgs(ast, macroInfo); expandMacrosExcludingDefinitions(ast, newcommands); } diff --git a/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts b/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts index 9db351c7..9bfbf178 100644 --- a/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts +++ b/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts @@ -16,11 +16,9 @@ export function reportMacrosUnsupportedByKatex(ast: Ast.Ast): string[] { visit(ast, (node, info) => { // macro in math mode if (anyMacro(node) && info.context.hasMathModeAncestor) { - const macro_name = node.content; - // check if not supported by katex if (!isSupported(node)) { - unsupported.push(macro_name); + unsupported.push((node as Ast.Macro).content); } } }); diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index 35ff3dae..60e8899e 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -25,7 +25,7 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { }); it("can expand renewcommand", () => { - value = String.raw`\renewcommand{\N}{\mathbb{N}} \mathbb{N}`; // not subbing at all + value = String.raw`\renewcommand{\O}{\mathcal{O}} \mathcal{O}`; // not subbing at all const parser = getParser(); const ast = parser.parse(value); @@ -33,7 +33,7 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { ExpandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual( - String.raw`\renewcommand{\N}{\mathbb{N}} \N` + String.raw`\renewcommand{\O}{\mathcal{O}} \O` ); }); From d2bc84f84858e5e78a178456556b82d81aed2d83 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:07:25 -0400 Subject: [PATCH 17/40] implemented for half the divisions --- .../libs/break-on-boundaries.ts | 98 +++++++++++++------ .../tests/break-on-boundaries.test.ts | 27 ++--- 2 files changed, 80 insertions(+), 45 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts index 0a9a97ee..f4e04bd8 100644 --- a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -1,10 +1,12 @@ import { env, m } from "@unified-latex/unified-latex-builder"; import * as Ast from "@unified-latex/unified-latex-types"; import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments"; -import { match } from "@unified-latex/unified-latex-util-match"; -import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; -import { splitOnMacro, unsplitOnMacro } from "@unified-latex/unified-latex-util-split"; -import { EXIT, visit } from "@unified-latex/unified-latex-util-visit"; +import { anyMacro } from "@unified-latex/unified-latex-util-match"; +import { replaceNode } from "@unified-latex/unified-latex-util-replace"; +import { + splitOnMacro, + unsplitOnMacro, +} from "@unified-latex/unified-latex-util-split"; export function breakOnBoundaries(ast: Ast.Ast): void { const divisions = [ @@ -17,49 +19,81 @@ export function breakOnBoundaries(ast: Ast.Ast): void { "subparagraph", ]; - // const splits = splitOnMacro(ast, divisions); - // console.log(ast); - // get list of nodes const content = (ast as Ast.Root).content; - // split by parts first (probably turn this to a function, for each division since want to keep segments seperated?) - const splits = splitOnMacro(content, "part"); // probably use splitOnCond when doing for groups - // console.log(splits.macros[0]._renderInfo?.namedArguments) + // split by parts first + const splits = splitOnMacro(content, "part"); + + // keep each segment seperated + for (let i = 0; i < splits.segments.length; i++) { + splits.segments[i] = breakOnChapters(splits.segments[i]); + } - // check if there are any parts - if (splits.macros.length != 0) { - // could replace first part with \begin{_part} - // but need to at \end - // use env to create env node and pass in segment as body + // create the environments + createEnvironments(splits, "_part"); - // loop through segments (skipping first segment) - for (let i = 1; i < splits.segments.length; i++) { - // create the title - const title = getNamedArgsContent(splits.macros[i-1] || [])["title"]; + // rebuild the ast + (ast as Ast.Root).content = unsplitOnMacro(splits); - // add title to the front of the segment - if (title) { - splits.segments[i].unshift(m("title", (title[0] as Ast.Macro).content)); - } + // remove all empty nodes + replaceNode(ast, (node) => { + if (anyMacro(node) && divisions.includes(node.content)) { + return null; + } + }); +} - // wrap segment around an environment - console.log(env("_part", splits.segments[i])); - splits.segments[i] = [env("_part", splits.segments[i])]; +function createEnvironments( + splits: { segments: Ast.Node[][]; macros: Ast.Macro[] }, + name: string +): void { + // loop through segments (skipping first segment) + for (let i = 1; i < splits.segments.length; i++) { + // create the title + const title = getNamedArgsContent(splits.macros[i - 1] || [])["title"]; - // remove the part macro - splits.macros[i-1] = m("") // didn't replace, appended + // add title to the front of the segment + if (title) { + splits.segments[i].unshift( + m("title", (title[0] as Ast.Macro).content) + ); } - (ast as Ast.Root).content = unsplitOnMacro(splits); + // wrap segment around an environment + splits.segments[i] = [env(name, splits.segments[i])]; } +} + +function breakOnChapters(segment: Ast.Node[]): Ast.Node[] { + // split by chapters + const splits = splitOnMacro(segment, "chapter"); + console.log(splits); - // remove all empty nodes with replaceNode return null + // keep each segment seperated + for (let i = 0; i < splits.segments.length; i++) { + splits.segments[i] = breakOnSections(splits.segments[i]); + } - // maybe do groups seperately after, since they stay contained in one segment + // create the environments + createEnvironments(splits, "_chapter"); + // rebuild the ast + return unsplitOnMacro(splits); } -function breakOnChapters(): void { +function breakOnSections(segment: Ast.Node[]): Ast.Node[] { + // split by chapters + const splits = splitOnMacro(segment, "section"); + + // keep each segment seperated + // for (let i = 0; i < splits.segments.length; i++) { + // splits.segments[i] = breakOnSections(splits.segments[i]); + // } + + // create the environments + createEnvironments(splits, "_section"); + // rebuild 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 ad407e4c..368ad169 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 @@ -1,6 +1,6 @@ import { describe, it, expect } from "vitest"; import util from "util"; -import { getParser, parseMath } from "@unified-latex/unified-latex-util-parse"; +import { getParser } from "@unified-latex/unified-latex-util-parse"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; import { breakOnBoundaries } from "../libs/break-on-boundaries"; @@ -27,30 +27,31 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { String.raw`\begin{_part}\title{Foo}Hi, this is a part\end{_part}\begin{_part}\title{Bar}This is another part\end{_part}` ); }); - it("can break on a subsection", () => { - value = String.raw`\subsection{subsection} - Hi, this is a subsection`; + + it("can break on a section", () => { + value = String.raw`\section{name}Hi, this is a subsection`; const parser = getParser(); const ast = parser.parse(value); // break-on-boundaries work done here + breakOnBoundaries(ast); - expect(printRaw(ast)).toEqual(String.raw``); + expect(printRaw(ast)).toEqual( + String.raw`\begin{_section}\title{name}Hi, this is a subsection\end{_section}` + ); }); - it("can break on subsection between subsections properly", () => { - value = String.raw`\section{First Section} - Hi, this is a section - \subsection{Inner Subsection} - This is a subsection - \section{Second Subsection} - This is another section`; + it("can break on combination of divisions", () => { + value = String.raw`\part{part1}\section{Section1}Hi, this is a section\chapter{chap1}This is a chapter\section{Subsection2}`; const parser = getParser(); const ast = parser.parse(value); // break-on-boundaries work done here + breakOnBoundaries(ast); - expect(printRaw(ast)).toEqual(String.raw``); + expect(printRaw(ast)).toEqual( + String.raw`\begin{_part}\title{part1}\begin{_section}\title{Section1}Hi, this is a section\end{_section}\begin{_chapter}\title{chap1}This is a chapter\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` + ); }); }); From 6f6221ad2a48245c7e082a0b9ba0b246c88a13fc Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:25:10 -0400 Subject: [PATCH 18/40] revised tests --- .../tests/report-unsupported-macro-katex.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts index d4bcc545..cb1740eb 100644 --- a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts +++ b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts @@ -12,7 +12,7 @@ console.log = (...args) => { describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { let value: string; - it("can reported unsupported macros", () => { + it("can reported unsupported macros in mathmode", () => { value = String.raw`$\mathbb{R} \fakemacro{X}$`; const parser = getParser(); @@ -30,7 +30,7 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); }); - it("can not report any unsupported macros outside of math mode", () => { + it("can report no unsupported macros outside of math mode", () => { value = String.raw`\fakemacro`; const parser = getParser(); From b55e08c848514a979148641c318fb64f53b298ff Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:21:22 -0400 Subject: [PATCH 19/40] revised some test descriptions --- .../tests/expand-user-defined-macros.test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index 60e8899e..ac5af9a6 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -25,7 +25,7 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { }); it("can expand renewcommand", () => { - value = String.raw`\renewcommand{\O}{\mathcal{O}} \mathcal{O}`; // not subbing at all + value = String.raw`\renewcommand{\O}{\mathcal{O}} \O`; // not subbing at all const parser = getParser(); const ast = parser.parse(value); @@ -33,24 +33,22 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { ExpandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual( - String.raw`\renewcommand{\O}{\mathcal{O}} \O` + String.raw`\renewcommand{\O}{\mathcal{O}} \mathcal{O}` ); }); - it("can expand multiple user-defined commands", () => { + it("can recursively expand multiple user-defined commands", () => { value = String.raw`\newcommand{\join}{\vee} \join \renewcommand{\vee}{\foo} - \join`; + \vee`; const parser = getParser(); const ast = parser.parse(value); ExpandUserDefinedMacros(ast); - expect(printRaw(ast)).toEqual(String.raw`\newcommand{\join}{\vee} - \vee - \renewcommand{\vee}{\foo} - \foo`); + expect(printRaw(ast)).toEqual(String.raw`\newcommand{\join}{\vee} `+ + String.raw`\foo \renewcommand{\vee}{\foo} \foo`); }); }); From fa392d0fbedc681157dcc60cf40a908aa4e49214 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:38:55 -0400 Subject: [PATCH 20/40] made tests more readable --- .../tests/break-on-boundaries.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 368ad169..11f7f0a5 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 @@ -21,7 +21,6 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { // break-on-boundaries work done here breakOnBoundaries(ast); - // console.log(ast); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}\title{Foo}Hi, this is a part\end{_part}\begin{_part}\title{Bar}This is another part\end{_part}` @@ -51,7 +50,10 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_part}\title{part1}\begin{_section}\title{Section1}Hi, this is a section\end{_section}\begin{_chapter}\title{chap1}This is a chapter\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` + String.raw`\begin{_part}\title{part1}` + + String.raw`\begin{_section}\title{Section1}Hi, this is a section\end{_section}` + + String.raw`\begin{_chapter}\title{chap1}This is a chapter` + + String.raw`\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` ); }); }); From 49e25bcce01141693c314e1159f192fceb6100d2 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:21:50 -0400 Subject: [PATCH 21/40] fixed expand userdefined macros --- .../libs/expand-user-defined-macros.ts | 52 ++++++++++++++++++- .../tests/expand-user-defined-macros.test.ts | 17 +++--- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts index 307befc5..0e0c25a4 100644 --- a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts +++ b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts @@ -1,9 +1,13 @@ import * as Ast from "@unified-latex/unified-latex-types"; import { expandMacrosExcludingDefinitions, - listNewcommands, + listNewcommands, newcommandMacroToName } from "@unified-latex/unified-latex-util-macros"; import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; +import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; +import { EXIT, visit } from "@unified-latex/unified-latex-util-visit"; + +type NewCommandSpec = ReturnType[number]; /** * Expands user-defined macros @@ -11,11 +15,57 @@ import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; export function ExpandUserDefinedMacros(ast: Ast.Ast): void { const newcommands = listNewcommands(ast); + // get a list of all macros to be expanded + const macros_to_expand = getToBeExpandedMacros(newcommands); + const macroInfo = Object.fromEntries( newcommands.map((m) => [m.name, { signature: m.signature }]) ); + // recursively expand at most 100 times + for (let i = 0; i < 100; i++) { + // check if any macros still need expanding + if (needToExpand(ast, macros_to_expand)) { + ExpandUseDefinedMacrosOnce(ast, newcommands, macroInfo); + } + } +} + +function getToBeExpandedMacros(newcommands: NewCommandSpec[]): string[] { + const macros = []; + + // loop through each new command + for (const command of newcommands) { + macros.push(command.name) + } + + return macros +} + +function needToExpand(ast: Ast.Ast, macros: string[]): boolean { + let needExpand = false; + + visit(ast, (node) => { + if (anyMacro(node) && macros.includes(node.content)) { + needExpand = true; + EXIT; + } + }) + + return needExpand; +} + +function ExpandUseDefinedMacrosOnce( + ast: Ast.Ast, + newcommands: NewCommandSpec[], + macroInfo: { + [k: string]: { + signature: string; + }; + } +): void { // attach the arguments to each macro before processing it attachMacroArgs(ast, macroInfo); expandMacrosExcludingDefinitions(ast, newcommands); } + diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index ac5af9a6..11505ef0 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -38,17 +38,22 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { }); it("can recursively expand multiple user-defined commands", () => { - value = String.raw`\newcommand{\join}{\vee} - \join - \renewcommand{\vee}{\foo} - \vee`; + value = + String.raw`\newcommand{\join}{\vee}` + + String.raw`\join` + + String.raw`\renewcommand{\vee}{\foo}` + + String.raw`\vee`; const parser = getParser(); const ast = parser.parse(value); ExpandUserDefinedMacros(ast); - expect(printRaw(ast)).toEqual(String.raw`\newcommand{\join}{\vee} `+ - String.raw`\foo \renewcommand{\vee}{\foo} \foo`); + expect(printRaw(ast)).toEqual( + String.raw`\newcommand{\join}{\vee}` + + String.raw`\foo` + + String.raw`\renewcommand{\vee}{\foo}` + + String.raw`\foo` + ); }); }); From 577d1be5ba5a86eda961ae52a6bad833ec89a228 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Sat, 8 Jun 2024 00:24:14 -0400 Subject: [PATCH 22/40] addressed more PR comments --- .../libs/expand-user-defined-macros.ts | 48 +++++-------------- .../tests/expand-user-defined-macros.test.ts | 31 ++++++++---- .../report-unsupported-macro-katex.test.ts | 24 ++++++++-- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts index 0e0c25a4..2c241d01 100644 --- a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts +++ b/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts @@ -1,10 +1,10 @@ import * as Ast from "@unified-latex/unified-latex-types"; import { expandMacrosExcludingDefinitions, - listNewcommands, newcommandMacroToName + listNewcommands, } from "@unified-latex/unified-latex-util-macros"; import { attachMacroArgs } from "@unified-latex/unified-latex-util-arguments"; -import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; +import { anyMacro } from "@unified-latex/unified-latex-util-match"; import { EXIT, visit } from "@unified-latex/unified-latex-util-visit"; type NewCommandSpec = ReturnType[number]; @@ -12,11 +12,11 @@ type NewCommandSpec = ReturnType[number]; /** * Expands user-defined macros */ -export function ExpandUserDefinedMacros(ast: Ast.Ast): void { +export function expandUserDefinedMacros(ast: Ast.Ast): void { const newcommands = listNewcommands(ast); - // get a list of all macros to be expanded - const macros_to_expand = getToBeExpandedMacros(newcommands); + // get a set of all macros to be expanded + const macrosToExpand = new Set(newcommands.map((command) => command.name)); const macroInfo = Object.fromEntries( newcommands.map((m) => [m.name, { signature: m.signature }]) @@ -25,47 +25,25 @@ export function ExpandUserDefinedMacros(ast: Ast.Ast): void { // recursively expand at most 100 times for (let i = 0; i < 100; i++) { // check if any macros still need expanding - if (needToExpand(ast, macros_to_expand)) { - ExpandUseDefinedMacrosOnce(ast, newcommands, macroInfo); + if (!needToExpand(ast, macrosToExpand)) { + break; } - } -} -function getToBeExpandedMacros(newcommands: NewCommandSpec[]): string[] { - const macros = []; - - // loop through each new command - for (const command of newcommands) { - macros.push(command.name) + // attach the arguments to each macro before processing it + attachMacroArgs(ast, macroInfo); + expandMacrosExcludingDefinitions(ast, newcommands); } - - return macros } -function needToExpand(ast: Ast.Ast, macros: string[]): boolean { +function needToExpand(ast: Ast.Ast, macros: Set): boolean { let needExpand = false; visit(ast, (node) => { - if (anyMacro(node) && macros.includes(node.content)) { + if (anyMacro(node) && macros.has(node.content)) { needExpand = true; EXIT; } - }) + }); return needExpand; } - -function ExpandUseDefinedMacrosOnce( - ast: Ast.Ast, - newcommands: NewCommandSpec[], - macroInfo: { - [k: string]: { - signature: string; - }; - } -): void { - // attach the arguments to each macro before processing it - attachMacroArgs(ast, macroInfo); - expandMacrosExcludingDefinitions(ast, newcommands); -} - diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index 11505ef0..8f9501ff 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; -import { ExpandUserDefinedMacros } from "@unified-latex/unified-latex-to-pretext/libs/expand-user-defined-macros"; +import { expandUserDefinedMacros } from "@unified-latex/unified-latex-to-pretext/libs/expand-user-defined-macros"; // Make console.log pretty-print by default const origLog = console.log; @@ -19,18 +19,18 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { const parser = getParser(); const ast = parser.parse(value); - ExpandUserDefinedMacros(ast); + expandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual(String.raw`\newcommand{\foo}{\bar} \bar`); }); it("can expand renewcommand", () => { - value = String.raw`\renewcommand{\O}{\mathcal{O}} \O`; // not subbing at all + value = String.raw`\renewcommand{\O}{\mathcal{O}} \O`; const parser = getParser(); const ast = parser.parse(value); - ExpandUserDefinedMacros(ast); + expandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual( String.raw`\renewcommand{\O}{\mathcal{O}} \mathcal{O}` @@ -42,18 +42,33 @@ describe("unified-latex-to-pretext:expand-user-deifned-macros", () => { String.raw`\newcommand{\join}{\vee}` + String.raw`\join` + String.raw`\renewcommand{\vee}{\foo}` + - String.raw`\vee`; + String.raw`\vee` + + String.raw`\renewcommand{\foo}{\bar}` + + String.raw`\foo`; const parser = getParser(); const ast = parser.parse(value); - ExpandUserDefinedMacros(ast); + expandUserDefinedMacros(ast); expect(printRaw(ast)).toEqual( String.raw`\newcommand{\join}{\vee}` + - String.raw`\foo` + + String.raw`\bar` + String.raw`\renewcommand{\vee}{\foo}` + - String.raw`\foo` + String.raw`\bar` + + String.raw`\renewcommand{\foo}{\bar}` + + String.raw`\bar` ); }); + + it("can expand providecommand", () => { + value = String.raw`\providecommand{\bar}{\b} \bar`; + + const parser = getParser(); + const ast = parser.parse(value); + + expandUserDefinedMacros(ast); + + expect(printRaw(ast)).toEqual(String.raw`\providecommand{\bar}{\b} \b`); + }); }); diff --git a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts index cb1740eb..5659ed85 100644 --- a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts +++ b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts @@ -12,7 +12,7 @@ console.log = (...args) => { describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { let value: string; - it("can reported unsupported macros in mathmode", () => { + it("can report unsupported macros in inline mathmode", () => { value = String.raw`$\mathbb{R} \fakemacro{X}$`; const parser = getParser(); @@ -30,7 +30,7 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); }); - it("can report no unsupported macros outside of math mode", () => { + it("doesn't report unsupported macros outside of math mode", () => { value = String.raw`\fakemacro`; const parser = getParser(); @@ -39,7 +39,7 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { expect(reportMacrosUnsupportedByKatex(ast)).toEqual([]); }); - it("can report unsupported macros in text mode with a math anscestor", () => { + it("reports unsupported macros in text mode with a math anscestor", () => { value = String.raw`$\frac{1}{\text{ hi \unsupported}}$`; const parser = getParser(); @@ -47,4 +47,22 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["unsupported"]); }); + + it("can report unsupported macros in display mathmode", () => { + value = String.raw`\[ \frac{a}{b} \fake \text{bar \baz}\] \bang`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["fake", "baz"]); + }); + + it("can report unsupported macros in equation environment", () => { + value = String.raw`\unsupported \begin{equation} \mathbb{N} \unsupported \text{\baz}\end{equation}`; + + const parser = getParser(); + const ast = parser.parse(value); + + expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["unsupported", "baz"]); + }); }); From 7648bc73fb8484a8265496e9d89b453a32774cfe Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:36:58 -0400 Subject: [PATCH 23/40] moved files to pre-conversion-subs --- .../expand-user-defined-macros.ts | 0 .../report-unsupported-macro-katex.ts | 2 +- .../tests/expand-user-defined-macros.test.ts | 2 +- .../tests/report-unsupported-macro-katex.test.ts | 7 +++++-- 4 files changed, 7 insertions(+), 4 deletions(-) rename packages/unified-latex-to-pretext/libs/{ => pre-conversion-subs}/expand-user-defined-macros.ts (100%) rename packages/unified-latex-to-pretext/libs/{ => pre-conversion-subs}/report-unsupported-macro-katex.ts (92%) diff --git a/packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/expand-user-defined-macros.ts similarity index 100% rename from packages/unified-latex-to-pretext/libs/expand-user-defined-macros.ts rename to packages/unified-latex-to-pretext/libs/pre-conversion-subs/expand-user-defined-macros.ts diff --git a/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts similarity index 92% rename from packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts rename to packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts index 9bfbf178..a98b4756 100644 --- a/packages/unified-latex-to-pretext/libs/report-unsupported-macro-katex.ts +++ b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts @@ -1,7 +1,7 @@ import * as Ast from "@unified-latex/unified-latex-types"; import { anyMacro, match } from "@unified-latex/unified-latex-util-match"; import { visit } from "@unified-latex/unified-latex-util-visit"; -import { KATEX_SUPPORT } from "./pre-conversion-subs/katex-subs"; +import { KATEX_SUPPORT } from "./katex-subs"; /** * Return list of macros unsupported by Katex diff --git a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts index 8f9501ff..6a31b1b7 100644 --- a/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts +++ b/packages/unified-latex-to-pretext/tests/expand-user-defined-macros.test.ts @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; -import { expandUserDefinedMacros } from "@unified-latex/unified-latex-to-pretext/libs/expand-user-defined-macros"; +import { expandUserDefinedMacros } from "@unified-latex/unified-latex-to-pretext/libs/pre-conversion-subs/expand-user-defined-macros"; // Make console.log pretty-print by default const origLog = console.log; diff --git a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts index 5659ed85..be40a3d8 100644 --- a/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts +++ b/packages/unified-latex-to-pretext/tests/report-unsupported-macro-katex.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect } from "vitest"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; -import { reportMacrosUnsupportedByKatex } from "@unified-latex/unified-latex-to-pretext/libs/report-unsupported-macro-katex"; +import { reportMacrosUnsupportedByKatex } from "@unified-latex/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex"; // Make console.log pretty-print by default const origLog = console.log; @@ -63,6 +63,9 @@ describe("unified-latex-to-pretext:report-unsupported-macro-katex", () => { const parser = getParser(); const ast = parser.parse(value); - expect(reportMacrosUnsupportedByKatex(ast)).toEqual(["unsupported", "baz"]); + expect(reportMacrosUnsupportedByKatex(ast)).toEqual([ + "unsupported", + "baz", + ]); }); }); From bdd9581d5118a9160995e787b7997f4078b66e74 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Tue, 11 Jun 2024 00:09:45 -0400 Subject: [PATCH 24/40] added support for groups --- .../libs/break-on-boundaries.ts | 108 ++++++++++-------- .../tests/break-on-boundaries.test.ts | 27 ++++- 2 files changed, 83 insertions(+), 52 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts index f4e04bd8..8faf51a5 100644 --- a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -1,12 +1,19 @@ import { env, m } from "@unified-latex/unified-latex-builder"; import * as Ast from "@unified-latex/unified-latex-types"; import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments"; -import { anyMacro } from "@unified-latex/unified-latex-util-match"; -import { replaceNode } from "@unified-latex/unified-latex-util-replace"; +import { + anyEnvironment, + anyMacro, + match +} from "@unified-latex/unified-latex-util-match"; +import { + replaceNode +} from "@unified-latex/unified-latex-util-replace"; import { splitOnMacro, unsplitOnMacro, } from "@unified-latex/unified-latex-util-split"; +import { visit } from "@unified-latex/unified-latex-util-visit"; export function breakOnBoundaries(ast: Ast.Ast): void { const divisions = [ @@ -19,24 +26,35 @@ export function breakOnBoundaries(ast: Ast.Ast): void { "subparagraph", ]; - // get list of nodes - const content = (ast as Ast.Root).content; - - // split by parts first - const splits = splitOnMacro(content, "part"); + const new_boundaries = [ + "_part", + "_chapter", + "_section", + "_subsection", + "_subsubsection", + "_paragraph", + "_subparagraph", + ]; - // keep each segment seperated - for (let i = 0; i < splits.segments.length; i++) { - splits.segments[i] = breakOnChapters(splits.segments[i]); - } + visit(ast, (node, info) => { + // needs to be an environment, root, or group node + if ( + !(anyEnvironment(node) || node.type === "root" || match.group(node)) || + info.context.hasMathModeAncestor + ) { + return; + } - // create the environments - createEnvironments(splits, "_part"); + // if it's an environment, make sure it isn't a newly created one + else if (anyEnvironment(node) && new_boundaries.includes(node.env)) { + return; + } - // rebuild the ast - (ast as Ast.Root).content = unsplitOnMacro(splits); + // now break up the divisions, starting at part + node.content = breakUp(node.content, divisions, 0); + }); - // remove all empty nodes + // remove all old division nodes replaceNode(ast, (node) => { if (anyMacro(node) && divisions.includes(node.content)) { return null; @@ -44,6 +62,31 @@ export function breakOnBoundaries(ast: Ast.Ast): void { }); } +function breakUp( + content: Ast.Node[], + divisions: string[], + index: number +): Ast.Node[] { + // went through all the divisions + if (index > 6) { + return content; + } + + const splits = splitOnMacro(content, divisions[index]); + + // go through each segment to recursively break + for (let i = 0; i < splits.segments.length; i++) { + splits.segments[i] = breakUp(splits.segments[i], divisions, index + 1); + } + + createEnvironments(splits, "_" + divisions[index]); + + index++; // go to next division + + // rebuild this part of the ast + return unsplitOnMacro(splits); +} + function createEnvironments( splits: { segments: Ast.Node[][]; macros: Ast.Macro[] }, name: string @@ -64,36 +107,3 @@ function createEnvironments( splits.segments[i] = [env(name, splits.segments[i])]; } } - -function breakOnChapters(segment: Ast.Node[]): Ast.Node[] { - // split by chapters - const splits = splitOnMacro(segment, "chapter"); - console.log(splits); - - // keep each segment seperated - for (let i = 0; i < splits.segments.length; i++) { - splits.segments[i] = breakOnSections(splits.segments[i]); - } - - // create the environments - createEnvironments(splits, "_chapter"); - - // rebuild the ast - return unsplitOnMacro(splits); -} - -function breakOnSections(segment: Ast.Node[]): Ast.Node[] { - // split by chapters - const splits = splitOnMacro(segment, "section"); - - // keep each segment seperated - // for (let i = 0; i < splits.segments.length; i++) { - // splits.segments[i] = breakOnSections(splits.segments[i]); - // } - - // create the environments - createEnvironments(splits, "_section"); - - // rebuild 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 11f7f0a5..4cb9eab1 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 @@ -27,8 +27,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { ); }); - it("can break on a section", () => { - value = String.raw`\section{name}Hi, this is a subsection`; + it("can break on divisions wrapped around document environment", () => { + value = String.raw`\begin{document}\section{name}Hi, this is a subsection\subsubsection{title}description.\end{document}`; const parser = getParser(); const ast = parser.parse(value); @@ -37,9 +37,12 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_section}\title{name}Hi, this is a subsection\end{_section}` + String.raw`\begin{document}\begin{_section}\title{name}Hi, this is a subsection` + + String.raw`\begin{_subsubsection}\title{title}description.\end{_subsubsection}` + + String.raw`\end{_section}\end{document}` ); }); + it("can break on combination of divisions", () => { value = String.raw`\part{part1}\section{Section1}Hi, this is a section\chapter{chap1}This is a chapter\section{Subsection2}`; @@ -56,4 +59,22 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { String.raw`\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` ); }); + + it("can break on divisions in a group", () => { + value = String.raw`\chapter{Chap}` + + String.raw`{\paragraph{Intro}Introduction.\subparagraph{Conclusion}Conclusion.}` + + String.raw`Chapter finished.`; + + const parser = getParser(); + const ast = parser.parse(value); + + // break-on-boundaries work done here + breakOnBoundaries(ast); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_chapter}\title{Chap}{\begin{_paragraph}\title{Intro}Introduction.` + + String.raw`\begin{_subparagraph}\title{Conclusion}Conclusion.` + + String.raw`\end{_subparagraph}\end{_paragraph}}Chapter finished.\end{_chapter}` + ); + }); }); From 422fd59ee7a0effd1fce59ab818e456390884f2a Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Tue, 11 Jun 2024 23:37:59 -0400 Subject: [PATCH 25/40] added more test cases --- .../libs/break-on-boundaries.ts | 25 +++++---- .../tests/break-on-boundaries.test.ts | 54 ++++++++++++------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts index 8faf51a5..b555eaaf 100644 --- a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts +++ b/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts @@ -4,17 +4,18 @@ import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments import { anyEnvironment, anyMacro, - match + match, } from "@unified-latex/unified-latex-util-match"; -import { - replaceNode -} from "@unified-latex/unified-latex-util-replace"; +import { replaceNode } from "@unified-latex/unified-latex-util-replace"; import { splitOnMacro, unsplitOnMacro, } from "@unified-latex/unified-latex-util-split"; import { visit } from "@unified-latex/unified-latex-util-visit"; +/** + * Breaks up division macros into environments + */ export function breakOnBoundaries(ast: Ast.Ast): void { const divisions = [ "part", @@ -39,7 +40,11 @@ export function breakOnBoundaries(ast: Ast.Ast): void { visit(ast, (node, info) => { // needs to be an environment, root, or group node if ( - !(anyEnvironment(node) || node.type === "root" || match.group(node)) || + !( + anyEnvironment(node) || + node.type === "root" || + match.group(node) + ) || info.context.hasMathModeAncestor ) { return; @@ -67,7 +72,7 @@ function breakUp( divisions: string[], index: number ): Ast.Node[] { - // went through all the divisions + // broke up all divisions if (index > 6) { return content; } @@ -89,12 +94,12 @@ function breakUp( function createEnvironments( splits: { segments: Ast.Node[][]; macros: Ast.Macro[] }, - name: string + newEnviron: string ): void { // loop through segments (skipping first segment) for (let i = 1; i < splits.segments.length; i++) { // create the title - const title = getNamedArgsContent(splits.macros[i - 1] || [])["title"]; + const title = getNamedArgsContent(splits.macros[i - 1])["title"]; // add title to the front of the segment if (title) { @@ -103,7 +108,7 @@ function createEnvironments( ); } - // wrap segment around an environment - splits.segments[i] = [env(name, splits.segments[i])]; + // wrap segment around a new environment + splits.segments[i] = [env(newEnviron, splits.segments[i])]; } } 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 4cb9eab1..ff4b8b95 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 @@ -19,7 +19,6 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - // break-on-boundaries work done here breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( @@ -27,54 +26,69 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { ); }); - it("can break on divisions wrapped around document environment", () => { + it("can break on a combination of divisions", () => { + value = String.raw`\part{part1}\section{Section1}Hi, this is a section\chapter{chap1}This is a chapter\section{Subsection2}`; + + const parser = getParser(); + const ast = parser.parse(value); + + breakOnBoundaries(ast); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_part}\title{part1}` + + String.raw`\begin{_section}\title{Section1}Hi, this is a section\end{_section}` + + String.raw`\begin{_chapter}\title{chap1}This is a chapter` + + String.raw`\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` + ); + }); + + it("can break on divisions wrapped around by a document environment", () => { value = String.raw`\begin{document}\section{name}Hi, this is a subsection\subsubsection{title}description.\end{document}`; const parser = getParser(); const ast = parser.parse(value); - // break-on-boundaries work done here breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{document}\begin{_section}\title{name}Hi, this is a subsection` + - String.raw`\begin{_subsubsection}\title{title}description.\end{_subsubsection}` + - String.raw`\end{_section}\end{document}` + String.raw`\begin{document}\begin{_section}\title{name}Hi, this is a subsection` + + String.raw`\begin{_subsubsection}\title{title}description.\end{_subsubsection}` + + String.raw`\end{_section}\end{document}` ); }); - it("can break on combination of divisions", () => { - value = String.raw`\part{part1}\section{Section1}Hi, this is a section\chapter{chap1}This is a chapter\section{Subsection2}`; + it("can break on divisions wrapped around by different environments", () => { + value = + String.raw`\begin{center}\part{name}Hi, this is a part\begin{environ}` + + String.raw`\subparagraph{title}description.\end{environ}\end{center}`; const parser = getParser(); const ast = parser.parse(value); - // break-on-boundaries work done here breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_part}\title{part1}` + - String.raw`\begin{_section}\title{Section1}Hi, this is a section\end{_section}` + - String.raw`\begin{_chapter}\title{chap1}This is a chapter` + - String.raw`\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` + String.raw`\begin{center}\begin{_part}\title{name}Hi, this is a part` + + String.raw`\begin{environ}\begin{_subparagraph}\title{title}description.` + + String.raw`\end{_subparagraph}\end{environ}\end{_part}\end{center}` ); }); it("can break on divisions in a group", () => { - value = String.raw`\chapter{Chap}` + - String.raw`{\paragraph{Intro}Introduction.\subparagraph{Conclusion}Conclusion.}` + - String.raw`Chapter finished.`; + value = + String.raw`\begin{document}\chapter{Chap}` + + String.raw`{\paragraph{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}}` + + String.raw`Chapter finished.\end{document}`; const parser = getParser(); const ast = parser.parse(value); - // break-on-boundaries work done here breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_chapter}\title{Chap}{\begin{_paragraph}\title{Intro}Introduction.` + - String.raw`\begin{_subparagraph}\title{Conclusion}Conclusion.` + - String.raw`\end{_subparagraph}\end{_paragraph}}Chapter finished.\end{_chapter}` + String.raw`\begin{document}\begin{_chapter}\title{Chap}{\begin{_paragraph}\title{Intro}Introduction.` + + String.raw`\begin{center}\begin{_subparagraph}\title{Conclusion}Conclusion.\end{_subparagraph}` + + String.raw`\end{center}\end{_paragraph}}Chapter finished.\end{_chapter}\end{document}` ); }); }); From 34214da33243c96e8a12488057c2aad4eeb4af1e Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Wed, 12 Jun 2024 16:34:07 -0400 Subject: [PATCH 26/40] Add pretext to tested branches --- .github/workflows/on-pull-request.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/on-pull-request.yml b/.github/workflows/on-pull-request.yml index 6b263a4f..7a41557a 100644 --- a/.github/workflows/on-pull-request.yml +++ b/.github/workflows/on-pull-request.yml @@ -2,9 +2,9 @@ name: Node.js CI on: push: - branches: ["main"] + branches: ["main", "pretext"] pull_request: - branches: ["main"] + branches: ["main", "pretext"] jobs: build: From 9631ed4d04768ce799f5b20947a3fbc12b1f34d7 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:38:58 -0400 Subject: [PATCH 27/40] moved files --- .../libs/{ => pre-conversion-subs}/break-on-boundaries.ts | 0 .../unified-latex-to-pretext/tests/break-on-boundaries.test.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/unified-latex-to-pretext/libs/{ => pre-conversion-subs}/break-on-boundaries.ts (100%) diff --git a/packages/unified-latex-to-pretext/libs/break-on-boundaries.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts similarity index 100% rename from packages/unified-latex-to-pretext/libs/break-on-boundaries.ts rename to packages/unified-latex-to-pretext/libs/pre-conversion-subs/break-on-boundaries.ts 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 ff4b8b95..ba115913 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 @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; -import { breakOnBoundaries } from "../libs/break-on-boundaries"; +import { breakOnBoundaries } from "../libs/pre-conversion-subs/break-on-boundaries"; // Make console.log pretty-print by default const origLog = console.log; From 1f483a4c848d566f8e518a31855a39398b051665 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Sat, 15 Jun 2024 01:07:56 -0400 Subject: [PATCH 28/40] resolved some PR comments --- .../break-on-boundaries.ts | 37 ++++++++++++------- .../tests/break-on-boundaries.test.ts | 22 +++++------ 2 files changed, 34 insertions(+), 25 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 b555eaaf..0c9da978 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 @@ -1,4 +1,4 @@ -import { env, m } from "@unified-latex/unified-latex-builder"; +import { env, m, arg } from "@unified-latex/unified-latex-builder"; import * as Ast from "@unified-latex/unified-latex-types"; import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments"; import { @@ -17,6 +17,9 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; * Breaks up division macros into environments */ export function breakOnBoundaries(ast: Ast.Ast): void { + // messages for any groups removed + const messages: string[] = []; + const divisions = [ "part", "chapter", @@ -27,7 +30,7 @@ export function breakOnBoundaries(ast: Ast.Ast): void { "subparagraph", ]; - const new_boundaries = [ + const newBoundaries = [ "_part", "_chapter", "_section", @@ -51,7 +54,7 @@ export function breakOnBoundaries(ast: Ast.Ast): void { } // if it's an environment, make sure it isn't a newly created one - else if (anyEnvironment(node) && new_boundaries.includes(node.env)) { + else if (anyEnvironment(node) && newBoundaries.includes(node.env)) { return; } @@ -67,26 +70,29 @@ export function breakOnBoundaries(ast: Ast.Ast): void { }); } +/** + * Recursively breaks up the ast at the parts macro to the subparagraph macro + */ function breakUp( content: Ast.Node[], divisions: string[], - index: number + depth: number ): Ast.Node[] { // broke up all divisions - if (index > 6) { + if (depth > 6) { return content; } - const splits = splitOnMacro(content, divisions[index]); + const splits = splitOnMacro(content, divisions[depth]); // go through each segment to recursively break for (let i = 0; i < splits.segments.length; i++) { - splits.segments[i] = breakUp(splits.segments[i], divisions, index + 1); + splits.segments[i] = breakUp(splits.segments[i], divisions, depth + 1); } - createEnvironments(splits, "_" + divisions[index]); + createEnvironments(splits, "_" + divisions[depth]); - index++; // go to next division + depth++; // go to next division // rebuild this part of the ast return unsplitOnMacro(splits); @@ -98,17 +104,20 @@ function createEnvironments( ): void { // loop through segments (skipping first segment) for (let i = 1; i < splits.segments.length; i++) { - // create the title + // get the title const title = getNamedArgsContent(splits.macros[i - 1])["title"]; + const titleArg: Ast.Argument[] = []; - // add title to the front of the segment + // create title argument if (title) { - splits.segments[i].unshift( - m("title", (title[0] as Ast.Macro).content) + titleArg.push( + arg((title[0] as Ast.Macro).content, { braces: "[]" }) ); } + console.log(titleArg); + // wrap segment around a new environment - splits.segments[i] = [env(newEnviron, splits.segments[i])]; + splits.segments[i] = [env(newEnviron, splits.segments[i], titleArg)]; } } 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 ba115913..f9ec2163 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 @@ -22,7 +22,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_part}\title{Foo}Hi, this is a part\end{_part}\begin{_part}\title{Bar}This is another part\end{_part}` + String.raw`\begin{_part}[Foo]Hi, this is a part\end{_part}\begin{_part}[Bar]This is another part\end{_part}` ); }); @@ -35,10 +35,10 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{_part}\title{part1}` + - String.raw`\begin{_section}\title{Section1}Hi, this is a section\end{_section}` + - String.raw`\begin{_chapter}\title{chap1}This is a chapter` + - String.raw`\begin{_section}\title{Subsection2}\end{_section}\end{_chapter}\end{_part}` + String.raw`\begin{_part}[part1]` + + String.raw`\begin{_section}[Section1]Hi, this is a section\end{_section}` + + String.raw`\begin{_chapter}[chap1]This is a chapter` + + String.raw`\begin{_section}[Subsection2]\end{_section}\end{_chapter}\end{_part}` ); }); @@ -51,8 +51,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{document}\begin{_section}\title{name}Hi, this is a subsection` + - String.raw`\begin{_subsubsection}\title{title}description.\end{_subsubsection}` + + String.raw`\begin{document}\begin{_section}[name]Hi, this is a subsection` + + String.raw`\begin{_subsubsection}[title]description.\end{_subsubsection}` + String.raw`\end{_section}\end{document}` ); }); @@ -68,8 +68,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{center}\begin{_part}\title{name}Hi, this is a part` + - String.raw`\begin{environ}\begin{_subparagraph}\title{title}description.` + + String.raw`\begin{center}\begin{_part}[name]Hi, this is a part` + + String.raw`\begin{environ}\begin{_subparagraph}[title]description.` + String.raw`\end{_subparagraph}\end{environ}\end{_part}\end{center}` ); }); @@ -86,8 +86,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{document}\begin{_chapter}\title{Chap}{\begin{_paragraph}\title{Intro}Introduction.` + - String.raw`\begin{center}\begin{_subparagraph}\title{Conclusion}Conclusion.\end{_subparagraph}` + + String.raw`\begin{document}\begin{_chapter}[Chap]{\begin{_paragraph}[Intro]Introduction.` + + String.raw`\begin{center}\begin{_subparagraph}[Conclusion]Conclusion.\end{_subparagraph}` + String.raw`\end{center}\end{_paragraph}}Chapter finished.\end{_chapter}\end{document}` ); }); From 8d4d570b8898eb6d0046692d1d12df9b407925c9 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Mon, 17 Jun 2024 01:07:10 -0400 Subject: [PATCH 29/40] added more test cases --- .../break-on-boundaries.ts | 27 +++++---- .../tests/break-on-boundaries.test.ts | 58 +++++++++++++++++-- 2 files changed, 68 insertions(+), 17 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 0c9da978..e4add0c4 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 @@ -1,4 +1,4 @@ -import { env, m, arg } from "@unified-latex/unified-latex-builder"; +import { env, arg } from "@unified-latex/unified-latex-builder"; import * as Ast from "@unified-latex/unified-latex-types"; import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments"; import { @@ -15,8 +15,10 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; /** * Breaks up division macros into environments + * + * Returns a list of warning messages if a group was hoisted. CHANGE FORMAT? */ -export function breakOnBoundaries(ast: Ast.Ast): void { +export function breakOnBoundaries(ast: Ast.Ast): string[] { // messages for any groups removed const messages: string[] = []; @@ -48,11 +50,11 @@ export function breakOnBoundaries(ast: Ast.Ast): void { node.type === "root" || match.group(node) ) || + // skip math mode info.context.hasMathModeAncestor ) { return; } - // if it's an environment, make sure it isn't a newly created one else if (anyEnvironment(node) && newBoundaries.includes(node.env)) { return; @@ -62,12 +64,19 @@ export function breakOnBoundaries(ast: Ast.Ast): void { node.content = breakUp(node.content, divisions, 0); }); - // remove all old division nodes replaceNode(ast, (node) => { + // remove all old division nodes if (anyMacro(node) && divisions.includes(node.content)) { return null; } + // remove groups + else if (match.group(node)) { + messages.push("Hoisted out of a group."); // prob make more specific + return node.content; + } }); + + return messages; } /** @@ -92,8 +101,6 @@ function breakUp( createEnvironments(splits, "_" + divisions[depth]); - depth++; // go to next division - // rebuild this part of the ast return unsplitOnMacro(splits); } @@ -110,14 +117,10 @@ function createEnvironments( // create title argument if (title) { - titleArg.push( - arg((title[0] as Ast.Macro).content, { braces: "[]" }) - ); + titleArg.push(arg(title, { braces: "[]" })); } - console.log(titleArg); - - // wrap segment around a new environment + // wrap segment with a new environment splits.segments[i] = [env(newEnviron, splits.segments[i], titleArg)]; } } 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 f9ec2163..fe992262 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 @@ -43,7 +43,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { }); it("can break on divisions wrapped around by a document environment", () => { - value = String.raw`\begin{document}\section{name}Hi, this is a subsection\subsubsection{title}description.\end{document}`; + value = String.raw`\begin{document}\section{Baz}Hi, this is a subsection\subsubsection{Foo}description.\end{document}`; const parser = getParser(); const ast = parser.parse(value); @@ -51,8 +51,8 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{document}\begin{_section}[name]Hi, this is a subsection` + - String.raw`\begin{_subsubsection}[title]description.\end{_subsubsection}` + + String.raw`\begin{document}\begin{_section}[Baz]Hi, this is a subsection` + + String.raw`\begin{_subsubsection}[Foo]description.\end{_subsubsection}` + String.raw`\end{_section}\end{document}` ); }); @@ -86,9 +86,57 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { breakOnBoundaries(ast); expect(printRaw(ast)).toEqual( - String.raw`\begin{document}\begin{_chapter}[Chap]{\begin{_paragraph}[Intro]Introduction.` + + String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` + String.raw`\begin{center}\begin{_subparagraph}[Conclusion]Conclusion.\end{_subparagraph}` + - String.raw`\end{center}\end{_paragraph}}Chapter finished.\end{_chapter}\end{document}` + String.raw`\end{center}\end{_paragraph}Chapter finished.\end{_chapter}\end{document}` + ); + }); + + it("can break on divisions in nested groups", () => { + value = + String.raw`\part{part1}{\subsection{Intro}description.` + + String.raw`\subsubsection{body}more text.{\subparagraph{Conclusion}Conclusion.}}` + + String.raw``; + + const parser = getParser(); + const ast = parser.parse(value); + + breakOnBoundaries(ast); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]description.` + + String.raw`\begin{_subsubsection}[body]more text.\begin{_subparagraph}[Conclusion]Conclusion.` + + String.raw`\end{_subparagraph}\end{_subsubsection}\end{_subsection}\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\" \(\mathbb{R}\)}This is subsection`; + + const parser = getParser(); + const ast = parser.parse(value); + + breakOnBoundaries(ast); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_chapter}[$x = \frac{1}{2}$]Chapter 1` + + String.raw`\begin{_subsection}[\"name\_1\" \(\mathbb{R}\)]This is subsection` + + String.raw`\end{_subsection}\end{_chapter}` + ); + }); + + it("can break on divisions and trim whitespace around division beginnings and endings", () => { + value = String.raw` \subsubsection{first}subsection 1 \paragraph{body}This is paragraph `; + + const parser = getParser(); + const ast = parser.parse(value); + + breakOnBoundaries(ast); + + expect(printRaw(ast)).toEqual( + String.raw`\begin{_subsubsection}[first]subsection 1` + + String.raw`\begin{_paragraph}[body]This is paragraph` + + String.raw`\end{_paragraph}\end{_subsubsection}` ); }); }); From ae7d000c9d2621e832249e3c57dd0f72f28e17be Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Tue, 18 Jun 2024 00:50:54 -0400 Subject: [PATCH 30/40] restructured code --- .../break-on-boundaries.ts | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 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 e4add0c4..9b8ed66b 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 @@ -13,6 +13,17 @@ import { } from "@unified-latex/unified-latex-util-split"; import { visit } from "@unified-latex/unified-latex-util-visit"; +// all divisions [division macro, environment] +const divisions: [string, string][] = [ + ["part", "_part"], + ["chapter", "_chapter"], + ["section", "_section"], + ["subsection", "_subsection"], + ["subsubsection", "_subsubsection"], + ["paragraph", "_paragraph"], + ["subparagraph", "_subparagraph"], +]; + /** * Breaks up division macros into environments * @@ -22,26 +33,6 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { // messages for any groups removed const messages: string[] = []; - const divisions = [ - "part", - "chapter", - "section", - "subsection", - "subsubsection", - "paragraph", - "subparagraph", - ]; - - const newBoundaries = [ - "_part", - "_chapter", - "_section", - "_subsection", - "_subsubsection", - "_paragraph", - "_subparagraph", - ]; - visit(ast, (node, info) => { // needs to be an environment, root, or group node if ( @@ -56,7 +47,10 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { return; } // if it's an environment, make sure it isn't a newly created one - else if (anyEnvironment(node) && newBoundaries.includes(node.env)) { + else if ( + anyEnvironment(node) && + divisions.map((x) => x[1]).includes(node.env) + ) { return; } @@ -66,7 +60,10 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { replaceNode(ast, (node) => { // remove all old division nodes - if (anyMacro(node) && divisions.includes(node.content)) { + if ( + anyMacro(node) && + divisions.map((x) => x[0]).includes(node.content) + ) { return null; } // remove groups @@ -80,11 +77,11 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { } /** - * Recursively breaks up the ast at the parts macro to the subparagraph macro + * Recursively breaks up the ast at the division macros */ function breakUp( content: Ast.Node[], - divisions: string[], + divisions: [string, string][], depth: number ): Ast.Node[] { // broke up all divisions @@ -92,19 +89,22 @@ function breakUp( return content; } - const splits = splitOnMacro(content, divisions[depth]); + const splits = splitOnMacro(content, divisions[depth][0]); // 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); } - createEnvironments(splits, "_" + divisions[depth]); + createEnvironments(splits, divisions[depth][1]); // rebuild this part of the ast return unsplitOnMacro(splits); } +/** + * Create the new environments that replace the division macros + */ function createEnvironments( splits: { segments: Ast.Node[][]; macros: Ast.Macro[] }, newEnviron: string From c5ed819121cedb6587f02d9881bc8556118e7d85 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:28:18 -0400 Subject: [PATCH 31/40] added warning messages --- .../break-on-boundaries.ts | 18 +++++++++---- .../tests/break-on-boundaries.test.ts | 27 ++++++++++++------- 2 files changed, 30 insertions(+), 15 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 9b8ed66b..a1601e29 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 @@ -6,6 +6,7 @@ import { anyMacro, match, } from "@unified-latex/unified-latex-util-match"; +import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; import { replaceNode } from "@unified-latex/unified-latex-util-replace"; import { splitOnMacro, @@ -25,9 +26,8 @@ const divisions: [string, string][] = [ ]; /** - * Breaks up division macros into environments - * - * Returns a list of warning messages if a group was hoisted. CHANGE FORMAT? + * Breaks up division macros into environments. Returns a list of warning messages + * for any groups that were removed. */ export function breakOnBoundaries(ast: Ast.Ast): string[] { // messages for any groups removed @@ -54,6 +54,15 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { return; } + // if it's a group, push a warning message + if (match.group(node)) { + 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, divisions, 0); }); @@ -68,7 +77,6 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { } // remove groups else if (match.group(node)) { - messages.push("Hoisted out of a group."); // prob make more specific return node.content; } }); @@ -77,7 +85,7 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { } /** - * Recursively breaks up the ast at the division macros + * Recursively breaks up the ast at the division macros. */ function breakUp( content: Ast.Node[], 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 fe992262..8489a640 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 @@ -19,7 +19,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[Foo]Hi, this is a part\end{_part}\begin{_part}[Bar]This is another part\end{_part}` @@ -32,7 +32,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[part1]` + @@ -48,7 +48,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_section}[Baz]Hi, this is a subsection` + @@ -65,7 +65,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{center}\begin{_part}[name]Hi, this is a part` + @@ -83,7 +83,10 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([ + String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + + String.raw`{ group: {\paragraph{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}} }`, + ]); expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` + @@ -95,13 +98,17 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { it("can break on divisions in nested groups", () => { value = String.raw`\part{part1}{\subsection{Intro}description.` + - String.raw`\subsubsection{body}more text.{\subparagraph{Conclusion}Conclusion.}}` + - String.raw``; + String.raw`\subsubsection{body}more text.{\subparagraph{Conclusion}Conclusion.}}`; const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([ + 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.} }`, + ]); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]description.` + @@ -116,7 +123,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{_chapter}[$x = \frac{1}{2}$]Chapter 1` + @@ -131,7 +138,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - breakOnBoundaries(ast); + expect(breakOnBoundaries(ast)).toEqual([]); expect(printRaw(ast)).toEqual( String.raw`\begin{_subsubsection}[first]subsection 1` + From fc8511c4cb355bb90c5159f57a5551607d2606f0 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:34:48 -0400 Subject: [PATCH 32/40] addressed some comments --- .../break-on-boundaries.ts | 17 +++++--- .../tests/break-on-boundaries.test.ts | 43 +++++++++++-------- 2 files changed, 35 insertions(+), 25 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 a1601e29..c3d1c15b 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 @@ -14,7 +14,10 @@ import { } from "@unified-latex/unified-latex-util-split"; import { visit } from "@unified-latex/unified-latex-util-visit"; -// all divisions [division macro, environment] +/** + * 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"], @@ -29,9 +32,9 @@ const divisions: [string, string][] = [ * Breaks up division macros into environments. Returns a list of warning messages * for any groups that were removed. */ -export function breakOnBoundaries(ast: Ast.Ast): string[] { +export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { // messages for any groups removed - const messages: string[] = []; + const messagesLst: { messages: string[] } = { messages: [] }; visit(ast, (node, info) => { // needs to be an environment, root, or group node @@ -56,7 +59,7 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { // if it's a group, push a warning message if (match.group(node)) { - messages.push( + messagesLst.messages.push( `Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw( node )} }` @@ -81,11 +84,13 @@ export function breakOnBoundaries(ast: Ast.Ast): string[] { } }); - return messages; + console.log(messagesLst); + + return messagesLst; } /** - * Recursively breaks up the ast at the division macros. + * Recursively breaks up the AST at the division macros. */ function breakUp( content: Ast.Node[], 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 8489a640..52d20513 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 @@ -19,7 +19,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[Foo]Hi, this is a part\end{_part}\begin{_part}[Bar]This is another part\end{_part}` @@ -32,10 +32,11 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( - String.raw`\begin{_part}[part1]` + + "" + + String.raw`\begin{_part}[part1]` + String.raw`\begin{_section}[Section1]Hi, this is a section\end{_section}` + String.raw`\begin{_chapter}[chap1]This is a chapter` + String.raw`\begin{_section}[Subsection2]\end{_section}\end{_chapter}\end{_part}` @@ -48,7 +49,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_section}[Baz]Hi, this is a subsection` + @@ -65,7 +66,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( String.raw`\begin{center}\begin{_part}[name]Hi, this is a part` + @@ -83,10 +84,12 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([ - String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + - String.raw`{ group: {\paragraph{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}} }`, - ]); + expect(breakOnBoundaries(ast)).toEqual({ + messages: [ + String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + + String.raw`{ group: {\paragraph{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}} }`, + ], + }); expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` + @@ -103,12 +106,14 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([ - 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.} }`, - ]); + expect(breakOnBoundaries(ast)).toEqual({ + messages: [ + 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.} }`, + ], + }); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]description.` + @@ -118,16 +123,16 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { }); it("can break on divisions with latex in their titles", () => { - value = String.raw`\chapter{$x = \frac{1}{2}$}Chapter 1\subsection{\"name\_1\" \(\mathbb{R}\)}This is subsection`; + value = String.raw`\chapter{$x = \frac{1}{2}$}Chapter 1\subsection{\"name\_1\" \$}This is subsection`; const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( String.raw`\begin{_chapter}[$x = \frac{1}{2}$]Chapter 1` + - String.raw`\begin{_subsection}[\"name\_1\" \(\mathbb{R}\)]This is subsection` + + String.raw`\begin{_subsection}[\"name\_1\" \$]This is subsection` + String.raw`\end{_subsection}\end{_chapter}` ); }); @@ -138,7 +143,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual([]); + expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); expect(printRaw(ast)).toEqual( String.raw`\begin{_subsubsection}[first]subsection 1` + From 35627d88f4a429658c06c304f10f468c4ab7a67a Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:44:49 -0400 Subject: [PATCH 33/40] used a custom macro matcher --- .../libs/pre-conversion-subs/break-on-boundaries.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 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 c3d1c15b..c770e6b3 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 @@ -36,6 +36,9 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { // messages for any groups removed const messagesLst: { messages: string[] } = { messages: [] }; + // check if a macro is a division macro + const isDivision = match.createMacroMatcher(divisions.map((x) => x[0])); + visit(ast, (node, info) => { // needs to be an environment, root, or group node if ( @@ -72,10 +75,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { replaceNode(ast, (node) => { // remove all old division nodes - if ( - anyMacro(node) && - divisions.map((x) => x[0]).includes(node.content) - ) { + if (anyMacro(node) && isDivision(node)) { return null; } // remove groups 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 34/40] 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); 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 35/40] 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}` ); 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 36/40] 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`; From 1a586ecb41a078bb1350b50202cc568090da4779 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:18:54 -0400 Subject: [PATCH 37/40] made custom environment matcher --- .../break-on-boundaries.ts | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 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 5825fac9..6205b190 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 @@ -28,6 +28,17 @@ const divisions: { division: string; mappedEnviron: string }[] = [ { division: "subparagraph", mappedEnviron: "_subparagraph" }, ]; + +// check if a macro is a division macro +const isDivision = match.createMacroMatcher( + divisions.map((x) => x.division) +); + +// check if an environment +const isMappedEnviron = match.createEnvironmentMatcher( + divisions.map((x) => x.mappedEnviron) +); + /** * Breaks up division macros into environments. Returns a list of warning messages * for any groups that were removed. @@ -36,10 +47,27 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { // messages for any groups removed const messagesLst: { messages: string[] } = { messages: [] }; - // check if a macro is a division macro - const isDivision = match.createMacroMatcher( - divisions.map((x) => x.division) - ); + replaceNode(ast, (node) => { + // 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)) + ) { + 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 @@ -65,11 +93,8 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { // 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 @@ -81,20 +106,6 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { 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; From 51ba97ec48f3313de3b14e58336c221d8abdc7fe Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:39:16 -0400 Subject: [PATCH 38/40] removed groups properly --- .../break-on-boundaries.ts | 33 ++++++------------- .../tests/break-on-boundaries.test.ts | 18 +++++----- 2 files changed, 20 insertions(+), 31 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 6205b190..6b8f4c25 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 @@ -28,14 +28,14 @@ const divisions: { division: string; mappedEnviron: string }[] = [ { division: "subparagraph", mappedEnviron: "_subparagraph" }, ]; - // check if a macro is a division macro -const isDivision = match.createMacroMatcher( +const isDivisionMacro = match.createMacroMatcher( divisions.map((x) => x.division) ); -// check if an environment +// check if an environment is a newly created environment const isMappedEnviron = match.createEnvironmentMatcher( + // doesn't seem to work divisions.map((x) => x.mappedEnviron) ); @@ -48,15 +48,12 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { const messagesLst: { messages: string[] } = { messages: [] }; replaceNode(ast, (node) => { - // remove groups in messages if (match.group(node)) { - // remove if it contains a division or new environment as an immediate child + // remove if it contains a division as an immediate child if ( - isDivision(node.content[0]) || - (anyEnvironment(node.content[0]) && - divisions - .map((x) => x.mappedEnviron) - .includes(node.content[0].env)) + node.content.some((child) => { + return anyMacro(child) && isDivisionMacro(child); + }) ) { messagesLst.messages.push( `Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw( @@ -67,7 +64,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { return node.content; } } - }) + }); visit(ast, (node, info) => { // needs to be an environment, root, or group node @@ -83,27 +80,17 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { return; } // if it's an environment, make sure it isn't a newly created one - else if ( - anyEnvironment(node) && - divisions.map((x) => x.mappedEnviron).includes(node.env) - ) { + else if (anyEnvironment(node) && isMappedEnviron(node)) { 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 - - - } - // now break up the divisions, starting at part node.content = breakUp(node.content, 0); }); replaceNode(ast, (node) => { // remove all old division nodes - if (anyMacro(node) && isDivision(node)) { + if (anyMacro(node) && isDivisionMacro(node)) { return null; } }); 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 0f71e227..0a88394a 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 @@ -94,7 +94,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` + String.raw`\begin{center}\begin{_subparagraph}[Conclusion]Conclusion.\end{_subparagraph}` + - String.raw`\end{center}\end{_paragraph}Chapter finished.\end{_chapter}\end{document}` + String.raw`\end{center}Chapter finished.\end{_paragraph}\end{_chapter}\end{document}` ); }); @@ -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.} }`, // ** Doesn't keep nested group + String.raw`{ group: {\subparagraph{Conclusion}Conclusion.} }`, ], }); @@ -124,8 +124,8 @@ 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}`; + String.raw`\part{part1}{\subsection{Intro}` + + String.raw`\subsubsection{body}{$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.}}{no divisions 2}`; const parser = getParser(); const ast = parser.parse(value); @@ -133,14 +133,16 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { 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} }`, + String.raw`{ group: {\subsection{Intro}\subsubsection{body}{$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.}} }`, + String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + + String.raw`{ group: {$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.} }`, ], }); 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}` + String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]\begin{_subsubsection}[body]` + + String.raw`$\mathbb{N}$\begin{_subparagraph}[Conclusion]{no divisions 1}Conclusion.{no divisions 2}` + + String.raw`\end{_subparagraph}\end{_subsubsection}\end{_subsection}\end{_part}` ); }); From 5e388aef4102bc000f247a41c5478b8f83a60175 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Thu, 11 Jul 2024 22:08:26 -0400 Subject: [PATCH 39/40] imported VFileMessage --- .../break-on-boundaries.ts | 34 ++++++++++++++----- .../tests/break-on-boundaries.test.ts | 31 ++++------------- 2 files changed, 32 insertions(+), 33 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 6b8f4c25..5237e678 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 @@ -6,13 +6,13 @@ import { anyMacro, match, } from "@unified-latex/unified-latex-util-match"; -import { printRaw } from "@unified-latex/unified-latex-util-print-raw"; import { replaceNode } from "@unified-latex/unified-latex-util-replace"; import { splitOnMacro, unsplitOnMacro, } from "@unified-latex/unified-latex-util-split"; import { visit } from "@unified-latex/unified-latex-util-visit"; +import { VFileMessage } from "vfile-message"; /** * All the divisions, where each item is {division macro, mapped environment}. @@ -35,7 +35,6 @@ const isDivisionMacro = match.createMacroMatcher( // check if an environment is a newly created environment const isMappedEnviron = match.createEnvironmentMatcher( - // doesn't seem to work divisions.map((x) => x.mappedEnviron) ); @@ -43,9 +42,9 @@ const isMappedEnviron = match.createEnvironmentMatcher( * Breaks up division macros into environments. Returns a list of warning messages * for any groups that were removed. */ -export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { +export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } { // messages for any groups removed - const messagesLst: { messages: string[] } = { messages: [] }; + const messagesLst: { messages: VFileMessage[] } = { messages: [] }; replaceNode(ast, (node) => { if (match.group(node)) { @@ -55,11 +54,28 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { return anyMacro(child) && isDivisionMacro(child); }) ) { - messagesLst.messages.push( - `Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw( - node - )} }` + const message = new VFileMessage( + "Warning: hoisted out of a group, which might break the LaTeX code.", + { + line: node.position?.start.line, + column: node.position?.start.column, + position: { + start: { line: node.position?.start.line, column: node.position?.start.column }, + end: { line: node.position?.end.line, column: node.position?.end.column } + }, + source: 'LatexConversion' + } ); + // line: null, + // column: null, + // position: { + // start: { line: null, column: null }, + // end: { line: null, column: null } + // }, + // source: null, + // ruleId: null + + messagesLst.messages.push(message); return node.content; } @@ -95,6 +111,8 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } { } }); + console.log(messagesLst.messages); + return messagesLst; } 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 0a88394a..69a16ae9 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 @@ -66,7 +66,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); + expect(breakOnBoundaries(ast).messages.length).toEqual(0); expect(printRaw(ast)).toEqual( String.raw`\begin{center}\begin{_part}[name]Hi, this is a part` + @@ -84,12 +84,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { 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{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}} }`, - ], - }); + expect(breakOnBoundaries(ast).messages.length).toEqual(1); expect(printRaw(ast)).toEqual( String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` + @@ -106,14 +101,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { 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: {\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.} }`, - ], - }); + expect(breakOnBoundaries(ast).messages.length).toEqual(2); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]description.` + @@ -130,14 +118,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { 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: {\subsection{Intro}\subsubsection{body}{$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.}} }`, - String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` + - String.raw`{ group: {$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.} }`, - ], - }); + expect(breakOnBoundaries(ast).messages.length).toEqual(2); expect(printRaw(ast)).toEqual( String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]\begin{_subsubsection}[body]` + @@ -152,7 +133,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); + expect(breakOnBoundaries(ast).messages.length).toEqual(0); expect(printRaw(ast)).toEqual( String.raw`\begin{_chapter}[$x = \frac{1}{2}$]Chapter 1` + @@ -167,7 +148,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => { const parser = getParser(); const ast = parser.parse(value); - expect(breakOnBoundaries(ast)).toEqual({ messages: [] }); + expect(breakOnBoundaries(ast).messages.length).toEqual(0); expect(printRaw(ast)).toEqual( String.raw`\begin{_subsubsection}[first]subsection 1 ` + From 5b467b95a51878d30360486bb8f00c9b71a60b8f Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Fri, 12 Jul 2024 23:06:20 -0400 Subject: [PATCH 40/40] finished VFileMessage implementation --- .../break-on-boundaries.ts | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 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 5237e678..83cf2b38 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 @@ -39,7 +39,7 @@ const isMappedEnviron = match.createEnvironmentMatcher( ); /** - * Breaks up division macros into environments. Returns a list of warning messages + * Breaks up division macros into environments. Returns an object of warning messages * for any groups that were removed. */ export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } { @@ -55,26 +55,26 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } { }) ) { const message = new VFileMessage( - "Warning: hoisted out of a group, which might break the LaTeX code.", - { - line: node.position?.start.line, - column: node.position?.start.column, - position: { - start: { line: node.position?.start.line, column: node.position?.start.column }, - end: { line: node.position?.end.line, column: node.position?.end.column } - }, - source: 'LatexConversion' - } + "Warning: hoisted out of a group, which might break the LaTeX code." ); - // line: null, - // column: null, - // position: { - // start: { line: null, column: null }, - // end: { line: null, column: null } - // }, - // source: null, - // ruleId: null + // add the position of the group if available + if (node.position) { + message.line = node.position.start.line; + message.column = node.position.start.column; + message.position = { + start: { + line: node.position.start.line, + column: node.position.start.column, + }, + end: { + line: node.position.end.line, + column: node.position.end.column, + }, + }; + } + + message.source = "LatexConversion"; messagesLst.messages.push(message); return node.content; @@ -111,8 +111,6 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } { } }); - console.log(messagesLst.messages); - return messagesLst; }