FairEmail/app/src/main/java/eu/faircode/email/HtmlHelper.java

117 lines
4.3 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
2018-08-03 15:33:57 +00:00
import android.text.Html;
2018-08-02 13:33:06 +00:00
import android.text.TextUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2018-08-02 13:33:06 +00:00
public class HtmlHelper implements NodeVisitor {
private Context context;
private String newline;
2018-08-03 15:33:57 +00:00
private List<String> refs = new ArrayList<>();
2018-08-02 13:33:06 +00:00
private StringBuilder sb = new StringBuilder();
private Pattern pattern = Pattern.compile("([http|https]+://[\\w\\S(\\.|:|/)]+)");
2018-08-02 13:33:06 +00:00
private HtmlHelper(Context context, boolean reply) {
this.context = context;
this.newline = (reply ? "<br>> " : "<br>");
}
public void head(Node node, int depth) {
String name = node.nodeName();
if (node instanceof TextNode) {
String text = ((TextNode) node).text();
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String ref = matcher.group();
if (!refs.contains(ref))
refs.add(ref);
String alt = context.getString(R.string.title_link);
2018-08-08 06:55:47 +00:00
text = text.replace(ref, String.format("<a href=\"%s\">%s [%d]</a>", ref, alt, refs.size()));
}
sb.append(text);
} else if (name.equals("li"))
2018-08-02 13:33:06 +00:00
sb.append(newline).append(" * ");
else if (name.equals("dt"))
sb.append(" ");
2018-08-08 06:55:47 +00:00
else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "tr", "div"))
2018-08-02 13:33:06 +00:00
sb.append(newline);
}
public void tail(Node node, int depth) {
String name = node.nodeName();
2018-08-08 06:55:47 +00:00
if (StringUtil.in(name, "br", "dd", "dt", "p", "h1", "h2", "h3", "h4", "h5", "div"))
2018-08-02 13:33:06 +00:00
sb.append(newline);
else if (name.equals("a")) {
2018-08-03 15:33:57 +00:00
String ref = node.absUrl("href");
if (!TextUtils.isEmpty(ref)) {
if (!refs.contains(ref))
refs.add(ref);
String alt = node.attr("alt");
if (TextUtils.isEmpty(alt))
alt = context.getString(R.string.title_link);
alt = Html.escapeHtml(alt);
2018-08-08 06:55:47 +00:00
sb.append(" ").append(String.format("<a href=\"%s\">%s [%d]</a>", ref, alt, refs.size()));
2018-08-02 13:33:06 +00:00
}
} else if (name.equals("img")) {
2018-08-03 15:33:57 +00:00
String ref = node.absUrl("src");
if (!TextUtils.isEmpty(ref)) {
if (!refs.contains(ref))
refs.add(ref);
String alt = node.attr("alt");
if (TextUtils.isEmpty(alt))
alt = context.getString(R.string.title_image);
alt = Html.escapeHtml(alt);
2018-08-08 06:55:47 +00:00
sb.append(" ").append(String.format("<a href=\"%s\">%s [%d]</a>", ref, alt, refs.size()));
2018-08-02 13:33:06 +00:00
}
}
}
@Override
public String toString() {
2018-08-03 15:33:57 +00:00
if (refs.size() > 0)
2018-08-02 13:33:06 +00:00
sb.append(newline).append(newline);
2018-08-03 15:33:57 +00:00
for (int i = 0; i < refs.size(); i++)
2018-08-08 06:55:47 +00:00
sb.append(String.format("[%d] %s ", i + 1, refs.get(i))).append(newline);
2018-08-02 13:33:06 +00:00
return sb.toString();
}
public static String sanitize(Context context, String html, boolean reply) {
Document document = Jsoup.parse(html);
HtmlHelper visitor = new HtmlHelper(context, reply);
NodeTraversor.traverse(visitor, document.body());
return visitor.toString();
}
}