diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index eef46aad..cf3aaef0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,11 +17,18 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - - - - - + + + + + \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/TestActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/TestActivity.java new file mode 100644 index 00000000..1ac4c05c --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TestActivity.java @@ -0,0 +1,28 @@ +package gr.thmmy.mthmmy.activities; + +import androidx.appcompat.app.AppCompatActivity; +import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.utils.parsing.BBParser; +import timber.log.Timber; + +import android.os.Bundle; +import android.text.SpannableStringBuilder; +import android.widget.TextView; + +public class TestActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_test); + + String bb = "[b]An [i]elep[u]hant[/i][/b] swi[/u]ms in [s]the[/s] tree"; + SpannableStringBuilder result = BBParser.bb2span(bb); + + TextView bbRaw = findViewById(R.id.bb_raw); + TextView bb2Text = findViewById(R.id.bb2text); + + bbRaw.setText(bb); + bb2Text.setText(result); + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 4a64499f..8b0476a0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -19,6 +19,7 @@ import androidx.preference.PreferenceManager; import androidx.viewpager.widget.ViewPager; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.LoginActivity; +import gr.thmmy.mthmmy.activities.TestActivity; import gr.thmmy.mthmmy.activities.board.BoardActivity; import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity; import gr.thmmy.mthmmy.activities.main.forum.ForumFragment; @@ -137,6 +138,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF , Toast.LENGTH_SHORT).show(); } mBackPressed = System.currentTimeMillis(); + + startActivity(new Intent(this, TestActivity.class)); } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java b/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java index 65a183fd..af8db970 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java @@ -1,5 +1,7 @@ package gr.thmmy.mthmmy.model; +import androidx.annotation.NonNull; + public class BBTag { private int start, end; private String name; @@ -9,6 +11,12 @@ public class BBTag { this.name = name; } + @NonNull + @Override + public String toString() { + return "start:" + start + ",end:" + end + ",name:" + name; + } + public int getStart() { return start; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java index 18d70d49..89e177c5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java @@ -5,12 +5,17 @@ import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.SpannedString; import android.text.TextUtils; +import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; +import android.text.style.UnderlineSpan; + +import org.commonmark.node.Link; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -18,20 +23,49 @@ import gr.thmmy.mthmmy.model.BBTag; import timber.log.Timber; public class BBParser { - private static final String[] supportedTags = {"b"}; + private static final String[] supportedTags = {"b", "i", "u", "s"}; - public static SpannedString bb2span(String bb) { + public static SpannableStringBuilder bb2span(String bb) { SpannableStringBuilder builder = new SpannableStringBuilder(bb); + // store the original indices of the string + LinkedList stringIndices = new LinkedList<>(); + for (int i = 0; i < builder.length(); i++) { + stringIndices.add(i); + } + BBTag[] tags = getTags(bb); for (BBTag tag : tags) { + int start = stringIndices.indexOf(tag.getStart()); + int end = stringIndices.indexOf(tag.getEnd()); + int startTagLength = tag.getName().length() + 2; + int endTagLength = tag.getName().length() + 3; switch (tag.getName()) { case "b": - builder.setSpan(new StyleSpan(Typeface.BOLD), tag.getStart(), tag.getEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + builder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + break; + case "i": + builder.setSpan(new StyleSpan(Typeface.ITALIC), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + break; + case "u": + builder.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + break; + case "s": + builder.setSpan(new StrikethroughSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); break; default: throw new UnsupportedCharsetException("Tag not supported"); } + //remove starting and ending tag and and do the same changes in the list + builder.delete(start, start + startTagLength); + for (int i = start; i < start + startTagLength; i++) { + stringIndices.remove(start); + } + builder.delete(end - startTagLength, end - startTagLength + endTagLength); + for (int i = end - startTagLength; i < end - startTagLength + endTagLength; i++) { + stringIndices.remove(end - startTagLength); + } } + return builder; } public static BBTag[] getTags(String bb) { @@ -40,7 +74,7 @@ public class BBParser { LinkedList tags = new LinkedList<>(); Matcher bbMatcher = bbtagPattern.matcher(bb); while (bbMatcher.find()) { - String name = bbMatcher.group(0); + String name = bbMatcher.group(1); if (name.startsWith("/")) { //closing tag name = name.substring(1); @@ -55,6 +89,10 @@ public class BBParser { if (isSupported(name)) tags.add(new BBTag(bbMatcher.start(), name)); } + // remove parsed tags with no end tag + for (BBTag bbTag : tags) + if (bbTag.getEnd() == 0) + tags.remove(bbTag); return tags.toArray(new BBTag[0]); } diff --git a/app/src/main/res/layout/activity_test.xml b/app/src/main/res/layout/activity_test.xml new file mode 100644 index 00000000..16accea6 --- /dev/null +++ b/app/src/main/res/layout/activity_test.xml @@ -0,0 +1,20 @@ + + + + + + + + \ No newline at end of file