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

Colorize quoted text that has been converted to html.

Like Thunderbird, but optimized for mobile (no right margin quote bar, thinner left margin quote bar).
This commit is contained in:
Andrew Chen 2012-04-23 15:10:34 -07:00
parent 0bb1f4ff56
commit 9f1e2717a6
2 changed files with 61 additions and 5 deletions

View File

@ -190,8 +190,9 @@ public class HtmlConverter {
return buff.toString(); return buff.toString();
} }
private static final String HTML_BLOCKQUOTE_COLOR_TOKEN = "$$COLOR$$";
private static final String HTML_BLOCKQUOTE_START = "<blockquote class=\"gmail_quote\" " + private static final String HTML_BLOCKQUOTE_START = "<blockquote class=\"gmail_quote\" " +
"style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">"; "style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid $$COLOR$$; padding-left: 1ex;\">";
private static final String HTML_BLOCKQUOTE_END = "</blockquote>"; private static final String HTML_BLOCKQUOTE_END = "</blockquote>";
private static final String HTML_NEWLINE = "<br />"; private static final String HTML_NEWLINE = "<br />";
@ -267,7 +268,7 @@ public class HtmlConverter {
// Add/remove blockquotes by comparing this line's quotes to the previous line's quotes. // Add/remove blockquotes by comparing this line's quotes to the previous line's quotes.
if(quotesThisLine > quoteDepth) { if(quotesThisLine > quoteDepth) {
for(int i = quoteDepth; i < quotesThisLine; i++) { for(int i = quoteDepth; i < quotesThisLine; i++) {
buff.append(HTML_BLOCKQUOTE_START); buff.append(HTML_BLOCKQUOTE_START.replace(HTML_BLOCKQUOTE_COLOR_TOKEN, getQuoteColor(i + 1)));
} }
} else if(quotesThisLine < quoteDepth) { } else if(quotesThisLine < quoteDepth) {
for(int i = quoteDepth; i > quotesThisLine; i--) { for(int i = quoteDepth; i > quotesThisLine; i--) {
@ -326,6 +327,35 @@ public class HtmlConverter {
return text; return text;
} }
protected static final String QUOTE_COLOR_DEFAULT = "#ccc";
protected static final String QUOTE_COLOR_LEVEL_1 = "#729fcf";
protected static final String QUOTE_COLOR_LEVEL_2 = "#ad7fa8";
protected static final String QUOTE_COLOR_LEVEL_3 = "#8ae234";
protected static final String QUOTE_COLOR_LEVEL_4 = "#fcaf3e";
protected static final String QUOTE_COLOR_LEVEL_5 = "#e9b96e";
/**
* Return an HTML hex color string for a given quote level.
* @param level Quote level
* @return Hex color string with prepended #.
*/
protected static String getQuoteColor(final int level) {
switch(level) {
case 1:
return QUOTE_COLOR_LEVEL_1;
case 2:
return QUOTE_COLOR_LEVEL_2;
case 3:
return QUOTE_COLOR_LEVEL_3;
case 4:
return QUOTE_COLOR_LEVEL_4;
case 5:
return QUOTE_COLOR_LEVEL_5;
default:
return QUOTE_COLOR_DEFAULT;
}
}
/** /**
* Searches for link-like text in a string and turn it into a link. Append the result to * Searches for link-like text in a string and turn it into a link. Append the result to
* <tt>outputBuffer</tt>. <tt>text</tt> is not modified. * <tt>outputBuffer</tt>. <tt>text</tt> is not modified.

View File

@ -7,9 +7,11 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import static junit.framework.Assert.*;
public class HtmlConverterTest { public class HtmlConverterTest {
// Useful if you want to write stuff to a file for debugging in a browser. // 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 boolean WRITE_TO_FILE = true;
private static final String OUTPUT_FILE = "C:/temp/parse.html"; private static final String OUTPUT_FILE = "C:/temp/parse.html";
@Test @Test
@ -27,7 +29,7 @@ public class HtmlConverterTest {
">> Guess!"; ">> Guess!";
String result = HtmlConverter.textToHtml(message, false); String result = HtmlConverter.textToHtml(message, false);
writeToFile(result); 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 1ex 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 1ex 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 1ex 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\"><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;\">Guess!</blockquote></blockquote></pre>", result); 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 1ex 0.8ex; border-left: 1px solid #729fcf; 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 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">A man, a plan...<br /></blockquote>Too easy!</blockquote><br />Nice job :)<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\"><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">Guess!</blockquote></blockquote></pre>", result);
} }
@Test @Test
@ -42,7 +44,31 @@ public class HtmlConverterTest {
"> :)"; "> :)";
String result = HtmlConverter.textToHtml(message, false); String result = HtmlConverter.textToHtml(message, false);
writeToFile(result); 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 1ex 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); 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 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">A wise man once said...<br /><br />LOL F1RST!!!!!<br /><br />:)</blockquote></pre>", result);
}
@Test
public void testQuoteDepthColor() {
assertEquals(HtmlConverter.getQuoteColor(1), HtmlConverter.QUOTE_COLOR_LEVEL_1);
assertEquals(HtmlConverter.getQuoteColor(2), HtmlConverter.QUOTE_COLOR_LEVEL_2);
assertEquals(HtmlConverter.getQuoteColor(3), HtmlConverter.QUOTE_COLOR_LEVEL_3);
assertEquals(HtmlConverter.getQuoteColor(4), HtmlConverter.QUOTE_COLOR_LEVEL_4);
assertEquals(HtmlConverter.getQuoteColor(5), HtmlConverter.QUOTE_COLOR_LEVEL_5);
assertEquals(HtmlConverter.getQuoteColor(-1), HtmlConverter.QUOTE_COLOR_DEFAULT);
assertEquals(HtmlConverter.getQuoteColor(0), HtmlConverter.QUOTE_COLOR_DEFAULT);
assertEquals(HtmlConverter.getQuoteColor(6), HtmlConverter.QUOTE_COLOR_DEFAULT);
String message = "zero\n" +
"> one\n" +
">> two\n" +
">>> three\n" +
">>>> four\n" +
">>>>> five\n" +
">>>>>> six";
String result = HtmlConverter.textToHtml(message, false);
writeToFile(result);
assertEquals("<pre style=\"white-space: pre-wrap; word-wrap:break-word; font-family: sans-serif\">zero<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">one<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">two<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;\">three<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #fcaf3e; padding-left: 1ex;\">four<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #e9b96e; padding-left: 1ex;\">five<br /><blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ccc; padding-left: 1ex;\">six</blockquote></blockquote></blockquote></blockquote></blockquote></blockquote></pre>", result);
} }
private void writeToFile(final String content) { private void writeToFile(final String content) {