-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
154 lines (130 loc) · 3.76 KB
/
Copy pathrunner.js
File metadata and controls
154 lines (130 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: AGPL-3.0
import init, {
Registry,
compact_ref,
convert as convertValue,
version,
} from "./regedited_web.js";
const aliases = new Map([
["l", "list"],
["s", "scan"],
["f", "fgrep"],
["rg", "ref-get"],
["ri", "resolve-index"],
["ist", "index-str-list"],
["hl", "hexline"],
["co", "content"],
["cv", "convert"],
]);
export class RegeditedRunner {
#registry;
constructor(content) {
this.rawContent = String(content);
this.#registry = new Registry(this.rawContent);
}
version() {
return version();
}
scan() {
return JSON.parse(this.#registry.scan());
}
list() {
return this.scan().sections;
}
grep(pattern, scope) {
return JSON.parse(this.#registry.grep(String(pattern), scope));
}
fgrep(pattern, scope) {
return this.grep(pattern, scope);
}
readIndex(index) {
return JSON.parse(this.#registry.read_index(index));
}
resolveIndex(index) {
return this.readIndex(index);
}
db(index) {
return this.readIndex(index).db;
}
indexStrList(index) {
return this.readIndex(index).strings;
}
hexline(index) {
return this.readIndex(index).hexLine;
}
content(index) {
return this.readIndex(index).content;
}
sectionContent(key) {
return this.#registry.section_content(String(key));
}
sectionData(key) {
return this.#registry.section_data(String(key));
}
compactRef(value) {
return compact_ref(String(value));
}
convert(values, defaultType = "markdown") {
const input = Array.isArray(values) ? values.join(" ") : String(values);
return convertValue(input, defaultType);
}
refGet(reference) {
const canonical = this.compactRef(reference);
const match = /^index:(\d+)(?::(string|db):(\d+)|:(dbline|hexline|hex-word-line))?$/.exec(
canonical,
);
if (!match) {
throw new Error(`Browser ref-get does not support '${canonical}'.`);
}
const data = this.readIndex(match[1]);
if (!match[2] && !match[4]) return data;
if (match[2] === "string") return data.strings[Number(match[3]) - 1];
if (match[2] === "db") return data.db[Number(match[3]) - 1];
if (match[4] === "dbline") return data.db;
return data.hexLine;
}
run(command, ...args) {
const canonical = aliases.get(command) ?? command;
switch (canonical) {
case "list":
return this.list();
case "scan":
return this.scan();
case "fgrep":
return this.fgrep(args[0], args[1]);
case "ref-get":
return this.refGet(args[0]);
case "resolve-index":
return this.resolveIndex(args[0]);
case "index-str-list":
return this.indexStrList(args[0]);
case "db":
return this.db(args[0]);
case "hexline":
return this.hexline(args[0]);
case "content":
return this.content(args[0]);
case "convert":
return this.convert(args[0], args[1]);
default:
throw new Error(
`Browser runner command '${command}' is unavailable. The browser package exposes read-only document operations only.`,
);
}
}
}
export async function createRegeditedRunner(content) {
await init();
return new RegeditedRunner(content);
}
export async function createPageRunner({ raw = false } = {}) {
const content = raw
? await fetch(globalThis.location.href, { cache: "no-store" }).then((response) => {
if (!response.ok) {
throw new Error(`Could not read page source: HTTP ${response.status}`);
}
return response.text();
})
: globalThis.document.documentElement.outerHTML;
return createRegeditedRunner(content);
}