From abb37707fbc34c5c1a77b3990642a44b560dcd71 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 24 Feb 2021 22:46:44 +0800 Subject: Try to fix superscript. --- .../activities/CommentActivity.java | 17 +---- .../activities/FullMarkdownActivity.java | 14 ++-- .../CommentAndPostRecyclerViewAdapter.java | 17 +---- .../CommentsListingRecyclerViewAdapter.java | 8 +-- .../adapters/MessageRecyclerViewAdapter.java | 14 ++-- .../PrivateMessagesDetailRecyclerViewAdapter.java | 12 +--- .../adapters/RulesRecyclerViewAdapter.java | 10 +-- .../fragments/SidebarFragment.java | 10 +-- .../infinityforreddit/utils/Utils.java | 79 +++++++++------------- 9 files changed, 55 insertions(+), 126 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index 44c393ff..74dbe822 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -7,7 +7,6 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.text.Spanned; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.Menu; import android.view.MenuItem; @@ -42,11 +41,11 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.adapters.MarkdownBottomBarRecyclerViewAdapter; @@ -136,6 +135,7 @@ public class CommentActivity extends BaseActivity { int linkColor = mCustomThemeWrapper.getLinkColor(); Markwon markwon = Markwon.builder(this) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @Override public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { @@ -153,12 +153,6 @@ public class CommentActivity extends BaseActivity { } }) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); if (parentTextMarkdown != null) { commentParentMarkwonView.setOnLongClickListener(view -> { @@ -182,6 +176,7 @@ public class CommentActivity extends BaseActivity { contentMarkdownRecyclerView.setVisibility(View.VISIBLE); contentMarkdownRecyclerView.setNestedScrollingEnabled(false); Markwon postBodyMarkwon = Markwon.builder(this) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @Override public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { @@ -214,12 +209,6 @@ public class CommentActivity extends BaseActivity { }) .usePlugin(StrikethroughPlugin.create()) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .usePlugin(TableEntryPlugin.create(this)) .build(); MarkwonAdapter markwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java index 79e3d8a5..312fe6cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -10,7 +10,6 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.style.ClickableSpan; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.MenuItem; import android.view.View; @@ -44,15 +43,15 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class FullMarkdownActivity extends BaseActivity { @@ -123,6 +122,7 @@ public class FullMarkdownActivity extends BaseActivity { int spoilerBackgroundColor = markdownColor | 0xFF000000; int linkColor = mCustomThemeWrapper.getLinkColor(); Markwon markwon = Markwon.builder(this) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @NonNull @Override @@ -204,12 +204,6 @@ public class FullMarkdownActivity extends BaseActivity { }) .usePlugin(StrikethroughPlugin.create()) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .usePlugin(TableEntryPlugin.create(this)) .build(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index d7116056..ad040c64 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -16,7 +16,6 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.style.ClickableSpan; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -79,12 +78,12 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import me.saket.bettermovementmethod.BetterLinkMovementMethod; @@ -266,6 +265,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .usePlugin(TableEntryPlugin.create(mActivity)) .build(); mCommentMarkwon = Markwon.builder(mActivity) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @NonNull @Override @@ -462,12 +457,6 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); mMarkwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) .include(TableBlock.class, TableEntry.create(builder -> builder diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java index c6ce9687..202166da 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java @@ -42,6 +42,7 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.simple.ext.SimpleExtPlugin; @@ -115,6 +116,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); mLocale = locale; mAccessToken = accessToken; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java index e6da1344..2c283669 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java @@ -10,7 +10,6 @@ import android.text.Spanned; import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -35,8 +34,10 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; +import ml.docilealligator.infinityforreddit.NetworkState; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.ViewPrivateMessagesActivity; import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; @@ -44,8 +45,6 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.message.FetchMessage; import ml.docilealligator.infinityforreddit.message.Message; import ml.docilealligator.infinityforreddit.message.ReadMessage; -import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.R; import retrofit2.Retrofit; public class MessageRecyclerViewAdapter extends PagedListAdapter { @@ -100,6 +99,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); mAccessToken = accessToken; if (where.equals(FetchMessage.WHERE_MESSAGES)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PrivateMessagesDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PrivateMessagesDetailRecyclerViewAdapter.java index 9d2de4ec..190b8ad5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PrivateMessagesDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PrivateMessagesDetailRecyclerViewAdapter.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.net.Uri; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -28,15 +27,15 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.ViewPrivateMessagesActivity; import ml.docilealligator.infinityforreddit.activities.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.message.Message; -import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -67,6 +66,7 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt mLocale = locale; mAccountName = accountName; mMarkwon = Markwon.builder(viewPrivateMessagesActivity) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @Override public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { @@ -85,12 +85,6 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt }) .usePlugin(StrikethroughPlugin.create()) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); mShowElapsedTime = sharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_ELAPSED_TIME_KEY, false); mTimeFormatPattern = sharedPreferences.getString(SharedPreferencesUtils.TIME_FORMAT_KEY, SharedPreferencesUtils.TIME_FORMAT_DEFAULT_VALUE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java index 5f7fd8e0..51d55400 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.adapters; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -23,9 +22,9 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.movement.MovementMethodPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; import me.saket.bettermovementmethod.BetterLinkMovementMethod; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Rule; @@ -41,6 +40,7 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .build(); mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor(); mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java index 0e76c543..7297cb54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -7,7 +7,6 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.text.Spanned; -import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -36,12 +35,12 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; import me.saket.bettermovementmethod.BetterLinkMovementMethod; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; @@ -103,6 +102,7 @@ public class SidebarFragment extends Fragment { markdownColor = mCustomThemeWrapper.getSecondaryTextColor(); Markwon markwon = Markwon.builder(activity) + .usePlugin(HtmlPlugin.create()) .usePlugin(new AbstractMarkwonPlugin() { @Override public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { @@ -134,12 +134,6 @@ public class SidebarFragment extends Fragment { return true; }))) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) .usePlugin(TableEntryPlugin.create(activity)) .build(); MarkwonAdapter markwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index de048168..f7a7d247 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -52,57 +52,42 @@ public class Utils { return fixSuperScript(regexed); } - public static String fixSuperScript(StringBuilder regexed) { - int newestCaretIndex = regexed.indexOf("^"); - if (newestCaretIndex >= 0) { - boolean hasBracket = false; - int caretWithLeftBracketIndex = -1; - for (int i = newestCaretIndex + 1; i < regexed.length(); i++) { - char currentChar = regexed.charAt(i); - if (currentChar == '^') { - if (!(i > 0 && regexed.charAt(i - 1) == '\\')) { - if (newestCaretIndex < 0) { - newestCaretIndex = i; - } else { - regexed.insert(i, '^'); - newestCaretIndex = i + 1; - i++; - if (newestCaretIndex == regexed.length() - 1) { - regexed.deleteCharAt(regexed.length() - 1); - newestCaretIndex = -1; - break; - } - } - } - } else if (currentChar == ' ' || currentChar == '\n') { - if (newestCaretIndex >= 0) { - if (i != newestCaretIndex + 1) { - regexed.insert(i, '^'); - newestCaretIndex = -1; - i++; - } else { - newestCaretIndex = -1; - } - } - } else if (currentChar == '(') { - if (newestCaretIndex >= 0 && i == newestCaretIndex + 1) { + private static String fixSuperScript(StringBuilder regexed) { + boolean hasBracket = false; + int nCarets = 0; + for (int i = 0; i < regexed.length(); i++) { + char currentChar = regexed.charAt(i); + if (currentChar == '^') { + if (!(i > 0 && regexed.charAt(i - 1) == '\\')) { + if (i < regexed.length() - 1 && regexed.charAt(i + 1) == '(') { + regexed.replace(i, i + 2, ""); hasBracket = true; - newestCaretIndex = -1; - caretWithLeftBracketIndex = i - 1; - } - } else if (currentChar == ')') { - if (hasBracket) { - hasBracket = false; - regexed.setCharAt(i, '^'); - regexed.replace(caretWithLeftBracketIndex, caretWithLeftBracketIndex + 2, "^"); - caretWithLeftBracketIndex = -1; - i--; + } else { + regexed.replace(i, i + 1, ""); } + nCarets++; } + } else if (currentChar == ')' && hasBracket) { + hasBracket = false; + regexed.replace(i, i + 1, ""); + nCarets--; + } else if (currentChar == '\n') { + hasBracket = false; + for (int j = 0; j < nCarets; j++) { + regexed.insert(i, ""); + i += 6; + } + nCarets = 0; + } else if (currentChar == ' ' && !hasBracket) { + for (int j = 0; j < nCarets; j++) { + regexed.insert(i, ""); + i += 6; + } + nCarets = 0; } - if (newestCaretIndex >=0 || caretWithLeftBracketIndex >= 0) { - regexed.insert(regexed.length(), '^'); - } + } + for (int j = 0; j < nCarets; j++) { + regexed.append(""); } return regexed.toString(); -- cgit v1.2.3