@johnhenry/sitefile 0.0.4 → 0.0.6
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 +71 -0
- package/README.md +47 -10
- 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 +3 -2
- package/lib/src/language/ast.d.ts +15 -3
- package/lib/src/language/parser.js +30 -7
- 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/precompress.d.ts +11 -2
- package/lib/src/precompress.js +36 -5
- 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/precompress.d.ts
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import type { SourceSpan } from './errors.js';
|
|
2
|
+
import type { PrecompressFormat } from './language/ast.js';
|
|
2
3
|
import { type Tree } from './tree.js';
|
|
3
4
|
/** Extensions (lowercase, after the last dot) of the file types PRECOMPRESS compresses: text, scripts, data, fonts without their own compression. */
|
|
4
5
|
export declare const COMPRESSIBLE: Set<string>;
|
|
5
6
|
/** Whether a path names a compressible file type (by its extension, ASCII case-insensitive). */
|
|
6
7
|
export declare function compressible(path: string): boolean;
|
|
8
|
+
/** The Brotli encoder version rust/vendor/brotli holds; br sidecars match the Rust port's only while Node bundles it. */
|
|
9
|
+
export declare const BROTLI_VERSION = "1.2.0";
|
|
10
|
+
/** `data` as Brotli: quality 11, window 22 (4 MiB), generic mode, one FINISH over the whole input (rust/src/brotli.rs). */
|
|
11
|
+
export declare function brotli(data: Uint8Array): Uint8Array;
|
|
12
|
+
/** The file a sidecar compresses: `path` without its `.gz` or `.br`. */
|
|
13
|
+
export declare function sidecarSource(path: string): string;
|
|
7
14
|
export interface PrecompressOp {
|
|
15
|
+
readonly formats: readonly PrecompressFormat[];
|
|
8
16
|
readonly patterns: readonly string[];
|
|
9
17
|
readonly min: number;
|
|
10
18
|
readonly overwrite: boolean;
|
|
11
19
|
readonly span: SourceSpan;
|
|
12
20
|
}
|
|
13
21
|
/**
|
|
14
|
-
* The tree with a `<path>.gz
|
|
15
|
-
*
|
|
22
|
+
* The tree with a sidecar per format (`<path>.gz`: gzip, fixed header; `<path>.br`: Brotli) for every file
|
|
23
|
+
* that matches a pattern, is compressible and has at least `min` bytes, when that format compresses it to
|
|
24
|
+
* fewer bytes than it has. Sidecars keep the
|
|
16
25
|
* file's mode and get the origin `origin`; one that would replace a different file is a conflict unless
|
|
17
26
|
* `overwrite`. Warns when no file matches the patterns.
|
|
18
27
|
*/
|
package/lib/src/precompress.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// PRECOMPRESS (docs/LANGUAGE.md, "Precompressed sidecars"): write `<file>.gz` and/or `<file>.br` next to
|
|
2
|
+
// compressible files, for hosts and servers that serve them in place of the file (nginx gzip_static,
|
|
3
|
+
// Caddy precompressed, sirv). Ported as rust/src/precompress.rs. gzip uses the shared deflate encoder
|
|
4
|
+
// (src/deflate.ts); br uses node:zlib's Brotli, and the Rust port compiles the same C encoder at the
|
|
5
|
+
// same version (rust/vendor/brotli, rust/src/brotli.rs) with the same settings, so the sidecars are
|
|
6
|
+
// byte-identical.
|
|
7
|
+
import { brotliCompressSync, constants } from 'node:zlib';
|
|
1
8
|
import { gzip } from './deflate.js';
|
|
2
9
|
import { globMatches } from './fingerprint.js';
|
|
3
10
|
import { makeFile } from './tree.js';
|
|
@@ -12,9 +19,30 @@ export function compressible(path) {
|
|
|
12
19
|
const dot = base.lastIndexOf('.');
|
|
13
20
|
return dot > 0 && COMPRESSIBLE.has(base.slice(dot + 1).toLowerCase());
|
|
14
21
|
}
|
|
22
|
+
/** The Brotli encoder version rust/vendor/brotli holds; br sidecars match the Rust port's only while Node bundles it. */
|
|
23
|
+
export const BROTLI_VERSION = '1.2.0';
|
|
24
|
+
/** `data` as Brotli: quality 11, window 22 (4 MiB), generic mode, one FINISH over the whole input (rust/src/brotli.rs). */
|
|
25
|
+
export function brotli(data) {
|
|
26
|
+
return new Uint8Array(brotliCompressSync(data, {
|
|
27
|
+
params: {
|
|
28
|
+
[constants.BROTLI_PARAM_QUALITY]: 11,
|
|
29
|
+
[constants.BROTLI_PARAM_LGWIN]: 22,
|
|
30
|
+
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_GENERIC,
|
|
31
|
+
},
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
const ENCODERS = {
|
|
35
|
+
gzip: { ext: '.gz', encode: gzip },
|
|
36
|
+
br: { ext: '.br', encode: brotli },
|
|
37
|
+
};
|
|
38
|
+
/** The file a sidecar compresses: `path` without its `.gz` or `.br`. */
|
|
39
|
+
export function sidecarSource(path) {
|
|
40
|
+
return path.endsWith('.gz') || path.endsWith('.br') ? path.slice(0, -3) : path;
|
|
41
|
+
}
|
|
15
42
|
/**
|
|
16
|
-
* The tree with a `<path>.gz
|
|
17
|
-
*
|
|
43
|
+
* The tree with a sidecar per format (`<path>.gz`: gzip, fixed header; `<path>.br`: Brotli) for every file
|
|
44
|
+
* that matches a pattern, is compressible and has at least `min` bytes, when that format compresses it to
|
|
45
|
+
* fewer bytes than it has. Sidecars keep the
|
|
18
46
|
* file's mode and get the origin `origin`; one that would replace a different file is a conflict unless
|
|
19
47
|
* `overwrite`. Warns when no file matches the patterns.
|
|
20
48
|
*/
|
|
@@ -26,9 +54,12 @@ export function precompress(tree, op, origin, warn) {
|
|
|
26
54
|
for (const [p, node] of matched) {
|
|
27
55
|
if (!compressible(p) || node.bytes.byteLength < op.min)
|
|
28
56
|
continue;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
57
|
+
for (const format of op.formats) {
|
|
58
|
+
const { ext, encode } = ENCODERS[format];
|
|
59
|
+
const out = encode(node.bytes);
|
|
60
|
+
if (out.byteLength < node.bytes.byteLength)
|
|
61
|
+
files.push([p + ext, makeFile(out, origin, node.mode)]);
|
|
62
|
+
}
|
|
32
63
|
}
|
|
33
64
|
return files.length ? tree.withFiles(files, op.overwrite ? 'overwrite' : 'error', op.span) : tree;
|
|
34
65
|
}
|
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 {
|