Correctly handle superscript. Fix usernames contain '-' cannot be set as link.

This commit is contained in:
Alex Ning 2020-08-11 13:39:12 +08:00
parent e487f27461
commit b8b1ce13df
3 changed files with 50 additions and 40 deletions

View File

@ -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+\\+?)+/?";

View File

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

View File

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