@podlite/schema 0.0.70 → 0.0.71
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.podlite +82 -0
- package/esm/ast-helpers.d.ts +11 -0
- package/esm/ast-helpers.js +277 -25
- package/esm/ast-helpers.js.map +1 -1
- package/esm/block-names.d.ts +2 -2
- package/esm/block-names.js +7 -0
- package/esm/block-names.js.map +1 -1
- package/esm/blocks-helpers.d.ts +1 -1
- package/esm/blocks-helpers.js +3 -1
- package/esm/blocks-helpers.js.map +1 -1
- package/esm/exportHtml.js +85 -28
- package/esm/exportHtml.js.map +1 -1
- package/esm/exportMarkdown.js +112 -27
- package/esm/exportMarkdown.js.map +1 -1
- package/esm/grammar.js +588 -454
- package/esm/grammar.js.map +1 -1
- package/esm/guard.js +11 -1
- package/esm/guard.js.map +1 -1
- package/esm/helpers/blockName.d.ts +8 -0
- package/esm/helpers/blockName.js +60 -0
- package/esm/helpers/blockName.js.map +1 -0
- package/esm/helpers/configPropagation.d.ts +12 -2
- package/esm/helpers/configPropagation.js +51 -13
- package/esm/helpers/configPropagation.js.map +1 -1
- package/esm/helpers/corePlugins.d.ts +1 -1
- package/esm/helpers/corePlugins.js +4 -2
- package/esm/helpers/corePlugins.js.map +1 -1
- package/esm/helpers/makeInterator.js +3 -2
- package/esm/helpers/makeInterator.js.map +1 -1
- package/esm/index.d.ts +13 -3
- package/esm/index.js +15 -5
- package/esm/index.js.map +1 -1
- package/esm/pluggableParser.d.ts +6 -2
- package/esm/pluggableParser.js +5 -5
- package/esm/pluggableParser.js.map +1 -1
- package/esm/plugin-data-table.js +4 -5
- package/esm/plugin-data-table.js.map +1 -1
- package/esm/plugin-formatting-codes.js +65 -24
- package/esm/plugin-formatting-codes.js.map +1 -1
- package/esm/plugin-set.js +15 -18
- package/esm/plugin-set.js.map +1 -1
- package/esm/plugin-tables.js +6 -1
- package/esm/plugin-tables.js.map +1 -1
- package/esm/selectors.d.ts +25 -8
- package/esm/selectors.js +398 -141
- package/esm/selectors.js.map +1 -1
- package/esm/set-assign.d.ts +17 -0
- package/esm/set-assign.js +143 -0
- package/esm/set-assign.js.map +1 -0
- package/esm/test-display.d.ts +3 -0
- package/esm/test-display.js +58 -0
- package/esm/test-display.js.map +1 -0
- package/esm/toc-caption.d.ts +7 -0
- package/esm/toc-caption.js +23 -0
- package/esm/toc-caption.js.map +1 -0
- package/esm/types.d.ts +16 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/ast-helpers.d.ts +11 -0
- package/lib/ast-helpers.js +279 -26
- package/lib/block-names.d.ts +2 -2
- package/lib/block-names.js +7 -0
- package/lib/blocks-helpers.d.ts +1 -1
- package/lib/blocks-helpers.js +3 -1
- package/lib/exportHtml.js +84 -27
- package/lib/exportMarkdown.js +111 -26
- package/lib/grammar.js +588 -454
- package/lib/guard.js +11 -1
- package/lib/helpers/blockName.d.ts +8 -0
- package/lib/helpers/blockName.js +66 -0
- package/lib/helpers/configPropagation.d.ts +12 -2
- package/lib/helpers/configPropagation.js +54 -14
- package/lib/helpers/corePlugins.d.ts +1 -1
- package/lib/helpers/corePlugins.js +4 -2
- package/lib/helpers/makeInterator.js +3 -2
- package/lib/index.d.ts +13 -3
- package/lib/index.js +30 -5
- package/lib/pluggableParser.d.ts +6 -2
- package/lib/pluggableParser.js +5 -5
- package/lib/plugin-data-table.js +4 -5
- package/lib/plugin-formatting-codes.js +65 -24
- package/lib/plugin-set.js +15 -18
- package/lib/plugin-tables.js +6 -1
- package/lib/selectors.d.ts +25 -8
- package/lib/selectors.js +401 -141
- package/lib/set-assign.d.ts +17 -0
- package/lib/set-assign.js +150 -0
- package/lib/test-display.d.ts +3 -0
- package/lib/test-display.js +67 -0
- package/lib/toc-caption.d.ts +7 -0
- package/lib/toc-caption.js +27 -0
- package/lib/types.d.ts +16 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/schema/AstTree.json +160 -0
- package/schema/PodliteDocument.json +160 -0
package/esm/selectors.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bindTarget, buildBindingIndex, getFromTree, getTextContentFromNode, makeAttrs, } from './index.js';
|
|
2
2
|
import { parseAttributes } from './helpers/parseAttributes.js';
|
|
3
|
-
|
|
3
|
+
import { blockNameOf, holderInside } from './helpers/blockName.js';
|
|
4
|
+
import { extractDataText, findDataBlockByKey, parseCsv, parseMimeType, parseTsv } from './plugin-tables.js';
|
|
4
5
|
const isIdentStart = (c) => /[a-zA-Z_]/.test(c);
|
|
5
6
|
const isIdentCont = (c) => /[a-zA-Z0-9_-]/.test(c);
|
|
7
|
+
// A value is read by the same grammar as a declaration: the delimiters around
|
|
8
|
+
// it decide whether it is a string, a list or a number, so an operator means
|
|
9
|
+
// the same thing in a predicate as on a block.
|
|
10
|
+
const readValue = (src) => {
|
|
11
|
+
const items = parseAttributes(src);
|
|
12
|
+
return items.length === 1 && items[0].name === 'x' ? { value: items[0].value, type: items[0].type } : undefined;
|
|
13
|
+
};
|
|
14
|
+
const readOperand = (raw) => readValue(`:x<${raw}>`);
|
|
15
|
+
const valuesOf = (typed) => (Array.isArray(typed.value) ? typed.value : [typed.value]);
|
|
6
16
|
// Read an angle-bracketed value starting at `<`. Returns the inner content
|
|
7
17
|
// (without delimiters) and the index right after the closing `>`.
|
|
8
18
|
// Nested `<>` pairs are matched recursively.
|
|
@@ -23,6 +33,120 @@ const readAngleValue = (s, start) => {
|
|
|
23
33
|
}
|
|
24
34
|
return undefined;
|
|
25
35
|
};
|
|
36
|
+
const QUOTES = { "'": "'", '"': '"', '「': '」' };
|
|
37
|
+
// Read a parenthesised value starting at `(`. A quotation mark opens a string
|
|
38
|
+
// only where an element begins, so an apostrophe inside a word stays a letter.
|
|
39
|
+
const readParenValue = (s, start) => {
|
|
40
|
+
if (s[start] !== '(')
|
|
41
|
+
return undefined;
|
|
42
|
+
let depth = 1;
|
|
43
|
+
let prev = '(';
|
|
44
|
+
let i = start + 1;
|
|
45
|
+
while (i < s.length) {
|
|
46
|
+
const c = s[i];
|
|
47
|
+
const close = QUOTES[c];
|
|
48
|
+
if (close && (prev === '(' || prev === ',' || /\s/.test(prev))) {
|
|
49
|
+
const at = s.indexOf(close, i + 1);
|
|
50
|
+
if (at === -1)
|
|
51
|
+
return undefined;
|
|
52
|
+
i = at + 1;
|
|
53
|
+
prev = close;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (c === '(')
|
|
57
|
+
depth++;
|
|
58
|
+
else if (c === ')') {
|
|
59
|
+
depth--;
|
|
60
|
+
if (depth === 0)
|
|
61
|
+
return { value: s.slice(start + 1, i), end: i + 1 };
|
|
62
|
+
}
|
|
63
|
+
prev = c;
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
};
|
|
68
|
+
// The index right after the `]` that closes the predicate opened at `start`.
|
|
69
|
+
// Brackets, parentheses and strings inside a value do not close it.
|
|
70
|
+
const skipPredicate = (s, start) => {
|
|
71
|
+
let i = start + 1;
|
|
72
|
+
while (i < s.length) {
|
|
73
|
+
const c = s[i];
|
|
74
|
+
if (c === ']')
|
|
75
|
+
return i + 1;
|
|
76
|
+
const read = c === '<' ? readAngleValue(s, i) : c === '(' ? readParenValue(s, i) : undefined;
|
|
77
|
+
if ((c === '<' || c === '(') && !read)
|
|
78
|
+
return undefined;
|
|
79
|
+
i = read ? read.end : i + 1;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
};
|
|
83
|
+
// Split a pattern list at a separator that stands outside every predicate.
|
|
84
|
+
const splitOutsidePredicates = (s, separator) => {
|
|
85
|
+
const chunks = [];
|
|
86
|
+
let from = 0;
|
|
87
|
+
let i = 0;
|
|
88
|
+
while (i < s.length) {
|
|
89
|
+
if (s[i] === '[') {
|
|
90
|
+
const end = skipPredicate(s, i);
|
|
91
|
+
if (end === undefined)
|
|
92
|
+
return undefined;
|
|
93
|
+
i = end;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (s[i] === separator) {
|
|
97
|
+
chunks.push(s.slice(from, i));
|
|
98
|
+
from = i + 1;
|
|
99
|
+
}
|
|
100
|
+
i++;
|
|
101
|
+
}
|
|
102
|
+
chunks.push(s.slice(from));
|
|
103
|
+
return chunks;
|
|
104
|
+
};
|
|
105
|
+
// Literal operands of `in`: numbers and non-empty strings, separated by commas
|
|
106
|
+
// or, in angle brackets, by whitespace.
|
|
107
|
+
const readLiterals = (operands) => {
|
|
108
|
+
if (!operands)
|
|
109
|
+
return undefined;
|
|
110
|
+
const angle = operands.startsWith('<') ? readAngleValue(operands, 0) : undefined;
|
|
111
|
+
if (angle && angle.end !== operands.length)
|
|
112
|
+
return undefined;
|
|
113
|
+
const typed = angle ? readOperand(angle.value) : readValue(`:x(${operands})`);
|
|
114
|
+
if (!typed)
|
|
115
|
+
return undefined;
|
|
116
|
+
const values = valuesOf(typed);
|
|
117
|
+
const literal = (v) => typeof v === 'number' || (typeof v === 'string' && v !== '');
|
|
118
|
+
return values.length > 0 && values.every(literal) ? typed : undefined;
|
|
119
|
+
};
|
|
120
|
+
const OPERAND_SCHEMES = new Set(['file', 'doc', 'data']);
|
|
121
|
+
const readInOperand = (operands) => {
|
|
122
|
+
if (!operands)
|
|
123
|
+
return undefined;
|
|
124
|
+
const angle = operands.startsWith('<') ? readAngleValue(operands, 0) : undefined;
|
|
125
|
+
const read = angle ? readOperand(angle.value) : readValue(`:x(${operands})`);
|
|
126
|
+
if (read || angle) {
|
|
127
|
+
const literal = readLiterals(operands);
|
|
128
|
+
return literal ? { literal } : undefined;
|
|
129
|
+
}
|
|
130
|
+
const selector = parseSelector(operands);
|
|
131
|
+
if (!selector || (selector.scheme && !OPERAND_SCHEMES.has(selector.scheme)))
|
|
132
|
+
return undefined;
|
|
133
|
+
if (selector.scheme === 'data' && selector.patterns.length > 0)
|
|
134
|
+
return undefined;
|
|
135
|
+
return { selector };
|
|
136
|
+
};
|
|
137
|
+
// What follows the opening parenthesis is a value when the declaration grammar
|
|
138
|
+
// reads it as one, and otherwise the name of an operation.
|
|
139
|
+
const parenCondition = (modifier, attrName, inner) => {
|
|
140
|
+
if (readValue(`:x(${inner})`))
|
|
141
|
+
return { modifier, attrName, valueSpec: { kind: 'paren', value: inner } };
|
|
142
|
+
const operation = inner.match(/^\s*([a-zA-Z_][a-zA-Z0-9_-]*)(?:\s+([\s\S]*))?$/);
|
|
143
|
+
if (!operation || operation[1] !== 'in' || modifier)
|
|
144
|
+
return undefined;
|
|
145
|
+
const operands = (operation[2] ?? '').trim();
|
|
146
|
+
if (!readInOperand(operands))
|
|
147
|
+
return undefined;
|
|
148
|
+
return { attrName, valueSpec: { kind: 'in', value: operands } };
|
|
149
|
+
};
|
|
26
150
|
const parseCondition = (raw) => {
|
|
27
151
|
const s = raw.trim();
|
|
28
152
|
if (!s.startsWith(':'))
|
|
@@ -53,6 +177,10 @@ const parseCondition = (raw) => {
|
|
|
53
177
|
const read = readAngleValue(s, i + 1);
|
|
54
178
|
if (!read || read.end !== s.length)
|
|
55
179
|
return undefined;
|
|
180
|
+
// an empty list occurs among any values, so the condition would test nothing
|
|
181
|
+
const operand = readOperand(read.value);
|
|
182
|
+
if (!operand || valuesOf(operand).length === 0)
|
|
183
|
+
return undefined;
|
|
56
184
|
return { modifier, attrName, valueSpec: { kind: 'contains', value: read.value } };
|
|
57
185
|
}
|
|
58
186
|
if (s[i] === '<') {
|
|
@@ -61,35 +189,39 @@ const parseCondition = (raw) => {
|
|
|
61
189
|
return undefined;
|
|
62
190
|
return { modifier, attrName, valueSpec: { kind: 'angle', value: read.value } };
|
|
63
191
|
}
|
|
192
|
+
if (s[i] === '(') {
|
|
193
|
+
const read = readParenValue(s, i);
|
|
194
|
+
if (!read || read.end !== s.length)
|
|
195
|
+
return undefined;
|
|
196
|
+
return parenCondition(modifier, attrName, read.value);
|
|
197
|
+
}
|
|
64
198
|
return undefined;
|
|
65
199
|
};
|
|
66
|
-
// Split predicate body
|
|
200
|
+
// Split a predicate body into conditions at whitespace outside any value.
|
|
67
201
|
const splitConditions = (body) => {
|
|
68
202
|
const chunks = [];
|
|
69
|
-
let
|
|
70
|
-
let
|
|
71
|
-
|
|
203
|
+
let from = 0;
|
|
204
|
+
let i = 0;
|
|
205
|
+
while (i < body.length) {
|
|
72
206
|
const c = body[i];
|
|
73
|
-
if (c === '<')
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (depth === 0)
|
|
207
|
+
if (c === '<' || c === '(') {
|
|
208
|
+
const read = c === '<' ? readAngleValue(body, i) : readParenValue(body, i);
|
|
209
|
+
if (!read)
|
|
77
210
|
return undefined;
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
if (depth === 0 && /\s/.test(c)) {
|
|
81
|
-
if (buf) {
|
|
82
|
-
chunks.push(buf);
|
|
83
|
-
buf = '';
|
|
84
|
-
}
|
|
211
|
+
i = read.end;
|
|
85
212
|
continue;
|
|
86
213
|
}
|
|
87
|
-
|
|
214
|
+
if (c === '>')
|
|
215
|
+
return undefined;
|
|
216
|
+
if (/\s/.test(c)) {
|
|
217
|
+
if (i > from)
|
|
218
|
+
chunks.push(body.slice(from, i));
|
|
219
|
+
from = i + 1;
|
|
220
|
+
}
|
|
221
|
+
i++;
|
|
88
222
|
}
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
if (buf)
|
|
92
|
-
chunks.push(buf);
|
|
223
|
+
if (from < body.length)
|
|
224
|
+
chunks.push(body.slice(from));
|
|
93
225
|
return chunks;
|
|
94
226
|
};
|
|
95
227
|
const parsePredicate = (body) => {
|
|
@@ -130,7 +262,7 @@ const parsePattern = (raw) => {
|
|
|
130
262
|
i++;
|
|
131
263
|
if (i >= s.length)
|
|
132
264
|
return { blockType };
|
|
133
|
-
if (s[i] !== '[' || s
|
|
265
|
+
if (s[i] !== '[' || skipPredicate(s, i) !== s.length)
|
|
134
266
|
return undefined;
|
|
135
267
|
const body = s.slice(i + 1, s.length - 1).trim();
|
|
136
268
|
if (!body)
|
|
@@ -140,44 +272,10 @@ const parsePattern = (raw) => {
|
|
|
140
272
|
return undefined;
|
|
141
273
|
return { blockType, predicate };
|
|
142
274
|
};
|
|
143
|
-
// Split pattern-list by comma at top level, respecting [...] and <...> nesting.
|
|
144
|
-
const splitPatterns = (s) => {
|
|
145
|
-
const chunks = [];
|
|
146
|
-
let bracketDepth = 0;
|
|
147
|
-
let angleDepth = 0;
|
|
148
|
-
let buf = '';
|
|
149
|
-
for (let i = 0; i < s.length; i++) {
|
|
150
|
-
const c = s[i];
|
|
151
|
-
if (c === '[')
|
|
152
|
-
bracketDepth++;
|
|
153
|
-
else if (c === ']') {
|
|
154
|
-
if (bracketDepth === 0)
|
|
155
|
-
return undefined;
|
|
156
|
-
bracketDepth--;
|
|
157
|
-
}
|
|
158
|
-
else if (c === '<')
|
|
159
|
-
angleDepth++;
|
|
160
|
-
else if (c === '>') {
|
|
161
|
-
if (angleDepth === 0)
|
|
162
|
-
return undefined;
|
|
163
|
-
angleDepth--;
|
|
164
|
-
}
|
|
165
|
-
if (c === ',' && bracketDepth === 0 && angleDepth === 0) {
|
|
166
|
-
chunks.push(buf);
|
|
167
|
-
buf = '';
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
buf += c;
|
|
171
|
-
}
|
|
172
|
-
if (bracketDepth !== 0 || angleDepth !== 0)
|
|
173
|
-
return undefined;
|
|
174
|
-
chunks.push(buf);
|
|
175
|
-
return chunks;
|
|
176
|
-
};
|
|
177
275
|
const parsePatternList = (filterPart) => {
|
|
178
276
|
if (!filterPart)
|
|
179
277
|
return [];
|
|
180
|
-
const chunks =
|
|
278
|
+
const chunks = splitOutsidePredicates(filterPart, ',');
|
|
181
279
|
if (!chunks)
|
|
182
280
|
return undefined;
|
|
183
281
|
const patterns = [];
|
|
@@ -196,8 +294,12 @@ export const parseSelector = (selector) => {
|
|
|
196
294
|
const trimmed = selector.trim();
|
|
197
295
|
if (!trimmed)
|
|
198
296
|
return undefined;
|
|
199
|
-
//
|
|
200
|
-
|
|
297
|
+
// The first '|' separates the source from the pattern list. A source named
|
|
298
|
+
// by a scheme is literal text up to the bar; elsewhere a bar inside a
|
|
299
|
+
// predicate belongs to its value.
|
|
300
|
+
const hasScheme = /^[a-zA-Z][a-zA-Z0-9-]*:/.test(trimmed);
|
|
301
|
+
const parts = hasScheme ? undefined : splitOutsidePredicates(trimmed, '|');
|
|
302
|
+
const pipeIdx = hasScheme ? trimmed.indexOf('|') : parts && parts.length > 1 ? parts[0].length : -1;
|
|
201
303
|
const sourcePart = (pipeIdx === -1 ? trimmed : trimmed.slice(0, pipeIdx)).trim();
|
|
202
304
|
const filterPart = pipeIdx === -1 ? '' : trimmed.slice(pipeIdx + 1).trim();
|
|
203
305
|
const patterns = parsePatternList(filterPart);
|
|
@@ -214,6 +316,20 @@ export const parseSelector = (selector) => {
|
|
|
214
316
|
patterns,
|
|
215
317
|
};
|
|
216
318
|
}
|
|
319
|
+
// Without a scheme the bar is what makes the text before it a source, and
|
|
320
|
+
// such a source is a file
|
|
321
|
+
if (pipeIdx !== -1 && sourcePart) {
|
|
322
|
+
const hash = sourcePart.indexOf('#');
|
|
323
|
+
const document = (hash === -1 ? sourcePart : sourcePart.slice(0, hash)).trim();
|
|
324
|
+
if (!document)
|
|
325
|
+
return undefined;
|
|
326
|
+
return {
|
|
327
|
+
scheme: 'file',
|
|
328
|
+
document,
|
|
329
|
+
anchor: hash === -1 ? undefined : sourcePart.slice(hash + 1) || undefined,
|
|
330
|
+
patterns,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
217
333
|
if (patterns.length > 0) {
|
|
218
334
|
return { patterns };
|
|
219
335
|
}
|
|
@@ -226,26 +342,21 @@ export const parseSelector = (selector) => {
|
|
|
226
342
|
}
|
|
227
343
|
return undefined;
|
|
228
344
|
};
|
|
229
|
-
//
|
|
230
|
-
// around it decide whether it is a string, a list or a number. Reading it as
|
|
231
|
-
// raw text would put values out of reach that a document can hold — a quoted
|
|
232
|
-
// string among them.
|
|
233
|
-
const readOperand = (raw) => {
|
|
234
|
-
const [item] = parseAttributes(`:x<${raw}>`);
|
|
235
|
-
return item ? { value: item.value, type: item.type } : undefined;
|
|
236
|
-
};
|
|
345
|
+
// --- predicate matcher --------------------------------------------------
|
|
237
346
|
// The declared value with its kind kept. makeAttrs flattens a list into the
|
|
238
|
-
// surrounding values, which loses the very thing equality compares.
|
|
239
|
-
|
|
347
|
+
// surrounding values, which loses the very thing equality compares. What a
|
|
348
|
+
// =config supplies is already on the block: the parser puts it there, within
|
|
349
|
+
// the block and the file the =config is written in.
|
|
350
|
+
const declaredValue = (node, name) => {
|
|
240
351
|
const anyNode = node;
|
|
241
352
|
const own = (Array.isArray(anyNode.config) ? anyNode.config : []).find(c => c && c.name === name);
|
|
242
|
-
|
|
243
|
-
return { value: own.value, type: own.type };
|
|
244
|
-
const configured = (anyNode.name && ctx.config?.[anyNode.name]) || [];
|
|
245
|
-
const inherited = configured.find(c => c && c.name === name);
|
|
246
|
-
return inherited ? { value: inherited.value, type: inherited.type } : undefined;
|
|
353
|
+
return own ? { value: own.value, type: own.type } : undefined;
|
|
247
354
|
};
|
|
248
355
|
const sameScalar = (a, b) => typeof a === typeof b && a === b;
|
|
356
|
+
const isEmptyOrFalse = (value) => value === false ||
|
|
357
|
+
value === '' ||
|
|
358
|
+
(Array.isArray(value) && value.length === 0) ||
|
|
359
|
+
(typeof value === 'object' && value !== null && !Array.isArray(value) && Object.keys(value).length === 0);
|
|
249
360
|
// A list equals another list when their elements match one by one, in order; a
|
|
250
361
|
// value of one kind never equals a value of another.
|
|
251
362
|
const sameValue = (a, b) => {
|
|
@@ -256,33 +367,40 @@ const sameValue = (a, b) => {
|
|
|
256
367
|
}
|
|
257
368
|
return sameScalar(a.value, b.value);
|
|
258
369
|
};
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const valuesOf = (typed) => (Array.isArray(typed.value) ? typed.value : [typed.value]);
|
|
262
|
-
const matchCondition = (node, cond, ctx) => {
|
|
263
|
-
const attrs = makeAttrs(node, ctx);
|
|
370
|
+
const matchCondition = (node, cond, operands) => {
|
|
371
|
+
const attrs = makeAttrs(node, {});
|
|
264
372
|
const exists = attrs.exists(cond.attrName);
|
|
265
373
|
if (!cond.valueSpec) {
|
|
374
|
+
// judged by the value as declared: a list is empty or not as a whole, and a
|
|
375
|
+
// number is never false
|
|
376
|
+
const held = declaredValue(node, cond.attrName)?.value;
|
|
266
377
|
switch (cond.modifier) {
|
|
267
378
|
case undefined:
|
|
268
|
-
return exists &&
|
|
379
|
+
return exists && !isEmptyOrFalse(held);
|
|
269
380
|
case '!':
|
|
270
|
-
return exists &&
|
|
381
|
+
return exists && held === false;
|
|
271
382
|
case '?':
|
|
272
383
|
return exists;
|
|
273
384
|
case '!?':
|
|
274
385
|
return !exists;
|
|
275
386
|
}
|
|
276
387
|
}
|
|
277
|
-
const declared = declaredValue(node, cond.attrName
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
388
|
+
const declared = declaredValue(node, cond.attrName);
|
|
389
|
+
const { kind, value } = cond.valueSpec;
|
|
390
|
+
if (kind === 'in') {
|
|
391
|
+
const wanted = operands.get(cond) ?? [];
|
|
392
|
+
return exists && declared !== undefined && valuesOf(declared).some(held => wanted.some(w => sameScalar(held, w)));
|
|
393
|
+
}
|
|
394
|
+
const operand = kind === 'paren' ? readValue(`:x(${value})`) : readOperand(value);
|
|
395
|
+
if (kind === 'angle' || kind === 'paren') {
|
|
280
396
|
if (!exists || !declared || !operand)
|
|
281
397
|
return false;
|
|
282
398
|
const equal = sameValue(declared, operand);
|
|
283
399
|
return cond.modifier === '!' ? !equal : equal;
|
|
284
400
|
}
|
|
285
|
-
|
|
401
|
+
// Membership runs over the values as declared. A string is one value, even
|
|
402
|
+
// when it holds spaces, so a word taken from its middle is not a member.
|
|
403
|
+
if (kind === 'contains') {
|
|
286
404
|
if (!exists || !declared || !operand)
|
|
287
405
|
return false;
|
|
288
406
|
const values = valuesOf(declared);
|
|
@@ -292,56 +410,74 @@ const matchCondition = (node, cond, ctx) => {
|
|
|
292
410
|
return false;
|
|
293
411
|
};
|
|
294
412
|
// Replicate name/level handling from getFromTree for backward compat with
|
|
295
|
-
// 'head1' / 'item' style block-types.
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
return true;
|
|
413
|
+
// 'head1' / 'item' style block-types. `*` finds any block a name would find,
|
|
414
|
+
// written with a directive or without.
|
|
415
|
+
const blockTypeMatches = (node, name, blockType) => {
|
|
299
416
|
const anyNode = node;
|
|
300
|
-
if (
|
|
417
|
+
if (blockType === '*')
|
|
301
418
|
return true;
|
|
302
419
|
const m = blockType.match(/^(head|item)(\d+)?$/);
|
|
303
|
-
if (m)
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return
|
|
312
|
-
|
|
313
|
-
return
|
|
420
|
+
if (!m)
|
|
421
|
+
return name === blockType;
|
|
422
|
+
const [, baseName, levelStr] = m;
|
|
423
|
+
if (name !== baseName)
|
|
424
|
+
return false;
|
|
425
|
+
// =item is =item1; a bare head is a heading of any level
|
|
426
|
+
const expectedLevel = levelStr ? parseInt(levelStr, 10) : baseName === 'item' ? 1 : undefined;
|
|
427
|
+
if (expectedLevel === undefined)
|
|
428
|
+
return true;
|
|
429
|
+
// Heading plugin stores level as the regex capture string; coerce.
|
|
430
|
+
return Number(anyNode.level) === expectedLevel;
|
|
314
431
|
};
|
|
315
|
-
const matchesPattern = (node, pattern,
|
|
316
|
-
if (!blockTypeMatches(node, pattern.blockType))
|
|
432
|
+
const matchesPattern = (node, name, pattern, operands) => {
|
|
433
|
+
if (!blockTypeMatches(node, name, pattern.blockType))
|
|
317
434
|
return false;
|
|
318
435
|
if (!pattern.predicate)
|
|
319
436
|
return true;
|
|
320
|
-
return pattern.predicate.every(c => matchCondition(node, c,
|
|
437
|
+
return pattern.predicate.every(c => matchCondition(node, c, operands));
|
|
321
438
|
};
|
|
322
|
-
// Walk one document in source order
|
|
323
|
-
//
|
|
324
|
-
|
|
325
|
-
const collectMatches = (node, patterns, config, seen, out) => {
|
|
439
|
+
// Walk one document in source order. A block is matched against the
|
|
440
|
+
// configuration it carries; a =config met on the way is not applied again.
|
|
441
|
+
const collectMatches = (node, holder, patterns, seen, out, operands) => {
|
|
326
442
|
if (Array.isArray(node)) {
|
|
327
443
|
for (const child of node)
|
|
328
|
-
collectMatches(child,
|
|
444
|
+
collectMatches(child, holder, patterns, seen, out, operands);
|
|
329
445
|
return;
|
|
330
446
|
}
|
|
331
447
|
if (!node || typeof node !== 'object')
|
|
332
448
|
return;
|
|
333
449
|
const anyNode = node;
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
else if (anyNode.type === 'block' && !seen.has(node) && patterns.some(p => matchesPattern(node, p, { config }))) {
|
|
450
|
+
const name = blockNameOf(anyNode, holder);
|
|
451
|
+
if (name !== undefined && !seen.has(node) && patterns.some(p => matchesPattern(node, name, p, operands))) {
|
|
338
452
|
out.push(node);
|
|
339
453
|
seen.add(node);
|
|
340
454
|
}
|
|
341
455
|
if (anyNode.content !== undefined) {
|
|
342
|
-
collectMatches(anyNode.content,
|
|
456
|
+
collectMatches(anyNode.content, holderInside(anyNode, holder), patterns, seen, out, operands);
|
|
343
457
|
}
|
|
344
458
|
};
|
|
459
|
+
// The found blocks that no other found block holds, in the order given. A
|
|
460
|
+
// found block brings its content with it, so one that lies inside another
|
|
461
|
+
// would be placed twice. A Markdown section holds its blocks in one node, not
|
|
462
|
+
// in a list.
|
|
463
|
+
export const outermost = (blocks) => {
|
|
464
|
+
const found = new Set(blocks);
|
|
465
|
+
const held = new Set();
|
|
466
|
+
const mark = (node) => {
|
|
467
|
+
if (Array.isArray(node))
|
|
468
|
+
return node.forEach(mark);
|
|
469
|
+
if (!node || typeof node !== 'object')
|
|
470
|
+
return;
|
|
471
|
+
if (found.has(node))
|
|
472
|
+
held.add(node);
|
|
473
|
+
mark(node.content);
|
|
474
|
+
};
|
|
475
|
+
// a block already held was walked with the block that holds it
|
|
476
|
+
for (const block of blocks)
|
|
477
|
+
if (!held.has(block))
|
|
478
|
+
mark(block.content);
|
|
479
|
+
return blocks.filter(block => !held.has(block));
|
|
480
|
+
};
|
|
345
481
|
// Normalize a path for loose suffix comparison:
|
|
346
482
|
// 'src/foo.podlite' ~= 'foo.podlite'
|
|
347
483
|
// './includes/x.podlite' ~= 'includes/x.podlite'
|
|
@@ -407,7 +543,17 @@ export const filePathMatches = (docFile, target) => {
|
|
|
407
543
|
}
|
|
408
544
|
return a === b || a.endsWith('/' + b) || b.endsWith('/' + a);
|
|
409
545
|
};
|
|
410
|
-
|
|
546
|
+
/*
|
|
547
|
+
=begin pod :kind<export>
|
|
548
|
+
|
|
549
|
+
=head2 getDocIDs
|
|
550
|
+
|
|
551
|
+
The names a document answers to in a C<doc:> source: the C<:id> of its C<=NAME>
|
|
552
|
+
and C<=TITLE> blocks and the text of those blocks.
|
|
553
|
+
|
|
554
|
+
=end pod
|
|
555
|
+
*/
|
|
556
|
+
export const getDocIDs = (doc) => {
|
|
411
557
|
const ids = [];
|
|
412
558
|
getFromTree(doc.node, 'NAME', 'TITLE').forEach(block => {
|
|
413
559
|
const conf = makeAttrs(block, {});
|
|
@@ -421,16 +567,125 @@ const getDocIDs = (doc) => {
|
|
|
421
567
|
});
|
|
422
568
|
return ids;
|
|
423
569
|
};
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
570
|
+
/*
|
|
571
|
+
=begin pod :kind<export>
|
|
572
|
+
|
|
573
|
+
=head2 SelectorError
|
|
574
|
+
|
|
575
|
+
Thrown by C<runSelector> when a selector, or a selector given as the operand of
|
|
576
|
+
C<in>, cannot be read: its source does not resolve (C<resolution>), the format of
|
|
577
|
+
the source is not known (C<format>), or the address is not in it (C<address>).
|
|
578
|
+
Such a selection neither matches nor fails to match, so it returns nothing at all.
|
|
579
|
+
A mask that no document answers is an empty source, not an error, unless it
|
|
580
|
+
carries an address.
|
|
581
|
+
|
|
582
|
+
=end pod
|
|
583
|
+
*/
|
|
584
|
+
export class SelectorError extends Error {
|
|
585
|
+
kind;
|
|
586
|
+
constructor(kind, message) {
|
|
587
|
+
super(message);
|
|
588
|
+
this.name = 'SelectorError';
|
|
589
|
+
this.kind = kind;
|
|
590
|
+
}
|
|
432
591
|
}
|
|
433
|
-
|
|
592
|
+
const termOf = (block) => {
|
|
593
|
+
const anyBlock = block;
|
|
594
|
+
if (anyBlock.type !== 'block' || anyBlock.name !== 'defn' || !Array.isArray(anyBlock.content))
|
|
595
|
+
return [];
|
|
596
|
+
const term = anyBlock.content.find(n => n && n.type === 'para' && n.name === 'term');
|
|
597
|
+
return term ? [getTextContentFromNode(term).trim()] : [];
|
|
598
|
+
};
|
|
599
|
+
// A column of a =data block: named by its heading when the block declares one,
|
|
600
|
+
// the only column otherwise. A heading is not a value.
|
|
601
|
+
const dataValues = (key, address, home) => {
|
|
602
|
+
const source = `data:${key}${address ? `#${address}` : ''}`;
|
|
603
|
+
const block = home.map(d => findDataBlockByKey(d.node, key)).find(Boolean);
|
|
604
|
+
if (!block)
|
|
605
|
+
throw new SelectorError('resolution', `no data block has the key ${key}: ${source}`);
|
|
606
|
+
const mime = makeAttrs(block, {}).getFirstValue('mime-type');
|
|
607
|
+
if (!mime)
|
|
608
|
+
throw new SelectorError('format', `the data block ${key} declares no :mime-type: ${source}`);
|
|
609
|
+
const { type, params } = parseMimeType(String(mime));
|
|
610
|
+
const text = extractDataText(block);
|
|
611
|
+
const rows = type === 'text/csv' ? parseCsv(text) : type === 'text/tab-separated-values' ? parseTsv(text) : undefined;
|
|
612
|
+
if (!rows)
|
|
613
|
+
throw new SelectorError('address', `${type} content has no columns: ${source}`);
|
|
614
|
+
const header = params.header === 'present';
|
|
615
|
+
const body = header ? rows.slice(1) : rows;
|
|
616
|
+
let column = 0;
|
|
617
|
+
if (address) {
|
|
618
|
+
column = header && rows.length > 0 ? rows[0].indexOf(address) : -1;
|
|
619
|
+
if (column === -1)
|
|
620
|
+
throw new SelectorError('address', `no column is headed ${address}: ${source}`);
|
|
621
|
+
}
|
|
622
|
+
else if (rows.some(row => row.length !== 1)) {
|
|
623
|
+
throw new SelectorError('address', `the data has several columns and none is named: ${source}`);
|
|
624
|
+
}
|
|
625
|
+
return body.map(row => row[column]).filter((cell) => cell !== undefined);
|
|
626
|
+
};
|
|
627
|
+
// The blocks an address names, found the way a link finds its target; the first
|
|
628
|
+
// of two blocks sharing it answers.
|
|
629
|
+
const addressed = (docs, anchor) => docs.flatMap(d => {
|
|
630
|
+
const binding = bindTarget(anchor, buildBindingIndex(d.node));
|
|
631
|
+
return binding.found ? [binding.node] : [];
|
|
632
|
+
});
|
|
633
|
+
const operandBlocks = (selector, reading) => {
|
|
634
|
+
const { scheme, document, anchor, patterns } = selector;
|
|
635
|
+
const { home, readFile, readSource, corpus } = reading;
|
|
636
|
+
const shown = scheme ? `${scheme}:${document}` : patterns.map(p => p.blockType).join(', ');
|
|
637
|
+
let sources = home;
|
|
638
|
+
if (scheme === 'file' && document) {
|
|
639
|
+
sources = readSource
|
|
640
|
+
? readSource('file', document)
|
|
641
|
+
: readFile
|
|
642
|
+
? readFile(document)
|
|
643
|
+
: corpus.filter(d => filePathMatches(d.file, document));
|
|
644
|
+
}
|
|
645
|
+
else if (scheme === 'doc' && document) {
|
|
646
|
+
sources = readSource?.('doc', document) ?? corpus.filter(d => getDocIDs(d).includes(document));
|
|
647
|
+
}
|
|
648
|
+
if (!sources || sources.length === 0)
|
|
649
|
+
throw new SelectorError('resolution', `the source does not resolve: ${shown}`);
|
|
650
|
+
if (anchor) {
|
|
651
|
+
const found = addressed(sources, anchor);
|
|
652
|
+
if (found.length === 0)
|
|
653
|
+
throw new SelectorError('address', `no block has the address ${anchor}: ${shown}`);
|
|
654
|
+
return found;
|
|
655
|
+
}
|
|
656
|
+
return patterns.length > 0 ? selectBlocks(patterns, sources, reading) : [];
|
|
657
|
+
};
|
|
658
|
+
const readOperands = (patterns, reading) => {
|
|
659
|
+
const values = new Map();
|
|
660
|
+
for (const cond of patterns.flatMap(p => p.predicate ?? [])) {
|
|
661
|
+
if (cond.valueSpec?.kind !== 'in')
|
|
662
|
+
continue;
|
|
663
|
+
const operand = readInOperand(cond.valueSpec.value);
|
|
664
|
+
if (!operand)
|
|
665
|
+
continue;
|
|
666
|
+
if ('literal' in operand) {
|
|
667
|
+
values.set(cond, valuesOf(operand.literal));
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const { selector } = operand;
|
|
671
|
+
values.set(cond, selector.scheme === 'data'
|
|
672
|
+
? dataValues(selector.document ?? '', selector.anchor, reading.home)
|
|
673
|
+
: operandBlocks(selector, reading).flatMap(termOf));
|
|
674
|
+
}
|
|
675
|
+
return values;
|
|
676
|
+
};
|
|
677
|
+
// Operands are read before any block is visited: a source that does not
|
|
678
|
+
// resolve is an error even where no block would reach the condition.
|
|
679
|
+
const selectBlocks = (patterns, docs, reading) => {
|
|
680
|
+
const operands = readOperands(patterns, reading);
|
|
681
|
+
const collectedBlocks = [];
|
|
682
|
+
const seen = new Set();
|
|
683
|
+
for (const d of docs) {
|
|
684
|
+
collectMatches(d.node, undefined, patterns, seen, collectedBlocks, operands);
|
|
685
|
+
}
|
|
686
|
+
return collectedBlocks;
|
|
687
|
+
};
|
|
688
|
+
export const runSelector = (selector, docs, options = {}) => {
|
|
434
689
|
const parsed = parseSelector(selector);
|
|
435
690
|
if (!parsed)
|
|
436
691
|
return [];
|
|
@@ -445,25 +700,27 @@ export const runSelector = (selector, docs) => {
|
|
|
445
700
|
else if (scheme && scheme !== 'doc' && scheme !== 'file') {
|
|
446
701
|
return [];
|
|
447
702
|
}
|
|
448
|
-
|
|
703
|
+
const shown = `${scheme}:${document}${anchor ? `#${anchor}` : ''}`;
|
|
704
|
+
// a mask that no document answers is empty; a named source must answer
|
|
705
|
+
if (scheme && document && matchedDocs.length === 0 && !(scheme === 'file' && isGlobPattern(document))) {
|
|
706
|
+
throw new SelectorError('resolution', `the source does not resolve: ${shown}`);
|
|
707
|
+
}
|
|
708
|
+
// An address takes precedence, and a selection after it is not applied
|
|
449
709
|
if (anchor) {
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (block)
|
|
455
|
-
collectedBlocks.push(block);
|
|
456
|
-
}
|
|
457
|
-
return collectedBlocks;
|
|
710
|
+
const found = addressed(matchedDocs, anchor);
|
|
711
|
+
if (found.length === 0)
|
|
712
|
+
throw new SelectorError('address', `no block has the address ${anchor}: ${shown}`);
|
|
713
|
+
return found;
|
|
458
714
|
}
|
|
459
|
-
// Patterns
|
|
715
|
+
// Patterns are applied in source order and a block found twice is kept once;
|
|
716
|
+
// an operand reads from all the documents given, not only the matched ones
|
|
460
717
|
if (patterns.length > 0) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
718
|
+
return selectBlocks(patterns, matchedDocs, {
|
|
719
|
+
home: options.home ?? docs,
|
|
720
|
+
readFile: options.readFile,
|
|
721
|
+
readSource: options.readSource,
|
|
722
|
+
corpus: docs,
|
|
723
|
+
});
|
|
467
724
|
}
|
|
468
725
|
// No anchor, no patterns — return whole docs
|
|
469
726
|
return matchedDocs.map(d => d.node);
|