Skip to content

Commit

Permalink
Revert "Fix: bibkey generated does not handle diacritics" (#4741)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr authored Mar 10, 2019
1 parent c75481f commit 26f7865
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class BibtexKeyGenerator extends BracketedPattern {
*/
public static final String APPENDIX_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
private static final Logger LOGGER = LoggerFactory.getLogger(BibtexKeyGenerator.class);
private static final String KEY_ILLEGAL_CHARACTERS = "{}(),\\\"-#~^':`";
private static final String KEY_UNWANTED_CHARACTERS = "{}(),\\\"-";
private static final String KEY_ILLEGAL_CHARACTERS = "{}(),\\\"#~^':`";
private static final String KEY_UNWANTED_CHARACTERS = "{}(),\\\"";
private final AbstractBibtexKeyPattern citeKeyPattern;
private final BibDatabase database;
private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences;
Expand Down Expand Up @@ -72,15 +72,15 @@ private static String getAppendix(int number) {
}
}

public static String removeUnwantedCharacters(String key, boolean enforceLegalKey) {
public static String cleanKey(String key, boolean enforceLegalKey) {
if (!enforceLegalKey) {
// User doesn't want us to enforce legal characters. We must still look
// for whitespace and some characters such as commas, since these would
// interfere with parsing:
StringBuilder newKey = new StringBuilder();
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (KEY_UNWANTED_CHARACTERS.indexOf(c) == -1) {
if (!Character.isWhitespace(c) && (KEY_UNWANTED_CHARACTERS.indexOf(c) == -1)) {
newKey.append(c);
}
}
Expand All @@ -90,7 +90,7 @@ public static String removeUnwantedCharacters(String key, boolean enforceLegalKe
StringBuilder newKey = new StringBuilder();
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (KEY_ILLEGAL_CHARACTERS.indexOf(c) == -1) {
if (!Character.isWhitespace(c) && (KEY_ILLEGAL_CHARACTERS.indexOf(c) == -1)) {
newKey.append(c);
}
}
Expand All @@ -100,10 +100,6 @@ public static String removeUnwantedCharacters(String key, boolean enforceLegalKe
return StringUtil.replaceSpecialCharacters(newKey.toString());
}

public static String cleanKey(String key, boolean enforceLegalKey) {
return removeUnwantedCharacters(key, enforceLegalKey).replaceAll("\\s","");
}

public String generateKey(BibEntry entry) {
String key;
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -127,7 +123,7 @@ public String generateKey(BibEntry entry) {
List<String> parts = parseFieldMarker(typeListEntry);
Character delimiter = bibtexKeyPatternPreferences.getKeywordDelimiter();
String pattern = "[" + parts.get(0) + "]";
String label = expandBrackets(pattern, delimiter, entry, database, bibtexKeyPatternPreferences.isEnforceLegalKey());
String label = expandBrackets(pattern, delimiter, entry, database);
// apply modifier if present
if (parts.size() > 1) {
label = applyModifiers(label, parts, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ public String expand(BibEntry bibentry, Character keywordDelimiter, BibDatabase
return expandBrackets(this.pattern, keywordDelimiter, bibentry, database);
}

public static String expandBrackets(String pattern, Character keywordDelimiter, BibEntry entry, BibDatabase database) {
return expandBrackets(pattern, keywordDelimiter, entry, database, false);
}

/**
* Expands a pattern
*
Expand All @@ -105,7 +101,7 @@ public static String expandBrackets(String pattern, Character keywordDelimiter,
* @param database The database for field resolving. May be null.
* @return The expanded pattern. Not null.
*/
public static String expandBrackets(String pattern, Character keywordDelimiter, BibEntry entry, BibDatabase database, boolean isEnforceLegalKey) {
public static String expandBrackets(String pattern, Character keywordDelimiter, BibEntry entry, BibDatabase database) {
Objects.requireNonNull(pattern);
Objects.requireNonNull(entry);
StringBuilder sb = new StringBuilder();
Expand All @@ -126,10 +122,10 @@ public static String expandBrackets(String pattern, Character keywordDelimiter,
// check whether there is a modifier on the end such as
// ":lower":
if (fieldParts.size() <= 1) {
sb.append(getFieldValue(entry, token, keywordDelimiter, database, isEnforceLegalKey));
sb.append(getFieldValue(entry, token, keywordDelimiter, database));
} else {
// apply modifiers:
String fieldValue = getFieldValue(entry, fieldParts.get(0), keywordDelimiter, database, isEnforceLegalKey);
String fieldValue = getFieldValue(entry, fieldParts.get(0), keywordDelimiter, database);
sb.append(applyModifiers(fieldValue, fieldParts, 1));
}
// Fetch and discard the closing ']'
Expand Down Expand Up @@ -160,7 +156,7 @@ public static String expandBrackets(String pattern, Character keywordDelimiter,
*
* @return String containing the evaluation result. Empty string if the pattern cannot be resolved.
*/
public static String getFieldValue(BibEntry entry, String value, Character keywordDelimiter, BibDatabase database, boolean isEnforceLegalKey) {
public static String getFieldValue(BibEntry entry, String value, Character keywordDelimiter, BibDatabase database) {

String val = value;
try {
Expand Down Expand Up @@ -228,8 +224,15 @@ else if ("authorLast".equals(val)) {
return authNofMth(authString, Integer.parseInt(nums[0]),
Integer.parseInt(nums[1]));
} else if (val.matches("auth\\d+")) {
// authN. First N chars of the first author's last
// name.

String fa = firstAuthor(authString);
int num = Integer.parseInt(val.substring(4));
return authN(authString, num, isEnforceLegalKey);
if (num > fa.length()) {
num = fa.length();
}
return fa.substring(0, num);
} else if (val.matches("authors\\d+")) {
return nAuthors(authString, Integer.parseInt(val.substring(7)));
} else {
Expand Down Expand Up @@ -837,18 +840,6 @@ public static String authNofMth(String authorField, int n, int m) {
}
}

/**
* First N chars of the first author's last name.
*/
public static String authN(String authString, int num, boolean isEnforceLegalKey) {
authString = BibtexKeyGenerator.removeUnwantedCharacters(authString, isEnforceLegalKey);
String fa = firstAuthor(authString);
if (num > fa.length()) {
num = fa.length();
}
return fa.substring(0, num);
}

/**
* authshort format:
* added by Kolja Brix, kbx@users.sourceforge.net
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,73 +119,73 @@ public void testMakeLabelAndCheckLegalKeys() throws ParseException {

Optional<BibEntry> entry0 = BibtexParser.singleFromString(
"@ARTICLE{kohn, author={Andreas Köning}, year={2000}}", importFormatPreferences, fileMonitor);
assertEquals("Koe",
assertEquals("Koen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Áöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Aoe",
assertEquals("Aoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Éöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Eoe",
assertEquals("Eoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Íöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Ioe",
assertEquals("Ioen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ĺöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Loe",
assertEquals("Loen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ńöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Noe",
assertEquals("Noen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Óöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Ooe",
assertEquals("Ooen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ŕöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Roe",
assertEquals("Roen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Śöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Soe",
assertEquals("Soen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Úöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Uoe",
assertEquals("Uoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ýöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Yoe",
assertEquals("Yoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Źöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Zoe",
assertEquals("Zoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));
}
Expand All @@ -197,43 +197,31 @@ public void testMakeLabelAndCheckLegalKeys() throws ParseException {
public void testMakeLabelAndCheckLegalKeysAccentGrave() throws ParseException {
Optional<BibEntry> entry0 = BibtexParser.singleFromString(
"@ARTICLE{kohn, author={Andreas Àöning}, year={2000}}", importFormatPreferences, fileMonitor);
assertEquals("Aoe",
assertEquals("Aoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Èöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Eoe",
assertEquals("Eoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ìöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Ioe",
assertEquals("Ioen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Òöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Ooe",
assertEquals("Ooen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andreas Ùöning}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("Uoe",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Oraib Al-Ketan}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("AlK",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));

entry0 = BibtexParser.singleFromString("@ARTICLE{kohn, author={Andrés D'Alessandro}, year={2000}}",
importFormatPreferences, fileMonitor);
assertEquals("DAl",
assertEquals("Uoen",
BibtexKeyGenerator.cleanKey(BibtexKeyGenerator.generateKey(entry0.get(), "auth3",
new BibDatabase()), true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ void generateKeyTitleRegexe() {
bibtexKeyPattern.setDefaultValue("[title:regex(\" \",\"-\")]");
entry.setField("title", "Please replace the spaces");
new BibtexKeyGenerator(bibtexKeyPattern, database, preferences).generateAndSetKey(entry);
assertEquals(Optional.of("PleaseReplacetheSpaces"), entry.getCiteKeyOptional());
assertEquals(Optional.of("Please-Replace-the-Spaces"), entry.getCiteKeyOptional());
}

@Test
Expand Down

0 comments on commit 26f7865

Please sign in to comment.