-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.ts
426 lines (405 loc) · 14.3 KB
/
rewrite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import AsciiDoctorFactory from 'asciidoctor.js';
import Document = AsciiDoctorJs.Document;
import AbstractBlock = AsciiDoctorJs.AbstractBlock;
import { isNil } from './opal-utils';
import Section = AsciiDoctorJs.Section;
import { isArrayOfBlocks } from './common';
import Block = AsciiDoctorJs.Block;
import Image = AsciiDoctorJs.Image;
import Table = AsciiDoctorJs.Table;
import Attributes = AsciiDoctorJs.Attributes;
import ListItem = AsciiDoctorJs.ListItem;
import {
ifBlockRewriteCellText,
ifBlockRewriterOpen,
rewriteIncludeProcessor,
rewritePreprocessor,
} from './conditionals';
import { nonLocalizableBuiltinAttributeKeys } from './attributes';
import Options = AsciiDoctorJs.Options;
import AttributeEntry = AsciiDoctorJs.AttributeEntry;
export type RewriteTransformer = (extraction: string) => string;
export type Write = (text: string) => void;
interface RewriteMap {
[key: string]: {
open?: () => void;
close?: () => void;
};
}
interface ReWriteState {
listStack: {
type: 'dlist' | 'olist' | 'ulist';
level: number;
}[];
}
function rewriteCommon(text: string, isPath: boolean,
transformer: RewriteTransformer, asciidocOptions: Options = {}): string {
let outputText = '';
function write(text: string) {
outputText += text;
}
const rewriteAsciidoctor = AsciiDoctorFactory();
rewriteAsciidoctor.Extensions.register('rewrite', function() {
this.preprocessor(rewritePreprocessor);
this.includeProcessor(rewriteIncludeProcessor);
});
const document = isPath ?
rewriteAsciidoctor.loadFile(text, asciidocOptions) : rewriteAsciidoctor.load(text, asciidocOptions);
const state: ReWriteState = {
listStack: [],
};
rewriteBlock(document, transformer, write, state);
rewriteAsciidoctor.Extensions.unregister(['rewrite']);
return outputText;
}
export function rewrite(text: string, transformer: RewriteTransformer, asciidocOptions: Options = {}): string {
return rewriteCommon(text, false, transformer, asciidocOptions);
}
export function rewriteFile(inputPath: string, transformer: RewriteTransformer, asciidocOptions: Options = {}): string {
return rewriteCommon(inputPath, true, transformer, asciidocOptions);
}
const attributeQuoteNeededRegex = / |,|"|'/;
const doubleQuotesRegex = /"/g;
export function quoteAttributeValueIfNeeded(value: string): string {
// See https://asciidoctor.org/docs/user-manual/#named-attribute
// If the value contains a space, comma, or quote character, it must be enclosed in double or single quotes
// (e.g., name="value with space"). In all other cases, the surrounding quotes are optional. If present,
// the enclosing quotes are dropped from the parsed value.
if (!attributeQuoteNeededRegex.test(value)) {
return value;
}
const escapedValue = value.replace(doubleQuotesRegex, '\\"');
return `"${escapedValue}"`;
}
function getAttributesString(block: AbstractBlock, transformer: RewriteTransformer,
localizableKeys: string[] = [], excludingKeys: string[] = [],
extraAttributes: Attributes = {}) {
const blockAttributes = block.getAttributes();
let attributesString = '';
for (const attributes of [blockAttributes, extraAttributes]) {
for (const attributeKey in attributes) {
if (excludingKeys.includes(attributeKey)) {
continue;
}
const origValue = attributes[attributeKey];
if (attributeKey === 'attribute_entries') {
continue;
}
const isLocalizable = localizableKeys.includes(attributeKey);
const value = quoteAttributeValueIfNeeded(isLocalizable ? transformer(origValue) : origValue);
const separator = (attributesString !== '') ? ',' : '';
attributesString += `${separator}${attributeKey}=${value}`;
}
}
return attributesString;
}
const pipeRegEx = /\|/g;
export function escapePipes(input: string): string {
return input.replace(pipeRegEx, '\\|');
}
function processCustomAttributes(block: AbstractBlock, transformer: RewriteTransformer,
write: Write, state: ReWriteState) {
const attributes = block.getAttributes();
const attributeEntries = attributes.attribute_entries as any;
if (attributeEntries === undefined) {
return;
}
for (const attributeEntry of attributeEntries as AttributeEntry[]) {
if (block.node_name === 'image' && attributeEntry.name === 'figure-number') {
continue;
}
write(`\n:${attributeEntry.name}: ${transformer(attributeEntry.value)}\n`);
}
}
function rewriteBlock(block: AbstractBlock, transformer: RewriteTransformer, write: Write, state: ReWriteState) {
const listRewrite = {
open: () => {
const listBlock = block as Block;
updateListStack('push', listBlock, state, write);
const attributesString = getAttributesString(
listBlock, transformer, [], ['id', 'style']);
if (attributesString !== '') {
const style = listBlock.getStyle();
const styleStr = isNil(style) ? '' : `${style}, `;
write(`[${styleStr}${attributesString}]\n`);
}
},
close: () => {
updateListStack('pop', block, state, write);
},
};
const rewriteMap: RewriteMap = {
admonition: {
open: () => {
const admonitionBlock = block as Block;
write(`[${admonitionBlock.getStyle()}]\n====\n`);
},
close: () => {
write('====\n');
},
},
dlist: listRewrite,
document: {
open: () => {
const document = block as Document;
if (!isNil(document.header)) {
const doctitle = transformer(document.header.title);
write(`= ${doctitle}\n`);
}
const attributes = document.getAttributes();
if (attributes.authors && attributes.authors !== '') {
write(`${attributes.authors}\n\n`);
}
for (const key of document.attributes_modified.hash.$$keys) {
const isNonLocalizable = nonLocalizableBuiltinAttributeKeys.includes(key);
const originalValue = attributes[key];
if (originalValue as any === undefined) {
continue;
}
const value = isNonLocalizable ? originalValue : transformer(originalValue);
write(`:${key}: ${value}\n`);
}
},
},
floating_title: {
open: () => {
const section = block as Section;
write(`[float]\n${transformer(section.title)}\n----------\n`);
},
},
image: {
open: () => {
const imageBlock = block as Image;
const attributes = imageBlock.getAttributes();
const localizableKeys = ['alt', 'default-alt', 'caption'];
const attributesString = getAttributesString(imageBlock, transformer, localizableKeys);
write(`image::${transformer(attributes.target)}[${attributesString}]\n`);
},
},
list_item: {
open: () => {
const li = block as ListItem;
const text = li.text;
if (isNil(text) || text === '') {
return;
}
const transformedText = transformer(text);
const topOfStack = state.listStack[state.listStack.length - 1];
const indent = ' '.repeat(topOfStack.level * 2);
switch (li.parent.node_name) {
case 'dlist':
const colons = ':'.repeat(1 + topOfStack.level);
write(`${indent}${transformedText}${colons}\n`);
break;
case 'olist':
const periods = '.'.repeat(topOfStack.level);
write(`${indent}${periods} ${transformedText}\n`);
break;
case 'ulist':
const asterisks = '*'.repeat(topOfStack.level);
write(`${indent}${asterisks} ${transformedText}\n`);
break;
}
},
},
listing: {
open: () => {
const listing = block as Block;
const attributesString = getAttributesString(listing, transformer, [], ['style']);
const text = transformer(listing.getSource());
write(`[${listing.getStyle()},${attributesString}]\n${text}\n`);
},
},
literal: {
open: () => {
const literal = block as Block;
const text = transformer(literal.getSource());
write(`....\n${text}\n....`);
},
},
olist: listRewrite,
page_break: {
open: () => {
write('<<<\n');
},
},
pass: {
open: () => {
if (ifBlockRewriterOpen(block as Block, transformer, write)) {
return;
}
const literal = block as Block;
const text = transformer(literal.getSource());
write(`++++\n${text}\n++++`);
},
},
quote: {
open: () => {
const localizableKeys = ['title', 'citetitle'];
const excludingKeys = ['style', 'title'];
const attributesString = getAttributesString(block, transformer, localizableKeys, excludingKeys);
write(`[quote, ${attributesString}]\n____\n`);
},
close: () => {
write('____\n');
},
},
section: {
open: () => {
const section = block as Section;
const attributes = section.getAttributes();
const attributesString = getAttributesString(section, transformer);
write(`[${attributesString}]\n`);
const prefix = '='.repeat(section.level + 1);
write(`${prefix} ${transformer(section.title)}\n`);
},
},
table: {
open: () => {
const table = block as Table;
const localizableKeys = [''];
const excludingKeys = ['rowcount', 'colcount', 'tablepcwidth'];
const extraAttributes: Attributes = {};
if (!isNil(table.caption)) {
extraAttributes.caption = table.caption;
}
const attrs = table.getAttributes();
if (attrs.cols as any === undefined) {
// Synthesize 'cols' attribute if it's missing. For some reason, if the table starts with
// colspans, the colcount is sometimes otherwise incorrectly calculated. Not sure why. Asciidoctor bug?
extraAttributes.cols = `${attrs.colcount}*`;
}
const attributesString = getAttributesString(table, transformer, localizableKeys, excludingKeys,
extraAttributes);
if (attributesString !== '') {
write(`[${attributesString}]\n`);
}
write('|===\n');
const rowsKeys: (keyof Table['rows'])[] = ['head', 'body', 'foot'];
for (const rowKey of rowsKeys) {
for (const row of table.rows[rowKey]) {
for (const cell of row) {
const rowspan = isNil(cell.rowspan) ? 1 : cell.rowspan;
const colspan = isNil(cell.colspan) ? 1 : cell.colspan;
let first = true;
const writeCellContent = (text: string) => {
if (first) {
write(`${colspan}.${rowspan}+|`);
first = false;
}
write(escapePipes(transformer(text.trim())));
};
ifBlockRewriteCellText(cell.text, writeCellContent, write, transformer);
write('\n');
}
}
}
write(`|===\n`);
},
},
thematic_break: {
open: () => {
write('---\n');
},
},
toc: {
open: () => {
write('toc::[]\n');
},
},
paragraph: {
open: () => {
const paragraphBlock = block as Block;
const attributesString = getAttributesString(paragraphBlock, transformer);
write(`[${attributesString}]\n`);
write(`${transformer(paragraphBlock.getSource())}\n\n`);
},
},
preamble: {},
sidebar: {
open: () => {
write('****\n');
},
close: () => {
write('****\n');
},
},
ulist: listRewrite,
verse: {
open: () => {
const verse = block as Block;
const localizableKeys = ['title', 'citetitle'];
const excludingKeys = ['style', 'title'];
const attributesString = getAttributesString(block, transformer, localizableKeys, excludingKeys);
const text = transformer(verse.getSource());
write(`[verse, ${attributesString}]\n____\n${text}\n____\n`);
},
},
};
const rewrite = rewriteMap[block.node_name];
if (!rewrite) {
console.error(`Unknown node name: '${block.node_name}'`);
return;
}
processCustomAttributes(block, transformer, write, state);
if (!isNil(block.id)) {
write(`[[${block.id}]]\n`);
}
if (block.node_name !== 'section' && !isNil(block.title)) {
write(`.${transformer(block.title)}\n`);
}
if (rewrite.open) {
rewrite.open();
}
rewriteBlocks(block.getBlocks(), transformer, write, state);
if (rewrite.close) {
rewrite.close();
}
}
function nodeNameIsList(nodeName: string): nodeName is 'dlist' | 'olist' | 'ulist' {
return ['dlist', 'olist', 'ulist'].includes(nodeName);
}
function updateListStack(pushOrPop: 'push' | 'pop', block: AbstractBlock, state: ReWriteState, write: Write) {
const name = block.node_name as 'dlist' | 'olist' | 'ulist';
const blockIsList = nodeNameIsList(name);
if (!blockIsList) {
throw new Error('Block is expected to be dlist, olist or ulist');
}
if (state.listStack.length > 0) {
const topOfStack = state.listStack[state.listStack.length - 1];
if (pushOrPop === 'push') {
if (topOfStack.type === name) {
topOfStack.level++;
} else {
state.listStack.push({ type: name, level: 1 });
}
} else {
topOfStack.level--;
if (topOfStack.level === 0) {
state.listStack.pop();
if (state.listStack.length === 0) {
// See https://asciidoctor.org/docs/user-manual/#separating-lists
// If you have adjacent lists, they have the tendency to want to fuse together. To force lists apart,
// insert a line comment (//) surrounded by blank lines between the two lists.
write('\n\n//-\n\n');
}
}
}
} else {
if (pushOrPop === 'pop') {
throw new Error('listStack contains no items!');
}
state.listStack.push({ type: name, level: 1 });
}
}
function rewriteBlocks(blocks: Array<AbstractBlock | AbstractBlock[]>,
transformer: RewriteTransformer, write: Write, state: ReWriteState) {
blocks.forEach((block: AbstractBlock | AbstractBlock[]) => {
// Oddly, the blocks of a dlist are arrays of arrays of blocks...
if (isArrayOfBlocks(block)) {
rewriteBlocks(block, transformer, write, state);
} else {
rewriteBlock(block, transformer, write, state);
}
});
}