bitran
Version:
📜 Highly customizable text processor and transpiler.
197 lines (196 loc) • 6 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { s as splitFirstLine, t as textToObj, i as indent, I as InlinerNode } from "./productNode-C0XZsQy2.js";
import YAML from "yaml";
import { B as BlocksNode, I as InlinersNode, t as textName } from "./shared-B4X4KQeO.js";
var RangeIntersection = /* @__PURE__ */ ((RangeIntersection2) => {
RangeIntersection2[RangeIntersection2["None"] = 0] = "None";
RangeIntersection2[RangeIntersection2["Partial"] = 1] = "Partial";
RangeIntersection2[RangeIntersection2["Inside"] = 2] = "Inside";
RangeIntersection2[RangeIntersection2["Contain"] = 3] = "Contain";
return RangeIntersection2;
})(RangeIntersection || {});
function range(start, end) {
if (end <= start)
throw new Error(`Error creating range: proivided end '${end}' can't be less or equal start '${start}'!`);
return {
start,
end
};
}
function tryRange(start, end) {
try {
return range(start, end);
} catch {
return null;
}
}
function getIntersection(range2, toCompareWithRange) {
let inverseLogic = false;
let rangeA, rangeB;
if (range2.start <= toCompareWithRange.start) {
rangeA = range2;
rangeB = toCompareWithRange;
} else {
rangeA = toCompareWithRange;
rangeB = range2;
inverseLogic = true;
}
if (rangeA.end < rangeB.start)
return 0;
if (rangeA.start < rangeB.start && rangeA.end > rangeB.end)
return inverseLogic ? 2 : 3;
if (rangeA.start > rangeB.start && rangeA.end < rangeB.end)
return inverseLogic ? 3 : 2;
if (rangeA.start === rangeB.start) {
if (rangeA.end >= rangeB.end)
return inverseLogic ? 2 : 3;
else
return inverseLogic ? 3 : 2;
}
if (rangeA.end === rangeB.end) {
if (rangeA.start <= rangeB.start)
return inverseLogic ? 2 : 3;
else
return inverseLogic ? 3 : 2;
}
return 1;
}
class ParseFactory {
constructor() {
__publicField(this, "parser");
__publicField(this, "parseOptions");
__publicField(this, "productNode");
}
getPayload() {
var _a, _b, _c, _d;
const bitranCore = this.parser.coreConfig;
const productCore = bitranCore.products[this.productNode.name];
return {
bitranCore,
productCore,
node: this.productNode,
parseData: (_a = this.productNode) == null ? void 0 : _a.parseData,
provide: (_d = (_c = this.parser.coreConfig.products) == null ? void 0 : _c[(_b = this.productNode) == null ? void 0 : _b.name]) == null ? void 0 : _d.provide
};
}
alterAutoId(autoId) {
return autoId;
}
async parseBlocks(text2) {
const blocks = await this.parser.parseBlocks(text2, this.parseOptions);
const blocksGroup = new BlocksNode(this.productNode);
blocksGroup.setNodes(blocks);
return blocksGroup;
}
async parseInliners(text2) {
const inliners = await this.parser.parseInliners(text2, this.parseOptions);
const inlinersGroup = new InlinersNode(this.productNode);
inlinersGroup.setNodes(inliners);
return inlinersGroup;
}
}
class BlockParseFactory extends ParseFactory {
}
class ObjBlockParseFactory extends BlockParseFactory {
canParse(strBlock) {
var _a;
return ((_a = strBlock.match(/^@(\S+)$/m)) == null ? void 0 : _a[1]) === this.objName;
}
async createParseData(strBlock) {
const { restText } = splitFirstLine(strBlock);
return this.parseDataFromObj(textToObj(restText), strBlock);
}
}
class InlinerParseFactory extends ParseFactory {
}
class RegexpInlinerParseFactory extends InlinerParseFactory {
outlineRanges(text2) {
const ranges = [];
const matches = text2.matchAll(new RegExp(this.regexp));
for (const match of matches)
ranges.push(tryRange(match.index, match.index + match[0].length));
return ranges;
}
async createParseData(strInliner) {
return this.parseDataFromRegexp(new RegExp(this.regexp).exec(strInliner));
}
}
class StrFactory {
constructor() {
__publicField(this, "stringifier");
__publicField(this, "strOptions");
__publicField(this, "node");
}
getPayload() {
return {
node: this.node
};
}
stringify(node) {
return this.stringifier.stringify(node, this.strOptions);
}
}
class ProductStrFactory extends StrFactory {
stringifyNode(node) {
return this.stringifyData(node.parseData);
}
}
class ObjProductStrFactory extends StrFactory {
stringifyNode(node) {
const obj = this.createObj(node.parseData);
return ObjProductStrFactory.strObjBlock(this.objName, obj);
}
static strObjBlock(objName, obj) {
let strObj = YAML.stringify(obj, { indent: 4 }).trim();
strObj = strObj.replace(/: \|(-|\+|>)\n/gm, ": |\n");
return `@${objName}
${indent(strObj)}`;
}
}
function defineProductCore(productCore) {
return finalizeCore(productCore);
}
function defineProductCoreFn(productCore) {
return (provide) => ({ ...finalizeCore(productCore), ...{ provide } });
}
function finalizeCore(core) {
if (!core.renderDataSide)
core.renderDataSide = "both";
return core;
}
class TextStr extends ProductStrFactory {
stringifyData(parseData) {
return parseData ?? "";
}
}
class TextNode extends InlinerNode {
constructor() {
super(...arguments);
__publicField(this, "name", textName);
}
}
const text = defineProductCore({
Node: TextNode,
Parser: null,
Stringifier: TextStr
});
export {
BlockParseFactory as B,
InlinerParseFactory as I,
ObjBlockParseFactory as O,
ParseFactory as P,
RegexpInlinerParseFactory as R,
StrFactory as S,
TextNode as T,
ProductStrFactory as a,
ObjProductStrFactory as b,
defineProductCoreFn as c,
defineProductCore as d,
RangeIntersection as e,
tryRange as f,
getIntersection as g,
text as t
};
//# sourceMappingURL=index-DiVYKXTv.js.map