mirror of https://github.com/adamdruppe/arsd.git
better email conversion stuff
This commit is contained in:
parent
62bf62aa4d
commit
09a44d8348
99
htmltotext.d
99
htmltotext.d
|
@ -28,7 +28,7 @@ class HtmlConverter {
|
||||||
// The table stuff is removed right now because while it looks
|
// 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
|
// ok for test tables, it isn't working well for the emails I have
|
||||||
// - it handles data ok but not really nested layouts.
|
// - it handles data ok but not really nested layouts.
|
||||||
case "trfixme":
|
case "trlol":
|
||||||
auto children = element.childElements;
|
auto children = element.childElements;
|
||||||
|
|
||||||
auto tdWidth = (width - cast(int)(children.length)*3) / cast(int)(children.length);
|
auto tdWidth = (width - cast(int)(children.length)*3) / cast(int)(children.length);
|
||||||
|
@ -91,6 +91,16 @@ class HtmlConverter {
|
||||||
s ~= "\n";
|
s ~= "\n";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "tr":
|
||||||
|
startBlock(2);
|
||||||
|
sinkChildren();
|
||||||
|
endBlock();
|
||||||
|
break;
|
||||||
|
case "td":
|
||||||
|
startBlock(0);
|
||||||
|
sinkChildren();
|
||||||
|
endBlock();
|
||||||
|
break;
|
||||||
case "a":
|
case "a":
|
||||||
sinkChildren();
|
sinkChildren();
|
||||||
if(element.href != element.innerText) {
|
if(element.href != element.innerText) {
|
||||||
|
@ -116,6 +126,8 @@ class HtmlConverter {
|
||||||
if(csc.length)
|
if(csc.length)
|
||||||
s ~= "\033[39m";
|
s ~= "\033[39m";
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
sinkChildren();
|
||||||
break;
|
break;
|
||||||
case "p":
|
case "p":
|
||||||
startBlock();
|
startBlock();
|
||||||
|
@ -139,20 +151,28 @@ class HtmlConverter {
|
||||||
break;
|
break;
|
||||||
case "ul":
|
case "ul":
|
||||||
ulDepth++;
|
ulDepth++;
|
||||||
|
startBlock(2);
|
||||||
sinkChildren();
|
sinkChildren();
|
||||||
|
endBlock();
|
||||||
ulDepth--;
|
ulDepth--;
|
||||||
break;
|
break;
|
||||||
case "ol":
|
case "ol":
|
||||||
olDepth++;
|
olDepth++;
|
||||||
|
startBlock(2);
|
||||||
sinkChildren();
|
sinkChildren();
|
||||||
|
endBlock();
|
||||||
olDepth--;
|
olDepth--;
|
||||||
break;
|
break;
|
||||||
case "li":
|
case "li":
|
||||||
startBlock();
|
startBlock();
|
||||||
|
|
||||||
//sink('\t', true);
|
//sink('\t', true);
|
||||||
sink(' ', true);
|
/*
|
||||||
sink(' ', true);
|
foreach(cnt; 0 .. olDepth + ulDepth) {
|
||||||
|
sink(' ', true);
|
||||||
|
sink(' ', true);
|
||||||
|
}
|
||||||
|
*/
|
||||||
if(olDepth)
|
if(olDepth)
|
||||||
sink('*', false);
|
sink('*', false);
|
||||||
if(ulDepth)
|
if(ulDepth)
|
||||||
|
@ -164,15 +184,33 @@ class HtmlConverter {
|
||||||
endBlock();
|
endBlock();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "h1", "h2":
|
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();
|
startBlock();
|
||||||
sinkChildren();
|
sinkChildren();
|
||||||
sink('\n', true);
|
sink('\n', true);
|
||||||
foreach(dchar ch; element.innerText)
|
foreach(dchar ch; element.innerText)
|
||||||
sink(element.tagName == "h1" ? '=' : '-', false);
|
sink(element.tagName == "h2" ? '=' : '-', false);
|
||||||
endBlock();
|
endBlock();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "hr":
|
case "hr":
|
||||||
startBlock();
|
startBlock();
|
||||||
foreach(i; 0 .. width / 4)
|
foreach(i; 0 .. width / 4)
|
||||||
|
@ -185,7 +223,6 @@ class HtmlConverter {
|
||||||
case "br":
|
case "br":
|
||||||
sink('\n', true);
|
sink('\n', true);
|
||||||
break;
|
break;
|
||||||
case "tr":
|
|
||||||
case "div":
|
case "div":
|
||||||
startBlock();
|
startBlock();
|
||||||
|
|
||||||
|
@ -207,7 +244,7 @@ class HtmlConverter {
|
||||||
endBlock();
|
endBlock();
|
||||||
break;
|
break;
|
||||||
case "pre":
|
case "pre":
|
||||||
startBlock();
|
startBlock(4);
|
||||||
foreach(child; element.childNodes)
|
foreach(child; element.childNodes)
|
||||||
htmlToText(child, true, width);
|
htmlToText(child, true, width);
|
||||||
endBlock();
|
endBlock();
|
||||||
|
@ -237,6 +274,10 @@ class HtmlConverter {
|
||||||
//auto stylesheet = new StyleSheet(readText("/var/www/dpldocs.info/experimental-docs/style.css"));
|
//auto stylesheet = new StyleSheet(readText("/var/www/dpldocs.info/experimental-docs/style.css"));
|
||||||
//stylesheet.apply(document);
|
//stylesheet.apply(document);
|
||||||
|
|
||||||
|
return convert(start, wantWordWrap, wrapAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
string convert(Element start, bool wantWordWrap = true, int wrapAmount = 74) {
|
||||||
htmlToText(start, false, wrapAmount);
|
htmlToText(start, false, wrapAmount);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -255,6 +296,12 @@ class HtmlConverter {
|
||||||
int lineLength;
|
int lineLength;
|
||||||
|
|
||||||
void sink(dchar item, bool preformatted, int lineWidthOverride = int.min) {
|
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;
|
int width = lineWidthOverride == int.min ? this.width : lineWidthOverride;
|
||||||
if(!preformatted && isWhite(item)) {
|
if(!preformatted && isWhite(item)) {
|
||||||
if(!justOutputWhitespace) {
|
if(!justOutputWhitespace) {
|
||||||
|
@ -282,8 +329,9 @@ class HtmlConverter {
|
||||||
auto os = s;
|
auto os = s;
|
||||||
s = os[0 .. idx];
|
s = os[0 .. idx];
|
||||||
s ~= '\n';
|
s ~= '\n';
|
||||||
s ~= os[idx + 1 .. $];
|
|
||||||
lineLength = cast(int)(os[idx+1..$].length);
|
lineLength = cast(int)(os[idx+1..$].length);
|
||||||
|
lineLength += doIndent();
|
||||||
|
s ~= os[idx + 1 .. $];
|
||||||
broken = true;
|
broken = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -295,15 +343,17 @@ class HtmlConverter {
|
||||||
if(!broken) {
|
if(!broken) {
|
||||||
s ~= '\n';
|
s ~= '\n';
|
||||||
lineLength = 0;
|
lineLength = 0;
|
||||||
|
needsIndent = true;
|
||||||
justOutputWhitespace = true;
|
justOutputWhitespace = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(item == '\n')
|
if(item == '\n') {
|
||||||
lineLength = 0;
|
lineLength = 0;
|
||||||
else
|
needsIndent = true;
|
||||||
|
} else
|
||||||
lineLength ++;
|
lineLength ++;
|
||||||
|
|
||||||
|
|
||||||
|
@ -312,22 +362,45 @@ class HtmlConverter {
|
||||||
justOutputMargin = false;
|
justOutputMargin = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void startBlock() {
|
|
||||||
|
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) {
|
if(!justOutputBlock) {
|
||||||
s ~= "\n";
|
s ~= "\n";
|
||||||
lineLength = 0;
|
lineLength = 0;
|
||||||
|
needsIndent = true;
|
||||||
justOutputBlock = true;
|
justOutputBlock = true;
|
||||||
}
|
}
|
||||||
if(!justOutputMargin) {
|
if(!justOutputMargin) {
|
||||||
s ~= "\n";
|
s ~= "\n";
|
||||||
lineLength = 0;
|
lineLength = 0;
|
||||||
|
needsIndent = true;
|
||||||
justOutputMargin = true;
|
justOutputMargin = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void endBlock() {
|
void endBlock() {
|
||||||
|
if(indentStack.length)
|
||||||
|
indentStack = indentStack[0 .. $ - 1];
|
||||||
|
|
||||||
if(!justOutputMargin) {
|
if(!justOutputMargin) {
|
||||||
s ~= "\n";
|
s ~= "\n";
|
||||||
lineLength = 0;
|
lineLength = 0;
|
||||||
|
needsIndent = true;
|
||||||
justOutputMargin = true;
|
justOutputMargin = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -403,13 +476,11 @@ void penis() {
|
||||||
ele.innerText = "^" ~ ele.innerText;
|
ele.innerText = "^" ~ ele.innerText;
|
||||||
ele.stripOut();
|
ele.stripOut();
|
||||||
break;
|
break;
|
||||||
/*
|
|
||||||
case "img":
|
case "img":
|
||||||
string alt = ele.getAttribute("alt");
|
string alt = ele.getAttribute("alt");
|
||||||
if(alt)
|
if(alt)
|
||||||
result ~= ele.alt;
|
result ~= ele.alt;
|
||||||
break;
|
break;
|
||||||
*/
|
|
||||||
default:
|
default:
|
||||||
ele.stripOut();
|
ele.stripOut();
|
||||||
goto again;
|
goto again;
|
||||||
|
|
Loading…
Reference in New Issue