@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,336 @@
1
+ "use strict";
2
+ /*
3
+ =begin pod
4
+
5
+ =TITLE Where markup codes begin and end
6
+
7
+ C<scanCodes> finds the boundaries of markup codes in one fragment: a paragraph
8
+ body, a code block with C<:allow>, a caption. It does not read what a code
9
+ means; it only says where each code opens, where its content ends and how it
10
+ ended.
11
+
12
+ A code ends at its own matching bracket. A code that never meets its own
13
+ bracket inside an enclosing code ends at the first bracket that closes the
14
+ enclosing code; at the top it runs to the end of the fragment. Whether a code
15
+ meets its own bracket does not depend on what surrounds it, so each code is
16
+ read once on its own, in isolation, and the result is kept for every place
17
+ that meets it again.
18
+
19
+ =end pod
20
+ */
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.scanCodes = void 0;
23
+ const CLOSING = { '<': '>', '«': '»' };
24
+ const VERBATIM_CODES = ['C', 'V'];
25
+ const isLetter = (c) => c >= 'A' && c <= 'Z';
26
+ const scanCodes = (text, scope = {}) => {
27
+ const n = text.length;
28
+ // the end of the run of one bracket each position stands in: a run is measured once
29
+ const runEnd = new Int32Array(n + 1);
30
+ runEnd[n] = n;
31
+ for (let i = n - 1; i >= 0; i--)
32
+ runEnd[i] = i + 1 < n && text[i + 1] === text[i] ? runEnd[i + 1] : i + 1;
33
+ const runFrom = (i) => runEnd[i] - i;
34
+ const fragmentAllows = (name) => !scope.allowed?.length || scope.allowed.includes(name);
35
+ const codeAllows = (outer, name, bracket, depth) => {
36
+ if (outer === null || !VERBATIM_CODES.includes(outer))
37
+ return true;
38
+ // the transition reads what the earlier reader read: E<…> with angles, not E«…»,
39
+ // and not inside a literal pair of the code, as in C<G<E<32>>>
40
+ if (outer === 'C' && name === 'E' && scope.entityInCode && bracket === '<' && depth === 0)
41
+ return true;
42
+ return (scope.allowedIn?.[outer] || []).includes(name);
43
+ };
44
+ const openerAt = (i, outer, depth = 0) => {
45
+ const name = text[i];
46
+ if (!isLetter(name) || i + 1 >= n)
47
+ return null;
48
+ const next = text[i + 1];
49
+ if (next !== '<' && next !== '«')
50
+ return null;
51
+ if (!fragmentAllows(name) || !codeAllows(outer, name, next, depth))
52
+ return null;
53
+ return { name, bracket: next, length: next === '«' ? 1 : runFrom(i + 1) };
54
+ };
55
+ const memo = new Map();
56
+ const makeFrame = (start, opener) => {
57
+ const bodyStart = start + 1 + opener.length;
58
+ return {
59
+ ...opener,
60
+ start,
61
+ bodyStart,
62
+ depth: 0,
63
+ angles: 0,
64
+ at: bodyStart,
65
+ own: [],
66
+ passed: [],
67
+ kids: [],
68
+ foreign: [],
69
+ done: false,
70
+ closed: false,
71
+ bodyEnd: n,
72
+ end: n,
73
+ waiting: -1,
74
+ };
75
+ };
76
+ const finish = (frame, closed, bodyEnd, end) => {
77
+ frame.done = true;
78
+ frame.closed = closed;
79
+ frame.bodyEnd = bodyEnd;
80
+ frame.end = end;
81
+ };
82
+ // A child that did not find its own bracket: the brackets it left free count for
83
+ // this code as if they stood in its own text, nearest first (decision 2.1, 6.1).
84
+ // The child ends at the bracket that closes this code.
85
+ const takeOpenChild = (frame, child) => {
86
+ const closing = CLOSING[frame.bracket];
87
+ const kept = [];
88
+ const pieces = child.passed.length ? child.own.concat(child.passed) : child.own;
89
+ for (let k = 0; k < pieces.length; k++) {
90
+ const piece = pieces[k];
91
+ if (piece.char === frame.bracket && (frame.bracket === '«' || piece.length === frame.length)) {
92
+ frame.depth++;
93
+ continue;
94
+ }
95
+ if (piece.char === closing) {
96
+ let rest = piece.length;
97
+ let at = piece.at;
98
+ while (frame.depth > 0 && rest >= frame.length) {
99
+ frame.depth--;
100
+ rest -= frame.length;
101
+ at += frame.length;
102
+ }
103
+ if (frame.depth === 0 && rest >= frame.length) {
104
+ frame.kids.push({ frame: child, cut: at });
105
+ finish(frame, true, at, at + frame.length);
106
+ return;
107
+ }
108
+ if (rest > 0)
109
+ kept.push({ at, char: piece.char, length: rest });
110
+ continue;
111
+ }
112
+ kept.push(piece);
113
+ }
114
+ frame.kids.push({ frame: child, cut: n });
115
+ frame.passed = kept;
116
+ finish(frame, false, n, n);
117
+ };
118
+ // the brackets a closed child left free, balanced among themselves by this code's kind
119
+ const noteForeign = (frame, child) => {
120
+ const closing = CLOSING[frame.bracket];
121
+ let open = 0;
122
+ const pieces = child.passed.length ? child.own.concat(child.passed) : child.own;
123
+ for (const piece of pieces) {
124
+ if (piece.char === frame.bracket && (frame.bracket === '«' || piece.length === frame.length))
125
+ open++;
126
+ else if (piece.char === closing && piece.length >= frame.length) {
127
+ // a run closes what is open first, nearest first (decision 2.1); what is left stands alone
128
+ let rest = piece.length;
129
+ let at = piece.at;
130
+ while (open > 0 && rest >= frame.length) {
131
+ open--;
132
+ rest -= frame.length;
133
+ at += frame.length;
134
+ }
135
+ if (rest >= frame.length)
136
+ frame.foreign.push({ at, length: rest, inner: child.name });
137
+ }
138
+ }
139
+ };
140
+ // Reads one code's body until it closes, meets a child not yet read, or the
141
+ // fragment ends. Returns the child to read first, or null when the code is done.
142
+ const step = (frame) => {
143
+ const closing = CLOSING[frame.bracket];
144
+ let i = frame.at;
145
+ while (i < n) {
146
+ const c = text[i];
147
+ const opener = openerAt(i, frame.name, frame.bracket === '«' ? frame.angles : frame.depth);
148
+ if (opener) {
149
+ const child = memo.get(i);
150
+ if (!child || !child.done) {
151
+ frame.at = i;
152
+ return i;
153
+ }
154
+ if (child.closed) {
155
+ noteForeign(frame, child);
156
+ frame.kids.push({ frame: child, cut: null });
157
+ i = child.end;
158
+ continue;
159
+ }
160
+ takeOpenChild(frame, child);
161
+ return null;
162
+ }
163
+ if (c === '<') {
164
+ const m = runFrom(i);
165
+ if (frame.bracket === '<' && m === frame.length)
166
+ frame.depth++;
167
+ else
168
+ frame.own.push({ at: i, char: '<', length: m });
169
+ if (frame.bracket === '«')
170
+ frame.angles += m;
171
+ i += m;
172
+ continue;
173
+ }
174
+ if (c === '>') {
175
+ const m = runFrom(i);
176
+ if (frame.bracket === '<') {
177
+ let rest = m;
178
+ let at = i;
179
+ while (frame.depth > 0 && rest >= frame.length) {
180
+ frame.depth--;
181
+ rest -= frame.length;
182
+ at += frame.length;
183
+ }
184
+ if (frame.depth === 0 && rest >= frame.length) {
185
+ finish(frame, true, at, at + frame.length);
186
+ return null;
187
+ }
188
+ if (rest > 0)
189
+ frame.own.push({ at, char: '>', length: rest });
190
+ }
191
+ else {
192
+ frame.own.push({ at: i, char: '>', length: m });
193
+ frame.angles = Math.max(0, frame.angles - m);
194
+ }
195
+ i += m;
196
+ continue;
197
+ }
198
+ if (c === '«') {
199
+ if (frame.bracket === '«')
200
+ frame.depth++;
201
+ else
202
+ frame.own.push({ at: i, char: '«', length: 1 });
203
+ i++;
204
+ continue;
205
+ }
206
+ if (c === '»') {
207
+ if (frame.bracket === '«') {
208
+ if (frame.depth > 0)
209
+ frame.depth--;
210
+ else {
211
+ finish(frame, true, i, i + 1);
212
+ return null;
213
+ }
214
+ }
215
+ else
216
+ frame.own.push({ at: i, char: '»', length: 1 });
217
+ i++;
218
+ continue;
219
+ }
220
+ i++;
221
+ }
222
+ finish(frame, false, n, n);
223
+ return null;
224
+ };
225
+ // reads the code at `start` and every code it needs first, without recursion
226
+ const read = (start, outer) => {
227
+ const known = memo.get(start);
228
+ if (known && known.done)
229
+ return known;
230
+ const first = makeFrame(start, openerAt(start, outer));
231
+ memo.set(start, first);
232
+ const stack = [first];
233
+ while (stack.length) {
234
+ const frame = stack[stack.length - 1];
235
+ const need = step(frame);
236
+ if (need === null) {
237
+ stack.pop();
238
+ continue;
239
+ }
240
+ const child = makeFrame(need, openerAt(need, frame.name, frame.bracket === '«' ? frame.angles : frame.depth));
241
+ memo.set(need, child);
242
+ stack.push(child);
243
+ }
244
+ return first;
245
+ };
246
+ // the fragment itself: never closes; a code without its own bracket runs to the end
247
+ const top = [];
248
+ let i = 0;
249
+ while (i < n) {
250
+ if (openerAt(i, null)) {
251
+ const frame = read(i, null);
252
+ top.push({ frame, cut: frame.closed ? null : n });
253
+ if (!frame.closed)
254
+ break;
255
+ i = frame.end;
256
+ continue;
257
+ }
258
+ i++;
259
+ }
260
+ const issues = [];
261
+ // the tree as it ends up: a code cut by its enclosing code keeps what it read up to the cut
262
+ const build = (kid, limit) => {
263
+ const root = shape(kid.frame, kid.cut, limit);
264
+ const work = [{ span: root, frame: kid.frame }];
265
+ while (work.length) {
266
+ const { span, frame } = work.pop();
267
+ for (const k of frame.kids) {
268
+ if (k.frame.start >= span.bodyEnd)
269
+ break;
270
+ const child = shape(k.frame, k.cut, span.bodyEnd);
271
+ span.children.push(child);
272
+ work.push({ span: child, frame: k.frame });
273
+ }
274
+ }
275
+ return root;
276
+ };
277
+ function shape(frame, cut, limit) {
278
+ const stop = Math.min(cut ?? frame.end, limit);
279
+ const own = frame.closed && frame.end <= stop;
280
+ const ending = own ? 'own' : stop < n ? 'cut' : 'fragment';
281
+ const span = {
282
+ name: frame.name,
283
+ bracket: frame.bracket,
284
+ length: frame.length,
285
+ start: frame.start,
286
+ bodyStart: Math.min(frame.bodyStart, stop),
287
+ bodyEnd: own ? frame.bodyEnd : stop,
288
+ end: own ? frame.end : stop,
289
+ ending,
290
+ children: [],
291
+ };
292
+ if (ending !== 'own')
293
+ issues.push({ kind: 'unclosed', code: span, ending });
294
+ for (const f of frame.foreign)
295
+ if (f.at < span.bodyEnd)
296
+ issues.push({ kind: 'foreign-closer', start: f.at, end: f.at + f.length, code: span, inner: f.inner });
297
+ return span;
298
+ }
299
+ const codes = top.map(k => build(k, n));
300
+ longerRuns(text, codes, runFrom, issues);
301
+ issues.sort((a, b) => issueAt(a) - issueAt(b));
302
+ return { codes, issues };
303
+ };
304
+ exports.scanCodes = scanCodes;
305
+ const issueAt = (issue) => (issue.kind === 'unclosed' ? issue.code.start : issue.start);
306
+ // Inside angle delimiters a run of opening angles longer than the delimiter is an
307
+ // error (Specification 3357–3365); a run of that length is a literal pair. Only
308
+ // the code's own text is looked at: what its children hold is theirs.
309
+ const longerRuns = (text, codes, runFrom, issues) => {
310
+ const work = [...codes];
311
+ while (work.length) {
312
+ const code = work.pop();
313
+ work.push(...code.children);
314
+ if (code.bracket !== '<')
315
+ continue;
316
+ let i = code.bodyStart;
317
+ let k = 0;
318
+ while (i < code.bodyEnd) {
319
+ const child = code.children[k];
320
+ if (child && i >= child.start) {
321
+ i = child.end;
322
+ k++;
323
+ continue;
324
+ }
325
+ if (text[i] === '<') {
326
+ const m = Math.min(runFrom(i), code.bodyEnd - i);
327
+ if (m > code.length)
328
+ issues.push({ kind: 'longer-run', start: i, end: i + m, code });
329
+ i += m;
330
+ continue;
331
+ }
332
+ i++;
333
+ }
334
+ }
335
+ };
336
+ //# sourceMappingURL=scan.js.map
package/lib/exportHtml.js CHANGED
@@ -59,7 +59,7 @@ const hrefAttr = (node, ctx) => {
59
59
  const target = (0, ast_helpers_1.linkTarget)(node);
60
60
  if (target === undefined)
61
61
  return '';
62
- const address = (0, ast_helpers_1.sameDocTarget)(target, ctx);
62
+ const address = (0, ast_helpers_1.sameDocTarget)(target, ctx, ctx?.__anchors, node);
63
63
  // A link level one refused points at nothing, and an anchor with no href is what
64
64
  // html gives that case. An address shaped from a target that was never there only
65
65
  // looks like a working link.
@@ -89,15 +89,32 @@ const anchorOf = (node, ctx) => {
89
89
  return written === null ? null : (ctx?.__anchors?.shape || ast_helpers_1.toFragment)(written);
90
90
  };
91
91
  // the words a test is shown by are hidden with the content they stand beside
92
+ // where a node stands and what it is, for a message that must not print what the node holds
93
+ const atLine = (node) => {
94
+ const line = node?.location?.start?.line;
95
+ return Number.isInteger(line) ? ` at line ${line}` : '';
96
+ };
97
+ const label = (value) => (typeof value === 'string' ? value : '?');
92
98
  const covered = (node, ctx, text) => ((0, guard_1.isCovered)(node, ctx) ? (0, handlers_1.maskText)(text) : text);
93
99
  const openTag = (tag, node, ctx, attrs = '') => {
94
100
  const id = anchorOf(node, ctx);
95
101
  return `<${tag}${id ? ` id="${id}"` : ''}${attrs}>`;
96
102
  };
103
+ const writtenCode = (writer, node, ctx, interator) => {
104
+ const [open, close] = node.written || [`${node.name}<`, '>'];
105
+ writer.write(open);
106
+ interator(node.content, ctx);
107
+ writer.write(close);
108
+ };
97
109
  const rules = {
98
110
  ':text': (writer, processor) => (node, ctx, interator) => {
99
111
  // handle text with content
100
- if (node.value) {
112
+ if (node.value && ctx.keepSpaces) {
113
+ // inside S<>: every space stays and does not break, a line break stays a line break
114
+ const text = writer.escape((0, handlers_1.expandTabs)((0, guard_1.isCovered)(node, ctx) ? (0, handlers_1.maskText)(node.value) : node.value, ctx.keepSpaces));
115
+ writer.writeRaw(text.replace(/ /g, '&nbsp;').replace(/\n/g, '<br>'));
116
+ }
117
+ else if (node.value) {
101
118
  writer.write((0, guard_1.isCovered)(node, ctx) ? (0, handlers_1.maskText)(node.value) : node.value);
102
119
  }
103
120
  else {
@@ -168,7 +185,12 @@ const rules = {
168
185
  return;
169
186
  }
170
187
  writer.writeRaw('<span class="masked">');
171
- writer.write((0, handlers_1.maskText)((0, handlers_1.collectText)(node.content)));
188
+ const masked = (0, handlers_1.maskText)((0, handlers_1.collectText)(node.content));
189
+ // inside S<> the spaces of the hidden phrase stay and do not break, as all its text
190
+ if (ctx.keepSpaces)
191
+ writer.writeRaw(writer.escape((0, handlers_1.expandTabs)(masked, ctx.keepSpaces)).replace(/ /g, '&nbsp;').replace(/\n/g, '<br>'));
192
+ else
193
+ writer.write(masked);
172
194
  writer.writeRaw('</span>');
173
195
  },
174
196
  'H<>': (0, handlers_1.wrapContent)('<sup>', '</sup>'),
@@ -236,11 +258,18 @@ const rules = {
236
258
  });
237
259
  };
238
260
  },
239
- 'S<>': (writer, processor) => (node, ctx, interator) => {
240
- const spaces = covered(node, ctx, (0, handlers_1.collectText)(node.content)).replace(/ /g, '&nbsp;');
241
- const newFeed = spaces.replace(/\n/g, '</br>');
242
- writer.writeRaw(newFeed);
243
- },
261
+ // the text inside is formatted as anywhere else, its whitespace kept (Specification, Space-preserving text)
262
+ 'S<>': (writer, processor) => (node, ctx, interator) => interator(node.content, {
263
+ ...ctx,
264
+ keepSpaces: typeof ctx.keepSpaces === 'object' ? ctx.keepSpaces : { column: 0 },
265
+ }),
266
+ // a module this renderer cannot run shows its text (Specification, M<>)
267
+ 'M<>': handlers_1.content,
268
+ // an inline picture is the picture node it holds
269
+ 'P<>': handlers_1.content,
270
+ // a reserved code is shown as written: its letter and brackets around what it holds
271
+ 'Q<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
272
+ 'Y<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
244
273
  'T<>': (0, handlers_1.wrapContent)('<samp>', '</samp>'),
245
274
  'U<>': (0, handlers_1.wrapContent)('<u>', '</u>'),
246
275
  'V<>': handlers_1.content,
@@ -337,7 +366,7 @@ const rules = {
337
366
  'item:block': (writer, processor) => (node, ctx, interator) => {
338
367
  // make text from first para
339
368
  if (!(node.content instanceof Array)) {
340
- console.error('[pod6] item:block : Error in content of ' + JSON.stringify(node));
369
+ console.error(`[podlite] item:block: content is not a list${atLine(node)}`);
341
370
  }
342
371
  const isTask = node.checked !== undefined;
343
372
  if (isTask) {
@@ -392,8 +421,10 @@ const rules = {
392
421
  writer.writeRaw('</code></pre></div>');
393
422
  },
394
423
  'boundary:block': (writer, processor) => (node, ctx) => {
395
- const conf = (0, config_1.default)(node, ctx);
396
- const id = anchorOf(node, ctx);
424
+ // =config does not reach a =boundary: only what is written on it counts
425
+ const own = { ...ctx, config: {} };
426
+ const conf = (0, config_1.default)(node, own);
427
+ const id = anchorOf(node, own);
397
428
  const idAttr = id ? ` id="${id}"` : '';
398
429
  if (conf.exists('caption')) {
399
430
  writer.writeRaw(`<hr${idAttr} title="`);
@@ -540,7 +571,7 @@ const toHtml = opt => (0, exportAny_1.default)({
540
571
  writer.writeRaw('</h1>');
541
572
  }
542
573
  else {
543
- console.warn('[podlite] Unhandled node' + JSON.stringify(node, null, 2));
574
+ console.warn(`[podlite] unhandled node ${label(node?.name)} (${label(node?.type)})${atLine(node)}`);
544
575
  }
545
576
  if (node.hasOwnProperty('content')) {
546
577
  interator(node.content, ctx);
@@ -82,7 +82,7 @@ const linkWrap = (node, ctx) => {
82
82
  const target = (0, ast_helpers_1.linkTarget)(node);
83
83
  if (target === undefined)
84
84
  return (0, handlers_1.wrapContent)('', '');
85
- const resolved = (0, ast_helpers_1.sameDocTarget)(target, ctx, markdownAnchors(ctx));
85
+ const resolved = (0, ast_helpers_1.sameDocTarget)(target, ctx, markdownAnchors(ctx), node);
86
86
  // Markdown has no anchor without an address either, so a refused link keeps its
87
87
  // text and loses the brackets.
88
88
  if (resolved === undefined)
@@ -138,9 +138,20 @@ const termPara = (writer, processor) => (node, ctx, interator) => {
138
138
  interator(node.content, ctx);
139
139
  writer.writeRaw(':** ');
140
140
  };
141
+ const writtenCode = (writer, node, ctx, interator) => {
142
+ const [open, close] = node.written || [`${node.name}<`, '>'];
143
+ writer.write(open);
144
+ interator(node.content, ctx);
145
+ writer.write(close);
146
+ };
141
147
  const rules = {
142
148
  ':text': (writer, processor) => (node, ctx, interator) => {
143
- if (node.value) {
149
+ if (node.value && ctx.keepSpaces) {
150
+ // inside S<>: every space stays and does not break, a line break stays a line break
151
+ const text = writer.escape((0, handlers_1.expandTabs)((0, guard_1.isCovered)(node, ctx) ? (0, handlers_1.maskText)(node.value) : node.value, ctx.keepSpaces));
152
+ writer.writeRaw(text.replace(/ /g, '&nbsp;').replace(/\n/g, ' \n'));
153
+ }
154
+ else if (node.value) {
144
155
  writer.write((0, guard_1.isCovered)(node, ctx) ? (0, handlers_1.maskText)(node.value) : node.value);
145
156
  }
146
157
  else {
@@ -185,7 +196,12 @@ const rules = {
185
196
  interator(node.content, ctx);
186
197
  return;
187
198
  }
188
- writer.write((0, handlers_1.maskText)((0, handlers_1.collectText)(node.content)));
199
+ const masked = (0, handlers_1.maskText)((0, handlers_1.collectText)(node.content));
200
+ // inside S<> the spaces of the hidden phrase stay and do not break, as all its text
201
+ if (ctx.keepSpaces)
202
+ writer.writeRaw(writer.escape((0, handlers_1.expandTabs)(masked, ctx.keepSpaces)).replace(/ /g, '&nbsp;').replace(/\n/g, ' \n'));
203
+ else
204
+ writer.write(masked);
189
205
  },
190
206
  'H<>': (0, handlers_1.wrapContent)('<sup>', '</sup>'),
191
207
  'J<>': (0, handlers_1.wrapContent)('<sub>', '</sub>'),
@@ -229,11 +245,18 @@ const rules = {
229
245
  });
230
246
  };
231
247
  },
232
- 'S<>': (writer, processor) => (node, ctx, interator) => {
233
- const text = covered(node, ctx, (0, handlers_1.collectText)(node.content));
234
- // preserve spaces and newlines
235
- writer.writeRaw(text.replace(/ /g, '&nbsp;').replace(/\n/g, ' \n'));
236
- },
248
+ // the text inside is formatted as anywhere else, its whitespace kept (Specification, Space-preserving text)
249
+ 'S<>': (writer, processor) => (node, ctx, interator) => interator(node.content, {
250
+ ...ctx,
251
+ keepSpaces: typeof ctx.keepSpaces === 'object' ? ctx.keepSpaces : { column: 0 },
252
+ }),
253
+ // a module this renderer cannot run shows its text (Specification, M<>)
254
+ 'M<>': handlers_1.content,
255
+ // an inline picture is the picture node it holds
256
+ 'P<>': handlers_1.content,
257
+ // a reserved code is shown as written: its letter and brackets around what it holds
258
+ 'Q<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
259
+ 'Y<>': (writer, processor) => (node, ctx, interator) => writtenCode(writer, node, ctx, interator),
237
260
  'T<>': (0, handlers_1.wrapContent)('`', '`'),
238
261
  'U<>': (0, handlers_1.wrapContent)('', ''),
239
262
  'V<>': handlers_1.content,
package/lib/guard.d.ts CHANGED
@@ -3,3 +3,12 @@ export declare const isCovered: (node: unknown, ctx?: {
3
3
  renderMode?: string;
4
4
  }) => boolean;
5
5
  export declare const markGuarded: <T>(node: T, inherited?: boolean) => T;
6
+ export declare const guardedTextLeaves: (node: unknown) => Array<{
7
+ value: string;
8
+ guarded: boolean;
9
+ }>;
10
+ export declare const getMaskedTextContent: (node: unknown) => string;
11
+ export declare const renderCodeText: (node: unknown, ctx?: {
12
+ maskMode?: boolean;
13
+ renderMode?: string;
14
+ }) => string;
package/lib/guard.js CHANGED
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.markGuarded = exports.isCovered = void 0;
6
+ exports.renderCodeText = exports.getMaskedTextContent = exports.guardedTextLeaves = exports.markGuarded = exports.isCovered = void 0;
7
7
  const config_1 = __importDefault(require("./helpers/config"));
8
+ const handlers_1 = require("./helpers/handlers");
8
9
  // Covered content is marked on the node, not decided at render time. A block
9
10
  // carrying :masked or a G<> code can be lifted out of its document by =include,
10
11
  // by a data: reference, by an alias expansion or by a selector; the mark travels
@@ -54,4 +55,106 @@ const markGuarded = (node, inherited = false) => {
54
55
  return node;
55
56
  };
56
57
  exports.markGuarded = markGuarded;
58
+ /*
59
+ =begin pod :kind<export>
60
+
61
+ =head2 guardedTextLeaves
62
+
63
+ The text of a node as the pieces it is made of, in order, each with whether it is
64
+ hidden. It reads what C<getTextContentFromNode> reads: text and verbatim leaves and
65
+ plain strings, through content held as an array or as a single node, and not a
66
+ caption. A leaf is hidden when it or any node above it is a C<G<>> code or carries
67
+ the mark of hidden content, so a leaf the mark did not reach is still reported
68
+ hidden.
69
+
70
+ =end pod
71
+ */
72
+ const guardedTextLeaves = (node) => {
73
+ const leaves = [];
74
+ const walk = (value, covered) => {
75
+ if (Array.isArray(value)) {
76
+ value.forEach(item => walk(item, covered));
77
+ return;
78
+ }
79
+ if (typeof value === 'string') {
80
+ leaves.push({ value, guarded: covered });
81
+ return;
82
+ }
83
+ if (!value || typeof value !== 'object')
84
+ return;
85
+ const target = value;
86
+ const hidden = covered || target.guarded === true || isGuardCode(target) || isMaskedBlock(target);
87
+ if (target.type === 'text' || target.type === 'verbatim') {
88
+ if (typeof target.value === 'string')
89
+ leaves.push({ value: target.value, guarded: hidden });
90
+ return;
91
+ }
92
+ if ('content' in target)
93
+ walk(target.content, hidden);
94
+ };
95
+ walk(node, false);
96
+ return leaves;
97
+ };
98
+ exports.guardedTextLeaves = guardedTextLeaves;
99
+ /*
100
+ =begin pod :kind<export>
101
+
102
+ =head2 getMaskedTextContent
103
+
104
+ The text C<getTextContentFromNode> gives, with each hidden piece under the mask.
105
+ Whitespace keeps its place. For a node with nothing hidden in it the two return
106
+ the same string. Meant for text a host writes out itself, past the renderer: a
107
+ page title, an index entry, a site name.
108
+
109
+ =end pod
110
+ */
111
+ const getMaskedTextContent = (node) => (0, exports.guardedTextLeaves)(node)
112
+ .map(leaf => (leaf.guarded ? (0, handlers_1.maskText)(leaf.value) : leaf.value))
113
+ .join('');
114
+ exports.getMaskedTextContent = getMaskedTextContent;
115
+ /*
116
+ =begin pod :kind<export>
117
+
118
+ =head2 renderCodeText
119
+
120
+ The text a code such as C<S<>> shows, ready for an output to write: its pieces in
121
+ order, each hidden piece under the mask when the context covers it, and nothing of
122
+ a C<Z<>> comment. The cover is inherited from the code itself and from every
123
+ C<G<>> code or hidden mark above a piece, so a piece the mark did not reach is
124
+ still hidden. A forced mask covers everything and draft covers nothing, as
125
+ C<isCovered> decides. The text is not escaped.
126
+
127
+ =end pod
128
+ */
129
+ const renderCodeText = (node, ctx) => {
130
+ const pieces = [];
131
+ const walk = (value, hidden) => {
132
+ if (Array.isArray(value)) {
133
+ value.forEach(item => walk(item, hidden));
134
+ return;
135
+ }
136
+ if (typeof value === 'string') {
137
+ pieces.push(hidden ? (0, handlers_1.maskText)(value) : value);
138
+ return;
139
+ }
140
+ if (!value || typeof value !== 'object')
141
+ return;
142
+ const target = value;
143
+ if (target.type === 'fcode' && target.name === 'Z')
144
+ return;
145
+ const covered = ctx?.maskMode === true ||
146
+ (ctx?.renderMode !== 'draft' &&
147
+ (hidden || target.guarded === true || isGuardCode(target) || isMaskedBlock(target)));
148
+ if (target.type === 'text' || target.type === 'verbatim') {
149
+ if (typeof target.value === 'string')
150
+ pieces.push(covered ? (0, handlers_1.maskText)(target.value) : target.value);
151
+ return;
152
+ }
153
+ if ('content' in target)
154
+ walk(target.content, covered);
155
+ };
156
+ walk(node, false);
157
+ return pieces.join('');
158
+ };
159
+ exports.renderCodeText = renderCodeText;
57
160
  //# sourceMappingURL=guard.js.map
@@ -5,5 +5,6 @@ export type Walked = {
5
5
  text?: unknown;
6
6
  };
7
7
  export declare const isWrapper: (node: Walked) => boolean;
8
+ export declare const isDirectiveNode: (node: Walked) => boolean;
8
9
  export declare const blockNameOf: (node: Walked, holder: Walked | undefined) => string | undefined;
9
10
  export declare const holderInside: (node: Walked, holder: Walked | undefined) => Walked | undefined;