@podlite/schema 0.0.74 → 0.0.76

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 (102) hide show
  1. package/CHANGELOG.podlite +48 -0
  2. package/esm/ast-helpers.d.ts +2 -1
  3. package/esm/ast-helpers.js +31 -2
  4. package/esm/ast-helpers.js.map +1 -1
  5. package/esm/codes/place.d.ts +13 -0
  6. package/esm/codes/place.js +105 -0
  7. package/esm/codes/place.js.map +1 -0
  8. package/esm/codes/read.d.ts +21 -0
  9. package/esm/codes/read.js +365 -0
  10. package/esm/codes/read.js.map +1 -0
  11. package/esm/codes/scan.d.ts +41 -0
  12. package/esm/codes/scan.js +332 -0
  13. package/esm/codes/scan.js.map +1 -0
  14. package/esm/exportHtml.js +44 -13
  15. package/esm/exportHtml.js.map +1 -1
  16. package/esm/exportMarkdown.js +32 -9
  17. package/esm/exportMarkdown.js.map +1 -1
  18. package/esm/guard.d.ts +9 -0
  19. package/esm/guard.js +100 -0
  20. package/esm/guard.js.map +1 -1
  21. package/esm/helpers/blockName.d.ts +1 -0
  22. package/esm/helpers/blockName.js +28 -2
  23. package/esm/helpers/blockName.js.map +1 -1
  24. package/esm/helpers/configPropagation.js +3 -1
  25. package/esm/helpers/configPropagation.js.map +1 -1
  26. package/esm/helpers/handlers.d.ts +3 -0
  27. package/esm/helpers/handlers.js +23 -0
  28. package/esm/helpers/handlers.js.map +1 -1
  29. package/esm/helpers/headingKeys.d.ts +2 -0
  30. package/esm/helpers/headingKeys.js +45 -0
  31. package/esm/helpers/headingKeys.js.map +1 -0
  32. package/esm/helpers/ids.js +1 -1
  33. package/esm/helpers/ids.js.map +1 -1
  34. package/esm/helpers/numberedShorthand.d.ts +1 -0
  35. package/esm/helpers/numberedShorthand.js +32 -0
  36. package/esm/helpers/numberedShorthand.js.map +1 -0
  37. package/esm/helpers/stripMarker.d.ts +1 -0
  38. package/esm/helpers/stripMarker.js +24 -0
  39. package/esm/helpers/stripMarker.js.map +1 -0
  40. package/esm/index.d.ts +5 -4
  41. package/esm/index.js +6 -5
  42. package/esm/index.js.map +1 -1
  43. package/esm/pluggableParser.js +35 -4
  44. package/esm/pluggableParser.js.map +1 -1
  45. package/esm/plugin-formatting-codes.js +44 -12
  46. package/esm/plugin-formatting-codes.js.map +1 -1
  47. package/esm/plugin-heading.js +3 -11
  48. package/esm/plugin-heading.js.map +1 -1
  49. package/esm/plugin-items.js +3 -32
  50. package/esm/plugin-items.js.map +1 -1
  51. package/esm/plugin-vmargin.js +16 -3
  52. package/esm/plugin-vmargin.js.map +1 -1
  53. package/esm/selectors.d.ts +3 -2
  54. package/esm/selectors.js +35 -4
  55. package/esm/selectors.js.map +1 -1
  56. package/esm/types.d.ts +30 -6
  57. package/esm/version.d.ts +1 -1
  58. package/esm/version.js +1 -1
  59. package/lib/ast-helpers.d.ts +2 -1
  60. package/lib/ast-helpers.js +31 -2
  61. package/lib/codes/place.d.ts +13 -0
  62. package/lib/codes/place.js +111 -0
  63. package/lib/codes/read.d.ts +21 -0
  64. package/lib/codes/read.js +369 -0
  65. package/lib/codes/scan.d.ts +41 -0
  66. package/lib/codes/scan.js +336 -0
  67. package/lib/exportHtml.js +43 -12
  68. package/lib/exportMarkdown.js +31 -8
  69. package/lib/guard.d.ts +9 -0
  70. package/lib/guard.js +104 -1
  71. package/lib/helpers/blockName.d.ts +1 -0
  72. package/lib/helpers/blockName.js +30 -3
  73. package/lib/helpers/configPropagation.js +3 -1
  74. package/lib/helpers/handlers.d.ts +3 -0
  75. package/lib/helpers/handlers.js +25 -1
  76. package/lib/helpers/headingKeys.d.ts +2 -0
  77. package/lib/helpers/headingKeys.js +50 -0
  78. package/lib/helpers/ids.js +1 -1
  79. package/lib/helpers/numberedShorthand.d.ts +1 -0
  80. package/lib/helpers/numberedShorthand.js +36 -0
  81. package/lib/helpers/stripMarker.d.ts +1 -0
  82. package/lib/helpers/stripMarker.js +28 -0
  83. package/lib/index.d.ts +5 -4
  84. package/lib/index.js +13 -3
  85. package/lib/pluggableParser.js +35 -4
  86. package/lib/plugin-formatting-codes.js +44 -45
  87. package/lib/plugin-heading.js +3 -11
  88. package/lib/plugin-items.js +4 -33
  89. package/lib/plugin-vmargin.js +16 -3
  90. package/lib/selectors.d.ts +3 -2
  91. package/lib/selectors.js +37 -5
  92. package/lib/types.d.ts +30 -6
  93. package/lib/version.d.ts +1 -1
  94. package/lib/version.js +1 -1
  95. package/package.json +2 -2
  96. package/schema/AstTree.json +174 -27
  97. package/schema/PodliteDocument.json +174 -27
  98. package/esm/grammarfc.d.ts +0 -14
  99. package/esm/grammarfc.js +0 -9824
  100. package/esm/grammarfc.js.map +0 -1
  101. package/lib/grammarfc.d.ts +0 -14
  102. package/lib/grammarfc.js +0 -9824
