Convert to lists on sending message

This commit is contained in:
M66B 2020-03-08 16:05:43 +01:00
parent 5f249f59b9
commit b66e940727
2 changed files with 57 additions and 3 deletions

View File

@ -1192,8 +1192,8 @@ public class HtmlHelper {
append(((TextNode) node).text());
else {
String name = node.nodeName();
if ("li".equals(name))
append("*");
if ("li".equals(name) && node.parent() != null)
append("ol".equals(node.parent().nodeName()) ? "-" : "*");
else if ("blockquote".equals(name))
qlevel++;
else if ("pre".equals(name))
@ -1259,6 +1259,46 @@ public class HtmlHelper {
return sb.toString();
}
static void convertLists(Document document) {
for (Element p : document.select("p")) {
Element list = null;
for (int i = 0; i < p.childNodeSize(); i++) {
boolean item = false;
Node node = p.childNode(i);
if (node instanceof TextNode) {
String text = ((TextNode) node).text().trim();
Node next = node.nextSibling();
if ((text.startsWith("* ") || text.startsWith("- ")) &&
(next == null || "br".equals(next.nodeName()))) {
item = true;
String type = (text.startsWith("* ") ? "ul" : "ol");
Element li = document.createElement("li");
li.text(text.substring(2));
if (list == null || !list.tagName().equals(type)) {
list = document.createElement(type);
list.appendChild(li);
node.replaceWith(list);
} else {
list.appendChild(li);
node.remove();
i--;
}
if (next != null)
next.remove();
}
}
if (!item)
list = null;
}
p.tagName("div");
p.appendElement("br");
}
}
static Spanned highlightHeaders(Context context, String headers) {
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
SpannableStringBuilder ssb = new SpannableStringBuilder(headers);

View File

@ -36,6 +36,7 @@ import com.sun.mail.util.QDecoderStream;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@ -513,7 +514,20 @@ public class MessageHelper {
// When sending message
if (identity != null) {
document.select("div[fairemail=signature],div[fairemail=reference]").removeAttr("fairemail");
Elements sig = document.select("div[fairemail=signature]");
Elements ref = document.select("div[fairemail=reference]");
sig.remove();
ref.remove();
HtmlHelper.convertLists(document);
sig.removeAttr("fairemail");
ref.removeAttr("fairemail");
for (Element e : sig)
document.body().appendChild(e);
for (Element e : ref)
document.body().appendChild(e);
DB db = DB.getInstance(context);
try {