@johnhenry/sitefile 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +75 -0
- package/README.md +46 -9
- package/lib/src/attest.d.ts +3 -2
- package/lib/src/attest.js +3 -2
- package/lib/src/cli/index.d.ts +2 -1
- package/lib/src/cli/index.js +11 -2
- package/lib/src/cli/serve-worker.js +136 -18
- package/lib/src/cli/servers.d.ts +11 -1
- package/lib/src/cli/servers.js +89 -32
- package/lib/src/cli/watch.d.ts +2 -0
- package/lib/src/cli/watch.js +5 -4
- package/lib/src/executor.js +27 -5
- package/lib/src/explain.js +1 -1
- package/lib/src/language/ast.d.ts +12 -0
- package/lib/src/language/parser.js +25 -3
- package/lib/src/language/validate.js +11 -0
- package/lib/src/pattern.d.ts +36 -0
- package/lib/src/pattern.js +389 -0
- package/lib/src/planner.d.ts +8 -0
- package/lib/src/planner.js +4 -2
- package/lib/src/render.d.ts +4 -1
- package/lib/src/render.js +11 -7
- package/lib/src/schema.d.ts +3 -0
- package/lib/src/schema.js +39 -6
- package/lib/src/servers.d.ts +25 -0
- package/lib/src/version.d.ts +7 -0
- package/lib/src/version.js +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// `pattern` in schemas (docs/LANGUAGE.md, "Schemas"): a small regular expression engine that the Rust port
|
|
2
|
+
// (rust/src/pattern.rs) implements identically, so both accept the same patterns, report the same errors and
|
|
3
|
+
// match the same strings. Shared test vectors: rust/tests/fixtures/pattern/vectors.json.
|
|
4
|
+
//
|
|
5
|
+
// The syntax is a subset of JavaScript regular expressions with the `u` flag, with the same meaning:
|
|
6
|
+
//
|
|
7
|
+
// literals any character other than ^ $ \ . * + ? ( ) [ ] { } |
|
|
8
|
+
// . any character except \n, \r, U+2028 and U+2029
|
|
9
|
+
// [abc] [a-z] [^…] classes (ranges, escapes; `-` is literal first or last; [] matches nothing, [^] anything)
|
|
10
|
+
// \d \w \s [0-9], [A-Za-z0-9_], JavaScript whitespace; \D \W \S their complements
|
|
11
|
+
// \n \r \t newline, carriage return, tab; \ before any of ^ $ \ . * + ? ( ) [ ] { } | / is that
|
|
12
|
+
// character (and \- inside a class)
|
|
13
|
+
// * + ? {n} {n,} {n,m} repetition (n, m ≤ 1000), optionally lazy with a trailing ? (same result here)
|
|
14
|
+
// ( … ) (?: … ) groups
|
|
15
|
+
// a|b alternation
|
|
16
|
+
// ^ $ start and end of the string (no multiline mode)
|
|
17
|
+
//
|
|
18
|
+
// A pattern matches when it matches somewhere in the string (like RegExp.test); anchor it with ^…$ to match
|
|
19
|
+
// the whole value. Characters are Unicode code points. Matching runs a Thompson NFA (Pike VM), so its time is
|
|
20
|
+
// linear in the string length whatever the pattern: no backtracking blow-up.
|
|
21
|
+
/** A compile error: `position` is the 1-based code point index in the pattern. */
|
|
22
|
+
export class PatternError extends Error {
|
|
23
|
+
reason;
|
|
24
|
+
position;
|
|
25
|
+
constructor(reason, position) {
|
|
26
|
+
super(`at character ${position}: ${reason}`);
|
|
27
|
+
this.reason = reason;
|
|
28
|
+
this.position = position;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const MAX_COUNT = 1000;
|
|
32
|
+
/** Compiled programs larger than this are refused (a {n,m} of a {n,m} …). */
|
|
33
|
+
const MAX_PROGRAM = 20000;
|
|
34
|
+
const DIGIT = { ranges: [[0x30, 0x39]], negate: false };
|
|
35
|
+
const WORD = { ranges: [[0x30, 0x39], [0x41, 0x5a], [0x5f, 0x5f], [0x61, 0x7a]], negate: false };
|
|
36
|
+
/** JavaScript's \s: WhiteSpace and LineTerminator. */
|
|
37
|
+
const SPACE = {
|
|
38
|
+
ranges: [[0x09, 0x0d], [0x20, 0x20], [0xa0, 0xa0], [0x1680, 0x1680], [0x2000, 0x200a], [0x2028, 0x2029], [0x202f, 0x202f], [0x205f, 0x205f], [0x3000, 0x3000], [0xfeff, 0xfeff]],
|
|
39
|
+
negate: false,
|
|
40
|
+
};
|
|
41
|
+
/** `.`: everything but line terminators. */
|
|
42
|
+
const DOT = { ranges: [[0x0a, 0x0a], [0x0d, 0x0d], [0x2028, 0x2029]], negate: true };
|
|
43
|
+
const SYNTAX = '^$\\.*+?()[]{}|/';
|
|
44
|
+
const single = (c) => ({ ranges: [[c, c]], negate: false });
|
|
45
|
+
const inSet = (s, c) => s.ranges.some(([lo, hi]) => c >= lo && c <= hi) !== s.negate;
|
|
46
|
+
class Parser {
|
|
47
|
+
cps;
|
|
48
|
+
i = 0;
|
|
49
|
+
constructor(cps) {
|
|
50
|
+
this.cps = cps;
|
|
51
|
+
}
|
|
52
|
+
peek() {
|
|
53
|
+
const c = this.cps[this.i];
|
|
54
|
+
return c === undefined ? undefined : String.fromCodePoint(c);
|
|
55
|
+
}
|
|
56
|
+
fail(reason, at = this.i) {
|
|
57
|
+
throw new PatternError(reason, at + 1);
|
|
58
|
+
}
|
|
59
|
+
alt() {
|
|
60
|
+
const xs = [this.cat()];
|
|
61
|
+
while (this.peek() === '|') {
|
|
62
|
+
this.i++;
|
|
63
|
+
xs.push(this.cat());
|
|
64
|
+
}
|
|
65
|
+
return xs.length === 1 ? xs[0] : { k: 'alt', xs };
|
|
66
|
+
}
|
|
67
|
+
cat() {
|
|
68
|
+
const xs = [];
|
|
69
|
+
for (;;) {
|
|
70
|
+
const c = this.peek();
|
|
71
|
+
if (c === undefined || c === '|' || c === ')')
|
|
72
|
+
break;
|
|
73
|
+
const start = this.i;
|
|
74
|
+
const atom = this.atom();
|
|
75
|
+
xs.push(this.quantified(atom, start));
|
|
76
|
+
}
|
|
77
|
+
return xs.length === 1 ? xs[0] : { k: 'cat', xs };
|
|
78
|
+
}
|
|
79
|
+
/** A quantifier after `atom` (which began at `start`), if any. */
|
|
80
|
+
quantified(atom, start) {
|
|
81
|
+
const c = this.peek();
|
|
82
|
+
let min;
|
|
83
|
+
let max;
|
|
84
|
+
if (c === '*' || c === '+' || c === '?') {
|
|
85
|
+
this.i++;
|
|
86
|
+
[min, max] = c === '*' ? [0, undefined] : c === '+' ? [1, undefined] : [0, 1];
|
|
87
|
+
}
|
|
88
|
+
else if (c === '{') {
|
|
89
|
+
[min, max] = this.counted();
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return atom;
|
|
93
|
+
}
|
|
94
|
+
if (atom.k === 'bol' || atom.k === 'eol')
|
|
95
|
+
this.fail('nothing to repeat', start);
|
|
96
|
+
if (this.peek() === '?')
|
|
97
|
+
this.i++; // lazy: the same answer for a yes/no match
|
|
98
|
+
const next = this.peek();
|
|
99
|
+
if (next === '*' || next === '+' || next === '?' || next === '{')
|
|
100
|
+
this.fail('nothing to repeat');
|
|
101
|
+
return { k: 'rep', x: atom, min, max };
|
|
102
|
+
}
|
|
103
|
+
/** `{n}`, `{n,}` or `{n,m}` at `{`. */
|
|
104
|
+
counted() {
|
|
105
|
+
const open = this.i;
|
|
106
|
+
this.i++;
|
|
107
|
+
const num = () => {
|
|
108
|
+
const from = this.i;
|
|
109
|
+
while (this.peek() !== undefined && /[0-9]/.test(this.peek()))
|
|
110
|
+
this.i++;
|
|
111
|
+
if (this.i === from)
|
|
112
|
+
return undefined;
|
|
113
|
+
const digits = String.fromCodePoint(...this.cps.slice(from, this.i));
|
|
114
|
+
return digits.length > 4 ? MAX_COUNT + 1 : Number(digits);
|
|
115
|
+
};
|
|
116
|
+
const min = num();
|
|
117
|
+
let max = min;
|
|
118
|
+
if (min !== undefined && this.peek() === ',') {
|
|
119
|
+
this.i++;
|
|
120
|
+
max = num();
|
|
121
|
+
}
|
|
122
|
+
if (min === undefined || this.peek() !== '}')
|
|
123
|
+
this.fail("'{' must be written \\{ unless it starts a repetition {n}, {n,} or {n,m}", open);
|
|
124
|
+
this.i++;
|
|
125
|
+
if (min > MAX_COUNT || (max !== undefined && max > MAX_COUNT))
|
|
126
|
+
this.fail(`repetition count above ${MAX_COUNT}`, open);
|
|
127
|
+
if (max !== undefined && max < min)
|
|
128
|
+
this.fail('numbers out of order in {} repetition', open);
|
|
129
|
+
return [min, max];
|
|
130
|
+
}
|
|
131
|
+
atom() {
|
|
132
|
+
const start = this.i;
|
|
133
|
+
const c = this.peek();
|
|
134
|
+
this.i++;
|
|
135
|
+
switch (c) {
|
|
136
|
+
case '(': {
|
|
137
|
+
if (this.peek() === '?') {
|
|
138
|
+
if (this.cps[this.i + 1] !== 0x3a)
|
|
139
|
+
this.fail("unsupported group '(?' (only ( … ) and (?: … ) are supported)", start);
|
|
140
|
+
this.i += 2;
|
|
141
|
+
}
|
|
142
|
+
const inner = this.alt();
|
|
143
|
+
if (this.peek() !== ')')
|
|
144
|
+
this.fail("missing ')'", start);
|
|
145
|
+
this.i++;
|
|
146
|
+
return inner;
|
|
147
|
+
}
|
|
148
|
+
case ')':
|
|
149
|
+
return this.fail("unmatched ')'", start);
|
|
150
|
+
case '[':
|
|
151
|
+
return this.cls(start);
|
|
152
|
+
case '.':
|
|
153
|
+
return { k: 'set', items: [DOT], negate: false };
|
|
154
|
+
case '^':
|
|
155
|
+
return { k: 'bol' };
|
|
156
|
+
case '$':
|
|
157
|
+
return { k: 'eol' };
|
|
158
|
+
case '\\':
|
|
159
|
+
return { k: 'set', items: [this.escape(false, start)], negate: false };
|
|
160
|
+
case '*':
|
|
161
|
+
case '+':
|
|
162
|
+
case '?':
|
|
163
|
+
return this.fail('nothing to repeat', start);
|
|
164
|
+
case '{':
|
|
165
|
+
return this.fail("'{' must be written \\{ unless it starts a repetition {n}, {n,} or {n,m}", start);
|
|
166
|
+
case '}':
|
|
167
|
+
case ']':
|
|
168
|
+
return this.fail(`'${c}' must be written \\${c}`, start);
|
|
169
|
+
default:
|
|
170
|
+
return { k: 'set', items: [single(this.cps[start])], negate: false };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** After `\` (at `start`): a class escape or a single character. */
|
|
174
|
+
escape(inClass, start) {
|
|
175
|
+
const c = this.peek();
|
|
176
|
+
if (c === undefined)
|
|
177
|
+
return this.fail("pattern ends with '\\'", start);
|
|
178
|
+
this.i++;
|
|
179
|
+
switch (c) {
|
|
180
|
+
case 'd':
|
|
181
|
+
return DIGIT;
|
|
182
|
+
case 'D':
|
|
183
|
+
return { ...DIGIT, negate: true };
|
|
184
|
+
case 'w':
|
|
185
|
+
return WORD;
|
|
186
|
+
case 'W':
|
|
187
|
+
return { ...WORD, negate: true };
|
|
188
|
+
case 's':
|
|
189
|
+
return SPACE;
|
|
190
|
+
case 'S':
|
|
191
|
+
return { ...SPACE, negate: true };
|
|
192
|
+
case 'n':
|
|
193
|
+
return single(0x0a);
|
|
194
|
+
case 'r':
|
|
195
|
+
return single(0x0d);
|
|
196
|
+
case 't':
|
|
197
|
+
return single(0x09);
|
|
198
|
+
}
|
|
199
|
+
if (SYNTAX.includes(c) || (inClass && c === '-'))
|
|
200
|
+
return single(c.codePointAt(0));
|
|
201
|
+
return this.fail(`unsupported escape '\\${c}'`, start);
|
|
202
|
+
}
|
|
203
|
+
/** A class after `[` (at `start`). */
|
|
204
|
+
cls(start) {
|
|
205
|
+
let negate = false;
|
|
206
|
+
if (this.peek() === '^') {
|
|
207
|
+
negate = true;
|
|
208
|
+
this.i++;
|
|
209
|
+
}
|
|
210
|
+
const items = [];
|
|
211
|
+
/** One class atom: a set (a class escape) or a single code point. */
|
|
212
|
+
const classAtom = () => {
|
|
213
|
+
const at = this.i;
|
|
214
|
+
const c = this.peek();
|
|
215
|
+
this.i++;
|
|
216
|
+
if (c === '\\') {
|
|
217
|
+
const set = this.escape(true, at);
|
|
218
|
+
const isSingle = !set.negate && set.ranges.length === 1 && set.ranges[0][0] === set.ranges[0][1];
|
|
219
|
+
return isSingle ? { set, cp: set.ranges[0][0], at } : { set, at };
|
|
220
|
+
}
|
|
221
|
+
return { set: single(this.cps[at]), cp: this.cps[at], at };
|
|
222
|
+
};
|
|
223
|
+
for (;;) {
|
|
224
|
+
const c = this.peek();
|
|
225
|
+
if (c === undefined)
|
|
226
|
+
this.fail('unterminated character class', start);
|
|
227
|
+
if (c === ']') {
|
|
228
|
+
this.i++;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
const a = classAtom();
|
|
232
|
+
if (this.peek() === '-' && this.cps[this.i + 1] !== undefined && this.cps[this.i + 1] !== 0x5d) {
|
|
233
|
+
this.i++;
|
|
234
|
+
const b = classAtom();
|
|
235
|
+
if (a.cp === undefined || b.cp === undefined)
|
|
236
|
+
this.fail('a class escape cannot start or end a range', a.at);
|
|
237
|
+
if (b.cp < a.cp)
|
|
238
|
+
this.fail('range out of order in character class', a.at);
|
|
239
|
+
items.push({ ranges: [[a.cp, b.cp]], negate: false });
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
items.push(a.set);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return { k: 'set', items, negate };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function emit(n, prog) {
|
|
249
|
+
if (prog.length > MAX_PROGRAM)
|
|
250
|
+
throw new PatternError('pattern is too large', 1);
|
|
251
|
+
switch (n.k) {
|
|
252
|
+
case 'set':
|
|
253
|
+
prog.push({ op: 'set', items: n.items, negate: n.negate });
|
|
254
|
+
return;
|
|
255
|
+
case 'bol':
|
|
256
|
+
case 'eol':
|
|
257
|
+
prog.push({ op: n.k });
|
|
258
|
+
return;
|
|
259
|
+
case 'cat':
|
|
260
|
+
for (const x of n.xs)
|
|
261
|
+
emit(x, prog);
|
|
262
|
+
return;
|
|
263
|
+
case 'alt': {
|
|
264
|
+
// split L1, next; L1: x0; jmp end; next: split L2, … ; last: xn
|
|
265
|
+
const jumps = [];
|
|
266
|
+
n.xs.forEach((x, i) => {
|
|
267
|
+
if (i < n.xs.length - 1) {
|
|
268
|
+
const split = { op: 'split', x: prog.length + 1, y: 0 };
|
|
269
|
+
prog.push(split);
|
|
270
|
+
emit(x, prog);
|
|
271
|
+
const j = { op: 'jmp', x: 0 };
|
|
272
|
+
prog.push(j);
|
|
273
|
+
jumps.push(j);
|
|
274
|
+
split.y = prog.length;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
emit(x, prog);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
for (const j of jumps)
|
|
281
|
+
j.x = prog.length;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
case 'rep': {
|
|
285
|
+
for (let i = 0; i < n.min; i++)
|
|
286
|
+
emit(n.x, prog);
|
|
287
|
+
if (n.max === undefined) {
|
|
288
|
+
// L: split body, end; body; jmp L
|
|
289
|
+
const split = { op: 'split', x: prog.length + 1, y: 0 };
|
|
290
|
+
const at = prog.length;
|
|
291
|
+
prog.push(split);
|
|
292
|
+
emit(n.x, prog);
|
|
293
|
+
prog.push({ op: 'jmp', x: at });
|
|
294
|
+
split.y = prog.length;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
// (x(x(x)?)?)? for the optional copies.
|
|
298
|
+
const splits = [];
|
|
299
|
+
for (let i = n.min; i < n.max; i++) {
|
|
300
|
+
const split = { op: 'split', x: prog.length + 1, y: 0 };
|
|
301
|
+
prog.push(split);
|
|
302
|
+
splits.push(split);
|
|
303
|
+
emit(n.x, prog);
|
|
304
|
+
}
|
|
305
|
+
for (const s of splits)
|
|
306
|
+
s.y = prog.length;
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/** Compile a pattern; throws PatternError. */
|
|
312
|
+
export function compilePattern(source) {
|
|
313
|
+
const cps = Array.from(source, (c) => c.codePointAt(0));
|
|
314
|
+
const p = new Parser(cps);
|
|
315
|
+
const node = p.alt();
|
|
316
|
+
if (p.i < cps.length)
|
|
317
|
+
p.fail("unmatched ')'"); // alt() stops only at the end or a ')'
|
|
318
|
+
const prog = [];
|
|
319
|
+
emit(node, prog);
|
|
320
|
+
if (prog.length > MAX_PROGRAM)
|
|
321
|
+
throw new PatternError('pattern is too large', 1);
|
|
322
|
+
prog.push({ op: 'match' });
|
|
323
|
+
return { source, prog };
|
|
324
|
+
}
|
|
325
|
+
/** Whether the pattern matches somewhere in `text` (Pike VM over code points). */
|
|
326
|
+
export function patternMatches(p, text) {
|
|
327
|
+
const cps = Array.from(text, (c) => c.codePointAt(0));
|
|
328
|
+
const n = p.prog.length;
|
|
329
|
+
let clist = [];
|
|
330
|
+
let mark = new Uint32Array(n);
|
|
331
|
+
let gen = 0;
|
|
332
|
+
// Follow jumps, splits and assertions from `pc` at position `pos`; true when Match is reachable.
|
|
333
|
+
const add = (list, pc, pos) => {
|
|
334
|
+
const stack = [pc];
|
|
335
|
+
while (stack.length > 0) {
|
|
336
|
+
const at = stack.pop();
|
|
337
|
+
if (mark[at] === gen)
|
|
338
|
+
continue;
|
|
339
|
+
mark[at] = gen;
|
|
340
|
+
const ins = p.prog[at];
|
|
341
|
+
switch (ins.op) {
|
|
342
|
+
case 'match':
|
|
343
|
+
return true;
|
|
344
|
+
case 'jmp':
|
|
345
|
+
stack.push(ins.x);
|
|
346
|
+
break;
|
|
347
|
+
case 'split':
|
|
348
|
+
stack.push(ins.y, ins.x);
|
|
349
|
+
break;
|
|
350
|
+
case 'bol':
|
|
351
|
+
if (pos === 0)
|
|
352
|
+
stack.push(at + 1);
|
|
353
|
+
break;
|
|
354
|
+
case 'eol':
|
|
355
|
+
if (pos === cps.length)
|
|
356
|
+
stack.push(at + 1);
|
|
357
|
+
break;
|
|
358
|
+
case 'set':
|
|
359
|
+
list.push(at);
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return false;
|
|
364
|
+
};
|
|
365
|
+
for (let pos = 0;; pos++) {
|
|
366
|
+
gen++;
|
|
367
|
+
// The start state at every position: an unanchored search. Threads already in the list keep their marks.
|
|
368
|
+
for (const pc of clist)
|
|
369
|
+
mark[pc] = gen;
|
|
370
|
+
if (add(clist, 0, pos))
|
|
371
|
+
return true;
|
|
372
|
+
if (pos === cps.length)
|
|
373
|
+
return false;
|
|
374
|
+
const c = cps[pos];
|
|
375
|
+
const next = [];
|
|
376
|
+
gen++;
|
|
377
|
+
for (const pc of clist) {
|
|
378
|
+
const ins = p.prog[pc];
|
|
379
|
+
if (ins.items.some((s) => inSet(s, c)) !== ins.negate && add(next, pc + 1, pos + 1))
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
clist = next;
|
|
383
|
+
if (gen > 0xfffffff0) {
|
|
384
|
+
mark = new Uint32Array(n);
|
|
385
|
+
gen = 0;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
//# sourceMappingURL=pattern.js.map
|
package/lib/src/planner.d.ts
CHANGED
|
@@ -210,8 +210,16 @@ export type Operation = {
|
|
|
210
210
|
readonly list: string;
|
|
211
211
|
readonly base: string;
|
|
212
212
|
readonly link: Operand;
|
|
213
|
+
/** Without `each`: the feed's path and title. */
|
|
213
214
|
readonly dest: string;
|
|
214
215
|
readonly title: string;
|
|
216
|
+
/** `FOR as IN list BY key`: a feed per group, `dest` and `title` interpolated per group (`${as…}`). */
|
|
217
|
+
readonly each?: {
|
|
218
|
+
readonly as: string;
|
|
219
|
+
readonly by: string;
|
|
220
|
+
readonly dest: Operand;
|
|
221
|
+
readonly title?: Operand;
|
|
222
|
+
};
|
|
215
223
|
readonly overwrite: boolean;
|
|
216
224
|
readonly span: SourceSpan;
|
|
217
225
|
} | {
|
package/lib/src/planner.js
CHANGED
|
@@ -274,13 +274,15 @@ export function plan(program, buildArgs = {}, argSource = (n) => `--build-arg ${
|
|
|
274
274
|
break;
|
|
275
275
|
case 'FEED': {
|
|
276
276
|
const base = checkBaseUrl(evaluateOperand(ins.base, vars), 'FEED BASE', ins.base.span);
|
|
277
|
+
const each = ins.each && ins.dest ? { as: ins.each.as, by: ins.each.by.key, dest: ins.dest, ...(ins.title && { title: ins.title }) } : undefined;
|
|
277
278
|
operations().push({
|
|
278
279
|
kind: 'FEED',
|
|
279
280
|
list: ins.list,
|
|
280
281
|
base,
|
|
281
282
|
link: ins.link,
|
|
282
|
-
dest: fileDest(ins.dest, vars, '/feed.xml', 'FEED'),
|
|
283
|
-
title: ins.title ? evaluateOperand(ins.title, vars) : base,
|
|
283
|
+
dest: each ? '' : fileDest(ins.dest, vars, '/feed.xml', 'FEED'),
|
|
284
|
+
title: each ? base : ins.title ? evaluateOperand(ins.title, vars) : base,
|
|
285
|
+
...(each && { each }),
|
|
284
286
|
overwrite: ins.overwrite,
|
|
285
287
|
span: ins.span,
|
|
286
288
|
});
|
package/lib/src/render.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ export type TemplateNode = {
|
|
|
37
37
|
} | {
|
|
38
38
|
readonly kind: 'each';
|
|
39
39
|
readonly list: Ref;
|
|
40
|
+
/** `${#each list by key as group}`: the loop runs over `{ key, count, items }` groups (src/pages.ts). */
|
|
41
|
+
readonly by?: string;
|
|
40
42
|
readonly as: string;
|
|
41
43
|
readonly body: readonly TemplateNode[];
|
|
42
44
|
readonly empty: readonly TemplateNode[];
|
|
@@ -71,7 +73,8 @@ interface Ref {
|
|
|
71
73
|
* - `${name}` / `${name.seg.seg}` substitutes a value (segments may be array
|
|
72
74
|
* indexes); `${name|raw}` substitutes it without escaping.
|
|
73
75
|
* - `${#each list as item}…${else}…${/each}` repeats for each element of a list
|
|
74
|
-
* (the `${else}` part renders when the list is empty).
|
|
76
|
+
* (the `${else}` part renders when the list is empty). `${#each list by key as g}`
|
|
77
|
+
* repeats for each group `{ key, count, items }` of the list's elements by a field. A missing key such as
|
|
75
78
|
* `post.tags` counts as an empty list; an unknown top-level name is an error.
|
|
76
79
|
* - `${#if x}…${else}…${/if}` / `${#if !x}`: false, null, missing, '', 0 and
|
|
77
80
|
* empty lists are falsy.
|
package/lib/src/render.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SitefileError } from './errors.js';
|
|
2
|
+
import { groupBy } from './pages.js';
|
|
2
3
|
import { FILE_HELPERS, renderFile, VALUE_HELPERS } from './media.js';
|
|
3
4
|
const REF = String.raw `[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_-]+)*`;
|
|
4
5
|
const VAR_RE = new RegExp(String.raw `^(${REF})(?:\|(raw))?$`);
|
|
5
|
-
const EACH_RE = new RegExp(String.raw `^#each\s+(${REF})\s+as\s+([A-Za-z_][A-Za-z0-9_]*)\s*$`);
|
|
6
|
+
const EACH_RE = new RegExp(String.raw `^#each\s+(${REF})\s+(?:by\s+([A-Za-z_][A-Za-z0-9_]*)\s+)?as\s+([A-Za-z_][A-Za-z0-9_]*)\s*$`);
|
|
6
7
|
const IF_RE = new RegExp(String.raw `^#if\s+(!?)\s*(${REF})\s*$`);
|
|
7
8
|
const BLOCK_RE = /^(#each|#if|else|\/each|\/if)(?![A-Za-z0-9_.])/;
|
|
8
9
|
/**
|
|
@@ -11,7 +12,8 @@ const BLOCK_RE = /^(#each|#if|else|\/each|\/if)(?![A-Za-z0-9_.])/;
|
|
|
11
12
|
* - `${name}` / `${name.seg.seg}` substitutes a value (segments may be array
|
|
12
13
|
* indexes); `${name|raw}` substitutes it without escaping.
|
|
13
14
|
* - `${#each list as item}…${else}…${/each}` repeats for each element of a list
|
|
14
|
-
* (the `${else}` part renders when the list is empty).
|
|
15
|
+
* (the `${else}` part renders when the list is empty). `${#each list by key as g}`
|
|
16
|
+
* repeats for each group `{ key, count, items }` of the list's elements by a field. A missing key such as
|
|
15
17
|
* `post.tags` counts as an empty list; an unknown top-level name is an error.
|
|
16
18
|
* - `${#if x}…${else}…${/if}` / `${#if !x}`: false, null, missing, '', 0 and
|
|
17
19
|
* empty lists are falsy.
|
|
@@ -293,8 +295,8 @@ function blockToken(expr, span) {
|
|
|
293
295
|
if (word === '#each') {
|
|
294
296
|
const m = EACH_RE.exec(expr);
|
|
295
297
|
if (!m)
|
|
296
|
-
throw new SitefileError('E_TEMPLATE', `expected \${#each list as item}, got '\${${expr}}'`, span);
|
|
297
|
-
return { kind: 'open-each', list: ref(m[1], span),
|
|
298
|
+
throw new SitefileError('E_TEMPLATE', `expected \${#each list as item} or \${#each list by key as group}, got '\${${expr}}'`, span);
|
|
299
|
+
return { kind: 'open-each', list: ref(m[1], span), ...(m[2] !== undefined && { by: m[2] }), as: m[3], span };
|
|
298
300
|
}
|
|
299
301
|
if (word === '#if') {
|
|
300
302
|
const m = IF_RE.exec(expr);
|
|
@@ -342,7 +344,7 @@ function buildTree(tokens, file, end) {
|
|
|
342
344
|
}
|
|
343
345
|
const o = f.open;
|
|
344
346
|
const node = o.kind === 'open-each'
|
|
345
|
-
? { kind: 'each', list: o.list, as: o.as, body: f.first, empty: f.second ?? [], span: o.span }
|
|
347
|
+
? { kind: 'each', list: o.list, ...(o.by !== undefined && { by: o.by }), as: o.as, body: f.first, empty: f.second ?? [], span: o.span }
|
|
346
348
|
: { kind: 'if', cond: o.cond, negate: o.negate, then: f.first, otherwise: f.second ?? [], span: o.span };
|
|
347
349
|
target().push(node);
|
|
348
350
|
break;
|
|
@@ -449,10 +451,12 @@ export function renderTemplate(nodes, lookup, escape, values, ctx = {}) {
|
|
|
449
451
|
// A missing key (post.tags on a post without tags) is an empty list;
|
|
450
452
|
// an unknown top-level name is still an error, so typos are caught.
|
|
451
453
|
const found = valueOf(n.list, n.list.path.length > 0);
|
|
452
|
-
const
|
|
453
|
-
if (!Array.isArray(
|
|
454
|
+
const list = found === undefined || found === null ? [] : found;
|
|
455
|
+
if (!Array.isArray(list)) {
|
|
454
456
|
throw new SitefileError('E_TYPE', `${dotted(n.list)} is ${describe(found)}, not a list; \${#each} needs a list`, n.list.span);
|
|
455
457
|
}
|
|
458
|
+
// `by key`: one `{ key, count, items }` per distinct value, as RENDER … FOR … BY.
|
|
459
|
+
const items = n.by === undefined ? list : groupBy(list, n.by, `\${#each ${[n.list.name, ...n.list.path].join('.')} by ${n.by}}`, n.span);
|
|
456
460
|
if (items.length === 0) {
|
|
457
461
|
out += run(n.empty);
|
|
458
462
|
break;
|
package/lib/src/schema.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type SourceSpan } from './errors.js';
|
|
2
|
+
import { type Pattern } from './pattern.js';
|
|
2
3
|
export type SchemaType = 'string' | 'number' | 'boolean' | 'date' | 'list';
|
|
3
4
|
type ItemType = Exclude<SchemaType, 'list'>;
|
|
4
5
|
type EnumValue = string | number | boolean;
|
|
@@ -7,6 +8,8 @@ export interface SchemaRule {
|
|
|
7
8
|
readonly type?: SchemaType;
|
|
8
9
|
readonly items?: ItemType;
|
|
9
10
|
readonly enum?: readonly EnumValue[];
|
|
11
|
+
/** `pattern`: strings (or a list's items) must match it somewhere (src/pattern.ts). */
|
|
12
|
+
readonly pattern?: Pattern;
|
|
10
13
|
}
|
|
11
14
|
/** A compiled schema; `label` names its file in messages. */
|
|
12
15
|
export interface Schema {
|
package/lib/src/schema.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// title: string # a type name ...
|
|
8
8
|
// tags: { type: list, items: string }
|
|
9
9
|
// status: { enum: [draft, published] }
|
|
10
|
+
// slug: { pattern: '^[a-z0-9-]+$' } # a string matching a regular expression (src/pattern.ts)
|
|
10
11
|
//
|
|
11
12
|
// Types: string, number, boolean, date (YYYY-MM-DD or an RFC 3339 date-time, as FEED reads it) and list
|
|
12
13
|
// (`items` gives the type of its elements). `enum` lists the allowed strings, numbers or booleans (for a list,
|
|
@@ -14,6 +15,7 @@
|
|
|
14
15
|
// `boolean` the strings `true` and `false`, enum entries match their text, and an empty cell counts as missing. docs/LANGUAGE.md has the rules.
|
|
15
16
|
import { SitefileError } from './errors.js';
|
|
16
17
|
import { parseFeedDate } from './site.js';
|
|
18
|
+
import { compilePattern, PatternError, patternMatches } from './pattern.js';
|
|
17
19
|
const TYPES = ['string', 'number', 'boolean', 'date', 'list'];
|
|
18
20
|
const ITEM_TYPES = ['string', 'number', 'boolean', 'date'];
|
|
19
21
|
const NUMERIC = /^-?\d+(\.\d+)?([eE][+-]?\d+)?$/;
|
|
@@ -66,11 +68,11 @@ export function compileSchema(value, label, what, span) {
|
|
|
66
68
|
continue;
|
|
67
69
|
}
|
|
68
70
|
if (!isObject(rule))
|
|
69
|
-
fail(`${at} expected a type name or a mapping with type, items or
|
|
71
|
+
fail(`${at} expected a type name or a mapping with type, items, enum or pattern`);
|
|
70
72
|
const r = rule;
|
|
71
73
|
for (const k of Object.keys(r)) {
|
|
72
|
-
if (k !== 'type' && k !== 'items' && k !== 'enum')
|
|
73
|
-
fail(`${at} unknown key '${k}' (expected type, items or
|
|
74
|
+
if (k !== 'type' && k !== 'items' && k !== 'enum' && k !== 'pattern')
|
|
75
|
+
fail(`${at} unknown key '${k}' (expected type, items, enum or pattern)`);
|
|
74
76
|
}
|
|
75
77
|
const type = r['type'] !== undefined ? typeName(r['type'], TYPES, 'type') : undefined;
|
|
76
78
|
let items;
|
|
@@ -87,7 +89,21 @@ export function compileSchema(value, label, what, span) {
|
|
|
87
89
|
}
|
|
88
90
|
values = e;
|
|
89
91
|
}
|
|
90
|
-
|
|
92
|
+
let pattern;
|
|
93
|
+
if (r['pattern'] !== undefined) {
|
|
94
|
+
const src = r['pattern'];
|
|
95
|
+
if (typeof src !== 'string')
|
|
96
|
+
fail(`${at} pattern must be a string (a regular expression)`);
|
|
97
|
+
try {
|
|
98
|
+
pattern = compilePattern(src);
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
if (!(e instanceof PatternError))
|
|
102
|
+
throw e;
|
|
103
|
+
fail(`${at} pattern '${src}' is invalid ${e.message}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
properties.push({ key, ...(type ? { type } : {}), ...(items ? { items } : {}), ...(values ? { enum: values } : {}), ...(pattern ? { pattern } : {}) });
|
|
91
107
|
}
|
|
92
108
|
}
|
|
93
109
|
return { label, required, properties };
|
|
@@ -120,6 +136,12 @@ function hasType(v, t, csv) {
|
|
|
120
136
|
return Array.isArray(v);
|
|
121
137
|
}
|
|
122
138
|
}
|
|
139
|
+
/** Why a value fails a `pattern`, or undefined when it matches. Only strings can match. */
|
|
140
|
+
function patternMismatch(v, p) {
|
|
141
|
+
if (typeof v !== 'string')
|
|
142
|
+
return `it should be a string matching the pattern '${p.source}'`;
|
|
143
|
+
return patternMatches(p, v) ? undefined : `it should match the pattern '${p.source}'`;
|
|
144
|
+
}
|
|
123
145
|
function inEnum(v, values, csv) {
|
|
124
146
|
return values.some((e) => e === v || (csv && typeof v === 'string' && v === String(e)));
|
|
125
147
|
}
|
|
@@ -158,10 +180,21 @@ export function checkSchema(value, schema, what, csv, span) {
|
|
|
158
180
|
}
|
|
159
181
|
if (rule.enum !== undefined && !inEnum(item, rule.enum, csv))
|
|
160
182
|
fail(`has '${rule.key}' item ${j + 1} ${describe(item)}; it should be one of ${enumText(rule.enum)}`);
|
|
183
|
+
if (rule.pattern !== undefined) {
|
|
184
|
+
const m = patternMismatch(item, rule.pattern);
|
|
185
|
+
if (m)
|
|
186
|
+
fail(`has '${rule.key}' item ${j + 1} ${describe(item)}; ${m}`);
|
|
187
|
+
}
|
|
161
188
|
});
|
|
162
189
|
}
|
|
163
|
-
else
|
|
164
|
-
|
|
190
|
+
else {
|
|
191
|
+
if (rule.enum !== undefined && !inEnum(v, rule.enum, csv))
|
|
192
|
+
fail(`has '${rule.key}' ${describe(v)}; it should be one of ${enumText(rule.enum)}`);
|
|
193
|
+
if (rule.pattern !== undefined) {
|
|
194
|
+
const m = patternMismatch(v, rule.pattern);
|
|
195
|
+
if (m)
|
|
196
|
+
fail(`has '${rule.key}' ${describe(v)}; ${m}`);
|
|
197
|
+
}
|
|
165
198
|
}
|
|
166
199
|
}
|
|
167
200
|
});
|
package/lib/src/servers.d.ts
CHANGED
|
@@ -15,6 +15,21 @@ import type { AuthEntry } from './serve-auth.js';
|
|
|
15
15
|
* file alone: a server is `running` only when its pid is alive AND the port answers with
|
|
16
16
|
* this server's `x-sitefile-server` header.
|
|
17
17
|
*/
|
|
18
|
+
/** The build inputs of a `serve -d --watch` server on a build context (johnhenry/sitefile#306). */
|
|
19
|
+
export interface WatchBuild {
|
|
20
|
+
/** `-f`, absolute (absent: `<context>/Sitefile`). */
|
|
21
|
+
readonly sitefile?: string;
|
|
22
|
+
/** `--target`. */
|
|
23
|
+
readonly target?: string;
|
|
24
|
+
/** `--build-arg` flags, `NAME=value` (a bare `NAME` is resolved from the environment when the server is created). */
|
|
25
|
+
readonly buildArgs?: readonly string[];
|
|
26
|
+
/** `--build-arg-file` files, absolute (re-read on every build). */
|
|
27
|
+
readonly buildArgFiles?: readonly string[];
|
|
28
|
+
/** `--no-cache`, `--no-lockfile`, `--no-rewrite-base` (absent: false). */
|
|
29
|
+
readonly noCache?: boolean;
|
|
30
|
+
readonly noLockfile?: boolean;
|
|
31
|
+
readonly noRewriteBase?: boolean;
|
|
32
|
+
}
|
|
18
33
|
export interface ServerState {
|
|
19
34
|
readonly id: string;
|
|
20
35
|
readonly name: string;
|
|
@@ -93,6 +108,16 @@ export interface ServerState {
|
|
|
93
108
|
readonly pollMs?: number;
|
|
94
109
|
/** `--sync` with `-d --watch`: pages report where they are and follow each other (`scroll`: their scrolling too) (absent: off). */
|
|
95
110
|
readonly sync?: 'nav' | 'scroll';
|
|
111
|
+
/** `--reload` (with or without `--watch`): live-reload script and stream in this mode, so open pages reload after a restart (absent: only with `watch`, in morph mode). */
|
|
112
|
+
readonly reload?: 'full' | 'morph';
|
|
113
|
+
/** `-d --watch` on a build context (a Sitefile, or any build flag): build it in memory and rebuild on change (absent: a plain directory). */
|
|
114
|
+
readonly build?: WatchBuild;
|
|
115
|
+
/** `--no-css-inject`, `--console`, `--no-overlay`, `--navigate-to-changed`, `--keep-service-workers` with `-d --watch` (absent: false). */
|
|
116
|
+
readonly noCssInject?: boolean;
|
|
117
|
+
readonly console?: boolean;
|
|
118
|
+
readonly noOverlay?: boolean;
|
|
119
|
+
readonly navigateToChanged?: boolean;
|
|
120
|
+
readonly keepServiceWorkers?: boolean;
|
|
96
121
|
/** The requested bind address (port 0 = pick a free port). */
|
|
97
122
|
readonly bind: {
|
|
98
123
|
readonly host: string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sitefile version: `sitefile --version`, the help text and the builder version in build
|
|
3
|
+
* attestations. package.json is the source of truth (read at load time; this file is compiled to
|
|
4
|
+
* lib/src/version.js, two levels below it). The Rust port takes it from rust/Cargo.toml, which a
|
|
5
|
+
* test in each implementation keeps equal to package.json (johnhenry/sitefile#146).
|
|
6
|
+
*/
|
|
7
|
+
export declare const SITEFILE_VERSION: string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
/**
|
|
3
|
+
* The Sitefile version: `sitefile --version`, the help text and the builder version in build
|
|
4
|
+
* attestations. package.json is the source of truth (read at load time; this file is compiled to
|
|
5
|
+
* lib/src/version.js, two levels below it). The Rust port takes it from rust/Cargo.toml, which a
|
|
6
|
+
* test in each implementation keeps equal to package.json (johnhenry/sitefile#146).
|
|
7
|
+
*/
|
|
8
|
+
export const SITEFILE_VERSION = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version;
|
|
9
|
+
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/sitefile",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Docker-inspired static artifact builder: compose immutable file trees from local and remote sources, render templates, and export a deployable site. No container runtime, no shell, no runtime dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"static-site",
|