commentparser: fix p not adding a newline

adds a new block element rule, that is automatically applied to p.
refactor-toolbar
Floens 8 years ago
parent cd2ec3b266
commit 489ca21877
  1. 6
      Clover/app/src/main/java/org/floens/chan/core/site/parser/CommentParser.java
  2. 21
      Clover/app/src/main/java/org/floens/chan/core/site/parser/StyleRule.java

@ -56,6 +56,9 @@ public class CommentParser {
private Map<String, List<StyleRule>> rules = new HashMap<>(); private Map<String, List<StyleRule>> rules = new HashMap<>();
public CommentParser() { public CommentParser() {
rule(tagRule("p"));
rule(tagRule("div"));
rule(tagRule("br").just("\n")); rule(tagRule("br").just("\n"));
rule(tagRule("a").action(this::handleAnchor)); rule(tagRule("a").action(this::handleAnchor));
@ -113,9 +116,6 @@ public class CommentParser {
} }
switch (tag) { switch (tag) {
case "p":
return appendBreakIfNotLastSibling(
handleParagraph(theme, post, text, element), element);
default: default:
// Unknown tag, return the text; // Unknown tag, return the text;
return text; return text;

@ -19,6 +19,7 @@ package org.floens.chan.core.site.parser;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan; import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
@ -31,6 +32,7 @@ import org.floens.chan.ui.theme.Theme;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class StyleRule { public class StyleRule {
@ -39,6 +41,8 @@ public class StyleRule {
QUOTE QUOTE
} }
private final List<String> blockElements = Arrays.asList("p", "div");
public static StyleRule tagRule(String tag) { public static StyleRule tagRule(String tag) {
return new StyleRule().tag(tag); return new StyleRule().tag(tag);
} }
@ -61,9 +65,15 @@ public class StyleRule {
private String justText = null; private String justText = null;
private boolean blockElement = false;
public StyleRule tag(String tag) { public StyleRule tag(String tag) {
this.tag = tag; this.tag = tag;
if (blockElements.contains(tag)) {
blockElement = true;
}
return this; return this;
} }
@ -140,6 +150,12 @@ public class StyleRule {
return this; return this;
} }
public StyleRule blockElement(boolean blockElement) {
this.blockElement = blockElement;
return this;
}
public boolean highPriority() { public boolean highPriority() {
return classes != null && !classes.isEmpty(); return classes != null && !classes.isEmpty();
} }
@ -208,6 +224,11 @@ public class StyleRule {
result = applySpan(result, spansToApply); result = applySpan(result, spansToApply);
} }
// Apply break if not the last element.
if (blockElement && element.nextSibling() != null) {
result = TextUtils.concat(result, "\n");
}
if (linkify) { if (linkify) {
CommentParserHelper.detectLinks(theme, post, result.toString(), new SpannableString(result)); CommentParserHelper.detectLinks(theme, post, result.toString(), new SpannableString(result));
} }

Loading…
Cancel
Save