Skip to content

Commit 4444c8b

Browse files
committed
refactor(transliteration): address review comments
1 parent ccb3839 commit 4444c8b

File tree

9 files changed

+26
-53
lines changed

9 files changed

+26
-53
lines changed

jablib/src/main/java/org/jabref/logic/bibtex/BibtexStandard.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

jablib/src/main/java/org/jabref/logic/citationkeypattern/CitationKeyGenerator.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package org.jabref.logic.citationkeypattern;
22

3-
import java.util.List;
4-
import java.util.Objects;
5-
import java.util.Optional;
6-
import java.util.function.Function;
7-
import java.util.regex.PatternSyntaxException;
8-
9-
import org.jabref.logic.bibtex.BibtexStandard;
103
import org.jabref.logic.util.strings.Transliteration;
114
import org.jabref.model.FieldChange;
125
import org.jabref.model.database.BibDatabase;
136
import org.jabref.model.database.BibDatabaseContext;
147
import org.jabref.model.entry.BibEntry;
158
import org.jabref.model.entry.types.EntryType;
169
import org.jabref.model.strings.StringUtil;
17-
1810
import org.slf4j.Logger;
1911
import org.slf4j.LoggerFactory;
2012

13+
import java.util.Arrays;
14+
import java.util.List;
15+
import java.util.Objects;
16+
import java.util.Optional;
17+
import java.util.function.Function;
18+
import java.util.regex.PatternSyntaxException;
19+
2120
/**
2221
* This is the utility class of the LabelPattern package.
2322
*/
@@ -35,6 +34,10 @@ public class CitationKeyGenerator extends BracketedPattern {
3534
/// See also #DISALLOWED_CHARACTERS
3635
public static final String DEFAULT_UNWANTED_CHARACTERS = "?!;^`ʹ";
3736

37+
/// Source of disallowed characters: <https://tex.stackexchange.com/a/408548/9075>
38+
/// These characters are disallowed in BibTeX keys.
39+
public static final List<Character> DISALLOWED_CHARACTERS = Arrays.asList('{', '}', '(', ')', ',', '=', '\\', '"', '#', '%', '~', '\'');
40+
3841
private static final Logger LOGGER = LoggerFactory.getLogger(CitationKeyGenerator.class);
3942

4043
private final AbstractCitationKeyPatterns citeKeyPattern;
@@ -77,7 +80,7 @@ public static String removeDefaultUnwantedCharacters(String key) {
7780
public static String removeUnwantedCharacters(String key, String unwantedCharacters) {
7881
String newKey = key.chars()
7982
.filter(c -> unwantedCharacters.indexOf(c) == -1)
80-
.filter(c -> !BibtexStandard.DISALLOWED_CHARACTERS.contains((char) c))
83+
.filter(c -> !DISALLOWED_CHARACTERS.contains((char) c))
8184
.collect(StringBuilder::new,
8285
StringBuilder::appendCodePoint, StringBuilder::append)
8386
.toString();

jablib/src/main/java/org/jabref/logic/citationkeypattern/CitationKeyPatternPreferences.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
import com.google.common.annotations.VisibleForTesting;
1212

13-
import static org.jabref.logic.citationkeypattern.CitationKeyGenerator.DEFAULT_UNWANTED_CHARACTERS;
14-
1513
public class CitationKeyPatternPreferences {
1614

1715
public enum KeySuffix {
@@ -84,23 +82,6 @@ public CitationKeyPatternPreferences(boolean shouldTransliterateFields,
8482
new SimpleObjectProperty<>(keywordDelimiter));
8583
}
8684

87-
@VisibleForTesting
88-
public static CitationKeyPatternPreferences getInstanceForTesting() {
89-
return new CitationKeyPatternPreferences(
90-
true,
91-
false,
92-
false,
93-
false,
94-
CitationKeyPatternPreferences.KeySuffix.SECOND_WITH_A,
95-
"",
96-
"",
97-
DEFAULT_UNWANTED_CHARACTERS,
98-
GlobalCitationKeyPatterns.fromPattern("[auth][year]"),
99-
"",
100-
','
101-
);
102-
}
103-
10485
public boolean shouldTransliterateFields() {
10586
return shouldTransliterateFields.get();
10687
}

jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ public JabRefCliPreferences() {
650650

651651
defaults.put(USE_OWNER, Boolean.FALSE);
652652
defaults.put(OVERWRITE_OWNER, Boolean.FALSE);
653-
defaults.put(TRANSLITERATE_FIELDS, Boolean.TRUE);
653+
defaults.put(TRANSLITERATE_FIELDS, Boolean.FALSE);
654654
defaults.put(AVOID_OVERWRITING_KEY, Boolean.FALSE);
655655
defaults.put(WARN_BEFORE_OVERWRITING_KEY, Boolean.TRUE);
656656
defaults.put(CONFIRM_LINKED_FILE_DELETE, Boolean.TRUE);

jablib/src/main/java/org/jabref/logic/util/strings/Transliteration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.jabref.logic.util.strings;
22

3-
import org.jabref.logic.bibtex.BibtexStandard;
4-
53
import com.ibm.icu.text.Transliterator;
4+
import org.jabref.logic.citationkeypattern.CitationKeyGenerator;
65

76
public class Transliteration {
87

@@ -18,7 +17,7 @@ public static String transliterate(String input) {
1817
private static String buildTransliteratorConfig() {
1918
StringBuilder pattern = new StringBuilder();
2019

21-
for (Character c : BibtexStandard.DISALLOWED_CHARACTERS) {
20+
for (Character c : CitationKeyGenerator.DISALLOWED_CHARACTERS) {
2221
// Generally, only characters like `-` or `[` need to be escaped with a backslash,
2322
// but for future proofing we escape all characters.
2423
pattern.append("\\").append(c);

jablib/src/test/java/org/jabref/logic/citationkeypattern/CitationKeyGeneratorTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.jabref.logic.citationkeypattern;
22

3-
import java.util.Optional;
4-
import java.util.stream.Stream;
5-
63
import org.jabref.logic.importer.ImportFormatPreferences;
74
import org.jabref.logic.importer.ParseException;
85
import org.jabref.logic.importer.fileformat.BibtexParser;
96
import org.jabref.model.database.BibDatabase;
107
import org.jabref.model.entry.BibEntry;
118
import org.jabref.model.entry.field.StandardField;
12-
139
import org.junit.jupiter.api.BeforeEach;
1410
import org.junit.jupiter.api.Test;
1511
import org.junit.jupiter.params.ParameterizedTest;
@@ -18,6 +14,9 @@
1814
import org.junit.jupiter.params.provider.MethodSource;
1915
import org.mockito.Answers;
2016

17+
import java.util.Optional;
18+
import java.util.stream.Stream;
19+
2120
import static org.jabref.logic.citationkeypattern.CitationKeyGenerator.DEFAULT_UNWANTED_CHARACTERS;
2221
import static org.junit.jupiter.api.Assertions.assertEquals;
2322
import static org.junit.jupiter.api.Assertions.assertThrows;

jablib/src/test/java/org/jabref/logic/crawler/CrawlerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javafx.collections.FXCollections;
1111

1212
import org.jabref.logic.JabRefException;
13+
import org.jabref.logic.citationkeypattern.CitationKeyGeneratorTestUtils;
1314
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
1415
import org.jabref.logic.exporter.SaveConfiguration;
1516
import org.jabref.logic.exporter.SaveException;
@@ -60,7 +61,7 @@ class CrawlerTest {
6061
void setUp() throws GitAPIException, URISyntaxException {
6162
setUpRepository();
6263

63-
CitationKeyPatternPreferences citationKeyPatternPreferences = CitationKeyPatternPreferences.getInstanceForTesting();
64+
CitationKeyPatternPreferences citationKeyPatternPreferences = CitationKeyGeneratorTestUtils.getInstanceForTesting();
6465

6566
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
6667
importerPreferences = mock(ImporterPreferences.class);

jablib/src/test/java/org/jabref/logic/crawler/StudyRepositoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.jabref.logic.JabRefException;
1515
import org.jabref.logic.LibraryPreferences;
1616
import org.jabref.logic.citationkeypattern.CitationKeyGenerator;
17+
import org.jabref.logic.citationkeypattern.CitationKeyGeneratorTestUtils;
1718
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
1819
import org.jabref.logic.database.DatabaseMerger;
1920
import org.jabref.logic.exporter.SaveConfiguration;
@@ -70,7 +71,7 @@ void setUpMocks() throws IOException, URISyntaxException, JabRefException {
7071
saveConfiguration = mock(SaveConfiguration.class, Answers.RETURNS_DEEP_STUBS);
7172
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
7273
preferences = mock(CliPreferences.class, Answers.RETURNS_DEEP_STUBS);
73-
citationKeyPatternPreferences = CitationKeyPatternPreferences.getInstanceForTesting();
74+
citationKeyPatternPreferences = CitationKeyGeneratorTestUtils.getInstanceForTesting();
7475
when(preferences.getCitationKeyPatternPreferences()).thenReturn(citationKeyPatternPreferences);
7576
when(preferences.getImporterPreferences().getApiKeys()).thenReturn(FXCollections.emptyObservableSet());
7677
when(importFormatPreferences.bibEntryPreferences().getKeywordSeparator()).thenReturn(',');

jablib/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.stream.Stream;
1010

1111
import org.jabref.logic.FilePreferences;
12-
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
12+
import org.jabref.logic.citationkeypattern.CitationKeyGeneratorTestUtils;
1313
import org.jabref.logic.journals.JournalAbbreviationLoader;
1414
import org.jabref.model.database.BibDatabase;
1515
import org.jabref.model.database.BibDatabaseContext;
@@ -136,7 +136,7 @@ void entryIsUnchangedAfterChecks() {
136136

137137
new IntegrityCheck(context,
138138
mock(FilePreferences.class),
139-
CitationKeyPatternPreferences.getInstanceForTesting(),
139+
CitationKeyGeneratorTestUtils.getInstanceForTesting(),
140140
JournalAbbreviationLoader.loadBuiltInRepository(),
141141
false)
142142
.check();
@@ -171,7 +171,7 @@ private void assertWrong(BibDatabaseContext context) {
171171

172172
messages = new IntegrityCheck(context,
173173
mock(FilePreferences.class),
174-
CitationKeyPatternPreferences.getInstanceForTesting(),
174+
CitationKeyGeneratorTestUtils.getInstanceForTesting(),
175175
JournalAbbreviationLoader.loadBuiltInRepository(),
176176
false)
177177
.check();
@@ -186,7 +186,7 @@ private void assertCorrect(BibDatabaseContext context) {
186186

187187
messages = new IntegrityCheck(context,
188188
filePreferencesMock,
189-
CitationKeyPatternPreferences.getInstanceForTesting(),
189+
CitationKeyGeneratorTestUtils.getInstanceForTesting(),
190190
JournalAbbreviationLoader.loadBuiltInRepository(),
191191
false)
192192
.check();

0 commit comments

Comments
 (0)