forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmltotext.d
473 lines (408 loc) · 9.91 KB
/
htmltotext.d
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/++
Converts HTML to plain text. Can also output VT escape sequences for terminal output.
The exact output of this is subject to change - it is just what appears nice for me. (I actually use this on my personal email setup.)
+/
module arsd.htmltotext;
import arsd.dom;
import arsd.color;
import std.string;
import std.uni : isWhite;
///
class HtmlConverter {
int width;
/++
Will enable color output using VT codes. Determines color through dom.d's css support, which means you need to apply a stylesheet first.
---
import arsd.dom;
auto document = new Document(source_code_for_html);
auto stylesheet = new Stylesheet(source_code_for_css);
stylesheet.apply(document);
---
+/
bool enableVtOutput;
string color;
string backgroundColor;
///
void htmlToText(Element element, bool preformatted, int width) {
string color, backgroundColor;
if(enableVtOutput) {
color = element.computedStyle.getValue("color");
backgroundColor = element.computedStyle.getValue("background-color");
}
string originalColor = this.color, originalBackgroundColor = this.backgroundColor;
this.color = color.length ? color : this.color;
this.backgroundColor = backgroundColor.length ? backgroundColor : this.backgroundColor;
scope(exit) {
// the idea is as we pop working back up the tree, it restores what it was here
this.color = originalColor;
this.backgroundColor = originalBackgroundColor;
}
this.width = width;
if(auto tn = cast(TextNode) element) {
foreach(dchar ch; tn.nodeValue) {
sink(ch, preformatted);
}
} else {
void sinkChildren() {
foreach(child; element.childNodes)
htmlToText(child, preformatted, width);
}
switch(element.tagName) {
case "head", "script", "style":
// intentionally blank
break;
// The table stuff is removed right now because while it looks
// ok for test tables, it isn't working well for the emails I have
// - it handles data ok but not really nested layouts.
case "trlol":
auto children = element.childElements;
auto tdWidth = (width - cast(int)(children.length)*3) / cast(int)(children.length);
if(tdWidth < 12) {
// too narrow to be reasonable
startBlock();
sinkChildren();
endBlock();
} else {
string[] tdBlocks;
int longestBlock;
foreach(child; children) {
auto fmt = new HtmlConverter();
fmt.htmlToText(child, false, tdWidth);
tdBlocks ~= fmt.s;
int lineCount = 1;
foreach(ch; fmt.s)
if(ch == '\n')
lineCount++;
if(lineCount > longestBlock)
longestBlock = lineCount;
}
if(s.length && s[$-1] != '\n')
s ~= '\n';
foreach(lineNumber; 0 .. longestBlock) {
foreach(bidx, ref block; tdBlocks) {
auto ob = block;
if(bidx)
s ~= " | ";
if(block.length) {
auto idx = block.indexOf("\n");
if(idx == -1)
idx = block.length;
s ~= block[0 .. idx];
if(idx == block.length)
block = block[$..$];
else
block = block[idx + 1 .. $];
}
if(ob.length < tdWidth)
foreach(a; 0 .. tdWidth - block.length)
s ~= " ";
}
s ~= "\n";
}
foreach(a; 0 .. children.length) {
foreach(w; 0 .. tdWidth) {
s ~= "-";
}
if(a +1 != children.length)
s ~= "-+-";
}
s ~= "\n";
}
break;
case "tr":
startBlock(2);
sinkChildren();
endBlock();
break;
case "td":
startBlock(0);
sinkChildren();
endBlock();
break;
case "a":
sinkChildren();
if(element.href != element.innerText) {
sink(' ', false);
sink('<', false);
// I want the link itself to NOT word wrap
// to make for easier double-clicking of it in
// the terminal
foreach(dchar ch; element.href)
sink(ch, false, int.max);
sink('>', false);
}
break;
case "span":
if(enableVtOutput) {
auto csc = color; // element.computedStyle.getValue("color");
if(csc.length) {
auto c = Color.fromString(csc);
s ~= format("\033[38;2;%d;%d;%dm", c.r, c.g, c.b);
}
bool bold = element.computedStyle.getValue("font-weight") == "bold";
if(bold)
s ~= "\033[1m";
sinkChildren();
if(bold)
s ~= "\033[0m";
if(csc.length)
s ~= "\033[39m";
} else {
sinkChildren();
}
break;
case "p":
startBlock();
sinkChildren();
endBlock();
break;
case "b", "strong":
case "em", "i":
if(element.innerText.length == 0)
break;
if(enableVtOutput) {
s ~= "\033[1m";
sinkChildren();
s ~= "\033[0m";
} else {
sink('*', false);
sinkChildren();
sink('*', false);
}
break;
case "u":
if(element.innerText.length == 0)
break;
sink('_', false);
sinkChildren();
sink('_', false);
break;
case "ul":
ulDepth++;
startBlock(2);
sinkChildren();
endBlock();
ulDepth--;
break;
case "ol":
olDepth++;
startBlock(2);
sinkChildren();
endBlock();
olDepth--;
break;
case "li":
startBlock();
//sink('\t', true);
/*
foreach(cnt; 0 .. olDepth + ulDepth) {
sink(' ', true);
sink(' ', true);
}
*/
if(olDepth)
sink('*', false);
if(ulDepth)
sink('*', false);
sink(' ', true);
sinkChildren();
endBlock();
break;
case "dl":
case "dt":
case "dd":
startBlock(element.tagName == "dd" ? 2 : 0);
sinkChildren();
endBlock();
break;
case "h1":
startBlock();
sink('#', true);
sink('#', true);
sink(' ', true);
sinkChildren();
sink(' ', true);
sink('#', true);
sink('#', true);
endBlock();
break;
case "h2", "h3":
startBlock();
sinkChildren();
sink('\n', true);
foreach(dchar ch; element.innerText)
sink(element.tagName == "h2" ? '=' : '-', false);
endBlock();
break;
case "hr":
startBlock();
foreach(i; 0 .. width / 4)
sink(' ', true);
foreach(i; 0 .. width / 2)
sink('-', false);
endBlock();
break;
case "br":
sink('\n', true);
break;
case "div":
startBlock();
/*
auto csc = element.computedStyle.getValue("background-color");
if(csc.length) {
auto c = Color.fromString(csc);
s ~= format("\033[48;2;%d;%d;%dm", c.r, c.g, c.b);
}
*/
sinkChildren();
/*
if(csc.length)
s ~= "\033[49m";
*/
endBlock();
break;
case "pre":
startBlock(4);
foreach(child; element.childNodes)
htmlToText(child, true, width);
endBlock();
break;
default:
sinkChildren();
}
}
}
int olDepth;
int ulDepth;
///
string convert(string html, bool wantWordWrap = true, int wrapAmount = 74) {
Document document = new Document;
document.parse("<roottag>" ~ html ~ "</roottag>");
Element start;
auto bod = document.getElementsByTagName("body");
if(bod.length)
start = bod[0];
else
start = document.root;
//import std.file;
//auto stylesheet = new StyleSheet(readText("/var/www/dpldocs.info/experimental-docs/style.css"));
//stylesheet.apply(document);
return convert(start, wantWordWrap, wrapAmount);
}
///
string convert(Element start, bool wantWordWrap = true, int wrapAmount = 74) {
htmlToText(start, false, wrapAmount);
return s;
}
///
void reset() {
s = null;
justOutputWhitespace = true;
justOutputBlock = true;
justOutputMargin = true;
}
///
string s;
bool justOutputWhitespace = true;
bool justOutputBlock = true;
bool justOutputMargin = true;
int lineLength;
void sink(dchar item, bool preformatted, int lineWidthOverride = int.min) {
if(needsIndent && item != '\n') {
lineLength += doIndent();
needsIndent = false;
}
int width = lineWidthOverride == int.min ? this.width : lineWidthOverride;
if(!preformatted && isWhite(item)) {
if(!justOutputWhitespace) {
item = ' ';
justOutputWhitespace = true;
} else {
return;
}
} else {
// if it is preformatted, we still need to keep track of if it is whitespace
// so stuff like <br> is somewhat sane
justOutputWhitespace = preformatted && isWhite(item);
}
s ~= item;
if(lineLength >= width) {
// rewind to the nearest space, if there is one, to break on a word boundary
int c = lineLength;
bool broken;
foreach_reverse(idx, char ch; s) {
if(ch == '\n')
break;
if(ch == ' ') {
auto os = s;
s = os[0 .. idx];
s ~= '\n';
lineLength = cast(int)(os[idx+1..$].length);
lineLength += doIndent();
s ~= os[idx + 1 .. $];
broken = true;
break;
}
c--;
if(c < 5)
break;
}
if(!broken) {
s ~= '\n';
lineLength = 0;
needsIndent = true;
justOutputWhitespace = true;
}
}
if(item == '\n') {
lineLength = 0;
needsIndent = true;
} else
lineLength ++;
if(!justOutputWhitespace) {
justOutputBlock = false;
justOutputMargin = false;
}
}
int doIndent() {
int cnt = 0;
foreach(i; indentStack)
foreach(lol; 0 .. i) {
s ~= ' ';
cnt++;
}
return cnt;
}
int[] indentStack;
bool needsIndent = false;
void startBlock(int indent = 0) {
indentStack ~= indent;
if(!justOutputBlock) {
s ~= "\n";
lineLength = 0;
needsIndent = true;
justOutputBlock = true;
}
if(!justOutputMargin) {
s ~= "\n";
lineLength = 0;
needsIndent = true;
justOutputMargin = true;
}
}
void endBlock() {
if(indentStack.length)
indentStack = indentStack[0 .. $ - 1];
if(!justOutputMargin) {
s ~= "\n";
lineLength = 0;
needsIndent = true;
justOutputMargin = true;
}
}
}
///
string htmlToText(string html, bool wantWordWrap = true, int wrapAmount = 74) {
auto converter = new HtmlConverter();
return converter.convert(html, wantWordWrap, wrapAmount);
}