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