add blockquote support

This commit is contained in:
sk 2022-11-30 11:20:58 +01:00
parent a0da73f76f
commit 58ba748ade
1 changed files with 10 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.BulletSpan; import android.text.style.BulletSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
@ -26,6 +27,7 @@ import org.jsoup.safety.Safelist;
import org.jsoup.select.NodeVisitor; import org.jsoup.select.NodeVisitor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
@ -127,24 +129,27 @@ public class HtmlParser{
openSpans.add(new SpanInfo(new InvisibleSpan(), ssb.length(), el)); openSpans.add(new SpanInfo(new InvisibleSpan(), ssb.length(), el));
} }
} }
case "li" -> openSpans.add(new SpanInfo(new BulletSpan(V.dp(6)), ssb.length(), el)); case "li" -> openSpans.add(new SpanInfo(new BulletSpan(V.dp(8)), ssb.length(), el));
case "em", "i" -> openSpans.add(new SpanInfo(new StyleSpan(Typeface.ITALIC), ssb.length(), el)); case "em", "i" -> openSpans.add(new SpanInfo(new StyleSpan(Typeface.ITALIC), ssb.length(), el));
case "strong", "b" -> openSpans.add(new SpanInfo(new StyleSpan(Typeface.BOLD), ssb.length(), el)); case "strong", "b" -> openSpans.add(new SpanInfo(new StyleSpan(Typeface.BOLD), ssb.length(), el));
case "u" -> openSpans.add(new SpanInfo(new UnderlineSpan(), ssb.length(), el)); case "u" -> openSpans.add(new SpanInfo(new UnderlineSpan(), ssb.length(), el));
case "code", "pre" -> openSpans.add(new SpanInfo(new TypefaceSpan("monospace"), ssb.length(), el)); case "code", "pre" -> openSpans.add(new SpanInfo(new TypefaceSpan("monospace"), ssb.length(), el));
case "blockquote" -> openSpans.add(new SpanInfo(new LeadingMarginSpan.Standard(V.dp(10)), ssb.length(), el));
} }
} }
} }
final List<String> blockElements = Arrays.asList("p", "ul", "ol", "blockquote");
@Override @Override
public void tail(@NonNull Node node, int depth){ public void tail(@NonNull Node node, int depth){
if(node instanceof Element el){ if(node instanceof Element el){
if("span".equals(el.nodeName()) && el.hasClass("ellipsis")){ if("span".equals(el.nodeName()) && el.hasClass("ellipsis")){
ssb.append("", new DeleteWhenCopiedSpan(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ssb.append("", new DeleteWhenCopiedSpan(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}else if("p".equals(el.nodeName()) || "ul".equals(el.nodeName()) || "ol".equals(el.nodeName())){ }else if(blockElements.contains(el.nodeName()) && node.nextSibling()!=null){
if(node.nextSibling()!=null) ssb.append("\n\n");
ssb.append("\n\n"); }
}else if(!openSpans.isEmpty()){ if(!openSpans.isEmpty()){
SpanInfo si=openSpans.get(openSpans.size()-1); SpanInfo si=openSpans.get(openSpans.size()-1);
if(si.element==el){ if(si.element==el){
ssb.setSpan(si.span, si.start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ssb.setSpan(si.span, si.start, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);