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