90 lines
3.1 KiB
JavaScript
90 lines
3.1 KiB
JavaScript
import { parseLine as v, createLineData as H } from "./parser.js";
|
|
function g(y, w = " ") {
|
|
let c = 0;
|
|
const i = H("", w);
|
|
async function s() {
|
|
switch (c) {
|
|
case 0:
|
|
c = 1;
|
|
break;
|
|
case 2:
|
|
return c = 1, !1;
|
|
}
|
|
const e = await y.next();
|
|
return e === null ? (c = 3, !0) : (i.line = e, v(i), !1);
|
|
}
|
|
const p = () => c = 2, o = () => i.level, h = () => i.line.slice(i.offsetHead), u = () => i.line.slice(i.offsetHead, i.offsetTail), f = () => i.line.slice(i.offsetTail), b = (e) => e === u();
|
|
async function d(e) {
|
|
const a = c === 0 ? -1 : o();
|
|
for (; ; ) {
|
|
if (await s())
|
|
return;
|
|
if (o() <= a)
|
|
return p();
|
|
if (await e())
|
|
return;
|
|
}
|
|
}
|
|
async function m(e = -1, a = [h()]) {
|
|
var n;
|
|
return e === -1 && (e = o() + 1), await s() ? a : o() < e ? (p(), a) : (a.push(((n = i.line) == null ? void 0 : n.slice(e)) || ""), m(e, a));
|
|
}
|
|
async function j(e = {}, a = !1) {
|
|
const n = [];
|
|
let t = {};
|
|
return Object.keys(e).length ? Object.keys(e).forEach((r) => {
|
|
e[r] === !0 && (t[r] = { type: a ? "collection" : "normal", handle: () => f().trim() }), typeof e[r] == "function" && (t[r] = { type: a ? "collection" : "normal", handle: e[r] }), typeof e[r] == "object" && (t[r] = e[r]);
|
|
}) : t = { "#any": { type: a ? "collection" : "normal", handle: () => f().trim() } }, await d(async () => {
|
|
const r = u();
|
|
if (!r)
|
|
return;
|
|
const l = t[r] || t["#any"];
|
|
!l || (l.type === "normal" ? n.push(await l.handle()) : l.type === "collection" && n.push({ [r]: await l.handle() }));
|
|
}), n;
|
|
}
|
|
async function x(e = {}) {
|
|
const a = {};
|
|
let n = {};
|
|
return Object.keys(e).length ? Object.keys(e).forEach((t) => {
|
|
t === "#tail" ? n[t] = { type: "tail", handle: () => {
|
|
} } : t === "#text" ? n[t] = { type: "text", handle: () => {
|
|
} } : e[t] === !0 ? n[t] = { type: "normal", handle: () => f().trim() } : typeof e[t] == "function" ? n[t] = { type: "normal", handle: e[t] } : typeof e[t] == "object" && (n[t] = e[t]);
|
|
}) : n = { "#any": { type: "normal", handle: () => f().trim() } }, n["#tail"] && (a["#tail"] = f().trim()), await d(async () => {
|
|
const t = u();
|
|
if (!t)
|
|
return;
|
|
const r = n[t] || n["#any"] || n["#text"];
|
|
if (!!r && (r.type === "normal" ? a[t] = await r.handle() : r.type === "collection" ? (a[t] || (a[t] = []), a[t].push(await r.handle())) : r.type === "text" && (a["#text"] = await m(o())), n && Object.keys(n).every((l) => ["collection"].includes(n[l].type) ? !1 : a[l] !== void 0)))
|
|
return !0;
|
|
}), a;
|
|
}
|
|
async function O() {
|
|
const e = [["root", []]];
|
|
for (; !await s(); ) {
|
|
const a = o(), n = a + 1, t = e[a];
|
|
if (!t)
|
|
continue;
|
|
e.length = n;
|
|
const r = e[n] = [h(), []];
|
|
t[1].push(r);
|
|
}
|
|
return e[0];
|
|
}
|
|
return {
|
|
next: s,
|
|
line: h,
|
|
head: u,
|
|
tail: f,
|
|
level: o,
|
|
match: b,
|
|
each: d,
|
|
blockAsText: m,
|
|
toObject: x,
|
|
toArray: j,
|
|
toLineArray: O
|
|
};
|
|
}
|
|
export {
|
|
g as useDocument
|
|
};
|