package com.fastaccess.provider.timeline.handler; import android.graphics.Color; import android.support.annotation.ColorInt; import android.text.SpannableStringBuilder; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.TypefaceSpan; import net.nightwhistler.htmlspanner.handlers.PreHandler; import org.htmlcleaner.ContentNode; import org.htmlcleaner.TagNode; import lombok.AllArgsConstructor; import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; /** * Created by Kosh on 22 Apr 2017, 1:07 PM */ @AllArgsConstructor public class PreTagHandler extends PreHandler { @ColorInt private final int color; private final boolean isPre; private boolean isDark; private void getPlainText(StringBuffer buffer, Object node) { if (node instanceof ContentNode) { ContentNode contentNode = (ContentNode) node; String text = contentNode.getContent().toString(); buffer.append(text); } else if (node instanceof TagNode) { TagNode tagNode = (TagNode) node; for (Object child : tagNode.getChildren()) { this.getPlainText(buffer, child); } } } private String replace(String text) { return text.replaceAll(" ", "\u00A0") .replaceAll("&", "&") .replaceAll(""", "\"") .replaceAll("¢", "¢") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll("§", "§") .replaceAll("“", "“") .replaceAll("”", "”") .replaceAll("‘", "‘") .replaceAll("’", "’") .replaceAll("–", "\u2013") .replaceAll("—", "\u2014") .replaceAll("―", "\u2015"); } @Override public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end) { if (isPre) { StringBuffer buffer = new StringBuffer(); buffer.append("\n");//fake padding top + make sure, pre is always by itself getPlainText(buffer, node); buffer.append("\n");//fake padding bottom + make sure, pre is always by itself builder.append(replace(buffer.toString())); builder.append("\n"); builder.setSpan(new CodeBackgroundRoundedSpan(color), start, builder.length(), SPAN_EXCLUSIVE_EXCLUSIVE); builder.append("\n"); this.appendNewLine(builder); this.appendNewLine(builder); } else { StringBuffer text = node.getText(); builder.append(replace(text.toString())); final int stringEnd = builder.length(); builder.setSpan(new BackgroundColorSpan(color), start, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE); if (!isDark) { builder.setSpan(new ForegroundColorSpan(Color.RED), start, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE); } builder.setSpan(new TypefaceSpan("monospace"), start, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE); } } }