allow exclamation/question marks in pronouns

This commit is contained in:
sk 2023-11-10 16:13:14 +01:00
parent a085744038
commit d6aeb753fc
2 changed files with 15 additions and 3 deletions

View File

@ -257,5 +257,9 @@ public class UiUtilsTest {
assertEquals("* (asterisk)", UiUtils.extractPronouns(MastodonApp.context, fakeAccount( assertEquals("* (asterisk)", UiUtils.extractPronouns(MastodonApp.context, fakeAccount(
makeField("pronouns", "-- * (asterisk) --") makeField("pronouns", "-- * (asterisk) --")
)).orElseThrow()); )).orElseThrow());
assertEquals("they/(she?)", UiUtils.extractPronouns(MastodonApp.context, fakeAccount(
makeField("pronouns", "they/(she?)...")
)).orElseThrow());
} }
} }

View File

@ -1683,7 +1683,9 @@ public class UiUtils {
"pronouns.page/" "pronouns.page/"
}; };
private static final Pattern trimPronouns=Pattern.compile("[^\\w*]*([\\w*].*[\\w*]|[\\w*])\\W*"); private static final String PRONOUN_CHARS="\\w*¿¡!?";
private static final Pattern trimPronouns=
Pattern.compile("[^"+PRONOUN_CHARS+"]*(["+PRONOUN_CHARS+"].*["+PRONOUN_CHARS+"]|["+PRONOUN_CHARS+"])\\W*");
private static String extractPronounsFromField(String localizedPronouns, AccountField field) { private static String extractPronounsFromField(String localizedPronouns, AccountField field) {
if(!field.name.toLowerCase().contains(localizedPronouns) && if(!field.name.toLowerCase().contains(localizedPronouns) &&
!field.name.toLowerCase().contains("pronouns")) return null; !field.name.toLowerCase().contains("pronouns")) return null;
@ -1707,14 +1709,20 @@ public class UiUtils {
Matcher matcher=trimPronouns.matcher(text); Matcher matcher=trimPronouns.matcher(text);
if(!matcher.find()) return null; if(!matcher.find()) return null;
String pronouns=matcher.group(1); String pronouns=matcher.group(1);
// crude fix to allow for pronouns like "it(/she)" or "(de) sie/ihr" // crude fix to allow for pronouns like "it(/she)" or "(de) sie/ihr"
int missingParens=0; int missingParens=0, missingBrackets=0;
for(char c : pronouns.toCharArray()){ for(char c : pronouns.toCharArray()){
if(c=='(') missingParens++; if(c=='(') missingParens++;
if(c==')') missingParens--; else if(c=='[') missingBrackets++;
else if(c==')') missingParens--;
else if(c==']') missingBrackets--;
} }
if(missingParens > 0) pronouns+=")".repeat(missingParens); if(missingParens > 0) pronouns+=")".repeat(missingParens);
else if(missingParens < 0) pronouns="(".repeat(missingParens*-1)+pronouns; else if(missingParens < 0) pronouns="(".repeat(missingParens*-1)+pronouns;
if(missingBrackets > 0) pronouns+="]".repeat(missingBrackets);
else if(missingBrackets < 0) pronouns="[".repeat(missingBrackets*-1)+pronouns;
// if ends with an un-closed custom emoji // if ends with an un-closed custom emoji
if(pronouns.matches("^.*\\s+:[a-zA-Z_]+$")) pronouns+=':'; if(pronouns.matches("^.*\\s+:[a-zA-Z_]+$")) pronouns+=':';
return pronouns; return pronouns;