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/15] 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/15] 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/15] 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/15] 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 27117651914aafacd8d66fb2ffa3c4fdd7ecc8ce Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 13:40:59 -0400 Subject: [PATCH 05/15] 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 a450ac12434367e4eb58045d94b3a89f82d4a0ba Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan Date: Wed, 29 May 2024 23:41:22 -0400 Subject: [PATCH 06/15] 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 07/15] 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 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 08/15] 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 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 09/15] 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 10/15] 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 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 11/15] 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 12/15] 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 13/15] 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 80fce28afd8119f468f21c51ed2ab2b20cd06a91 Mon Sep 17 00:00:00 2001 From: Nahlee Naria Khan <95993773+renee-k@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:32:38 -0400 Subject: [PATCH 14/15] edited docstrings --- .../libs/pre-conversion-subs/report-unsupported-macro-katex.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts index a98b4756..38651036 100644 --- a/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts +++ b/packages/unified-latex-to-pretext/libs/pre-conversion-subs/report-unsupported-macro-katex.ts @@ -4,7 +4,7 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; import { KATEX_SUPPORT } from "./katex-subs"; /** - * Return list of macros unsupported by Katex + * Return a list of macros used in ast that are unsupported by KaTeX */ export function reportMacrosUnsupportedByKatex(ast: Ast.Ast): string[] { const unsupported: string[] = []; From 34214da33243c96e8a12488057c2aad4eeb4af1e Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Wed, 12 Jun 2024 16:34:07 -0400 Subject: [PATCH 15/15] 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: