diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-08-11 05:39:12 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-08-11 05:39:12 +0000 |
commit | b8b1ce13df1f8fd7d44dea48c69c69018d6a9367 (patch) | |
tree | aeb2b2b0b9b9407dba5c149ebbd6d9ad9dd92211 /app/src/main/java/ml | |
parent | e487f27461e0bf7e404764ce790d3ed38180b9d6 (diff) | |
download | infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar.gz infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar.bz2 infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar.lz infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar.xz infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.tar.zst infinity-for-reddit-b8b1ce13df1f8fd7d44dea48c69c69018d6a9367.zip |
Correctly handle superscript. Fix usernames contain '-' cannot be set as link.
Diffstat (limited to 'app/src/main/java/ml')
3 files changed, 50 insertions, 40 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index 15a072d8..fcbc0608 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -33,8 +33,8 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/?\\w+/?"; private static final String COMMENT_PATTERN = "/(r|u|U|user)/\\w+/comments/\\w+/?\\w+/\\w+/?"; - private static final String SUBREDDIT_PATTERN = "/[rR]/\\w+/?"; - private static final String USER_PATTERN = "/(u|U|user)/\\w+/?"; + private static final String SUBREDDIT_PATTERN = "/[rR]/[\\w-]+/?"; + private static final String USER_PATTERN = "/(u|U|user)/[\\w-]+/?"; private static final String SIDEBAR_PATTERN = "/[rR]/\\w+/about/sidebar"; private static final String MULTIREDDIT_PATTERN = "/user/\\w+/m/\\w+/?"; private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 4330cb64..fc6bfd61 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -407,15 +407,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - if (accessToken == null) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } else { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { subredditName = getArguments().getString(EXTRA_NAME); String sort; @@ -475,15 +469,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - if (accessToken == null) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } else { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else if(postType == PostDataSource.TYPE_MULTI_REDDIT) { multiRedditPath = getArguments().getString(EXTRA_NAME); String sort; @@ -529,15 +517,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - if (accessToken == null) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } else { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, filter, nsfw)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { username = getArguments().getString(EXTRA_USER_NAME); String where = getArguments().getString(EXTRA_USER_WHERE); @@ -582,15 +564,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); - if (accessToken == null) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); - } else { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, getResources().getConfiguration().locale, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + accountName, getResources().getConfiguration().locale, mSharedPreferences, + postFeedScrolledPositionSharedPreferences, username, postType, sortType, where, filter, nsfw)).get(PostViewModel.class); } else { String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name()); if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { 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 14cdb57a..dca08778 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/Utils.java @@ -19,6 +19,8 @@ import androidx.core.graphics.drawable.DrawableCompat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SortType; @@ -32,8 +34,40 @@ public class Utils { private static final long YEAR_MILLIS = 12 * MONTH_MILLIS; public static String modifyMarkdown(String markdown) { - return markdown.replaceAll("((?<=[\\s])|^)/{0,1}[rRuU]/\\w+/{0,1}", "[$0]($0)") - .replaceAll("\\^\\w+", "$0^"); + StringBuilder regexed = new StringBuilder(markdown.replaceAll("((?<=[\\s])|^)/{0,1}[rRuU]/[\\w-]+/{0,1}", "[$0]($0)")); + + int startIndex = 0; + + Pattern pattern = Pattern.compile("\\^.+"); + Matcher matcher = pattern.matcher(regexed); + // Check all occurrences + while (matcher.find(startIndex)) { + int count = 0; + Pattern pattern2 = Pattern.compile("(\\^\\([^)]+\\))"); + Matcher matcher2 = pattern2.matcher(matcher.group()); + if (matcher2.find()) { + regexed.setCharAt(matcher2.end() + matcher.start() - 1, '^'); + regexed.deleteCharAt(matcher2.start() + matcher.start() + 1); + + startIndex = matcher.start() + matcher2.end(); + String substring = regexed.substring(matcher.start() + matcher2.start() + 1, matcher.start() + matcher2.end() - 2); + String trimmedSubstring = substring.trim(); + regexed.replace(matcher.start() + matcher2.start() + 1, matcher.start() + matcher2.end() - 2, trimmedSubstring); + startIndex -= (substring.length() - trimmedSubstring.length()); + continue; + } + + regexed.insert(matcher.end(), '^'); + + for (int i = matcher.end() - 1; i >= matcher.start() + 1; i--) { + if (regexed.charAt(i) == '^') { + regexed.deleteCharAt(i); + count++; + } + } + startIndex = matcher.end() - count; + } + return regexed.toString(); } public static CharSequence trimTrailingWhitespace(CharSequence source) { |