1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-10 11:35:11 -05:00

Issue 3105: Generate proper HTML-ified versions of text messages so that textification of the HTMLified text message preserves the original newlines.

This commit is contained in:
Andrew Chen 2011-05-05 00:17:34 -07:00
parent 3fcca67ae5
commit 1ea27d7020

View File

@ -123,6 +123,43 @@ public class HtmlConverter {
private static final int MAX_SMART_HTMLIFY_MESSAGE_LENGTH = 1024 * 256 ;
/**
* Naively convert a text string into an HTML document. This method avoids using regular expressions on the entire
* message body to save memory.
* @param text Plain text string.
* @return HTML string.
*/
private static String simpleTextToHtml(String text) {
// Encode HTML entities to make sure we don't display something evil.
text = TextUtils.htmlEncode(text);
StringReader reader = new StringReader(text);
StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);
buff.append("<html><head/><body>");
int c;
try {
while ((c = reader.read()) != -1) {
switch (c) {
case '\n':
buff.append("<br/>\n");
break;
case '\r':
break;
default:
buff.append((char)c);
}//switch
}
} catch (IOException e) {
//Should never happen
Log.e(K9.LOG_TAG, "Could not read string to convert text to HTML:", e);
}
buff.append("</body></html>");
return buff.toString();
}
/**
* Convert a text string into an HTML document. Attempts to do smart replacement for large
* documents to prevent OOM errors. This method adds headers and footers to create a proper HTML
@ -136,11 +173,7 @@ public class HtmlConverter {
// if the message is big and plain text, just do
// a trivial htmlification
if (text.length() > MAX_SMART_HTMLIFY_MESSAGE_LENGTH) {
return "<html><head/><body>" +
htmlifyMessageHeader() +
text +
htmlifyMessageFooter() +
"</body></html>";
return simpleTextToHtml(text);
}
StringReader reader = new StringReader(text);
StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH);
@ -148,6 +181,9 @@ public class HtmlConverter {
try {
while ((c = reader.read()) != -1) {
switch (c) {
case '\n':
buff.append("<br/>\n");
break;
case '&':
buff.append("&amp;");
break;
@ -1076,7 +1112,7 @@ public class HtmlConverter {
final String font = K9.messageViewFixedWidthFont()
? "monospace"
: "sans-serif";
return "<pre style=\"white-space: pre-wrap; word-wrap:break-word; font-family: " + font + "\">";
return "<pre style=\"white-space: normal; word-wrap:break-word; font-family: " + font + "\">";
}
private static String htmlifyMessageFooter() {