1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 09:47:13 +00:00

Process plain text TABs, refactoring

This commit is contained in:
M66B 2019-11-14 09:15:18 +01:00
parent ac9634553f
commit 86afc8a978
2 changed files with 53 additions and 68 deletions

View file

@ -71,6 +71,7 @@ public class HtmlHelper {
private static final int PREVIEW_SIZE = 500; // characters
private static final float MIN_LUMINANCE = 0.5f;
private static final int TAB_SIZE = 2;
private static final int MAX_AUTO_LINK = 250;
private static final int TRACKING_PIXEL_SURFACE = 25; // pixels
@ -266,31 +267,68 @@ public class HtmlHelper {
// Pre formatted text
for (Element pre : document.select("pre")) {
Element div = document.createElement("font");
div.attr("face", "monospace");
int level = 0;
StringBuilder sb = new StringBuilder();
String[] lines = pre.wholeText().split("\\r?\\n");
for (String line : lines) {
// Opening quotes
int tlevel = 0;
while (line.startsWith(">")) {
tlevel++;
if (tlevel > level)
sb.append("<blockquote>");
line = line.substring(1); // >
if (line.startsWith(" "))
line = line.substring(1);
}
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
level = tlevel;
// Tabs characters
StringBuilder l = new StringBuilder();
for (int j = 0; j < line.length(); j++) {
char kar = line.charAt(j);
if (kar == '\t') {
l.append(' ');
while (l.length() % TAB_SIZE != 0)
l.append(' ');
} else
l.append(kar);
}
line = l.toString();
// Html characters
line = Html.escapeHtml(line);
StringBuilder sb = new StringBuilder();
// Space characters
int len = line.length();
for (int j = 0; j < len; j++) {
char kar = line.charAt(j);
if (kar == ' ' &&
j + 1 < len && line.charAt(j + 1) == ' ')
sb.append("&nbsp;");
else
if (kar == ' ') {
// Prevent trimming start
// Keep one space for word wrapping
if (j == 0 || (j + 1 < len && line.charAt(j + 1) == ' '))
sb.append("&nbsp;");
else
sb.append(' ');
} else
sb.append(kar);
}
Element span = document.createElement("span");
span.html(sb.toString());
div.appendChild(span);
div.appendElement("br");
sb.append("<br>");
}
pre.replaceWith(div);
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
pre.tagName("tt"); // monospace
pre.html(sb.toString());
}
// Code

View file

@ -23,7 +23,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.net.MailTo;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import androidx.preference.PreferenceManager;
@ -1041,60 +1040,8 @@ public class MessageHelper {
warnings.add(Helper.formatThrowable(ex, false));
}
// Prevent Jsoup throwing an exception
if (part == plain) {
StringBuilder sb = new StringBuilder();
sb.append("<span>");
int level = 0;
String[] lines = result.split("\\r?\\n");
for (String line : lines) {
// Opening quotes
int tlevel = 0;
while (line.startsWith(">")) {
tlevel++;
if (tlevel > level)
sb.append("<blockquote>");
line = line.substring(1); // >
if (line.startsWith(" "))
line = line.substring(1);
}
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
level = tlevel;
// Show as-is
line = Html.escapeHtml(line);
// Non breaking spaces
boolean start = true;
int len = line.length();
for (int j = 0; j < len; j++) {
char kar = line.charAt(j);
if (kar == ' ' &&
(start || j + 1 < len && line.charAt(j + 1) == ' '))
sb.append("&nbsp;");
else {
start = false;
sb.append(kar);
}
}
sb.append("<br>");
}
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
sb.append("</span>");
result = sb.toString();
}
if (part == plain)
result = "<pre>" + result + "</pre>";
return result;
}