end-of-semester-report/node_modules/@unified-latex/unified-latex-util-print-raw/index.cjs
2025-05-14 16:14:35 +07:00

92 lines
2.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const linebreak = Symbol("linebreak");
const ESCAPE = "\\";
function _printRaw(node) {
if (typeof node === "string") {
return [node];
}
if (Array.isArray(node)) {
return [].concat(
...node.map((n) => _printRaw(n))
);
}
let argsString, escape;
switch (node.type) {
case "root":
return _printRaw(node.content);
case "argument":
return [node.openMark, ..._printRaw(node.content), node.closeMark];
case "comment":
let suffix = node.suffixParbreak ? "" : linebreak;
let leadingWhitespace = "";
if (node.sameline && node.leadingWhitespace) {
leadingWhitespace = " ";
}
if (node.sameline) {
return [
leadingWhitespace,
"%",
..._printRaw(node.content),
suffix
];
}
return [linebreak, "%", ..._printRaw(node.content), suffix];
case "environment":
case "mathenv":
case "verbatim":
let env = _printRaw(node.env);
let envStart = [ESCAPE + "begin{", ...env, "}"];
let envEnd = [ESCAPE + "end{", ...env, "}"];
argsString = node.args == null ? [] : _printRaw(node.args);
return [
...envStart,
...argsString,
..._printRaw(node.content),
...envEnd
];
case "displaymath":
return [ESCAPE + "[", ..._printRaw(node.content), ESCAPE + "]"];
case "group":
return ["{", ..._printRaw(node.content), "}"];
case "inlinemath":
return ["$", ..._printRaw(node.content), "$"];
case "macro":
argsString = node.args == null ? [] : _printRaw(node.args);
escape = node.escapeToken == null ? ESCAPE : node.escapeToken;
return [escape, ..._printRaw(node.content), ...argsString];
case "parbreak":
return [linebreak, linebreak];
case "string":
return [node.content];
case "verb":
return [
ESCAPE,
node.env,
node.escape,
..._printRaw(node.content),
node.escape
];
case "whitespace":
return [" "];
default:
console.warn(
"Cannot find render for node ",
node,
`(of type ${typeof node})`
);
return ["" + node];
}
}
function printRaw(node, options) {
const asArray = options != null ? options.asArray : false;
const printedTokens = _printRaw(node);
if (asArray) {
return printedTokens;
}
return printedTokens.map((x) => x === linebreak ? "\n" : x).join("");
}
exports.linebreak = linebreak;
exports.printRaw = printRaw;
//# sourceMappingURL=index.cjs.map