@unified-latex/unified-latex-util-arguments
Advanced tools
Comparing version 1.0.12 to 1.1.0
85
index.js
@@ -12,3 +12,7 @@ // libs/gobble-arguments.ts | ||
if (typeof argSpec === "string" || !argSpec.type) { | ||
throw new Error(`argSpec must be an already-parsed argument specification, not "${JSON.stringify(argSpec)}"`); | ||
throw new Error( | ||
`argSpec must be an already-parsed argument specification, not "${JSON.stringify( | ||
argSpec | ||
)}"` | ||
); | ||
} | ||
@@ -28,7 +32,9 @@ let argument = null; | ||
const closeMark = argSpec.closeBrace || ""; | ||
const acceptGroup = argSpec.type === "mandatory" && openMark === "{" && closeMark === "}"; | ||
const acceptGroup = (argSpec.type === "mandatory" || argSpec.type === "optional") && openMark === "{" && closeMark === "}"; | ||
function findBracePositions() { | ||
let openMarkPos = null; | ||
if (openMark) { | ||
openMarkPos = nodes.findIndex((node, i) => i >= currPos && match.string(node, openMark)); | ||
openMarkPos = nodes.findIndex( | ||
(node, i) => i >= currPos && match.string(node, openMark) | ||
); | ||
if (openMarkPos < currPos) { | ||
@@ -40,3 +46,5 @@ openMarkPos = null; | ||
if (openMarkPos != null) { | ||
closeMarkPos = nodes.findIndex((node, i) => i >= openMarkPos + 1 && match.string(node, closeMark)); | ||
closeMarkPos = nodes.findIndex( | ||
(node, i) => i >= openMarkPos + 1 && match.string(node, closeMark) | ||
); | ||
if (closeMarkPos < openMarkPos + 1) { | ||
@@ -68,2 +76,10 @@ closeMarkPos = null; | ||
case "optional": | ||
if (acceptGroup && match.group(currNode)) { | ||
argument = arg(currNode.content, { | ||
openMark, | ||
closeMark | ||
}); | ||
currPos++; | ||
break; | ||
} | ||
if (match.string(currNode, openMark)) { | ||
@@ -82,3 +98,7 @@ const [openMarkPos, closeMarkPos] = findBracePositions(); | ||
case "optionalStar": | ||
if (match.string(currNode, "*")) { | ||
case "optionalToken": | ||
if (match.string( | ||
currNode, | ||
argSpec.type === "optionalStar" ? "*" : argSpec.token | ||
)) { | ||
argument = arg([currNode], { openMark: "", closeMark: "" }); | ||
@@ -90,3 +110,5 @@ currPos++; | ||
default: | ||
console.warn(`Don't know how to find an argument of argspec type "${argSpec.type}"`); | ||
console.warn( | ||
`Don't know how to find an argument of argspec type "${argSpec.type}"` | ||
); | ||
} | ||
@@ -106,3 +128,7 @@ const nodesRemoved = argument ? currPos - startPos : 0; | ||
for (const spec of argSpec) { | ||
const { argument, nodesRemoved: removed } = gobbleSingleArgument(nodes, spec, startPos); | ||
const { argument, nodesRemoved: removed } = gobbleSingleArgument( | ||
nodes, | ||
spec, | ||
startPos | ||
); | ||
if (argument) { | ||
@@ -156,5 +182,9 @@ args.push(argument); | ||
function attachMacroArgs(tree, macros) { | ||
visit(tree, (nodes) => { | ||
attachMacroArgsInArray(nodes, macros); | ||
}, { includeArrays: true, test: Array.isArray }); | ||
visit( | ||
tree, | ||
(nodes) => { | ||
attachMacroArgsInArray(nodes, macros); | ||
}, | ||
{ includeArrays: true, test: Array.isArray } | ||
); | ||
} | ||
@@ -168,7 +198,13 @@ | ||
if (Object.keys(macros).length === 0) { | ||
console.warn("Attempting to attach macro arguments but no macros are specified."); | ||
console.warn( | ||
"Attempting to attach macro arguments but no macros are specified." | ||
); | ||
} | ||
visit2(tree, (nodes) => { | ||
attachMacroArgsInArray(nodes, macros); | ||
}, { includeArrays: true, test: Array.isArray }); | ||
visit2( | ||
tree, | ||
(nodes) => { | ||
attachMacroArgsInArray(nodes, macros); | ||
}, | ||
{ includeArrays: true, test: Array.isArray } | ||
); | ||
}; | ||
@@ -189,2 +225,22 @@ }; | ||
} | ||
function getNamedArgsContent(node, namedArgumentsFallback = []) { | ||
var _a; | ||
const names = ((_a = node._renderInfo) == null ? void 0 : _a.namedArguments) || namedArgumentsFallback; | ||
if (!Array.isArray(node.args) || !Array.isArray(names) || names.length === 0) { | ||
return {}; | ||
} | ||
const ret = {}; | ||
node.args.forEach((arg3, i) => { | ||
const name = names[i]; | ||
if (name == null) { | ||
return; | ||
} | ||
let val = arg3.content; | ||
if (arg3.openMark === "" && arg3.content.length === 0) { | ||
val = null; | ||
} | ||
ret[name] = val; | ||
}); | ||
return ret; | ||
} | ||
export { | ||
@@ -194,2 +250,3 @@ attachMacroArgs, | ||
getArgsContent, | ||
getNamedArgsContent, | ||
gobbleArguments, | ||
@@ -196,0 +253,0 @@ unifiedLatexAttachMacroArguments |
@@ -7,2 +7,10 @@ import * as Ast from "@unified-latex/unified-latex-types"; | ||
export declare function getArgsContent(node: Ast.Macro | Ast.Environment): (Ast.Node[] | null)[]; | ||
/** | ||
* Returns the content of `args` for a macro or environment as an object whose keys are the "names" | ||
* of each argument. These names of the arguments must be specified in the `_renderInfo` prop. If `_renderInfo` | ||
* does not contain a `namedArguments` array, then an empty object will be returned. | ||
* | ||
* @namedArgumentsFallback - If `_renderInfo.namedArguments` is not provided, `namedArgumentsFallback` is ued. | ||
*/ | ||
export declare function getNamedArgsContent(node: Ast.Macro | Ast.Environment, namedArgumentsFallback?: readonly (string | null)[]): Record<string, Ast.Node[] | null>; | ||
//# sourceMappingURL=get-args-content.d.ts.map |
{ | ||
"name": "@unified-latex/unified-latex-util-arguments", | ||
"version": "1.0.12", | ||
"version": "1.1.0", | ||
"description": "Tools for manipulating unified-latex ASTs", | ||
@@ -8,8 +8,8 @@ "main": "index.js", | ||
"dependencies": { | ||
"@unified-latex/unified-latex-builder": "^1.0.12", | ||
"@unified-latex/unified-latex-types": "^1.0.12", | ||
"@unified-latex/unified-latex-util-argspec": "^1.0.12", | ||
"@unified-latex/unified-latex-util-match": "^1.0.12", | ||
"@unified-latex/unified-latex-util-render-info": "^1.0.12", | ||
"@unified-latex/unified-latex-util-visit": "^1.0.12", | ||
"@unified-latex/unified-latex-builder": "^1.1.0", | ||
"@unified-latex/unified-latex-types": "^1.1.0", | ||
"@unified-latex/unified-latex-util-argspec": "^1.1.0", | ||
"@unified-latex/unified-latex-util-match": "^1.1.0", | ||
"@unified-latex/unified-latex-util-render-info": "^1.1.0", | ||
"@unified-latex/unified-latex-util-visit": "^1.1.0", | ||
"unified": "^10.1.2" | ||
@@ -16,0 +16,0 @@ }, |
@@ -75,1 +75,21 @@ <!-- DO NOT MODIFY --> | ||
| node | `Ast.Macro \| Ast.Environment` | | ||
## `getNamedArgsContent(node, namedArgumentsFallback)` | ||
Returns the content of `args` for a macro or environment as an object whose keys are the "names" | ||
of each argument. These names of the arguments must be specified in the `_renderInfo` prop. If `_renderInfo` | ||
does not contain a `namedArguments` array, then an empty object will be returned. | ||
```typescript | ||
function getNamedArgsContent( | ||
node: Ast.Macro | Ast.Environment, | ||
namedArgumentsFallback: readonly string[] | ||
): Record<string, Ast.Node[]>; | ||
``` | ||
**Parameters** | ||
| Param | Type | | ||
| :--------------------- | :----------------------------- | | ||
| node | `Ast.Macro \| Ast.Environment` | | ||
| namedArgumentsFallback | `readonly string[]` | |
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
Sorry, the diff of this file is not supported yet
66232
586
95