podlite 0.0.83 → 0.0.84

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.
Files changed (168) hide show
  1. package/CHANGELOG.podlite +90 -0
  2. package/esm/assemble/async.d.ts +31 -0
  3. package/esm/assemble/async.js +113 -0
  4. package/esm/assemble/async.js.map +1 -0
  5. package/esm/assemble/files.d.ts +2 -0
  6. package/esm/assemble/files.js +77 -0
  7. package/esm/assemble/files.js.map +1 -0
  8. package/esm/assemble/index.d.ts +59 -0
  9. package/esm/assemble/index.js +1003 -0
  10. package/esm/assemble/index.js.map +1 -0
  11. package/esm/cli.js +151 -26
  12. package/esm/cli.js.map +1 -1
  13. package/esm/file-format.d.ts +3 -0
  14. package/esm/file-format.js +53 -0
  15. package/esm/file-format.js.map +1 -0
  16. package/esm/index.d.ts +4 -0
  17. package/esm/index.js +3 -0
  18. package/esm/index.js.map +1 -1
  19. package/esm/lint/grammar/scan.d.ts +1 -1
  20. package/esm/lint/grammar/scan.js +2 -1
  21. package/esm/lint/grammar/scan.js.map +1 -1
  22. package/esm/lint/index.d.ts +1 -1
  23. package/esm/lint/index.js +3 -3
  24. package/esm/lint/index.js.map +1 -1
  25. package/esm/lint/loader.js +5 -4
  26. package/esm/lint/loader.js.map +1 -1
  27. package/esm/lint/mute.js +2 -1
  28. package/esm/lint/mute.js.map +1 -1
  29. package/esm/lint/rules/include-resolves.d.ts +6 -0
  30. package/esm/lint/rules/include-resolves.js +89 -0
  31. package/esm/lint/rules/include-resolves.js.map +1 -0
  32. package/esm/lint/rules/index.js +2 -0
  33. package/esm/lint/rules/index.js.map +1 -1
  34. package/esm/lint/rules/table-shape.js +7 -1
  35. package/esm/lint/rules/table-shape.js.map +1 -1
  36. package/esm/lint/types.d.ts +6 -1
  37. package/esm/lint/types.js.map +1 -1
  38. package/esm/query-blocks.d.ts +6 -0
  39. package/esm/query-blocks.js +127 -0
  40. package/esm/query-blocks.js.map +1 -0
  41. package/esm/query.d.ts +2 -0
  42. package/esm/query.js +233 -26
  43. package/esm/query.js.map +1 -1
  44. package/esm/reader.d.ts +15 -0
  45. package/esm/reader.js +119 -0
  46. package/esm/reader.js.map +1 -0
  47. package/esm/refresh-tocs.d.ts +2 -0
  48. package/esm/refresh-tocs.js +52 -0
  49. package/esm/refresh-tocs.js.map +1 -0
  50. package/esm/resolve-includes.d.ts +27 -1
  51. package/esm/resolve-includes.js +80 -40
  52. package/esm/resolve-includes.js.map +1 -1
  53. package/esm/test/collect.d.ts +16 -0
  54. package/esm/test/collect.js +368 -0
  55. package/esm/test/collect.js.map +1 -0
  56. package/esm/test/documents.d.ts +47 -0
  57. package/esm/test/documents.js +211 -0
  58. package/esm/test/documents.js.map +1 -0
  59. package/esm/test/evaluate.d.ts +41 -0
  60. package/esm/test/evaluate.js +104 -0
  61. package/esm/test/evaluate.js.map +1 -0
  62. package/esm/test/formatters/json.d.ts +2 -0
  63. package/esm/test/formatters/json.js +11 -0
  64. package/esm/test/formatters/json.js.map +1 -0
  65. package/esm/test/formatters/text.d.ts +2 -0
  66. package/esm/test/formatters/text.js +104 -0
  67. package/esm/test/formatters/text.js.map +1 -0
  68. package/esm/test/index.d.ts +11 -0
  69. package/esm/test/index.js +19 -0
  70. package/esm/test/index.js.map +1 -0
  71. package/esm/test/resources.d.ts +5 -0
  72. package/esm/test/resources.js +45 -0
  73. package/esm/test/resources.js.map +1 -0
  74. package/esm/test/run.d.ts +52 -0
  75. package/esm/test/run.js +91 -0
  76. package/esm/test/run.js.map +1 -0
  77. package/esm/test/sources.d.ts +48 -0
  78. package/esm/test/sources.js +162 -0
  79. package/esm/test/sources.js.map +1 -0
  80. package/esm/test/types.d.ts +104 -0
  81. package/esm/test/types.js +3 -0
  82. package/esm/test/types.js.map +1 -0
  83. package/esm/version.d.ts +1 -1
  84. package/esm/version.js +1 -1
  85. package/lib/assemble/async.d.ts +31 -0
  86. package/lib/assemble/async.js +118 -0
  87. package/lib/assemble/async.js.map +1 -0
  88. package/lib/assemble/files.d.ts +2 -0
  89. package/lib/assemble/files.js +81 -0
  90. package/lib/assemble/files.js.map +1 -0
  91. package/lib/assemble/index.d.ts +59 -0
  92. package/lib/assemble/index.js +1013 -0
  93. package/lib/assemble/index.js.map +1 -0
  94. package/lib/cli.js +149 -24
  95. package/lib/cli.js.map +1 -1
  96. package/lib/file-format.d.ts +3 -0
  97. package/lib/file-format.js +58 -0
  98. package/lib/file-format.js.map +1 -0
  99. package/lib/index.d.ts +4 -0
  100. package/lib/index.js +22 -1
  101. package/lib/index.js.map +1 -1
  102. package/lib/lint/grammar/scan.d.ts +1 -1
  103. package/lib/lint/grammar/scan.js +2 -1
  104. package/lib/lint/grammar/scan.js.map +1 -1
  105. package/lib/lint/index.d.ts +1 -1
  106. package/lib/lint/index.js +3 -3
  107. package/lib/lint/index.js.map +1 -1
  108. package/lib/lint/loader.js +5 -4
  109. package/lib/lint/loader.js.map +1 -1
  110. package/lib/lint/mute.js +2 -1
  111. package/lib/lint/mute.js.map +1 -1
  112. package/lib/lint/rules/include-resolves.d.ts +6 -0
  113. package/lib/lint/rules/include-resolves.js +92 -0
  114. package/lib/lint/rules/include-resolves.js.map +1 -0
  115. package/lib/lint/rules/index.js +2 -0
  116. package/lib/lint/rules/index.js.map +1 -1
  117. package/lib/lint/rules/table-shape.js +7 -1
  118. package/lib/lint/rules/table-shape.js.map +1 -1
  119. package/lib/lint/types.d.ts +6 -1
  120. package/lib/lint/types.js.map +1 -1
  121. package/lib/query-blocks.d.ts +6 -0
  122. package/lib/query-blocks.js +136 -0
  123. package/lib/query-blocks.js.map +1 -0
  124. package/lib/query.d.ts +2 -0
  125. package/lib/query.js +232 -25
  126. package/lib/query.js.map +1 -1
  127. package/lib/reader.d.ts +15 -0
  128. package/lib/reader.js +124 -0
  129. package/lib/reader.js.map +1 -0
  130. package/lib/refresh-tocs.d.ts +2 -0
  131. package/lib/refresh-tocs.js +56 -0
  132. package/lib/refresh-tocs.js.map +1 -0
  133. package/lib/resolve-includes.d.ts +27 -1
  134. package/lib/resolve-includes.js +84 -40
  135. package/lib/resolve-includes.js.map +1 -1
  136. package/lib/test/collect.d.ts +16 -0
  137. package/lib/test/collect.js +374 -0
  138. package/lib/test/collect.js.map +1 -0
  139. package/lib/test/documents.d.ts +47 -0
  140. package/lib/test/documents.js +250 -0
  141. package/lib/test/documents.js.map +1 -0
  142. package/lib/test/evaluate.d.ts +41 -0
  143. package/lib/test/evaluate.js +142 -0
  144. package/lib/test/evaluate.js.map +1 -0
  145. package/lib/test/formatters/json.d.ts +2 -0
  146. package/lib/test/formatters/json.js +15 -0
  147. package/lib/test/formatters/json.js.map +1 -0
  148. package/lib/test/formatters/text.d.ts +2 -0
  149. package/lib/test/formatters/text.js +141 -0
  150. package/lib/test/formatters/text.js.map +1 -0
  151. package/lib/test/index.d.ts +11 -0
  152. package/lib/test/index.js +31 -0
  153. package/lib/test/index.js.map +1 -0
  154. package/lib/test/resources.d.ts +5 -0
  155. package/lib/test/resources.js +83 -0
  156. package/lib/test/resources.js.map +1 -0
  157. package/lib/test/run.d.ts +52 -0
  158. package/lib/test/run.js +95 -0
  159. package/lib/test/run.js.map +1 -0
  160. package/lib/test/sources.d.ts +48 -0
  161. package/lib/test/sources.js +200 -0
  162. package/lib/test/sources.js.map +1 -0
  163. package/lib/test/types.d.ts +104 -0
  164. package/lib/test/types.js +8 -0
  165. package/lib/test/types.js.map +1 -0
  166. package/lib/version.d.ts +1 -1
  167. package/lib/version.js +1 -1
  168. package/package.json +28 -7
