mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-26 19:05:23 +01:00
Heighlight text improvements
This commit is contained in:
@@ -2960,78 +2960,79 @@ public class HtmlHelper {
|
||||
return ssb;
|
||||
}
|
||||
|
||||
static Document highlightSearched(Context context, Document document, String query) {
|
||||
int color = Helper.resolveColor(context, R.attr.colorHighlight);
|
||||
query = Normalizer.normalize(query, Normalizer.Form.NFKD)
|
||||
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
|
||||
static void highlightSearched(Context context, Document document, String query) {
|
||||
try {
|
||||
int color = Helper.resolveColor(context, R.attr.colorHighlight);
|
||||
query = Fts4DbHelper.preprocessText(query);
|
||||
|
||||
// TODO: fix highlighting pre processed text
|
||||
// TODO breakText
|
||||
// TODO: fix highlighting pre processed text
|
||||
|
||||
List<String> word = new ArrayList<>();
|
||||
List<String> plus = new ArrayList<>();
|
||||
for (String w : query.trim().split("\\s+"))
|
||||
if (w.length() > 1 && (w.startsWith("+") || w.startsWith("-"))) {
|
||||
if (w.startsWith("+"))
|
||||
plus.add(w.substring(1));
|
||||
} else
|
||||
word.add(w);
|
||||
List<String> word = new ArrayList<>();
|
||||
List<String> plus = new ArrayList<>();
|
||||
for (String w : query.trim().split("\\s+"))
|
||||
if (w.length() > 1 && (w.startsWith("+") || w.startsWith("-"))) {
|
||||
if (w.startsWith("+"))
|
||||
plus.add(Pattern.quote(w.substring(1)));
|
||||
} else
|
||||
word.add(Pattern.quote(w));
|
||||
|
||||
int flags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE;
|
||||
List<Pattern> pat = new ArrayList<>();
|
||||
pat.add(Pattern.compile(".*?\\b(" + TextUtils.join("\\s+", word) + ")\\b.*?", flags));
|
||||
for (String w : plus)
|
||||
pat.add(Pattern.compile(".*?\\b(" + w + ")\\b.*?", flags));
|
||||
int flags = Pattern.DOTALL | Pattern.CASE_INSENSITIVE;
|
||||
List<Pattern> pat = new ArrayList<>();
|
||||
pat.add(Pattern.compile(".*?\\b(" + TextUtils.join("\\s+", word) + ")\\b.*?", flags));
|
||||
for (String w : plus)
|
||||
pat.add(Pattern.compile(".*?\\b(" + w + ")\\b.*?", flags));
|
||||
|
||||
for (Pattern p : pat)
|
||||
NodeTraversor.traverse(new NodeVisitor() {
|
||||
@Override
|
||||
public void head(Node node, int depth) {
|
||||
if (node instanceof TextNode)
|
||||
try {
|
||||
TextNode tnode = (TextNode) node;
|
||||
String text = Normalizer.normalize(tnode.getWholeText(), Normalizer.Form.NFKD)
|
||||
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
|
||||
for (Pattern p : pat)
|
||||
NodeTraversor.traverse(new NodeVisitor() {
|
||||
@Override
|
||||
public void head(Node node, int depth) {
|
||||
if (node instanceof TextNode)
|
||||
try {
|
||||
TextNode tnode = (TextNode) node;
|
||||
String text = Fts4DbHelper.preprocessText(tnode.getWholeText());
|
||||
|
||||
Matcher result = p.matcher(text);
|
||||
Matcher result = p.matcher(text);
|
||||
|
||||
int prev = 0;
|
||||
Element holder = document.createElement("span");
|
||||
while (result.find()) {
|
||||
int start = result.start(1);
|
||||
int end = result.end(1);
|
||||
int prev = 0;
|
||||
Element holder = document.createElement("span");
|
||||
while (result.find()) {
|
||||
int start = result.start(1);
|
||||
int end = result.end(1);
|
||||
|
||||
holder.appendText(text.substring(prev, start));
|
||||
holder.appendText(text.substring(prev, start));
|
||||
|
||||
Element span = document.createElement("span");
|
||||
span.attr("style", mergeStyles(
|
||||
span.attr("style"),
|
||||
"font-size:larger; background-color:" + encodeWebColor(color)
|
||||
));
|
||||
span.text(text.substring(start, end));
|
||||
holder.appendChild(span);
|
||||
Element span = document.createElement("span");
|
||||
span.attr("style", mergeStyles(
|
||||
span.attr("style"),
|
||||
"font-size:larger; background-color:" + encodeWebColor(color)
|
||||
));
|
||||
span.text(text.substring(start, end));
|
||||
holder.appendChild(span);
|
||||
|
||||
prev = end;
|
||||
prev = end;
|
||||
}
|
||||
|
||||
if (prev == 0) // No matches
|
||||
return;
|
||||
|
||||
if (prev < text.length())
|
||||
holder.appendText(text.substring(prev));
|
||||
|
||||
tnode.before(holder);
|
||||
tnode.text("");
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (prev == 0) // No matches
|
||||
return;
|
||||
|
||||
if (prev < text.length())
|
||||
holder.appendText(text.substring(prev));
|
||||
|
||||
tnode.before(holder);
|
||||
tnode.text("");
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tail(Node node, int depth) {
|
||||
}
|
||||
}, document);
|
||||
|
||||
return document;
|
||||
@Override
|
||||
public void tail(Node node, int depth) {
|
||||
}
|
||||
}, document);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
static Document markText(Document document) {
|
||||
|
||||
Reference in New Issue
Block a user