@@ -0,0 +1,369 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readCodes = void 0;
4
+ /*
5
+ =begin pod
6
+
7
+ =TITLE Reading markup codes
8
+
9
+ C<readCodes> turns a fragment into text and markup-code nodes. Where a code
10
+ begins and ends is decided first, by C<scanCodes>; each code then reads only
11
+ the body it was given, and nothing a code reads moves a boundary. A code
12
+ whose body cannot be read is written out as it stands, with a warning.
13
+
14
+ The nodes keep the form the earlier reader gave them, so renderers and other
15
+ readers of the tree see the same fields.
16
+
17
+ =end pod
18
+ */
19
+ const scan_1 = require("./scan");
20
+ const handlers_1 = require("../helpers/handlers");
21
+ const entities_1 = require("entities");
22
+ const RESERVED = ['Q', 'Y'];
23
+ const HS = /^[   \t\u000C ]+/;
24
+ // the closing delimiter a code was opened with
25
+ const closingOf = (code) => (code.bracket === '«' ? '»' : '>'.repeat(code.length));
26
+ const openingOf = (code) => (code.bracket === '«' ? '«' : '<'.repeat(code.length));
27
+ const readCodes = (text, options = {}) => {
28
+ const scope = {
29
+ allowed: options.allowed,
30
+ allowedIn: options.allowedIn,
31
+ entityInCode: options.entityInCode,
32
+ };
33
+ const { codes, issues } = (0, scan_1.scanCodes)(text, scope);
34
+ const report = (code, message, start, end) => options.issues?.push({ code, message, start, end });
35
+ for (const issue of issues)
36
+ reportScan(issue, report);
37
+ // each code is read once, after the codes it holds, so deep nesting needs no recursion
38
+ const built = new Map();
39
+ // text between codes, the codes themselves read by their letter
40
+ const contentOf = (list, from, to) => {
41
+ const out = [];
42
+ let i = from;
43
+ for (const code of list) {
44
+ if (code.start > i)
45
+ out.push(text.slice(i, code.start));
46
+ out.push(...(built.get(code) || []));
47
+ i = code.end;
48
+ }
49
+ if (to > i)
50
+ out.push(text.slice(i, to));
51
+ return out;
52
+ };
53
+ // the parts of a body split at a separator standing at its top level: outside its
54
+ // children and outside the literal pairs of its own kind
55
+ const topLevel = (code, separator) => {
56
+ const at = [];
57
+ let depth = 0;
58
+ let k = 0;
59
+ let i = code.bodyStart;
60
+ while (i < code.bodyEnd) {
61
+ const child = code.children[k];
62
+ if (child && i >= child.start) {
63
+ i = child.end;
64
+ k++;
65
+ continue;
66
+ }
67
+ const c = text[i];
68
+ if (code.bracket === '<' && (c === '<' || c === '>')) {
69
+ let j = i;
70
+ while (j < code.bodyEnd && text[j] === c)
71
+ j++;
72
+ const run = j - i;
73
+ if (c === '<' && run === code.length)
74
+ depth++;
75
+ if (c === '>' && run >= code.length)
76
+ depth = Math.max(0, depth - Math.floor(run / code.length));
77
+ i = j;
78
+ continue;
79
+ }
80
+ if (code.bracket === '«' && c === '«')
81
+ depth++;
82
+ else if (code.bracket === '«' && c === '»')
83
+ depth = Math.max(0, depth - 1);
84
+ else if (c === separator && depth === 0)
85
+ at.push(i);
86
+ i++;
87
+ }
88
+ return at;
89
+ };
90
+ // the children and text of a code between two offsets of its body
91
+ const part = (code, from, to) => contentOf(code.children.filter(c => c.start >= from && c.end <= to), from, to);
92
+ const node = (code) => {
93
+ const name = code.name;
94
+ const body = text.slice(code.bodyStart, code.bodyEnd);
95
+ const children = () => contentOf(code.children, code.bodyStart, code.bodyEnd);
96
+ switch (name) {
97
+ case 'A':
98
+ return [{ content: body, type: 'fcode', name }];
99
+ case 'E': {
100
+ // E<> with nothing in it stands for no character, as before
101
+ const items = body.trim() === '' ? [] : readEntities(body);
102
+ // shown as written: its letter and brackets around what it holds, read as anywhere
103
+ // else, so a G<> or Z<> inside stays hidden; the warning does not repeat the body
104
+ if (items === null) {
105
+ report('markup-code-unreadable', 'E<> holds no entity it can read; it is shown as written', code.start, code.end);
106
+ return [name + openingOf(code), ...children(), code.ending === 'own' ? closingOf(code) : ''];
107
+ }
108
+ return [{ content: items, type: 'fcode', name }];
109
+ }
110
+ case 'D':
111
+ case 'X': {
112
+ const bar = topLevel(code, '|')[0];
113
+ const field = name === 'D' ? 'synonyms' : 'entry';
114
+ if (bar === undefined)
115
+ return [{ content: children(), type: 'fcode', name, [field]: null }];
116
+ return [
117
+ {
118
+ content: part(code, code.bodyStart, bar),
119
+ type: 'fcode',
120
+ name,
121
+ [field]: splitItems(text.slice(bar + 1, code.bodyEnd)),
122
+ },
123
+ ];
124
+ }
125
+ case 'L':
126
+ case 'W':
127
+ return [readLink(code)];
128
+ case 'M': {
129
+ const bar = topLevel(code, '|')[0];
130
+ const content = bar === undefined ? children() : part(code, code.bodyStart, bar);
131
+ const spec = bar === undefined ? '' : text.slice(bar + 1, code.bodyEnd);
132
+ const configAt = spec.search(/\s+:/);
133
+ const module = (configAt < 0 ? spec : spec.slice(0, configAt)).trim();
134
+ const config = configAt < 0 ? [] : attributes(spec.slice(configAt).replace(/^\s+/, ''));
135
+ report('markup-code-unsupported', `M<> names a module this reader cannot run${module ? `: ${module}` : ''}; its text is shown as it is`, code.start, code.end);
136
+ return [{ content, type: 'fcode', name, module: module || null, config }];
137
+ }
138
+ case 'P': {
139
+ const bar = topLevel(code, '|')[0];
140
+ const altContent = bar === undefined ? undefined : part(code, code.bodyStart, bar);
141
+ const src = (bar === undefined ? addressOf(code, code.bodyStart, code.bodyEnd) : addressOf(code, bar + 1, code.bodyEnd)).trim();
142
+ const image = { type: 'image', src };
143
+ if (altContent !== undefined) {
144
+ // an alternative text is a plain string: a comment in it is not written, a hidden
145
+ // word in it is masked, an entity is its character
146
+ image.alt = plain(altContent, true);
147
+ }
148
+ return [{ content: [image], type: 'fcode', name }];
149
+ }
150
+ default:
151
+ if (RESERVED.includes(name)) {
152
+ report('markup-code-reserved', `${name}<> is reserved; it is shown as written`, code.start, code.end);
153
+ return [
154
+ {
155
+ content: children(),
156
+ type: 'fcode',
157
+ name,
158
+ written: [name + openingOf(code), code.ending === 'own' ? closingOf(code) : ''],
159
+ },
160
+ ];
161
+ }
162
+ if (name === 'C' && !(options.allowedIn?.C || []).includes('E'))
163
+ for (const child of code.children)
164
+ if (child.name === 'E')
165
+ report('markup-code-entity-in-code', 'E<> inside C<> reads as an entity only in this release; to keep it, write =config C<> :allow<E>', child.start, child.end);
166
+ return [{ content: children(), type: 'fcode', name }];
167
+ }
168
+ };
169
+ const attributes = (spec) => typeof options.parseAttributes === 'function' ? options.parseAttributes(spec) : spec;
170
+ // L<text|address :config>, L<address :config>, L<address>: the address is what is left
171
+ // between the bar, the first whitespace before a colon, and the end of the code
172
+ const readLink = (code) => {
173
+ const name = code.name;
174
+ const bar = topLevel(code, '|')[0];
175
+ const specFrom = bar === undefined ? code.bodyStart : bar + 1;
176
+ const spec = text.slice(specFrom, code.bodyEnd);
177
+ const configAt = spec.search(/\s+:/);
178
+ if (bar === undefined) {
179
+ if (configAt < 0)
180
+ return { content: contentOf(code.children, code.bodyStart, code.bodyEnd), type: 'fcode', name, meta: null };
181
+ return {
182
+ content: [unquote(addressOf(code, specFrom, specFrom + configAt))],
183
+ type: 'fcode',
184
+ name,
185
+ meta: null,
186
+ config: attributes(spec.slice(configAt).replace(/^\s+/, '')),
187
+ };
188
+ }
189
+ const content = part(code, code.bodyStart, bar);
190
+ // a specification holding spaces is quoted (Specification 4015); the quotes are not part of the address
191
+ const written = addressOf(code, specFrom, configAt < 0 ? code.bodyEnd : specFrom + configAt);
192
+ const address = configAt < 0 ? written : unquote(written);
193
+ if (configAt < 0)
194
+ return { content, type: 'fcode', name, meta: address };
195
+ return {
196
+ content,
197
+ type: 'fcode',
198
+ name,
199
+ meta: address,
200
+ config: attributes(spec.slice(configAt).replace(/^\s+/, '')),
201
+ };
202
+ };
203
+ // the address as written, unless a code that hides or is not written stands in it:
204
+ // then a comment is left out and a hidden word is masked, so neither reaches the link
205
+ // An address as written: its text and codes stay as they stand, except that a
206
+ // comment in it is left out and a hidden word masked, at any depth, so neither
207
+ // reaches a link or a picture. Walked without recursion.
208
+ const addressOf = (code, from, to) => {
209
+ const out = [];
210
+ const work = [];
211
+ const visit = (spans, start, end) => {
212
+ const steps = [];
213
+ let i = start;
214
+ for (const span of spans) {
215
+ if (span.start < start || span.end > end)
216
+ continue;
217
+ if (span.start > i)
218
+ steps.push({ raw: text.slice(i, span.start) });
219
+ steps.push({ span });
220
+ i = span.end;
221
+ }
222
+ if (end > i)
223
+ steps.push({ raw: text.slice(i, end) });
224
+ for (let k = steps.length - 1; k >= 0; k--)
225
+ work.push(steps[k]);
226
+ };
227
+ visit(code.children, from, to);
228
+ while (work.length) {
229
+ const step = work.pop();
230
+ if ('raw' in step) {
231
+ out.push(step.raw);
232
+ continue;
233
+ }
234
+ const span = step.span;
235
+ if (span.name === 'Z')
236
+ continue;
237
+ if (span.name === 'G') {
238
+ out.push((0, handlers_1.maskText)(text.slice(span.bodyStart, span.bodyEnd)));
239
+ continue;
240
+ }
241
+ // the code itself as written, its body walked for what it holds
242
+ work.push({ raw: text.slice(span.bodyEnd, span.end) });
243
+ const before = work.length;
244
+ visit(span.children, span.bodyStart, span.bodyEnd);
245
+ if (work.length === before)
246
+ work.push({ raw: '' });
247
+ out.push(text.slice(span.start, span.bodyStart));
248
+ }
249
+ return out.join('');
250
+ };
251
+ // read the codes after the codes they hold, without recursion
252
+ const order = [];
253
+ const stack = codes.map(code => ({ code, open: false })).reverse();
254
+ while (stack.length) {
255
+ const top = stack.pop();
256
+ if (top.open)
257
+ order.push(top.code);
258
+ else {
259
+ stack.push({ code: top.code, open: true });
260
+ for (let k = top.code.children.length - 1; k >= 0; k--)
261
+ stack.push({ code: top.code.children[k], open: false });
262
+ }
263
+ }
264
+ for (const code of order)
265
+ built.set(code, node(code));
266
+ return contentOf(codes, 0, text.length);
267
+ };
268
+ exports.readCodes = readCodes;
269
+ // the text of read content, without recursion: Z<> left out, G<> masked when asked,
270
+ // an entity its character, the letters of other codes dropped
271
+ const plain = (content, mask) => {
272
+ const out = [];
273
+ const work = content
274
+ .map(item => ({ item, masked: false }))
275
+ .reverse();
276
+ while (work.length) {
277
+ const { item, masked } = work.pop();
278
+ if (typeof item === 'string') {
279
+ out.push(masked ? (0, handlers_1.maskText)(item) : item);
280
+ continue;
281
+ }
282
+ if (item.name === 'Z')
283
+ continue;
284
+ if (item.name === 'E' && Array.isArray(item.content)) {
285
+ const chars = item.content
286
+ .map(e => e.type === 'number'
287
+ ? String.fromCodePoint(Number(e.value))
288
+ : e.type === 'html_named'
289
+ ? (0, entities_1.decodeHTMLStrict)(`&${e.value};`)
290
+ : '')
291
+ .join('');
292
+ out.push(masked ? (0, handlers_1.maskText)(chars) : chars);
293
+ continue;
294
+ }
295
+ const inner = item.content;
296
+ const hide = masked || (mask && item.name === 'G');
297
+ if (typeof inner === 'string')
298
+ out.push(hide ? (0, handlers_1.maskText)(inner) : inner);
299
+ else if (Array.isArray(inner))
300
+ for (let k = inner.length - 1; k >= 0; k--)
301
+ work.push({ item: inner[k], masked: hide });
302
+ }
303
+ return out.join('');
304
+ };
305
+ const holds = (content, name) => {
306
+ const work = [...content];
307
+ while (work.length) {
308
+ const item = work.pop();
309
+ if (typeof item === 'string')
310
+ continue;
311
+ if (item.name === name)
312
+ return true;
313
+ if (Array.isArray(item.content))
314
+ work.push(...item.content);
315
+ }
316
+ return false;
317
+ };
318
+ const reportScan = (issue, report) => {
319
+ if (issue.kind === 'unclosed') {
320
+ const where = issue.ending === 'cut' ? 'by the bracket of the code around it' : 'by the end of the block';
321
+ report('markup-code-unclosed', `${issue.code.name}<> is closed ${where}, not by its own bracket`, issue.code.start, issue.code.end);
322
+ return;
323
+ }
324
+ if (issue.kind === 'foreign-closer') {
325
+ report('markup-code-foreign-closer', `a closing bracket of ${issue.code.name}<> stands inside ${issue.inner}<>, which ends at its own bracket; ${issue.code.name}<> does not end there`, issue.start, issue.end);
326
+ return;
327
+ }
328
+ report('markup-code-longer-run', `a run of angles longer than the delimiter of ${issue.code.name}<> stands inside it`, issue.start, issue.end);
329
+ };
330
+ // entries of D<> and X<> after the bar: split at semicolons, the blanks after one dropped
331
+ const splitItems = (spec) => {
332
+ if (spec === '')
333
+ return [];
334
+ return spec.split(';').map((item, k) => (k === 0 ? item : item.replace(HS, '')));
335
+ };
336
+ // E<lt>, E<0xAB>, E<LEFT DOUBLE ANGLE BRACKET>, E<lt;hellip;0xBB>
337
+ const readEntities = (body) => {
338
+ const items = body.split(';');
339
+ const out = [];
340
+ for (const raw of items) {
341
+ const item = raw.replace(/^[ \t\u000C]+/, '').replace(/[   \t\u000C ]+$/, '');
342
+ let m;
343
+ if ((m = item.match(/^0o([0-9]+)$/)))
344
+ out.push({ type: 'number', value: parseInt(m[1], 8) });
345
+ else if ((m = item.match(/^0d([0-9]+)$/)))
346
+ out.push({ type: 'number', value: parseInt(m[1], 10) });
347
+ else if ((m = item.match(/^0b([0-9]+)$/)))
348
+ out.push({ type: 'number', value: parseInt(m[1], 2) });
349
+ else if ((m = item.match(/^0x[0-9A-Fa-f]+$/)))
350
+ out.push({ type: 'number', value: parseInt(item) });
351
+ else if ((m = item.match(/^[0-9]+$/)))
352
+ out.push({ type: 'number', value: parseInt(item, 10) });
353
+ else if (/^[A-Z ]+$/.test(item))
354
+ out.push({ type: 'unicode_name', value: item });
355
+ else if (/^[a-z][a-z0-9]+$/.test(item))
356
+ out.push({ type: 'html_named', value: item });
357
+ else
358
+ return null;
359
+ }
360
+ return out;
361
+ };
362
+ // quotes around the whole specification are not part of the address
363
+ const unquote = (spec) => {
364
+ const quote = spec[0];
365
+ if ((quote === "'" || quote === '"') && spec.length > 1 && spec[spec.length - 1] === quote)
366
+ return spec.slice(1, -1);
367
+ return spec;
368
+ };
369
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1,41 @@
1
+ export type Bracket = '<' | '«';
2
+ export interface ScanScope {
3
+ allowed?: string[];
4
+ allowedIn?: {
5
+ [code: string]: string[];
6
+ };
7
+ entityInCode?: boolean;
8
+ }
9
+ export type Ending = 'own' | 'cut' | 'fragment';
10
+ export interface CodeSpan {
11
+ name: string;
12
+ bracket: Bracket;
13
+ length: number;
14
+ start: number;
15
+ bodyStart: number;
16
+ bodyEnd: number;
17
+ end: number;
18
+ ending: Ending;
19
+ children: CodeSpan[];
20
+ }
21
+ export type ScanIssue = {
22
+ kind: 'unclosed';
23
+ code: CodeSpan;
24
+ ending: 'cut' | 'fragment';
25
+ } | {
26
+ kind: 'longer-run';
27
+ start: number;
28
+ end: number;
29
+ code: CodeSpan;
30
+ } | {
31
+ kind: 'foreign-closer';
32
+ start: number;
33
+ end: number;
34
+ code: CodeSpan;
35
+ inner: string;
36
+ };
37
+ export interface ScanResult {
38
+ codes: CodeSpan[];
39
+ issues: ScanIssue[];
40
+ }
41
+ export declare const scanCodes: (text: string, scope?: ScanScope) => ScanResult;