@@ -0,0 +1,1003 @@
1
+ import { applySetToFirst, holderInside, isSetTarget, isSetTransparent, markGuarded, bindTarget, mergeConfigSettings, propagateConfigDefaults, buildBindingIndex, getTextContentFromNode, mergeSet, outermost, parseSelector, podlitePluggable, runSelector, SelectorError, } from '@podlite/schema';
2
+ import { rebuildToc } from '@podlite/toc';
3
+ import { formatOfFile, formatOfType } from '../file-format.js';
4
+ export const isWarning = (problem) => problem.kind === 'ambiguous' ||
5
+ problem.kind === 'unparsed-selector' ||
6
+ problem.kind === 'unsupported-scheme' ||
7
+ problem.kind === 'cycle' ||
8
+ problem.kind === 'set-target' ||
9
+ problem.kind === 'include-reading-differs' ||
10
+ problem.kind === 'external';
11
+ // The configuration as the parser reads it on a block, when it reads the text
12
+ // whole: no value is dropped and nothing spills into the body.
13
+ let configParser;
14
+ const readWhole = (tail) => {
15
+ configParser = configParser ?? podlitePluggable();
16
+ const diagnostics = [];
17
+ const tree = configParser.parse(`=for para ${tail}\nbody\n`, { podMode: 1, diagnostics });
18
+ const block = (tree.content ?? []).find((node) => node && node.type === 'block');
19
+ if (diagnostics.length || !block || !Array.isArray(block.config) || block.config.length === 0)
20
+ return undefined;
21
+ return String(getTextContentFromNode(block.content)).trim() === 'body' ? block.config : undefined;
22
+ };
23
+ // The line of a directive holds the selector and, after the address of the
24
+ // source, its configuration: it begins at the first white space followed by a
25
+ // colon before the selection, and ends at the last bar before which it reads
26
+ // whole, or at the end of the line. A configuration that does not
27
+ // read whole leaves the line unread.
28
+ const splitDirective = (line) => {
29
+ // without the address of a source there is no place for a configuration: a
30
+ // source is written with its scheme, or without one when a selection follows
31
+ if (!/^[a-z][\w+.-]*:/i.test(line) && !line.includes('|'))
32
+ return { selector: line };
33
+ let start = -1;
34
+ for (let i = 0; i < line.length; i++) {
35
+ if (line[i] === '|')
36
+ break;
37
+ if (/\s/.test(line[i]) && line[i + 1] === ':') {
38
+ start = i;
39
+ break;
40
+ }
41
+ }
42
+ if (start === -1)
43
+ return { selector: line };
44
+ const ends = [];
45
+ for (let i = start; i < line.length; i++)
46
+ if (line[i] === '|')
47
+ ends.push(i);
48
+ ends.push(line.length);
49
+ // the longest one that reads whole: a bar or a quote inside a value can make a
50
+ // shorter one read whole too
51
+ for (const end of ends.reverse()) {
52
+ const items = readWhole(line.slice(start, end).trim());
53
+ if (!items)
54
+ continue;
55
+ const rest = line.slice(end).trim();
56
+ const selector = rest ? `${line.slice(0, start).trim()} ${rest}` : line.slice(0, start).trim();
57
+ const types = items.filter(item => item.name === 'mime-type');
58
+ if (types.length > 1)
59
+ return { selector, unread: true };
60
+ return { selector, mimeType: types.length ? String(types[0].value) : undefined };
61
+ }
62
+ return { selector: line, unread: true };
63
+ };
64
+ /*
65
+ =begin pod :kind<export>
66
+
67
+ =head2 externalMessage
68
+
69
+ The text of an C<external> warning: the external sources an include brought, by
70
+ count and reason, and the path as the directive writes it. A host that shows part
71
+ of a document gives it the sources whose blocks it shows.
72
+
73
+ =end pod
74
+ */
75
+ export const externalMessage = (sources, written) => {
76
+ const reasons = new Map();
77
+ for (const source of sources) {
78
+ const reason = source.external ?? '';
79
+ reasons.set(reason, (reasons.get(reason) ?? 0) + 1);
80
+ }
81
+ const total = sources.length;
82
+ if (reasons.size === 1) {
83
+ const [reason] = [...reasons.keys()];
84
+ return total === 1
85
+ ? `included file comes from ${reason}: ${written}`
86
+ : `${total} included files come from ${reason}: ${written}`;
87
+ }
88
+ const parts = [...reasons].map(([reason, count]) => `${count} ${count === 1 ? 'comes' : 'come'} from ${reason}`);
89
+ return `${total} included files: ${parts.join(', ')}: ${written}`;
90
+ };
91
+ /*
92
+ =begin pod :kind<export>
93
+
94
+ =head2 silently
95
+
96
+ Runs C<work> with warnings of the plugins it calls kept from the console: a text
97
+ read a second time, or read ahead of the answers it waits for, would otherwise
98
+ say the same thing again.
99
+
100
+ =end pod
101
+ */
102
+ export const silently = (work) => {
103
+ const warn = console.warn;
104
+ console.warn = () => { };
105
+ try {
106
+ return work();
107
+ }
108
+ finally {
109
+ console.warn = warn;
110
+ }
111
+ };
112
+ const isIncludeBlock = (node) => node && typeof node === 'object' && node.type === 'block' && node.name === 'include';
113
+ const keepBlocks = (items) => items.filter(item => item && typeof item === 'object' && !('file' in item));
114
+ // A parser may hand back a list of nodes rather than a document; a selection
115
+ // needs one node to walk, and it must hold the parsed nodes themselves.
116
+ const asDocument = (tree) => Array.isArray(tree) ? { type: 'block', name: 'root', margin: '', content: tree } : tree;
117
+ const recordOrigin = (tree, where, origin) => {
118
+ const visit = (node) => {
119
+ if (!node || typeof node !== 'object')
120
+ return;
121
+ if (Array.isArray(node))
122
+ return node.forEach(visit);
123
+ if (!origin.has(node))
124
+ origin.set(node, where);
125
+ // a Markdown section holds its parsed blocks in one node, not in a list
126
+ visit(node.content);
127
+ };
128
+ visit(tree);
129
+ };
130
+ /*
131
+ =begin pod :kind<export>
132
+
133
+ =head2 assembleIncludes
134
+
135
+ Puts the blocks each C<=include> finds in its place. The text of an included
136
+ source comes from C<sources>; nothing is read from a disk here. A problem that
137
+ loses included content goes to C<onError>, or is thrown without it; the others
138
+ go to C<onWarning>.
139
+
140
+ =end pod
141
+ */
142
+ export const assembleIncludes = (tree, opts) => {
143
+ const origin = opts.origin ?? new WeakMap();
144
+ const provider = opts.sources;
145
+ const mainFile = opts.file ?? '<document>';
146
+ if (opts.text !== undefined)
147
+ recordOrigin(tree, { file: mainFile, text: opts.text }, origin);
148
+ // Read once per call: a file brought in twice is parsed twice, so each place it
149
+ // lands holds nodes of its own.
150
+ const texts = new Map();
151
+ const textOf = (source) => {
152
+ if (texts.has(source.id))
153
+ return texts.get(source.id) ?? null;
154
+ const text = provider.read(source);
155
+ if (text !== undefined)
156
+ texts.set(source.id, text);
157
+ return text;
158
+ };
159
+ const schemes = provider.schemes ?? ['file'];
160
+ // why a path gives no source to read: the provider said so, or it named more than one
161
+ const refusal = (located) => {
162
+ if (located.failed !== undefined)
163
+ return located.failed;
164
+ if (located.masked)
165
+ return undefined;
166
+ const places = new Map(located.sources.map((source) => [source.id, source]));
167
+ if (places.size < 2)
168
+ return undefined;
169
+ return `more than one source answers: ${[...places.values()].map(s => `${s.name} (${s.id})`).join(', ')}`;
170
+ };
171
+ const emit = (problem) => {
172
+ if (isWarning(problem)) {
173
+ opts.onWarning?.(problem);
174
+ return;
175
+ }
176
+ if (!opts.onError)
177
+ throw new Error(problem.message);
178
+ opts.onError(problem);
179
+ };
180
+ const held = [];
181
+ // the problem an error thrown with no handler was made from
182
+ const thrownFrom = new WeakMap();
183
+ // For each include being resolved, whether a block of its content was met
184
+ // before the walk went on: an error thrown past it names the assignments of
185
+ // its including file only when none was, since otherwise they may have had a
186
+ // target. An include by an address is taken as met: the address names the
187
+ // block whatever stands before it.
188
+ const reached = [];
189
+ // A file is also read on its own, for the selection: that reading reports
190
+ // nothing and marks no include outside it.
191
+ let quiet = 0;
192
+ let reachedFrom = 0;
193
+ const markReached = () => {
194
+ for (let i = reachedFrom; i < reached.length; i++)
195
+ reached[i] = true;
196
+ };
197
+ const hold = (entries) => {
198
+ if (quiet)
199
+ return;
200
+ for (const entry of entries) {
201
+ if (entry.shown && !isWarning(entry.problem) && !opts.onError) {
202
+ const error = new Error(entry.problem.message);
203
+ thrownFrom.set(error, entry);
204
+ throw error;
205
+ }
206
+ }
207
+ held.push(...entries);
208
+ };
209
+ const report = (problem) => hold([{ problem, shown: true }]);
210
+ const flush = () => {
211
+ held
212
+ .splice(0)
213
+ .filter(entry => entry.shown)
214
+ .forEach(entry => emit(entry.problem));
215
+ };
216
+ // how many includes brought their content so far
217
+ let brought = 0;
218
+ // how deep in lists the walk is: the document's own list is the first
219
+ let level = 0;
220
+ // the problems of each failed include, in the order met
221
+ const failures = [];
222
+ // Where an include failed, the search for a target stops: a directive left in
223
+ // the tree is known by its node, one that left nothing by a mark put in its
224
+ // place and taken out when the walk ends.
225
+ const failedAt = new WeakMap();
226
+ const FAILED = 'include-failed';
227
+ const isMark = (node) => node && typeof node === 'object' && node.type === FAILED;
228
+ const firstStop = (nodes, holder) => {
229
+ for (const node of nodes) {
230
+ if (!node || typeof node !== 'object')
231
+ continue;
232
+ const failed = failedAt.get(node);
233
+ if (failed)
234
+ return failed;
235
+ if (node.type === 'block' && (node.name === 'root' || node.name === '_folded_section')) {
236
+ const inner = Array.isArray(node.content) ? node.content : node.content ? [node.content] : [];
237
+ const within = holderInside(node, holder);
238
+ const found = firstStop(inner, within);
239
+ if (found !== undefined)
240
+ return found;
241
+ if (inner.some((n) => isSetTarget(n, within)))
242
+ return undefined;
243
+ continue;
244
+ }
245
+ if (!isSetTarget(node, holder))
246
+ continue;
247
+ return undefined;
248
+ }
249
+ return undefined;
250
+ };
251
+ // The block the assignments would go to, before any is given: the first that
252
+ // is not transparent, looking inside the wrappers the tree adds.
253
+ const firstTarget = (nodes, holder) => {
254
+ for (const node of nodes) {
255
+ if (!node || typeof node !== 'object')
256
+ continue;
257
+ if (failedAt.has(node))
258
+ return node;
259
+ if (node.type === 'block' && (node.name === 'root' || node.name === '_folded_section')) {
260
+ const inner = Array.isArray(node.content) ? node.content : node.content ? [node.content] : [];
261
+ const found = firstTarget(inner, holderInside(node, holder));
262
+ if (found)
263
+ return found;
264
+ continue;
265
+ }
266
+ if (!isSetTarget(node, holder))
267
+ continue;
268
+ return node;
269
+ }
270
+ return undefined;
271
+ };
272
+ // A selection keeps no mark of an include that failed inside the files it ran
273
+ // over; which comes first there, the target or a failure, is read off the
274
+ // files themselves, in document order. Without a target, any failure counts.
275
+ const failedBefore = (roots, target) => {
276
+ let found;
277
+ let done = false;
278
+ const visit = (node) => {
279
+ if (done || !node || typeof node !== 'object')
280
+ return;
281
+ if (Array.isArray(node))
282
+ return node.forEach(visit);
283
+ const failed = failedAt.get(node);
284
+ if (failed) {
285
+ found = failed;
286
+ done = true;
287
+ return;
288
+ }
289
+ if (node === target) {
290
+ done = true;
291
+ return;
292
+ }
293
+ visit(node.content);
294
+ };
295
+ roots.forEach(visit);
296
+ return found;
297
+ };
298
+ // For a block placed as its file reads on its own the order is read off that
299
+ // reading, and the failure named is the first of the files as placed.
300
+ const stoppedBefore = (roots, target) => {
301
+ const alone = target ? readAlone.get(target) : undefined;
302
+ if (!alone)
303
+ return failedBefore(roots, target);
304
+ return failedBefore(alone.roots, alone.node) ? failedBefore(roots, undefined) : undefined;
305
+ };
306
+ const unmark = (node) => {
307
+ if (Array.isArray(node)) {
308
+ for (let i = node.length - 1; i >= 0; i--) {
309
+ if (isMark(node[i]))
310
+ node.splice(i, 1);
311
+ else
312
+ unmark(node[i]);
313
+ }
314
+ return node;
315
+ }
316
+ if (node && typeof node === 'object' && Array.isArray(node.content))
317
+ unmark(node.content);
318
+ return node;
319
+ };
320
+ // the message a problem had before assignments were named in it, and the
321
+ // names, so that a second loss joins the first
322
+ const lost = new WeakMap();
323
+ const lose = (entries, set) => {
324
+ const final = entries[entries.length - 1].problem;
325
+ const before = lost.get(final) ?? { message: final.message, set: [] };
326
+ const all = mergeSet(before.set, set);
327
+ lost.set(final, { message: before.message, set: all });
328
+ final.message = `${before.message}; =set assignments not applied: ${names(all)}`;
329
+ final.lost = all.map(c => c.name);
330
+ // a cycle is reported when it loses assignments; otherwise it is left
331
+ // alone, as before
332
+ entries.forEach(entry => (entry.shown = true));
333
+ };
334
+ const names = (set) => set.map(c => c.name).join(', ');
335
+ // A directive left in the tree no longer waits for its assignments: they were
336
+ // for the content it failed to bring.
337
+ const withoutSet = (node) => {
338
+ if (!node.set)
339
+ return node;
340
+ const { set, ...rest } = node;
341
+ const known = origin.get(node);
342
+ if (known)
343
+ origin.set(rest, known);
344
+ opts.onCopy?.(node, rest);
345
+ return rest;
346
+ };
347
+ // `home` is the file a directive is written in: an operand of a selector
348
+ // without a source, or with data:, reads from it. `holder` is the block the
349
+ // list is the content of; none means the document. The =set assignments before
350
+ // an include go to the first block it brings; when it brings none they go on
351
+ // to the next block of the same list, and when it fails they go nowhere and
352
+ // the failure says so.
353
+ const walkList = (list, context, stack, chain, file, home, config, holder) => {
354
+ const out = [];
355
+ let pending = [];
356
+ let last;
357
+ const visit = (n) => {
358
+ if (n && n.type === 'config' && typeof n.name === 'string' && Array.isArray(n.config)) {
359
+ config[n.name] = mergeConfigSettings(n.config, config[n.name]);
360
+ }
361
+ if (isIncludeBlock(n)) {
362
+ // assignments carried from an earlier include are older than its own
363
+ const set = mergeSet(pending, n.set);
364
+ pending = [];
365
+ const here = [...chain, { file, location: n.location }];
366
+ let resolved;
367
+ const at = reached.length;
368
+ try {
369
+ resolved = resolveInclude(n, context, stack, here, file, home, config);
370
+ }
371
+ catch (e) {
372
+ // the walk stops at an error with no handler; the assignments lost
373
+ // on the way out are named in it as well
374
+ const held = e && typeof e === 'object' ? thrownFrom.get(e) : undefined;
375
+ if (held && set.length && !reached[at]) {
376
+ const error = e;
377
+ const before = error.message;
378
+ // the stack is made from the message when first read
379
+ const stack = error.stack;
380
+ lose([held], set);
381
+ error.message = held.problem.message;
382
+ if (typeof stack === 'string')
383
+ error.stack = stack.replace(before, () => error.message);
384
+ }
385
+ throw e;
386
+ }
387
+ finally {
388
+ reached.length = at;
389
+ }
390
+ if (resolved.waiting) {
391
+ const kept = set.length ? { ...n, set } : n;
392
+ const known = origin.get(n);
393
+ if (kept !== n && known)
394
+ origin.set(kept, known);
395
+ out.push(kept);
396
+ return;
397
+ }
398
+ const { nodes, failure, inner, roots, selector } = resolved;
399
+ if (!failure)
400
+ brought++;
401
+ if (!failure)
402
+ markVia(nodes, `${file}@${n.location?.start?.offset ?? ''}`, { file, location: n.location });
403
+ if (!failure && resolved.outside.length) {
404
+ const step = stepOf(file, n.location);
405
+ const problem = { kind: 'external', target: selector, message: '', chain: here };
406
+ hold([{ problem, shown: false, outside: { step, written: resolved.written, sources: resolved.outside } }]);
407
+ }
408
+ if (failure) {
409
+ const entries = failure.map(problem => ({ problem, shown: problem.kind !== 'cycle' }));
410
+ if (set.length)
411
+ lose(entries, set);
412
+ hold(entries);
413
+ failures.push(entries);
414
+ const stops = nodes.length ? nodes : [{ type: FAILED }];
415
+ stops.forEach(stop => failedAt.set(stop, entries));
416
+ out.push(...stops);
417
+ return;
418
+ }
419
+ if (!set.length) {
420
+ out.push(...nodes);
421
+ return;
422
+ }
423
+ // an include inside that failed before the target stops the search
424
+ const stopped = roots ? stoppedBefore(roots, firstTarget(nodes, holder)) : firstStop(nodes, holder);
425
+ if (stopped) {
426
+ lose(stopped, set);
427
+ out.push(...nodes);
428
+ return;
429
+ }
430
+ const applied = applySetToFirst(nodes, set, { mode: 'include', origin, onCopy: opts.onCopy, holder });
431
+ out.push(...applied.nodes);
432
+ if (applied.outcome !== 'none')
433
+ return;
434
+ // nothing came in because an include inside failed: what it would
435
+ // have brought is not known, and the assignments go nowhere
436
+ if (inner.length) {
437
+ lose(inner[inner.length - 1], set);
438
+ return;
439
+ }
440
+ pending = set;
441
+ last = { chain: here, selector };
442
+ return;
443
+ }
444
+ const walked = walkNode(n, context, stack, chain, file, home, config, holder);
445
+ const items = Array.isArray(walked) ? walked : [walked];
446
+ if (!pending.length) {
447
+ out.push(...items);
448
+ return;
449
+ }
450
+ const stopped = firstStop(items, holder);
451
+ if (stopped) {
452
+ lose(stopped, pending);
453
+ pending = [];
454
+ out.push(...items);
455
+ return;
456
+ }
457
+ const applied = applySetToFirst(items, pending, { mode: 'carry', origin, onCopy: opts.onCopy, holder });
458
+ if (applied.outcome !== 'none')
459
+ pending = [];
460
+ out.push(...applied.nodes);
461
+ };
462
+ level++;
463
+ try {
464
+ for (const n of list) {
465
+ const from = out.length;
466
+ visit(n);
467
+ if (level === 1) {
468
+ confirmOutside(out.slice(from));
469
+ flush();
470
+ }
471
+ }
472
+ }
473
+ finally {
474
+ level--;
475
+ }
476
+ if (pending.length && last) {
477
+ report({
478
+ kind: 'set-target',
479
+ target: last.selector,
480
+ message: `=set before =include has no target block in scope: ${names(pending)}`,
481
+ lost: pending.map(c => c.name),
482
+ chain: last.chain,
483
+ });
484
+ }
485
+ return out;
486
+ };
487
+ // The directive a block came through is kept with its origin, so that the
488
+ // two readings of a file name the same block the same way.
489
+ const markVia = (nodes, step, at) => {
490
+ const visit = (node) => {
491
+ if (!node || typeof node !== 'object')
492
+ return;
493
+ if (Array.isArray(node))
494
+ return node.forEach(visit);
495
+ const known = origin.get(node);
496
+ if (known) {
497
+ const via = known.via ? `${step}>${known.via}` : step;
498
+ origin.set(node, { ...known, via, steps: [at, ...(known.steps ?? [])] });
499
+ }
500
+ visit(node.content);
501
+ };
502
+ visit(nodes);
503
+ };
504
+ // Which directive of which entry: the file it is written in, as the walk names
505
+ // it, and its offset there.
506
+ const stepOf = (file, location) => `${file}\u0000${location?.start?.offset ?? ''}`;
507
+ const said = new Map();
508
+ // The external sources whose blocks stand in what an entry of the document's
509
+ // own list became confirm the includes that brought them.
510
+ const confirmOutside = (nodes) => {
511
+ const waiting = held.filter(entry => entry.outside && !entry.shown);
512
+ if (waiting.length === 0)
513
+ return;
514
+ const placed = new Map();
515
+ const visit = (node) => {
516
+ if (!node || typeof node !== 'object')
517
+ return;
518
+ if (Array.isArray(node))
519
+ return node.forEach(visit);
520
+ // what a directive or a failed include holds stands nowhere; a comment does
521
+ const comment = node.type === 'block' && node.name === 'comment';
522
+ if ((isSetTransparent(node) && !comment) || isMark(node))
523
+ return;
524
+ const where = origin.get(node);
525
+ const wrapper = node.type === 'block' && (node.name === 'root' || node.name === '_folded_section');
526
+ if (where && !wrapper) {
527
+ for (const at of where.steps ?? []) {
528
+ const step = stepOf(at.file, at.location);
529
+ const files = placed.get(step) ?? new Set();
530
+ files.add(where.file);
531
+ placed.set(step, files);
532
+ }
533
+ }
534
+ visit(node.content);
535
+ };
536
+ visit(nodes);
537
+ for (const entry of waiting) {
538
+ if (!entry.outside)
539
+ continue;
540
+ const { step, written, sources } = entry.outside;
541
+ const files = placed.get(step);
542
+ const shown = sources.filter((source, at) => files?.has(source.id) && sources.findIndex(other => other.id === source.id) === at);
543
+ if (shown.length === 0)
544
+ continue;
545
+ const message = externalMessage(shown, written);
546
+ const at = entry.problem.chain[entry.problem.chain.length - 1];
547
+ const key = `${at?.file}\u0000${at?.location?.start?.offset ?? ''}\u0000${message}`;
548
+ // the same line again is not shown, but what it brought is kept with the first
549
+ const first = said.get(key);
550
+ if (first) {
551
+ const known = first.sources ?? [];
552
+ first.sources = [...known, ...shown.filter(source => !known.some(other => other.id === source.id))];
553
+ continue;
554
+ }
555
+ said.set(key, entry.problem);
556
+ entry.problem.message = message;
557
+ entry.problem.sources = shown;
558
+ entry.shown = true;
559
+ }
560
+ };
561
+ const isFolded = (node) => node && typeof node === 'object' && node.type === 'block' && node.name === '_folded_section';
562
+ // a folded section is put around written blocks; they are counted as if it were not there
563
+ const childrenOf = (node) => {
564
+ const list = Array.isArray(node.content) ? node.content : node.content ? [node.content] : [];
565
+ return list.flatMap((child) => (isFolded(child) ? childrenOf(child) : [child]));
566
+ };
567
+ // A block written in a file is named by the directives it came through and
568
+ // its place in that file. A node the reading adds has no place of its own, and
569
+ // a block of a Markdown section counts its place from the section: both are
570
+ // named by the nearest written block above and the way down from it.
571
+ const namesOf = (root) => {
572
+ const out = [];
573
+ const visit = (node, above, step, counted) => {
574
+ if (!node || typeof node !== 'object')
575
+ return;
576
+ const where = origin.get(node);
577
+ const offset = node.location?.start?.offset;
578
+ const name = !counted && where && typeof offset === 'number'
579
+ ? `${where.via ?? ''}|${where.file}|${offset}|${node.type}|${node.name ?? ''}`
580
+ : `${above}/${step}|${node.type}|${node.name ?? ''}`;
581
+ out.push([node, name]);
582
+ const section = node.type === 'block' && (node.name === 'markdown' || node.name === 'Markdown');
583
+ childrenOf(node).forEach((child, i) => visit(child, name, String(i), counted || section));
584
+ };
585
+ visit(root, '', 'root', false);
586
+ return out;
587
+ };
588
+ const copyDeep = (node) => {
589
+ if (Array.isArray(node))
590
+ return node.map(copyDeep);
591
+ if (!node || typeof node !== 'object')
592
+ return node;
593
+ const copy = {};
594
+ for (const key of Object.keys(node))
595
+ copy[key] = key === 'location' ? node[key] : copyDeep(node[key]);
596
+ const known = origin.get(node);
597
+ if (known)
598
+ origin.set(copy, known);
599
+ opts.onCopy?.(node, copy);
600
+ return copy;
601
+ };
602
+ // A block placed as its file reads on its own, and where it was found: the
603
+ // files read with the settings at the directive do not hold it.
604
+ const readAlone = new WeakMap();
605
+ const isToc = (node) => node && typeof node === 'object' && node.type === 'block' && (node.name === 'toc' || node.name === 'Toc');
606
+ // A table of contents is built while its file is parsed, before the includes
607
+ // of that file are in. The tables written in an included file are built again
608
+ // over it once they are; those an include of its own brought were built over
609
+ // their own file already.
610
+ const finishFile = (root, file) => {
611
+ markGuarded(root);
612
+ // a table that cannot be built said so when its file was read
613
+ const rebuilt = (node) => silently(() => rebuildToc(node, root));
614
+ const walk = (node) => {
615
+ if (!node || typeof node !== 'object')
616
+ return node;
617
+ if (Array.isArray(node)) {
618
+ const mapped = node.map(walk);
619
+ return mapped.some((n, i) => n !== node[i]) ? mapped : node;
620
+ }
621
+ const where = origin.get(node);
622
+ const made = isToc(node) && where?.file === file && !where.via ? rebuilt(node) : node;
623
+ const content = made === node && Array.isArray(node.content) ? walk(node.content) : node.content;
624
+ if (made === node && content === node.content)
625
+ return node;
626
+ const copy = made === node ? { ...node, content } : made;
627
+ if (where)
628
+ origin.set(copy, where);
629
+ opts.onCopy?.(node, copy);
630
+ return copy;
631
+ };
632
+ return walk(root);
633
+ };
634
+ // A file as it reads on its own, its includes in. It is the same wherever it
635
+ // is included from, apart from the files already on the way.
636
+ const sources = new Map();
637
+ const sourceOf = (target, text, dir, stack, here, format) => {
638
+ const key = `${stack.join('\n')}\u0000${format}`;
639
+ const known = sources.get(key);
640
+ if (known)
641
+ return known;
642
+ const own = silently(() => opts.parse(text, target, undefined, { format }));
643
+ recordOrigin(own, { file: target, text }, origin);
644
+ const failed = failures.length;
645
+ const from = reachedFrom;
646
+ quiet++;
647
+ reachedFrom = reached.length;
648
+ try {
649
+ const before = brought;
650
+ const walked = asDocument(walkNode(own, dir, stack, here, target, own, {}));
651
+ const tree = brought > before ? finishFile(walked, target) : walked;
652
+ sources.set(key, tree);
653
+ return tree;
654
+ }
655
+ finally {
656
+ quiet--;
657
+ reachedFrom = from;
658
+ failures.length = failed;
659
+ }
660
+ };
661
+ // What an include comes to: the nodes that take its place, or the problems
662
+ // that left it with nothing, not yet reported
663
+ const resolveInclude = (node, context, stack, here, file, home, config) => {
664
+ const mark = failures.length;
665
+ reached.push(false);
666
+ // the walked files a selection ran over, in order
667
+ let roots;
668
+ const outside = [];
669
+ const done = (nodes, failure) => ({
670
+ nodes,
671
+ failure,
672
+ inner: failures.slice(mark),
673
+ roots,
674
+ selector,
675
+ outside,
676
+ written: parsed?.document ?? selector,
677
+ });
678
+ const line = getTextContentFromNode(node.content)?.toString().trim() ?? '';
679
+ const split = splitDirective(line);
680
+ const selector = split.selector;
681
+ const parsed = selector && !split.unread ? parseSelector(selector) : undefined;
682
+ if (parsed?.anchor)
683
+ reached[reached.length - 1] = true;
684
+ // The directive stays in the tree as before when its selector is not read;
685
+ // what it would have brought in is missing, and a reader of the tree cannot
686
+ // tell that on its own.
687
+ const fail = (problem, keep = false) => done(keep ? [withoutSet(node)] : [], [problem]);
688
+ if (!selector || !parsed || !parsed.scheme || !parsed.document) {
689
+ return fail({
690
+ kind: 'unparsed-selector',
691
+ target: line,
692
+ message: `include selector cannot be read: ${line || '(empty)'}`,
693
+ chain: here,
694
+ }, true);
695
+ }
696
+ // a type declared on the directive decides the reading; one with no reader brings nothing
697
+ const declared = split.mimeType === undefined ? undefined : formatOfType(split.mimeType);
698
+ if (split.mimeType !== undefined && declared === undefined) {
699
+ return fail({
700
+ kind: 'format',
701
+ target: selector,
702
+ message: `include format is not supported: ${split.mimeType}: ${parsed.document}`,
703
+ chain: here,
704
+ });
705
+ }
706
+ const formatOf = (source) => declared ?? source.format ?? formatOfFile(source.id);
707
+ if (!schemes.includes(parsed.scheme)) {
708
+ return fail({
709
+ kind: 'unsupported-scheme',
710
+ target: selector,
711
+ message: `include scheme is not supported: ${parsed.scheme}:`,
712
+ chain: here,
713
+ }, true);
714
+ }
715
+ const wait = () => ({ ...done([node]), roots: undefined, waiting: true });
716
+ const located = provider.locate(parsed.document, context, false, here[here.length - 1], parsed.scheme);
717
+ if (!located)
718
+ return wait();
719
+ const refused = refusal(located);
720
+ if (refused !== undefined) {
721
+ return located.masked
722
+ ? fail({
723
+ kind: 'source',
724
+ target: selector,
725
+ message: `include mask cannot be expanded: ${parsed.document}: ${refused}`,
726
+ chain: here,
727
+ })
728
+ : fail({
729
+ kind: 'source',
730
+ target: selector,
731
+ message: `include source cannot be resolved: ${parsed.scheme}:${parsed.document}: ${refused}`,
732
+ chain: here,
733
+ }, true);
734
+ }
735
+ const { masked } = located;
736
+ // two answers with one id are one source
737
+ const written = masked
738
+ ? located.sources
739
+ : located.sources.filter((source, at, all) => all.findIndex(other => other.id === source.id) === at);
740
+ // a source already on the way in is not read again to find that out
741
+ const back = !masked && written.length > 0 && stack.includes(written[0].id);
742
+ const first = !masked && written.length && !back ? textOf(written[0]) : null;
743
+ if (first === undefined)
744
+ return wait();
745
+ if (!masked && !back && first === null) {
746
+ return fail({
747
+ kind: 'source',
748
+ target: selector,
749
+ message: `include target not found: ${parsed.document}`,
750
+ chain: here,
751
+ });
752
+ }
753
+ // With settings in effect at the directive a file is read twice: with them,
754
+ // for the blocks that take its place, and on its own, for the selection.
755
+ const scoped = Object.keys(config).length > 0;
756
+ const docs = [];
757
+ const placedDocs = [];
758
+ const unread = [];
759
+ let cyclic = false;
760
+ let waiting = false;
761
+ for (const source of written) {
762
+ const { id: target, name } = source;
763
+ if (stack.includes(target)) {
764
+ cyclic = true;
765
+ continue;
766
+ }
767
+ const text = textOf(source);
768
+ if (text === undefined) {
769
+ waiting = true;
770
+ continue;
771
+ }
772
+ if (text === null) {
773
+ unread.push({
774
+ kind: 'source',
775
+ target: selector,
776
+ message: `include target cannot be read: ${name}`,
777
+ chain: here,
778
+ });
779
+ continue;
780
+ }
781
+ // read on its own first: a text that fails this reading brings nothing, and
782
+ // what its includes would have said is not said
783
+ let alone;
784
+ if (scoped) {
785
+ try {
786
+ alone = sourceOf(target, text, source.context, [...stack, target], here, formatOf(source));
787
+ }
788
+ catch (e) {
789
+ if (!opts.tolerant)
790
+ throw e;
791
+ unread.push({
792
+ kind: 'source',
793
+ target: selector,
794
+ message: `include target cannot be read: ${name}: ${e?.message ?? e}`,
795
+ chain: here,
796
+ });
797
+ continue;
798
+ }
799
+ }
800
+ let own;
801
+ try {
802
+ own = opts.parse(text, target, scoped ? config : undefined, { format: formatOf(source) });
803
+ }
804
+ catch (e) {
805
+ if (!opts.tolerant)
806
+ throw e;
807
+ unread.push({
808
+ kind: 'source',
809
+ target: selector,
810
+ message: `include target cannot be read: ${name}: ${e?.message ?? e}`,
811
+ chain: here,
812
+ });
813
+ continue;
814
+ }
815
+ recordOrigin(own, { file: target, text }, origin);
816
+ const dir = source.context;
817
+ const before = brought;
818
+ const walked = asDocument(walkNode(own, dir, [...stack, target], here, target, own, { ...config }));
819
+ const placed = brought > before ? finishFile(walked, target) : walked;
820
+ placedDocs.push(placed);
821
+ docs.push({ file: name, node: alone ?? placed });
822
+ if (source.external !== undefined)
823
+ outside.push(source);
824
+ }
825
+ if (waiting)
826
+ return wait();
827
+ // The blocks found in the files as they read on their own are placed as the
828
+ // same files read them at the directive.
829
+ const placedFor = (found) => {
830
+ if (!scoped)
831
+ return found;
832
+ const pairs = docs.map((doc, i) => ({
833
+ source: new Map(namesOf(doc.node)),
834
+ placed: new Map(namesOf(placedDocs[i])
835
+ .map(([node, name]) => [name, node])
836
+ .reverse()),
837
+ }));
838
+ let differs = false;
839
+ const out = found.map(node => {
840
+ for (const pair of pairs) {
841
+ const name = pair.source.get(node);
842
+ if (name === undefined)
843
+ continue;
844
+ const twin = pair.placed.get(name);
845
+ if (twin)
846
+ return twin;
847
+ differs = true;
848
+ const copy = propagateConfigDefaults([copyDeep(node)], config)[0];
849
+ readAlone.set(copy, { node, roots: docs.map(doc => doc.node) });
850
+ return copy;
851
+ }
852
+ return node;
853
+ });
854
+ if (differs) {
855
+ report({
856
+ kind: 'include-reading-differs',
857
+ target: selector,
858
+ message: `include places a block as its file reads on its own; the settings at the directive read it differently: ${selector}`,
859
+ chain: here,
860
+ });
861
+ }
862
+ return out;
863
+ };
864
+ if (docs.length === 0) {
865
+ // a mask that names no file holds no address
866
+ if (parsed.anchor && written.length === 0) {
867
+ return fail({
868
+ kind: 'address',
869
+ target: selector,
870
+ message: `include address not found: #${parsed.anchor} in ${parsed.document}`,
871
+ chain: here,
872
+ });
873
+ }
874
+ if (unread.length)
875
+ return done([], unread);
876
+ if (cyclic) {
877
+ return fail({
878
+ kind: 'cycle',
879
+ target: selector,
880
+ message: `include brings nothing: ${parsed.document} is already being included`,
881
+ chain: here,
882
+ });
883
+ }
884
+ return done([]);
885
+ }
886
+ // the files that were read stand; the others are reported as before
887
+ unread.forEach(report);
888
+ if (parsed.anchor) {
889
+ // The address is found the way a link finds its target, in the file as it
890
+ // stands once its own includes are in; a selection after it is not applied.
891
+ const found = [];
892
+ for (const doc of docs) {
893
+ const binding = bindTarget(parsed.anchor, buildBindingIndex(doc.node));
894
+ if (!binding.found)
895
+ continue;
896
+ if (binding.ambiguous) {
897
+ report({
898
+ kind: 'ambiguous',
899
+ target: selector,
900
+ message: `include address #${parsed.anchor} names more than one block in ${doc.file}`,
901
+ chain: here,
902
+ });
903
+ }
904
+ found.push(binding.node);
905
+ }
906
+ if (found.length > 0)
907
+ return done(placedFor(found));
908
+ return fail({
909
+ kind: 'address',
910
+ target: selector,
911
+ message: `include address not found: #${parsed.anchor} in ${parsed.document}`,
912
+ chain: here,
913
+ });
914
+ }
915
+ roots = placedDocs;
916
+ // a file an operand names is read the way an included file is, from the
917
+ // directory of the directive; one already on the way does not resolve
918
+ let operandWaits = false;
919
+ const readSource = (scheme, document) => {
920
+ if (!schemes.includes(scheme))
921
+ return undefined;
922
+ const found = provider.locate(document, context, true, undefined, scheme);
923
+ const refused = found && refusal(found);
924
+ if (refused !== undefined) {
925
+ throw new SelectorError('resolution', `the source does not resolve: ${scheme}:${document}: ${refused}`);
926
+ }
927
+ const source = found?.sources[0];
928
+ const target = source?.id ?? '';
929
+ const text = !source || stack.includes(target) ? null : textOf(source);
930
+ if (!found || text === undefined)
931
+ operandWaits = true;
932
+ if (!source || text === null || text === undefined)
933
+ return undefined;
934
+ let own;
935
+ try {
936
+ own = opts.parse(text, target, undefined, { format: source.format ?? formatOfFile(target) });
937
+ }
938
+ catch (e) {
939
+ // a file that fails to parse is one the operand cannot be read from
940
+ if (!opts.tolerant)
941
+ throw e;
942
+ return undefined;
943
+ }
944
+ recordOrigin(own, { file: target, text }, origin);
945
+ return [
946
+ {
947
+ file: document,
948
+ node: asDocument(walkNode(own, source.context, [...stack, target], here, target, own, {})),
949
+ },
950
+ ];
951
+ };
952
+ try {
953
+ const found = outermost(keepBlocks(runSelector(selector, docs, { home: [{ file, node: asDocument(home) }], readSource })));
954
+ if (operandWaits)
955
+ return wait();
956
+ return done(placedFor(found));
957
+ }
958
+ catch (e) {
959
+ if (!(e instanceof SelectorError))
960
+ throw e;
961
+ if (operandWaits)
962
+ return wait();
963
+ return fail({
964
+ kind: 'operand',
965
+ target: selector,
966
+ message: `include selector cannot be read: ${e.message}`,
967
+ chain: here,
968
+ });
969
+ }
970
+ };
971
+ const walkNode = (node, context, stack, chain, file, home, config, holder) => {
972
+ if (!node || typeof node !== 'object')
973
+ return node;
974
+ if (Array.isArray(node))
975
+ return walkList(node, context, stack, chain, file, home, config, holder);
976
+ if (isIncludeBlock(node))
977
+ return walkList([node], context, stack, chain, file, home, config, holder);
978
+ const wrapper = node.type === 'block' && (node.name === 'root' || node.name === '_folded_section');
979
+ if (isSetTarget(node, holder))
980
+ markReached();
981
+ if (Array.isArray(node.content)) {
982
+ // a block is a lexical scope for the settings declared inside it
983
+ const scope = node.type === 'block' && !wrapper ? { ...config } : config;
984
+ const inner = holderInside(node, holder);
985
+ const copy = { ...node, content: walkList(node.content, context, stack, chain, file, home, scope, inner) };
986
+ const known = origin.get(node);
987
+ if (known)
988
+ origin.set(copy, known);
989
+ opts.onCopy?.(node, copy);
990
+ return copy;
991
+ }
992
+ return node;
993
+ };
994
+ try {
995
+ return unmark(walkNode(tree, opts.context, opts.self ? [opts.self] : [], [], mainFile, tree, {}));
996
+ }
997
+ finally {
998
+ flush();
999
+ }
1000
+ };
1001
+ export { sourcesFromFiles } from './files.js';
1002
+ export { assembleAsync, createSourceStore } from './async.js';
1003
+ //# sourceMappingURL=index.js.map