@unified-latex/unified-latex-util-parse
Advanced tools
Comparing version 1.0.12 to 1.1.0
200
index.js
@@ -54,22 +54,30 @@ // libs/compiler-ast.ts | ||
return (tree) => { | ||
visit(tree, (node) => { | ||
if (match.anyMacro(node)) { | ||
for (const arg of node.args || []) { | ||
if (arg.content.length > 0 && !wasParsedInMathMode(arg.content)) { | ||
arg.content = parseMathMinimal(printRaw(arg.content)); | ||
visit( | ||
tree, | ||
(node) => { | ||
if (match.anyMacro(node)) { | ||
for (const arg of node.args || []) { | ||
if (arg.content.length > 0 && !wasParsedInMathMode(arg.content)) { | ||
arg.content = parseMathMinimal( | ||
printRaw(arg.content) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
if (match.anyEnvironment(node)) { | ||
if (!wasParsedInMathMode(node.content)) { | ||
node.content = parseMathMinimal(printRaw(node.content)); | ||
if (match.anyEnvironment(node)) { | ||
if (!wasParsedInMathMode(node.content)) { | ||
node.content = parseMathMinimal(printRaw(node.content)); | ||
} | ||
} | ||
}, | ||
{ | ||
test: (node) => isMathEnvironment(node) || isMathMacro(node) | ||
} | ||
}, { | ||
test: (node) => isMathEnvironment(node) || isMathMacro(node) | ||
}); | ||
); | ||
}; | ||
} | ||
function wasParsedInMathMode(nodes) { | ||
return !nodes.some((node) => match.anyString(node) && node.content.length > 1 || match.string(node, "^") || match.string(node, "_")); | ||
return !nodes.some( | ||
(node) => match.anyString(node) && node.content.length > 1 || match.string(node, "^") || match.string(node, "_") | ||
); | ||
} | ||
@@ -80,12 +88,20 @@ | ||
import { processEnvironment } from "@unified-latex/unified-latex-util-environments"; | ||
var unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse = function unifiedLatexAttachMacroArguments(options) { | ||
var unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse = function unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse2(options) { | ||
const { environments = {}, macros = {} } = options || {}; | ||
const mathMacros = Object.fromEntries(Object.entries(macros).filter(([_, info]) => { | ||
var _a; | ||
return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; | ||
})); | ||
const mathEnvs = Object.fromEntries(Object.entries(environments).filter(([_, info]) => { | ||
var _a; | ||
return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; | ||
})); | ||
const mathMacros = Object.fromEntries( | ||
Object.entries(macros).filter( | ||
([_, info]) => { | ||
var _a; | ||
return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; | ||
} | ||
) | ||
); | ||
const mathEnvs = Object.fromEntries( | ||
Object.entries(environments).filter( | ||
([_, info]) => { | ||
var _a; | ||
return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true; | ||
} | ||
) | ||
); | ||
const mathReparser = unifiedLatexReparseMathConstructPlugin({ | ||
@@ -98,44 +114,91 @@ mathEnvs: Object.keys(mathEnvs), | ||
return (tree) => { | ||
visit2(tree, { | ||
enter: (nodes) => { | ||
if (!Array.isArray(nodes)) { | ||
return; | ||
visit2( | ||
tree, | ||
{ | ||
enter: (nodes) => { | ||
if (!Array.isArray(nodes)) { | ||
return; | ||
} | ||
attachMacroArgsInArray(nodes, mathMacros); | ||
}, | ||
leave: (node) => { | ||
if (!isRelevantMathEnvironment(node)) { | ||
return; | ||
} | ||
const envName = printRaw2(node.env); | ||
const envInfo = environments[envName]; | ||
if (!envInfo) { | ||
throw new Error( | ||
`Could not find environment info for environment "${envName}"` | ||
); | ||
} | ||
processEnvironment(node, envInfo); | ||
} | ||
attachMacroArgsInArray(nodes, mathMacros); | ||
}, | ||
leave: (node) => { | ||
if (!isRelevantMathEnvironment(node)) { | ||
return; | ||
} | ||
const envName = printRaw2(node.env); | ||
const envInfo = environments[envName]; | ||
if (!envInfo) { | ||
throw new Error(`Could not find environment info for environment "${envName}"`); | ||
} | ||
processEnvironment(node, envInfo); | ||
} | ||
}, { includeArrays: true }); | ||
{ includeArrays: true } | ||
); | ||
mathReparser(tree); | ||
visit2(tree, { | ||
enter: (nodes) => { | ||
if (!Array.isArray(nodes)) { | ||
return; | ||
visit2( | ||
tree, | ||
{ | ||
enter: (nodes) => { | ||
if (!Array.isArray(nodes)) { | ||
return; | ||
} | ||
attachMacroArgsInArray(nodes, macros); | ||
}, | ||
leave: (node) => { | ||
if (!isRelevantEnvironment(node)) { | ||
return; | ||
} | ||
const envName = printRaw2(node.env); | ||
const envInfo = environments[envName]; | ||
if (!envInfo) { | ||
throw new Error( | ||
`Could not find environment info for environment "${envName}"` | ||
); | ||
} | ||
processEnvironment(node, envInfo); | ||
} | ||
attachMacroArgsInArray(nodes, macros); | ||
}, | ||
leave: (node) => { | ||
if (!isRelevantEnvironment(node)) { | ||
return; | ||
} | ||
const envName = printRaw2(node.env); | ||
const envInfo = environments[envName]; | ||
if (!envInfo) { | ||
throw new Error(`Could not find environment info for environment "${envName}"`); | ||
} | ||
processEnvironment(node, envInfo); | ||
} | ||
}, { includeArrays: true }); | ||
{ includeArrays: true } | ||
); | ||
}; | ||
}; | ||
// libs/process-at-letter-and-expl-macros.ts | ||
import { reparseExpl3AndAtLetterRegions } from "@unified-latex/unified-latex-util-catcode"; | ||
import { | ||
hasReparsableMacroNames, | ||
reparseMacroNames | ||
} from "@unified-latex/unified-latex-util-catcode"; | ||
var unifiedLatexProcessAtLetterAndExplMacros = function unifiedLatexProcessAtLetterAndExplMacros2(options) { | ||
let { | ||
atLetter = false, | ||
expl3 = false, | ||
autodetectExpl3AndAtLetter = false | ||
} = options || {}; | ||
return (tree) => { | ||
reparseExpl3AndAtLetterRegions(tree); | ||
if (atLetter || expl3) { | ||
autodetectExpl3AndAtLetter = false; | ||
} | ||
if (autodetectExpl3AndAtLetter) { | ||
atLetter = hasReparsableMacroNames(tree, "@"); | ||
expl3 = hasReparsableMacroNames(tree, "_"); | ||
} | ||
const charSet = /* @__PURE__ */ new Set(); | ||
if (atLetter) { | ||
charSet.add("@"); | ||
} | ||
if (expl3) { | ||
charSet.add(":"); | ||
charSet.add("_"); | ||
} | ||
if (charSet.size > 0) { | ||
reparseMacroNames(tree, charSet); | ||
} | ||
}; | ||
}; | ||
// libs/plugin-from-string.ts | ||
@@ -146,7 +209,24 @@ var unifiedLatexFromString = function unifiedLatexFromString2(options) { | ||
macros = {}, | ||
environments = {} | ||
environments = {}, | ||
flags: { | ||
atLetter = false, | ||
expl3 = false, | ||
autodetectExpl3AndAtLetter = false | ||
} = {} | ||
} = options || {}; | ||
const allMacroInfo = Object.assign({}, macros, ...Object.values(macroInfo)); | ||
const allEnvInfo = Object.assign({}, environments, ...Object.values(environmentInfo)); | ||
const fullParser = unified().use(unifiedLatexFromStringMinimal, { mode }).use(unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse, { | ||
const allMacroInfo = Object.assign( | ||
{}, | ||
macros, | ||
...Object.values(macroInfo) | ||
); | ||
const allEnvInfo = Object.assign( | ||
{}, | ||
environments, | ||
...Object.values(environmentInfo) | ||
); | ||
const fullParser = unified().use(unifiedLatexFromStringMinimal, { mode }).use(unifiedLatexProcessAtLetterAndExplMacros, { | ||
atLetter, | ||
expl3, | ||
autodetectExpl3AndAtLetter | ||
}).use(unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse, { | ||
macros: allMacroInfo, | ||
@@ -153,0 +233,0 @@ environments: allEnvInfo |
@@ -8,2 +8,17 @@ import { Plugin } from "unified"; | ||
environments?: EnvInfoRecord; | ||
flags?: { | ||
/** | ||
* Whether to parse macros as if `\makeatletter` is set (i.e., parse `@` as a regular macro character) | ||
*/ | ||
atLetter?: boolean; | ||
/** | ||
* Whether to parse macros as if `\ExplSyntaxOn` is set (i.e., parse `_` and `:` as a regular macro character) | ||
*/ | ||
expl3?: boolean; | ||
/** | ||
* Attempt to autodetect whether there are macros that look like they should contain `@`, `_`, or `:`. | ||
* Defaults to `false`. | ||
*/ | ||
autodetectExpl3AndAtLetter?: boolean; | ||
}; | ||
} | undefined; | ||
@@ -10,0 +25,0 @@ /** |
{ | ||
"name": "@unified-latex/unified-latex-util-parse", | ||
"version": "1.0.12", | ||
"version": "1.1.0", | ||
"description": "Tools for manipulating unified-latex ASTs", | ||
@@ -8,11 +8,12 @@ "main": "index.js", | ||
"dependencies": { | ||
"@unified-latex/unified-latex-ctan": "^1.0.12", | ||
"@unified-latex/unified-latex-types": "^1.0.12", | ||
"@unified-latex/unified-latex-util-arguments": "^1.0.12", | ||
"@unified-latex/unified-latex-util-environments": "^1.0.12", | ||
"@unified-latex/unified-latex-util-match": "^1.0.12", | ||
"@unified-latex/unified-latex-util-pegjs": "^1.0.12", | ||
"@unified-latex/unified-latex-util-print-raw": "^1.0.12", | ||
"@unified-latex/unified-latex-util-trim": "^1.0.12", | ||
"@unified-latex/unified-latex-util-visit": "^1.0.12", | ||
"@unified-latex/unified-latex-ctan": "^1.1.0", | ||
"@unified-latex/unified-latex-types": "^1.1.0", | ||
"@unified-latex/unified-latex-util-arguments": "^1.1.0", | ||
"@unified-latex/unified-latex-util-catcode": "^1.1.0", | ||
"@unified-latex/unified-latex-util-environments": "^1.1.0", | ||
"@unified-latex/unified-latex-util-match": "^1.1.0", | ||
"@unified-latex/unified-latex-util-pegjs": "^1.1.0", | ||
"@unified-latex/unified-latex-util-print-raw": "^1.1.0", | ||
"@unified-latex/unified-latex-util-trim": "^1.1.0", | ||
"@unified-latex/unified-latex-util-visit": "^1.1.0", | ||
"unified": "^10.1.2" | ||
@@ -19,0 +20,0 @@ }, |
@@ -54,3 +54,3 @@ <!-- DO NOT MODIFY --> | ||
```typescript | ||
{ mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; } | ||
{ mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; flags?: { atLetter?: boolean; expl3?: boolean; autodetectExpl3AndAtLetter?: boolean; }; } | ||
``` | ||
@@ -60,3 +60,3 @@ | ||
`Plugin<{ mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; }[], string, Ast.Root>` | ||
`Plugin<{ mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; flags?: { atLetter?: boolean; expl3?: boolean; autodetectExpl3AndAtLetter?: boolean; }; }[], string, Ast.Root>` | ||
@@ -68,2 +68,7 @@ ```typescript | ||
environments?: Ast.EnvInfoRecord; | ||
flags?: { | ||
atLetter?: boolean; | ||
expl3?: boolean; | ||
autodetectExpl3AndAtLetter?: boolean; | ||
}; | ||
}): void; | ||
@@ -171,4 +176,19 @@ ``` | ||
environments?: EnvInfoRecord; | ||
flags?: { | ||
/** | ||
* Whether to parse macros as if `\makeatletter` is set (i.e., parse `@` as a regular macro character) | ||
*/ | ||
atLetter?: boolean; | ||
/** | ||
* Whether to parse macros as if `\ExplSyntaxOn` is set (i.e., parse `_` and `:` as a regular macro character) | ||
*/ | ||
expl3?: boolean; | ||
/** | ||
* Attempt to autodetect whether there are macros that look like they should contain `@`, `_`, or `:`. | ||
* Defaults to `false`. | ||
*/ | ||
autodetectExpl3AndAtLetter?: boolean; | ||
}; | ||
} | ||
| undefined; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
79913
26
676
191
11
+ Added@unified-latex/unified-latex-util-catcode@1.8.0(transitive)
Updated@unified-latex/unified-latex-util-environments@^1.1.0