Browse Source

progress

pull/61/merge
Thodoris1999 6 years ago
parent
commit
ca3281387b
  1. 41
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java

41
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java

@ -1,9 +1,13 @@
package gr.thmmy.mthmmy.utils.parsing; package gr.thmmy.mthmmy.utils.parsing;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.SpannedString; import android.text.SpannedString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.StyleSpan;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
@ -14,30 +18,47 @@ import gr.thmmy.mthmmy.model.BBTag;
import timber.log.Timber; import timber.log.Timber;
public class BBParser { public class BBParser {
public static final String[] supportedTags = {"b"}; private static final String[] supportedTags = {"b"};
public SpannedString bb2span(String bb) { public static SpannedString bb2span(String bb) {
SpannableStringBuilder builder = new SpannableStringBuilder(bb); SpannableStringBuilder builder = new SpannableStringBuilder(bb);
BBTag[] tags = getTags(bb); BBTag[] tags = getTags(bb);
for (BBTag tag : tags) {
switch (tag.getName()) {
case "b":
builder.setSpan(new StyleSpan(Typeface.BOLD), tag.getStart(), tag.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
break;
default:
throw new UnsupportedCharsetException("Tag not supported");
}
}
} }
public BBTag[] getTags(String bb) { public static BBTag[] getTags(String bb) {
Pattern bbtagPattern = Pattern.compile("[*+]"); Pattern bbtagPattern = Pattern.compile("\\[(.+?)\\]");
LinkedList<BBTag> tags = new LinkedList<>(); LinkedList<BBTag> tags = new LinkedList<>();
String searcingString = bb; Matcher bbMatcher = bbtagPattern.matcher(bb);
Matcher bbMatcher = bbtagPattern.matcher(searcingString);
while (bbMatcher.find()) { while (bbMatcher.find()) {
String name = bbMatcher.group(0); String name = bbMatcher.group(0);
if (!isSupported(name)) continue; if (name.startsWith("/")) {
//closing tag
name = name.substring(1);
for (int i = tags.size() - 1; i >= 0; i--) {
if (tags.get(i).getName().equals(name)) {
tags.get(i).setEnd(bbMatcher.start());
break;
}
}
continue;
}
if (isSupported(name))
tags.add(new BBTag(bbMatcher.start(), name)); tags.add(new BBTag(bbMatcher.start(), name));
searcingString = searcingString.substring(bbMatcher.start() + name.length());
bbMatcher = bbtagPattern.matcher(searcingString);
} }
return tags.toArray(new BBTag[0]); return tags.toArray(new BBTag[0]);
} }
public boolean isSupported(String tagName) { public static boolean isSupported(String tagName) {
for (String tag : supportedTags) for (String tag : supportedTags)
if (TextUtils.equals(tag, tagName)) return true; if (TextUtils.equals(tag, tagName)) return true;
return false; return false;

Loading…
Cancel
Save