1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Convert quote characters in plain text messages to blockquotes in the html version. "Be like Thunderbird."

This commit is contained in:
Andrew Chen 2012-04-17 10:22:43 -07:00
parent 32ae7bad9c
commit 32ce196b5a
2 changed files with 120 additions and 3 deletions

View File

@ -190,6 +190,11 @@ public class HtmlConverter {
return buff.toString(); return buff.toString();
} }
private static final String HTML_BLOCKQUOTE_START = "<blockquote class=\"gmail_quote\" " +
"style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">";
private static final String HTML_BLOCKQUOTE_END = "</blockquote>";
private static final String HTML_NEWLINE = "<br />";
/** /**
* Convert a text string into an HTML document. * Convert a text string into an HTML document.
* *
@ -217,14 +222,19 @@ public class HtmlConverter {
} }
StringReader reader = new StringReader(text); StringReader reader = new StringReader(text);
StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);
int c; boolean isStartOfLine = false; // Are we currently at the start of a line?
int quoteDepth = 0; // Number of DIVs deep we are.
int quotesThisLine = 0; // How deep we should be quoting for this line.
try { try {
int c;
while ((c = reader.read()) != -1) { while ((c = reader.read()) != -1) {
switch (c) { switch (c) {
case '\n': case '\n':
// pine treats <br> as two newlines, but <br/> as one newline. Use <br/> so our messages aren't // pine treats <br> as two newlines, but <br/> as one newline. Use <br/> so our messages aren't
// doublespaced. // doublespaced.
buff.append("<br />"); buff.append(HTML_NEWLINE);
isStartOfLine = true;
quotesThisLine = 0;
break; break;
case '&': case '&':
buff.append("&amp;"); buff.append("&amp;");
@ -233,11 +243,39 @@ public class HtmlConverter {
buff.append("&lt;"); buff.append("&lt;");
break; break;
case '>': case '>':
buff.append("&gt;"); if(isStartOfLine) {
quotesThisLine++;
} else {
buff.append("&gt;");
}
break; break;
case '\r': case '\r':
break; break;
case ' ':
if(isStartOfLine) {
// If we're still in the start of the line and we have spaces, don't output them, since they
// may be collapsed by our div-converting magic.
break;
}
default: default:
if(isStartOfLine) {
// Not a quote character and not a space. Content is starting now.
isStartOfLine = false;
if(K9.DEBUG) {
Log.d(K9.LOG_TAG, "currentQuoteDepth: " + quoteDepth + " quotesThisLine: " + quotesThisLine);
}
// Add/remove blockquotes by comparing this line's quotes to the previous line's quotes.
if(quotesThisLine > quoteDepth) {
for(int i = quoteDepth; i < quotesThisLine; i++) {
buff.append(HTML_BLOCKQUOTE_START);
}
} else if(quotesThisLine < quoteDepth) {
for(int i = quoteDepth; i > quotesThisLine; i--) {
buff.append(HTML_BLOCKQUOTE_END);
}
}
quoteDepth = quotesThisLine;
}
buff.append((char)c); buff.append((char)c);
}//switch }//switch
} }
@ -245,8 +283,21 @@ public class HtmlConverter {
//Should never happen //Should never happen
Log.e(K9.LOG_TAG, "Could not read string to convert text to HTML:", e); Log.e(K9.LOG_TAG, "Could not read string to convert text to HTML:", e);
} }
// Close off any quotes we may have opened.
if (quoteDepth > 0) {
for (int i = quoteDepth; i > 0; i--) {
buff.append(HTML_BLOCKQUOTE_END);
}
}
text = buff.toString(); text = buff.toString();
// Make newlines at the end of blockquotes nicer by putting newlines beyond the first one outside of the
// blockquote.
text = text.replaceAll(
"\\Q" + HTML_NEWLINE + "\\E((\\Q" + HTML_NEWLINE + "\\E)+?)\\Q" + HTML_BLOCKQUOTE_END + "\\E",
HTML_BLOCKQUOTE_END + "$1"
);
// Replace lines of -,= or _ with horizontal rules // Replace lines of -,= or _ with horizontal rules
text = text.replaceAll("\\s*([-=_]{30,}+)\\s*", "<hr />"); text = text.replaceAll("\\s*([-=_]{30,}+)\\s*", "<hr />");

View File

@ -0,0 +1,66 @@
package com.fsck.k9.helper;
import junit.framework.Assert;
import org.junit.Test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class HtmlConverterTest {
// Useful if you want to write stuff to a file for debugging in a browser.
private static final boolean WRITE_TO_FILE = false;
private static final String OUTPUT_FILE = "C:/temp/parse.html";
@Test
public void testTextQuoteToHtmlBlockquote() {
String message = "Panama!\n" +
"\n" +
"Bob Barker <bob@aol.com> wrote:\n" +
"> a canal\n" +
">\n" +
"> Dorothy Jo Gideon <dorothy@aol.com> espoused:\n" +
"> >A man, a plan...\n" +
"> Too easy!\n" +
"\n" +
"Nice job :)\n" +
">> Guess!";
String result = HtmlConverter.textToHtml(message, false);
writeToFile(result);
Assert.assertEquals("<pre style=\"white-space: pre-wrap; word-wrap:break-word; font-family: sans-serif\">Panama!<br /><br />Bob Barker &lt;bob@aol.com&gt; wrote:<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">a canal<br /><br />Dorothy Jo Gideon &lt;dorothy@aol.com&gt; espoused:<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">A man, a plan...<br /></blockquote>Too easy!</blockquote><br />Nice job :)<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\"><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">Guess!</blockquote></blockquote></pre>", result);
}
@Test
public void testTextQuoteToHtmlBlockquoteIndented() {
String message = "*facepalm*\n" +
"\n" +
"Bob Barker <bob@aol.com> wrote:\n" +
"> A wise man once said...\n" +
">\n" +
"> LOL F1RST!!!!!\n" +
">\n" +
"> :)";
String result = HtmlConverter.textToHtml(message, false);
writeToFile(result);
Assert.assertEquals("<pre style=\"white-space: pre-wrap; word-wrap:break-word; font-family: sans-serif\">*facepalm*<br /><br />Bob Barker &lt;bob@aol.com&gt; wrote:<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">A wise man once said...<br /><br />LOL F1RST!!!!!<br /><br />:)</blockquote></pre>", result);
}
private void writeToFile(final String content) {
if(!WRITE_TO_FILE) {
return;
}
try {
System.err.println(content);
File f = new File(OUTPUT_FILE);
f.delete();
FileWriter fstream = new FileWriter(OUTPUT_FILE);
BufferedWriter out = new BufferedWriter(fstream);
out.write(content);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}