-
Notifications
You must be signed in to change notification settings - Fork 41
/
read-textile.ts
executable file
·235 lines (219 loc) · 7.45 KB
/
read-textile.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
import readline from 'readline';
import util from 'util';
import fs from 'fs';
import Turndown from 'turndown';
import * as Diff from 'diff';
import {
textileToHtml,
textileToCheerio,
processTextile,
fullyParseTextile,
} from './data/test-utilities/process-textile';
import { preParserSteps, Step } from './data/cli-functionality/parser-steps/pre-parser';
import { textileWorkaroundSteps } from './data/cli-functionality/parser-steps/textile-workarounds';
import { postParserSteps } from './data/cli-functionality/parser-steps/post-parser';
import { htmlParserSteps } from './data/cli-functionality/parser-steps/html-parser';
/**
* Flags:
* --diff - output each step with the difference to the last step highlighted
* --diffOnly - output only the difference between each step
*/
process.on('exit', () => fs.rmSync('./dist', { recursive: true, force: true }));
const argsToRead = process.argv
.slice(2)
.filter((arg) => !arg.startsWith('--'))
.map((arg) => {
try {
const content = fs.readFileSync(arg);
return content.toString();
} catch (e) {
//Fine to ignore.
}
return arg;
})
.map((arg) => arg.replace(/\\n/g, '\n'));
const flags: Record<string, boolean | string> = process.argv
.slice(2)
.filter((arg) => arg.startsWith('--'))
.reduce((acc, arg) => {
const [key, value] = arg.replace(/^--/, '').split('=');
return {
...acc,
[key]: value ?? true,
};
}, {});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = util.promisify(rl.question).bind(rl);
const turndownService = new Turndown();
turndownService.keep(['div', 'span']);
const queryUser = async (query: string, outputHandler: (answer: string) => void) => {
rl.question(query, outputHandler);
};
const logFunctionWithDescription = (step: Step) => {
console.log(step.fn);
if (step.description) {
console.log(`\n\x1b[33m${step.description}\x1b[0m\n`);
}
};
const executeSequentially = (queries: string[], outputHandlers: ((answer: string) => void)[]) => {
queries.forEach(async (query, i) => await queryUser(query, outputHandlers[i]));
};
const steps = [...preParserSteps, ...textileWorkaroundSteps, ...postParserSteps, ...htmlParserSteps];
const logArgProcessingError = (name: string, arg: string, i: number) =>
console.error(`\n\nError processing ${name} ${i + 1}:\n${arg}\n`);
const diffText = (oldText: string, newText: string) => {
const diffResult = Diff.diffLines(oldText, newText);
diffResult.forEach((diffPart) => {
const color = diffPart.added ? '\x1b[32m' : diffPart.removed ? '\x1b[31m' : '\x1b[1;30m';
console.log(`${color}${diffPart.value}\x1b[0m`);
});
};
const diffOnlyText = (oldText: string, newText: string) => {
const diffResult = Diff.diffLines(oldText, newText);
diffResult.forEach((diffPart) => {
if (diffPart.added) {
console.log(`${'\x1b[32m'}${diffPart.value}\x1b[0m`);
} else if (diffPart.removed) {
console.log(`${'\x1b[31m'}${diffPart.value}\x1b[0m`);
}
});
};
const initialQuery = ['How would you like to debug the textile? (h)tml, (m)arkdown, (s)tep by step or (a)dvanced?'];
const initialOutputHandler = [
async (answer: string) => {
switch (answer) {
case 'h':
argsToRead.forEach((arg, i) => {
console.log('\nHTML mode\n');
try {
if (flags.diff) {
diffText(arg, textileToHtml(arg));
} else if (flags.diffOnly) {
diffOnlyText(arg, textileToHtml(arg));
} else {
console.log(`\n${textileToHtml(arg)}\n`);
}
} catch (e) {
logArgProcessingError('argument', arg, i);
console.error(e);
}
});
process.exit();
break;
case 'm':
argsToRead.forEach((arg, i) => {
console.log('\nMarkdown mode\n');
try {
const markdown = turndownService.turndown(textileToHtml(arg));
if (flags.diff) {
diffText(arg, markdown);
} else if (flags.diffOnly) {
diffOnlyText(arg, markdown);
} else {
console.log(`\n${markdown}\n`);
}
} catch (e) {
logArgProcessingError('argument', arg, i);
console.error(e);
}
});
process.exit();
break;
case 's':
console.log('\nStep by step mode - \n\x1b[33mwill only operate on first argument provided\x1b[0m\n\n');
// eslint-disable-next-line no-case-declarations
let result = argsToRead[0];
// eslint-disable-next-line no-case-declarations
const recursivelyReadSteps = async (i: number) => {
if (i >= steps.length) {
return;
}
const step = steps[i];
try {
console.log(`\nOutput on step ${i + 1}\n`);
logFunctionWithDescription(step);
if (flags.diff) {
const nextResult = step.fn(result);
diffText(result, nextResult);
result = nextResult;
logFunctionWithDescription(step);
} else if (flags.diffOnly) {
const nextResult = step.fn(result);
diffOnlyText(result, nextResult);
result = nextResult;
} else {
result = step.fn(result);
console.log(`\x1b[32m${result}\x1b[0m\n`);
}
const possiblyQuit = await question('Press enter to proceed, q to quit: ');
// @ts-ignore
if (possiblyQuit === 'q') {
process.exit();
}
await recursivelyReadSteps(i + 1);
} catch (e) {
logArgProcessingError('argument', result, 0);
logArgProcessingError('step', step.description ?? step.fn.prototype?.name ?? '', i);
console.error(e);
return;
}
return result;
};
await recursivelyReadSteps(0);
process.exit();
break;
case 'a':
// eslint-disable-next-line no-case-declarations
const selected = await question(
'\nSelecting from advanced modes, (c)heerio rendered into html, (a)rray of cheerio values, (f)inal objects:',
);
switch (selected) {
// @ts-ignore
case 'c':
argsToRead.forEach((arg, i) => {
try {
console.log(`\n${textileToCheerio(arg)}\n`);
} catch (e) {
logArgProcessingError('argument', arg, i);
console.error(e);
}
});
break;
// @ts-ignore
case 'a':
argsToRead.forEach((arg, i) => {
try {
console.log('\n');
console.log(processTextile(arg));
console.log('\n');
} catch {
logArgProcessingError('argument', arg, i);
}
});
break;
// @ts-ignore
case 'f':
argsToRead.forEach((arg, i) => {
try {
console.log(`\n${JSON.stringify(fullyParseTextile(arg), null, ' ')}\n`);
} catch {
logArgProcessingError('argument', arg, i);
}
});
break;
default:
process.exit();
break;
}
process.exit();
break;
default:
process.exit();
break;
}
},
];
executeSequentially(initialQuery, initialOutputHandler);