Truncate large texts

pull/174/head
M66B 5 years ago
parent 792f6b16e3
commit cdd7e8686c

@ -1215,17 +1215,40 @@ public class HtmlHelper {
} }
static boolean truncate(Document d, boolean reformat) { static boolean truncate(Document d, boolean reformat) {
int at = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE); int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);
int length = 0; int length = 0;
for (Element elm : d.select("*")) { for (Element elm : d.select("*")) {
for (Node child : elm.childNodes()) boolean skip = false;
if (child instanceof TextNode)
length += ((TextNode) child).text().length(); for (Node child : elm.childNodes()) {
if (length > at) if (child instanceof TextNode) {
TextNode tnode = ((TextNode) child);
String text = tnode.getWholeText();
if (length < max) {
if (length + text.length() >= max) {
text = text.substring(0, max - length) + " ...";
tnode.text(text);
skip = true;
}
} else {
if (skip)
child.remove();
}
length += text.length();
} else {
if (skip)
child.remove();
}
}
if (length >= max && !skip)
elm.remove(); elm.remove();
} }
return (length > at);
return (length >= max);
} }
static Spanned fromHtml(@NonNull String html) { static Spanned fromHtml(@NonNull String html) {

Loading…
Cancel
Save