Try to fix superscript.

This commit is contained in:
Alex Ning 2021-02-24 22:46:44 +08:00
parent f4bfa4fc40
commit abb37707fb
10 changed files with 56 additions and 126 deletions

View File

@ -138,6 +138,7 @@ dependencies {
implementation "io.noties.markwon:linkify:$markwonVersion"
implementation "io.noties.markwon:recycler-table:$markwonVersion"
implementation "io.noties.markwon:simple-ext:$markwonVersion"
implementation "io.noties.markwon:html:$markwonVersion"
implementation 'com.atlassian.commonmark:commonmark-ext-gfm-tables:0.14.0'
implementation 'me.saket:better-link-movement-method:2.2.0'

View File

@ -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)

View File

@ -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();

View File

@ -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<Recy
int postSpoilerBackgroundColor = markdownColor | 0xFF000000;
int linkColor = customThemeWrapper.getLinkColor();
mPostDetailMarkwon = Markwon.builder(mActivity)
.usePlugin(HtmlPlugin.create())
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
@ -368,15 +368,10 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
return true;
})))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(SimpleExtPlugin.create(plugin ->
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<Recy
return true;
})))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(SimpleExtPlugin.create(plugin ->
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

View File

@ -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<Comment
mCommentColor = customThemeWrapper.getCommentColor();
int commentSpoilerBackgroundColor = mCommentColor | 0xFF000000;
mMarkwon = Markwon.builder(mActivity)
.usePlugin(HtmlPlugin.create())
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
@ -203,12 +205,6 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
})))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(StrikethroughPlugin.create())
.usePlugin(SimpleExtPlugin.create(plugin ->
plugin.addExtension(1, '^', (configuration, props) -> {
return new SuperscriptSpan();
})
)
)
.build();
mLocale = locale;
mAccessToken = accessToken;

View File

@ -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<Message, RecyclerView.ViewHolder> {
@ -100,6 +99,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
mButtonTextColor = customThemeWrapper.getButtonTextColor();
mMarkwon = Markwon.builder(mContext)
.usePlugin(HtmlPlugin.create())
.usePlugin(new AbstractMarkwonPlugin() {
@NonNull
@Override
@ -177,12 +177,6 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
})
.usePlugin(StrikethroughPlugin.create())
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(SimpleExtPlugin.create(plugin ->
plugin.addExtension(1, '^', (configuration, props) -> {
return new SuperscriptSpan();
})
)
)
.build();
mAccessToken = accessToken;
if (where.equals(FetchMessage.WHERE_MESSAGES)) {

View File

@ -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);

View File

@ -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<RulesRecycler
public RulesRecyclerViewAdapter(AppCompatActivity activity, CustomThemeWrapper customThemeWrapper) {
markwon = Markwon.builder(activity)
.usePlugin(HtmlPlugin.create())
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
@ -69,12 +69,6 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
})))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(StrikethroughPlugin.create())
.usePlugin(SimpleExtPlugin.create(plugin ->
plugin.addExtension(1, '^', (configuration, props) -> {
return new SuperscriptSpan();
})
)
)
.build();
mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor();
mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor();

View File

@ -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)

View File

@ -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, "<sup>");
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, "<sup>");
}
nCarets++;
}
} else if (currentChar == ')' && hasBracket) {
hasBracket = false;
regexed.replace(i, i + 1, "</sup>");
nCarets--;
} else if (currentChar == '\n') {
hasBracket = false;
for (int j = 0; j < nCarets; j++) {
regexed.insert(i, "</sup>");
i += 6;
}
nCarets = 0;
} else if (currentChar == ' ' && !hasBracket) {
for (int j = 0; j < nCarets; j++) {
regexed.insert(i, "</sup>");
i += 6;
}
nCarets = 0;
}
if (newestCaretIndex >=0 || caretWithLeftBracketIndex >= 0) {
regexed.insert(regexed.length(), '^');
}
}
for (int j = 0; j < nCarets; j++) {
regexed.append("</sup>");
}
return regexed.toString();