text-doc-ir 0.0.13 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/script/main.js DELETED
@@ -1,957 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FixedWidthTextVisitor = void 0;
4
- const baseEncoder_js_1 = require("./baseEncoder.js");
5
- const deps_js_1 = require("./deps.js");
6
- class FixedWidthTextVisitor extends deps_js_1.NodeVisitor {
7
- constructor(width) {
8
- super();
9
- Object.defineProperty(this, "width", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, "lines", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "lazyLines", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- Object.defineProperty(this, "breakLazy", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- Object.defineProperty(this, "spaceLazy", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: void 0
38
- });
39
- Object.defineProperty(this, "breakCount", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, "eatSpaces", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: void 0
50
- });
51
- Object.defineProperty(this, "state", {
52
- enumerable: true,
53
- configurable: true,
54
- writable: true,
55
- value: void 0
56
- });
57
- this.width = width;
58
- this.lines = [];
59
- this.lazyLines = [];
60
- this.breakLazy = false;
61
- this.spaceLazy = false;
62
- this.breakCount = 0;
63
- this.eatSpaces = true;
64
- this.state = {
65
- images: new Map(),
66
- imagesReverse: new Map(),
67
- imageCount: 0,
68
- links: new Map(),
69
- linksReverse: new Map(),
70
- linkCount: 0,
71
- numericDepth: 0,
72
- tocDepth: 0,
73
- };
74
- }
75
- setState(state) {
76
- this.state = state;
77
- }
78
- restoreState(parent) {
79
- parent.state.imageCount = this.state.imageCount;
80
- parent.state.linkCount = this.state.linkCount;
81
- }
82
- text(node) {
83
- this.pushText(node.text);
84
- }
85
- formattedText(node) {
86
- this.pushBlockContentBegin();
87
- for (const line of node.text.split("\n")) {
88
- this.pushLine();
89
- this.eatSpaces = false;
90
- this.pushText(line);
91
- }
92
- this.pushBlockContentEnd();
93
- }
94
- // deno-lint-ignore no-unused-vars
95
- horizontalRule(node) {
96
- this.pushBlockContentBegin();
97
- this.pushText("-".repeat(this.width));
98
- this.pushBlockContentEnd();
99
- }
100
- // deno-lint-ignore no-unused-vars
101
- break_(node) {
102
- this.breakCount++;
103
- }
104
- paragraph(node) {
105
- this.pushBlockContentBegin();
106
- super.paragraph(node);
107
- this.pushBlockContentEnd();
108
- }
109
- list(node) {
110
- if (this.lines.length > 0) {
111
- this.breakLazy = true;
112
- }
113
- this.lazyLines = [];
114
- this.pushBlockContentBegin();
115
- if (node.style == "ordered") {
116
- let counter = 0;
117
- const widthOffset = 4 + this.counterToDepth(node.content.length).length;
118
- for (const item of node.content) {
119
- counter++;
120
- if (counter > 1) {
121
- this.lines.push("");
122
- }
123
- const counterText = this.counterToDepth(counter);
124
- const visitor = new FixedWidthTextVisitor(this.width - widthOffset);
125
- visitor.setState({
126
- ...this.state,
127
- numericDepth: this.state.numericDepth + 1,
128
- });
129
- visitor.visit({ type: "array", content: item.content });
130
- const lines = visitor.getLines();
131
- for (let i = 0; i < lines.length; i++) {
132
- if (i == 0) {
133
- this.lines.push(`${" ".repeat(widthOffset - counterText.length - 2)}${counterText}. ${lines[0]}`);
134
- }
135
- else {
136
- if (lines[i] == "") {
137
- this.lines.push("");
138
- }
139
- else {
140
- this.lines.push(" ".repeat(widthOffset) + lines[i]);
141
- }
142
- }
143
- }
144
- visitor.restoreState(this);
145
- }
146
- }
147
- else {
148
- for (const item of node.content) {
149
- const visitor = new FixedWidthTextVisitor(this.width - 5);
150
- visitor.setState({ ...this.state, numericDepth: 0 });
151
- visitor.visit({ type: "array", content: item.content });
152
- const lines = visitor.getLines();
153
- for (let i = 0; i < lines.length; i++) {
154
- if (i == 0) {
155
- this.lines.push(" * " + lines[0]);
156
- }
157
- else {
158
- this.lines.push(" " + lines[i]);
159
- }
160
- }
161
- visitor.restoreState(this);
162
- }
163
- }
164
- this.pushBlockContentEnd();
165
- }
166
- columns(node) {
167
- const count = node["column-count"];
168
- if (count == 1) {
169
- this.visit({ type: "array", content: node.columns[0] });
170
- return;
171
- }
172
- let consumedWidth = 0;
173
- const generalWidth = Math.floor((this.width - ((count - 1) * 2)) / count);
174
- const columns = [];
175
- let maxLines = 0;
176
- for (let i = 0; i < count; i++) {
177
- if (i > 0) {
178
- consumedWidth += 2;
179
- }
180
- const width = i == count - 1 ? this.width - consumedWidth : generalWidth;
181
- const visitor = new FixedWidthTextVisitor(width);
182
- visitor.setState({ ...this.state, numericDepth: 0 });
183
- visitor.visit({ type: "array", content: node.columns[i] });
184
- const lines = visitor.getLines();
185
- columns.push(lines);
186
- maxLines = Math.max(maxLines, lines.length);
187
- consumedWidth += width;
188
- visitor.restoreState(this);
189
- }
190
- this.pushBlockContentBegin();
191
- for (let i = 0; i < maxLines; i++) {
192
- let line = "";
193
- for (let j = 0; j < count; j++) {
194
- if (j > 0) {
195
- line += " ";
196
- }
197
- const colLine = columns[j][i] || "";
198
- line += colLine + " ".repeat(generalWidth - colLine.length);
199
- }
200
- this.lines.push(line.trimEnd());
201
- }
202
- this.pushBlockContentEnd();
203
- }
204
- link(node) {
205
- super.link(node);
206
- if (this.state.linkCount == 83) {
207
- // console.log(JSON.stringify(this.state));
208
- }
209
- let key;
210
- if (this.state.links.has(node.url)) {
211
- key = this.state.links.get(node.url) || "unreachable";
212
- }
213
- else {
214
- this.state.linkCount++;
215
- key = `L${this.state.linkCount}`;
216
- this.state.links.set(node.url, key);
217
- this.state.linksReverse.set(key, node.url);
218
- }
219
- this.spaceLazy = true;
220
- if (this.spaceLazy && this.lines.length > 0 &&
221
- this.lines[this.lines.length - 1].match(/[{\(\[]$/)) {
222
- this.spaceLazy = false;
223
- }
224
- this.pushText(`[${key}]`);
225
- this.spaceLazy = true;
226
- }
227
- image(node) {
228
- let key;
229
- if (this.state.images.has(node.url)) {
230
- key = this.state.images.get(node.url) || "unreachable";
231
- }
232
- else {
233
- this.state.imageCount++;
234
- key = `I${this.state.imageCount}`;
235
- this.state.images.set(node.url, key);
236
- this.state.imagesReverse.set(key, node.url);
237
- }
238
- this.pushBlockContentBegin();
239
- this.pushText(`[${key}: `);
240
- this.pushText((node.alt || "unspecified").replaceAll(/[\n\r\t]/g, " "));
241
- this.pushText("]");
242
- this.pushBlockContentEnd();
243
- }
244
- figureImage(node) {
245
- let key;
246
- if (this.state.images.has(node.url)) {
247
- key = this.state.images.get(node.url) || "unreachable";
248
- }
249
- else {
250
- this.state.imageCount++;
251
- key = `I${this.state.imageCount}`;
252
- this.state.images.set(node.url, key);
253
- }
254
- this.pushBlockContentBegin();
255
- this.pushText(`[${key}: `);
256
- this.pushText((node.alt || "unspecified").replaceAll(/[\n\r\t]/g, " "));
257
- this.pushText("]");
258
- this.pushBlockContentEnd();
259
- this.visit({
260
- type: "paragraph",
261
- content: node.content,
262
- });
263
- }
264
- emoji(node) {
265
- let key;
266
- if (this.state.images.has(node.url)) {
267
- key = this.state.images.get(node.url) || "unreachable";
268
- }
269
- else {
270
- this.state.imageCount++;
271
- key = `I${this.state.imageCount}`;
272
- this.state.images.set(node.url, key);
273
- }
274
- this.spaceLazy = true;
275
- this.pushText(`[${key}: ${(node.alt || "unspecified").replaceAll(/[\n\r\t]/g, " ")}]`);
276
- this.spaceLazy = true;
277
- }
278
- video(node) {
279
- this.image({
280
- type: "image",
281
- url: node.poster,
282
- alt: `Video: ${node.alt || "unspecified"}`,
283
- });
284
- }
285
- embed(node) {
286
- if (node.content.type == "youtube") {
287
- this.spaceLazy = true;
288
- this.link({
289
- type: "link",
290
- url: `https://youtu.be/${node.content.id}`,
291
- content: [{ type: "text", text: "Youtube Video" }],
292
- });
293
- }
294
- }
295
- header(node) {
296
- const startIndex = this.lines.length;
297
- super.header(node);
298
- const end = this.lines.length;
299
- let maxWidth = 1;
300
- for (let i = startIndex; i < end; i++) {
301
- const line = this.lines[i];
302
- maxWidth = Math.min(this.width, line.length);
303
- }
304
- this.pushLine();
305
- let border = "=";
306
- if (node.level == 2) {
307
- border = "-";
308
- }
309
- else if (node.level == 3) {
310
- border = "^";
311
- }
312
- else if (node.level == 4) {
313
- border = '"';
314
- }
315
- else if (node.level == 5) {
316
- border = "'";
317
- }
318
- else if (node.level == 6) {
319
- border = "`";
320
- }
321
- this.lines[this.lines.length - 1] = border.repeat(maxWidth);
322
- this.breakLazy = false;
323
- this.lazyLines = [""];
324
- }
325
- strikeThrough(node) {
326
- this.pushText("(Strike through: ");
327
- super.strikeThrough(node);
328
- this.pushText(")");
329
- }
330
- note(node) {
331
- this.paragraph({
332
- type: "paragraph",
333
- content: [
334
- { type: "text", text: "Note: " },
335
- ...node.content,
336
- ],
337
- });
338
- }
339
- center(node) {
340
- const visitor = new FixedWidthTextVisitor(this.width);
341
- visitor.setState({ ...this.state, numericDepth: 0 });
342
- visitor.visit({
343
- type: "array",
344
- content: node.content,
345
- });
346
- this.pushBlockContentBegin();
347
- for (const line of visitor.getLines()) {
348
- this.pushEndOfLineIfAnyContent();
349
- this.lines[Math.max(0, this.lines.length - 1)] =
350
- " ".repeat(Math.floor((this.width - line.length) / 2)) + line;
351
- }
352
- visitor.restoreState(this);
353
- this.pushBlockContentEnd();
354
- }
355
- warning(node) {
356
- this.pushBlockContentBegin();
357
- const visitor = new FixedWidthTextVisitor(this.width - 2);
358
- visitor.setState({ ...this.state, numericDepth: 0 });
359
- visitor.visit({
360
- type: "array",
361
- content: node.content,
362
- });
363
- for (const line of visitor.getLines()) {
364
- this.pushEndOfLineIfAnyContent();
365
- this.lines[Math.max(0, this.lines.length - 1)] = "| " + line;
366
- }
367
- visitor.restoreState(this);
368
- this.pushBlockContentEnd();
369
- }
370
- highTechAlert(node) {
371
- this.pushBlockContentBegin();
372
- this.pushText("/");
373
- this.pushText("-".repeat(this.width - 2));
374
- this.pushText("\\");
375
- const centerVisitor = new FixedWidthTextVisitor(this.width - 4);
376
- centerVisitor.setState({ ...this.state, numericDepth: 0 });
377
- centerVisitor.center({
378
- type: "center",
379
- content: node.warning,
380
- });
381
- let anyWarningLines = false;
382
- for (const line of centerVisitor.getLines()) {
383
- anyWarningLines = true;
384
- this.pushEndOfLineIfAnyContent();
385
- this.lines[this.lines.length - 1] = "| " + line +
386
- " ".repeat(this.width - 4 - line.length) + " |";
387
- }
388
- if (anyWarningLines) {
389
- this.pushLine();
390
- this.pushText("|" + "-".repeat(this.width - 2) + "|");
391
- }
392
- centerVisitor.restoreState(this);
393
- const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
394
- contentVisitor.setState({ ...this.state, numericDepth: 0 });
395
- contentVisitor.visit({
396
- type: "array",
397
- content: node.content,
398
- });
399
- for (const line of contentVisitor.getLines()) {
400
- this.pushEndOfLineIfAnyContent();
401
- this.lines[this.lines.length - 1] = "| " + line +
402
- " ".repeat(this.width - 4 - line.length) + " |";
403
- }
404
- this.pushEndOfLineIfAnyContent();
405
- this.pushText("\\");
406
- this.pushText("-".repeat(this.width - 2));
407
- this.pushText("/");
408
- contentVisitor.restoreState(this);
409
- this.pushBlockContentEnd();
410
- }
411
- blockQuote(node) {
412
- this.pushBlockContentBegin();
413
- const visitor = new FixedWidthTextVisitor(this.width - 2);
414
- visitor.setState({ ...this.state, numericDepth: 0 });
415
- visitor.visit({
416
- type: "array",
417
- content: node.content,
418
- });
419
- for (const line of visitor.getLines()) {
420
- this.pushEndOfLineIfAnyContent();
421
- this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
422
- }
423
- visitor.restoreState(this);
424
- this.pushBlockContentEnd();
425
- }
426
- quote(node) {
427
- this.pushBlockContentBegin();
428
- const visitor = new FixedWidthTextVisitor(this.width - 2);
429
- visitor.setState({ ...this.state, numericDepth: 0 });
430
- visitor.visit({
431
- type: "array",
432
- content: [
433
- { type: "text", text: node.name },
434
- { type: "break" },
435
- { type: "break" },
436
- ...node.content,
437
- ],
438
- });
439
- for (const line of visitor.getLines()) {
440
- this.pushEndOfLineIfAnyContent();
441
- this.lines[Math.max(0, this.lines.length - 1)] = "> " + line;
442
- }
443
- visitor.restoreState(this);
444
- this.pushBlockContentEnd();
445
- }
446
- bubble(node) {
447
- this.pushBlockContentBegin();
448
- const visitor = new FixedWidthTextVisitor(this.width - 4);
449
- visitor.setState({ ...this.state, numericDepth: 0 });
450
- visitor.visit({
451
- type: "array",
452
- content: [
453
- ...node.content,
454
- ],
455
- });
456
- for (const line of visitor.getLines()) {
457
- this.pushEndOfLineIfAnyContent();
458
- if (node.orientation == "left") {
459
- this.lines[Math.max(0, this.lines.length - 1)] = "| " + line;
460
- }
461
- else if (node.orientation == "right") {
462
- this.lines[Math.max(0, this.lines.length - 1)] =
463
- " ".repeat(this.width - 2 - line.length) + line + " |";
464
- }
465
- }
466
- visitor.restoreState(this);
467
- this.pushBlockContentEnd();
468
- }
469
- sticker(node) {
470
- this.pushBlockContentBegin();
471
- const visitor = new FixedWidthTextVisitor(this.width - 4);
472
- visitor.setState({ ...this.state, numericDepth: 0 });
473
- visitor.visit({
474
- type: "array",
475
- content: [
476
- ...(node.content || []),
477
- ],
478
- });
479
- this.pushEndOfLineIfAnyContent();
480
- const label = `[${node.character}: ${node.name}]`;
481
- if (node.orientation == "left") {
482
- this.lines[Math.max(0, this.lines.length - 1)] = "/" + label +
483
- "-".repeat(this.width - label.length - 2) + "\\";
484
- }
485
- else if (node.orientation == "right") {
486
- this.lines[Math.max(0, this.lines.length - 1)] = "/" +
487
- "-".repeat(this.width - label.length - 2) + label + "\\";
488
- }
489
- else if (node.orientation == "center") {
490
- const length = this.width - label.length - 2;
491
- const leftLength = Math.floor(length / 2);
492
- const rightLength = length - leftLength;
493
- this.lines[Math.max(0, this.lines.length - 1)] = "/" +
494
- "-".repeat(leftLength) + label + "-".repeat(rightLength) + "\\";
495
- }
496
- for (const line of visitor.getLines()) {
497
- this.pushEndOfLineIfAnyContent();
498
- const length = this.width - 3 - line.length;
499
- if (node.orientation == "left") {
500
- this.lines[Math.max(0, this.lines.length - 1)] = "| " + line +
501
- " ".repeat(length) + "|";
502
- }
503
- else if (node.orientation == "right") {
504
- this.lines[Math.max(0, this.lines.length - 1)] = "|" +
505
- " ".repeat(length) + line + " |";
506
- }
507
- else if (node.orientation == "center") {
508
- const leftLength = Math.floor(length / 2);
509
- const rightLength = length - leftLength;
510
- this.lines[Math.max(0, this.lines.length - 1)] = "|" +
511
- " ".repeat(leftLength + 1) + line + " ".repeat(rightLength) + "|";
512
- }
513
- }
514
- this.pushEndOfLineIfAnyContent();
515
- this.pushText("\\");
516
- this.pushText("-".repeat(this.width - 2));
517
- this.pushText("/");
518
- visitor.restoreState(this);
519
- this.pushBlockContentEnd();
520
- }
521
- card(node) {
522
- this.pushBlockContentBegin();
523
- this.pushText("/");
524
- this.pushText("-".repeat(this.width - 2));
525
- this.pushText("\\");
526
- let anyHeaderLines = false;
527
- if (node.header) {
528
- const centerVisitor = new FixedWidthTextVisitor(this.width - 4);
529
- centerVisitor.setState({ ...this.state, numericDepth: 0 });
530
- centerVisitor.center({
531
- type: "center",
532
- content: [
533
- ...node.header.title,
534
- ...(node.header.username &&
535
- [{
536
- type: "text",
537
- text: ` @${node.header.username}`,
538
- }] || []),
539
- ...(node.header.username &&
540
- [{
541
- type: "text",
542
- text: `@${node.header.usernameDomain}`,
543
- }] || []),
544
- ],
545
- });
546
- for (const line of centerVisitor.getLines()) {
547
- anyHeaderLines = true;
548
- this.pushEndOfLineIfAnyContent();
549
- this.lines[this.lines.length - 1] = "| " + line +
550
- " ".repeat(this.width - 4 - line.length) + " |";
551
- }
552
- if (anyHeaderLines) {
553
- this.pushLine();
554
- this.pushText("|" + "-".repeat(this.width - 2) + "|");
555
- }
556
- centerVisitor.restoreState(this);
557
- }
558
- const contentVisitor = new FixedWidthTextVisitor(this.width - 4);
559
- contentVisitor.setState({ ...this.state, numericDepth: 0 });
560
- const content = [
561
- ...(node.content && node.content.content || []),
562
- ...(node.media && node.media.content || []),
563
- ];
564
- if (node.attribution) {
565
- let date;
566
- if (node.attribution.date) {
567
- try {
568
- date = new Date(node.attribution.date);
569
- }
570
- catch (_e) {
571
- // Oh well
572
- }
573
- }
574
- content.push({
575
- type: "paragraph",
576
- content: [
577
- ...(node.attribution.title || []),
578
- ...(node.attribution.url && [{
579
- type: "link",
580
- url: node.attribution.url,
581
- content: [],
582
- }] || []),
583
- ...(node.attribution.archiveUrl && [{
584
- type: "link",
585
- url: node.attribution.archiveUrl,
586
- content: [],
587
- }] || []),
588
- ...(date && [{
589
- type: "text",
590
- text: date.toLocaleDateString(),
591
- }] || []),
592
- ],
593
- });
594
- }
595
- contentVisitor.visit({
596
- type: "array",
597
- content,
598
- });
599
- for (const line of contentVisitor.getLines()) {
600
- this.pushEndOfLineIfAnyContent();
601
- this.lines[this.lines.length - 1] = "| " + line +
602
- " ".repeat(this.width - 4 - line.length) + " |";
603
- }
604
- this.pushEndOfLineIfAnyContent();
605
- this.pushText("\\");
606
- this.pushText("-".repeat(this.width - 2));
607
- this.pushText("/");
608
- contentVisitor.restoreState(this);
609
- this.pushBlockContentEnd();
610
- }
611
- definitionReference(node) {
612
- this.visit({
613
- type: "array",
614
- content: [
615
- ...node.content,
616
- ],
617
- });
618
- }
619
- definition(node) {
620
- this.pushBlockContentBegin();
621
- this.chooseChildren(node.title);
622
- this.pushText(" (");
623
- this.chooseChildren(node.abbreviation);
624
- this.pushText("):");
625
- this.pushLine();
626
- const visitor = new FixedWidthTextVisitor(this.width - 2);
627
- visitor.setState({ ...this.state, numericDepth: 0 });
628
- visitor.visit({
629
- type: "array",
630
- content: node.content,
631
- });
632
- for (const line of visitor.getLines()) {
633
- this.pushEndOfLineIfAnyContent();
634
- this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
635
- }
636
- visitor.restoreState(this);
637
- this.pushBlockContentEnd();
638
- }
639
- toc(node) {
640
- if (this.state.tocDepth > 0) {
641
- const visitor = new FixedWidthTextVisitor(this.width - 3);
642
- visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
643
- if (node.date) {
644
- visitor.choose(node.date);
645
- visitor.pushText(" ");
646
- }
647
- visitor.chooseChildren(node.content);
648
- if (node.href && !node.href.startsWith("#")) {
649
- visitor.link({
650
- type: "link",
651
- url: node.href,
652
- content: [],
653
- });
654
- visitor.spaceLazy = true;
655
- }
656
- visitor.restoreState(this);
657
- visitor.visit({
658
- type: "array",
659
- content: node.children,
660
- });
661
- let counter = 0;
662
- for (const line of visitor.getLines()) {
663
- this.pushEndOfLineIfAnyContent();
664
- // First line gets a bullet point
665
- if (counter == 0) {
666
- this.lines[Math.max(0, this.lines.length - 1)] = "* " + line;
667
- }
668
- else {
669
- this.lines[Math.max(0, this.lines.length - 1)] = " " + line;
670
- }
671
- counter++;
672
- }
673
- visitor.restoreState(this);
674
- }
675
- else {
676
- const visitor = new FixedWidthTextVisitor(this.width - 4);
677
- visitor.setState({ ...this.state, tocDepth: this.state.tocDepth + 1 });
678
- if (node.date) {
679
- visitor.choose(node.date);
680
- visitor.pushText(" ");
681
- }
682
- visitor.chooseChildren(node.content);
683
- if (node.href && !node.href.startsWith("#")) {
684
- visitor.link({
685
- type: "link",
686
- url: node.href,
687
- content: [],
688
- });
689
- visitor.spaceLazy = true;
690
- }
691
- visitor.pushLine();
692
- visitor.visit({
693
- type: "array",
694
- content: node.children,
695
- });
696
- const tocLen = " Table of contents ".length;
697
- const lines = visitor.getLines();
698
- const maxLength = lines.reduce((prev, next) => Math.max(prev, next.length + 1), tocLen + 2);
699
- const left = Math.floor((maxLength - tocLen) / 2) + 1;
700
- const right = maxLength - tocLen - left + 1;
701
- this.pushBlockContentBegin();
702
- this.lines[Math.max(0, this.lines.length - 1)] = "/" + "-".repeat(left) +
703
- " Table of contents " + "-".repeat(right) + "\\";
704
- for (const line of lines) {
705
- this.pushEndOfLineIfAnyContent();
706
- this.lines[Math.max(0, this.lines.length - 1)] = "| " + line +
707
- " ".repeat(maxLength - line.length) + "|";
708
- }
709
- this.pushEndOfLineIfAnyContent();
710
- this.lines[Math.max(0, this.lines.length - 1)] = "\\" +
711
- "-".repeat(maxLength + 1) + "/";
712
- visitor.restoreState(this);
713
- this.pushBlockContentEnd();
714
- }
715
- }
716
- document(node) {
717
- this.pushBlockContentBegin();
718
- let date;
719
- try {
720
- if (node["pub-date"]) {
721
- date = new Date(node["pub-date"] * 1000);
722
- }
723
- }
724
- catch (_e) {
725
- // Nothing
726
- }
727
- this.center({
728
- type: "center",
729
- content: [
730
- { type: "text", text: node.title },
731
- { type: "break" },
732
- ...(date &&
733
- [{ type: "text", text: date.toDateString() }, {
734
- type: "break",
735
- }] || []),
736
- ...(node.description &&
737
- [{ type: "text", text: node.description }] || []),
738
- { type: "horizontal-rule" },
739
- ],
740
- });
741
- super.document(node);
742
- this.horizontalRule({ type: "horizontal-rule" });
743
- if (this.state.linkCount > 0) {
744
- const max = this.state.linkCount;
745
- const length = Math.max(7, `[L${max}]: `.length);
746
- for (let i = 1; i <= max; i++) {
747
- const label = `[L${i}]: `;
748
- const visitor = new FixedWidthTextVisitor(this.width - length);
749
- visitor.text({
750
- "type": "text",
751
- text: this.state.linksReverse.get(`L${i}`) || "",
752
- });
753
- let firstLine = true;
754
- for (const line of visitor.getLines()) {
755
- if (!firstLine) {
756
- this.lines.push(" ".repeat(length) + line);
757
- }
758
- else {
759
- this.lines.push(" ".repeat(length - label.length) + label + line);
760
- firstLine = false;
761
- }
762
- }
763
- }
764
- }
765
- if (this.state.imageCount > 0) {
766
- const length = Math.max(7, `[L${this.state.imageCount}]: `.length);
767
- for (let i = 1; i <= this.state.imageCount; i++) {
768
- const label = `[I${i}]: `;
769
- const visitor = new FixedWidthTextVisitor(this.width - length);
770
- visitor.text({
771
- "type": "text",
772
- text: this.state.imagesReverse.get(`I${i}`) || "",
773
- });
774
- let firstLine = true;
775
- for (const line of visitor.getLines()) {
776
- if (!firstLine) {
777
- this.lines.push(" ".repeat(length) + line);
778
- }
779
- else {
780
- this.lines.push(" ".repeat(length - label.length) + label + line);
781
- firstLine = false;
782
- }
783
- }
784
- }
785
- }
786
- }
787
- counterToDepth(counter) {
788
- const num = this.state.numericDepth % 3;
789
- if (num == 0) {
790
- return `${counter}`;
791
- }
792
- else if (num == 1) {
793
- return (0, baseEncoder_js_1.encodeBase)(baseEncoder_js_1.UPPER_ALPHA, counter, true);
794
- }
795
- else {
796
- return (0, baseEncoder_js_1.encodeBase)(baseEncoder_js_1.LOWER_ALPHA, counter, true);
797
- }
798
- }
799
- pushBlockContentBegin() {
800
- this.pushLazyLines();
801
- if (this.lines.length > 0) {
802
- // Clear last line conditionally
803
- this.pushEndOfLineIfAnyContent();
804
- }
805
- }
806
- pushBlockContentEnd() {
807
- this.lazyLines.push("");
808
- }
809
- pushLazyLines() {
810
- let newLines = false;
811
- if (this.breakCount > 0) {
812
- this.breakCount--;
813
- if (this.lines[this.lines.length - 1] &&
814
- this.lines[this.lines.length - 1].length > 0) {
815
- this.lines.push("");
816
- }
817
- while (this.breakCount > 0) {
818
- this.breakCount--;
819
- this.lines.push("");
820
- }
821
- }
822
- if (this.breakLazy && this.lines[this.lines.length - 1] &&
823
- this.lines[this.lines.length - 1].length > 0) {
824
- this.lines.push("");
825
- newLines = true;
826
- }
827
- if (this.lazyLines.length > 0) {
828
- this.pushEndOfLineIfAnyContent();
829
- newLines = true;
830
- }
831
- for (const line of this.lazyLines) {
832
- this.lines.push(line);
833
- newLines = true;
834
- }
835
- if (!newLines && this.spaceLazy && this.lines.length > 0) {
836
- const lastLine = this.getLastLine();
837
- if (!lastLine.endsWith(" ") && lastLine.length < this.width &&
838
- lastLine.length > 0) {
839
- this.lines[this.lines.length - 1] += " ";
840
- }
841
- }
842
- if (this.getLastLine().length == this.width) {
843
- this.lines.push("");
844
- }
845
- this.lazyLines = [];
846
- this.breakLazy = false;
847
- this.spaceLazy = false;
848
- }
849
- pushEndOfLineIfAnyContent() {
850
- if (this.getLastLine() != "") {
851
- this.pushLine();
852
- }
853
- }
854
- getLastLine() {
855
- if (this.lines.length == 0) {
856
- return "";
857
- }
858
- return this.lines[this.lines.length - 1];
859
- }
860
- pushText(text) {
861
- if (this.spaceLazy && text.match(/^[\.,}\)\]]/)) {
862
- this.spaceLazy = false;
863
- }
864
- this.pushLazyLines();
865
- let index = this.lines.length - 1;
866
- let line;
867
- if (index >= 0) {
868
- line = this.lines[index];
869
- }
870
- else {
871
- line = "";
872
- index = 0;
873
- }
874
- let sliceStart = 0;
875
- do {
876
- if ((line == "" && this.eatSpaces) || line.endsWith(" ")) {
877
- // skip white space
878
- while (true) {
879
- const nextChar = text.slice(sliceStart, sliceStart + 1);
880
- if (nextChar == "") {
881
- break;
882
- }
883
- if (nextChar == " ") {
884
- sliceStart++;
885
- }
886
- else {
887
- break;
888
- }
889
- }
890
- }
891
- this.eatSpaces = true;
892
- let sliceEnd = sliceStart + this.width - line.length;
893
- let slice = text.slice(sliceStart, sliceEnd);
894
- const nextChar = text.slice(sliceEnd, sliceEnd + 1);
895
- if (slice == "") {
896
- break;
897
- }
898
- else if (nextChar && nextChar.match(/[a-zA-Z0-9\.\?,\]\)]/)) {
899
- let success = false;
900
- for (let i = 0; i < slice.length; i++) {
901
- const index = slice.length - 1 - i;
902
- const char = slice.charAt(index);
903
- if (char && char.match(/[ =\-_\/\\\n\r\t]/)) {
904
- sliceEnd -= i;
905
- slice = text.slice(sliceStart, sliceEnd);
906
- success = true;
907
- break;
908
- }
909
- }
910
- if (success) {
911
- this.lines[index] = line + slice;
912
- if (this.lines[index].length > this.width) {
913
- console.log("oops 1");
914
- }
915
- }
916
- else {
917
- if (this.getLastLine().length > 0) {
918
- sliceEnd = sliceStart;
919
- this.lines[index] = line;
920
- }
921
- else {
922
- // No choice but to break it;
923
- this.lines[index] = line + slice;
924
- }
925
- }
926
- this.pushLine();
927
- index++;
928
- line = this.lines[index];
929
- sliceStart = sliceEnd;
930
- }
931
- else {
932
- this.lines[index] = line + slice;
933
- sliceStart = sliceEnd;
934
- if (nextChar == "") {
935
- break;
936
- }
937
- }
938
- if (this.lines[index].length == this.width) {
939
- this.pushLine();
940
- index++;
941
- line = this.lines[index];
942
- }
943
- } while (true);
944
- }
945
- pushLine() {
946
- const lastIndex = this.lines.length - 1;
947
- if (lastIndex >= 0 && this.lines[lastIndex].endsWith(" ")) {
948
- const line = this.lines[lastIndex];
949
- this.lines[lastIndex] = line.trimEnd();
950
- }
951
- this.lines.push("");
952
- }
953
- getLines() {
954
- return this.lines;
955
- }
956
- }
957
- exports.FixedWidthTextVisitor = FixedWidthTextVisitor;