Browse Source

maybe finish the parse/modify string logic

pull/61/merge
Thodoris1999 6 years ago
parent
commit
28e2bdfe74
  1. 22
      app/src/main/AndroidManifest.xml
  2. 28
      app/src/main/java/gr/thmmy/mthmmy/activities/TestActivity.java
  3. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  4. 8
      app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java
  5. 46
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java
  6. 20
      app/src/main/res/layout/activity_test.xml

22
app/src/main/AndroidManifest.xml

@ -17,11 +17,18 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />
<meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
<meta-data android:name="google_analytics_adid_collection_enabled" android:value="false" />
<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" />
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />
<meta-data
android:name="google_analytics_adid_collection_enabled"
android:value="false" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<activity
android:name=".activities.main.MainActivity"
@ -118,8 +125,8 @@
</activity>
<activity
android:name=".activities.bookmarks.BookmarkActivity"
android:parentActivityName=".activities.main.MainActivity"
android:launchMode="singleTop"
android:parentActivityName=".activities.main.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -127,8 +134,8 @@
</activity>
<activity
android:name=".activities.settings.SettingsActivity"
android:parentActivityName=".activities.main.MainActivity"
android:launchMode="singleTop"
android:parentActivityName=".activities.main.MainActivity"
android:theme="@style/AppTheme.PreferenceTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -166,6 +173,7 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.main.MainActivity" />
</activity>
<activity android:name=".activities.TestActivity"></activity>
</application>
</manifest>

28
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);
}
}

3
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

8
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;
}

46
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<Integer> 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<BBTag> 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]);
}

20
app/src/main/res/layout/activity_test.xml

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.TestActivity">
<TextView
android:id="@+id/bb_raw"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bb2text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Loading…
Cancel
Save