Skip to content

Commit

Permalink
Readability for citation key patterns (#6706)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3KAW8Pnf7mkmdSMPHz27 authored Sep 21, 2020
1 parent c2698bb commit efdd24c
Show file tree
Hide file tree
Showing 6 changed files with 788 additions and 903 deletions.
1,029 changes: 467 additions & 562 deletions src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CitationKeyGenerator extends BracketedPattern {
private final AbstractCitationKeyPattern citeKeyPattern;
private final BibDatabase database;
private final CitationKeyPatternPreferences citationKeyPatternPreferences;
private final String unwantedCharacters;

public CitationKeyGenerator(BibDatabaseContext bibDatabaseContext, CitationKeyPatternPreferences citationKeyPatternPreferences) {
this(bibDatabaseContext.getMetaData().getCiteKeyPattern(citationKeyPatternPreferences.getKeyPattern()),
Expand All @@ -43,6 +44,7 @@ public CitationKeyGenerator(AbstractCitationKeyPattern citeKeyPattern, BibDataba
this.citeKeyPattern = Objects.requireNonNull(citeKeyPattern);
this.database = Objects.requireNonNull(database);
this.citationKeyPatternPreferences = Objects.requireNonNull(citationKeyPatternPreferences);
this.unwantedCharacters = citationKeyPatternPreferences.getUnwantedCharacters();
}

@Deprecated
Expand Down Expand Up @@ -84,6 +86,10 @@ private static String getAppendix(int number) {
}
}

public static String removeDefaultUnwantedCharacters(String key) {
return removeUnwantedCharacters(key, DEFAULT_UNWANTED_CHARACTERS);
}

public static String removeUnwantedCharacters(String key, String unwantedCharacters) {
String newKey = key.chars()
.filter(c -> unwantedCharacters.indexOf(c) == -1)
Expand Down Expand Up @@ -124,13 +130,13 @@ public String generateKey(BibEntry entry) {
List<String> parts = parseFieldMarker(typeListEntry);
Character delimiter = citationKeyPatternPreferences.getKeywordDelimiter();
String pattern = "[" + parts.get(0) + "]";
String label = expandBrackets(pattern, delimiter, entry, database);
String label = removeUnwantedCharacters(expandBrackets(pattern, delimiter, entry, database), unwantedCharacters);
// apply modifier if present
if (parts.size() > 1) {
label = applyModifiers(label, parts, 1);
label = removeUnwantedCharacters(applyModifiers(label, parts, 1), unwantedCharacters);
}
// Remove all illegal characters from the label.
label = cleanKey(label, citationKeyPatternPreferences.getUnwantedCharacters());
label = cleanKey(label, unwantedCharacters);
stringBuilder.append(label);
} else {
stringBuilder.append(typeListEntry);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ private Optional<String> genericGetResolvedFieldOrAlias(Field field, BibDatabase
}
}
}
return result.map(resultText -> BibDatabase.getText(resultText, database));

return (database == null || result.isEmpty()) ?
result :
Optional.of(database.resolveForStrings(result.get()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ class BracketedPatternTest {

@BeforeEach
void setUp() {
bibentry = new BibEntry();
bibentry.setField(StandardField.AUTHOR, "O. Kitsune");
bibentry.setField(StandardField.YEAR, "2017");
bibentry.setField(StandardField.PAGES, "213--216");

dbentry = new BibEntry();
dbentry.setType(StandardEntryType.Article);
dbentry.setCitationKey("HipKro03");
dbentry.setField(StandardField.AUTHOR, "Eric von Hippel and Georg von Krogh");
dbentry.setField(StandardField.TITLE, "Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science");
dbentry.setField(StandardField.JOURNAL, "Organization Science");
dbentry.setField(StandardField.YEAR, "2003");
dbentry.setField(StandardField.VOLUME, "14");
dbentry.setField(StandardField.PAGES, "209--223");
dbentry.setField(StandardField.NUMBER, "2");
dbentry.setField(StandardField.ADDRESS, "Institute for Operations Research and the Management Sciences (INFORMS), Linthicum, Maryland, USA");
dbentry.setField(StandardField.DOI, "http://dx.doi.org/10.1287/orsc.14.2.209.14992");
dbentry.setField(StandardField.ISSN, "1526-5455");
dbentry.setField(StandardField.PUBLISHER, "INFORMS");
bibentry = new BibEntry().withField(StandardField.AUTHOR, "O. Kitsune")
.withField(StandardField.YEAR, "2017")
.withField(StandardField.PAGES, "213--216");

dbentry = new BibEntry(StandardEntryType.Article)
.withCitationKey("HipKro03")
.withField(StandardField.AUTHOR, "Eric von Hippel and Georg von Krogh")
.withField(StandardField.TITLE, "Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science")
.withField(StandardField.JOURNAL, "Organization Science")
.withField(StandardField.YEAR, "2003")
.withField(StandardField.VOLUME, "14")
.withField(StandardField.PAGES, "209--223")
.withField(StandardField.NUMBER, "2")
.withField(StandardField.ADDRESS, "Institute for Operations Research and the Management Sciences (INFORMS), Linthicum, Maryland, USA")
.withField(StandardField.DOI, "http://dx.doi.org/10.1287/orsc.14.2.209.14992")
.withField(StandardField.ISSN, "1526-5455")
.withField(StandardField.PUBLISHER, "INFORMS");

database = new BibDatabase();
database.insertEntry(dbentry);
Expand Down Expand Up @@ -102,10 +100,10 @@ void databaseWithStringsExpansionTest() {
BibDatabase another_database = new BibDatabase();
BibtexString string = new BibtexString("sgr", "Saulius Gražulis");
another_database.addString(string);
bibentry = new BibEntry();
bibentry.setField(StandardField.AUTHOR, "#sgr#");
bibentry.setField(StandardField.YEAR, "2017");
bibentry.setField(StandardField.PAGES, "213--216");
bibentry = new BibEntry()
.withField(StandardField.AUTHOR, "#sgr#")
.withField(StandardField.YEAR, "2017")
.withField(StandardField.PAGES, "213--216");
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
assertEquals("2017_Gražulis_213", pattern.expand(bibentry,
another_database));
Expand Down Expand Up @@ -139,10 +137,10 @@ void entryTypeExpansionLowercaseTest() {
void suppliedBibentryBracketExpansionTest() {
BibDatabase another_database = null;
BracketedPattern pattern = new BracketedPattern("[year]_[auth]_[firstpage]");
BibEntry another_bibentry = new BibEntry();
another_bibentry.setField(StandardField.AUTHOR, "Gražulis, Saulius");
another_bibentry.setField(StandardField.YEAR, "2017");
another_bibentry.setField(StandardField.PAGES, "213--216");
BibEntry another_bibentry = new BibEntry()
.withField(StandardField.AUTHOR, "Gražulis, Saulius")
.withField(StandardField.YEAR, "2017")
.withField(StandardField.PAGES, "213--216");
assertEquals("2017_Gražulis_213", pattern.expand(another_bibentry, ';', another_database));
}

Expand Down Expand Up @@ -208,8 +206,7 @@ void lowerFormatterWorksOnVonNamePrefixes() {

@Test
void testResolvedFieldAndFormat() {
BibEntry child = new BibEntry();
child.setField(StandardField.CROSSREF, "HipKro03");
BibEntry child = new BibEntry().withField(StandardField.CROSSREF, "HipKro03");
database.insertEntry(child);

Character separator = ';';
Expand All @@ -233,8 +230,8 @@ void testResolvedFieldAndFormat() {

@Test
void testResolvedParentNotInDatabase() {
BibEntry child = new BibEntry();
child.setField(StandardField.CROSSREF, "HipKro03");
BibEntry child = new BibEntry()
.withField(StandardField.CROSSREF, "HipKro03");
database.removeEntry(dbentry);
database.insertEntry(child);

Expand Down Expand Up @@ -273,4 +270,12 @@ void expandBracketsDoesNotTruncateWithoutAnArgumentToTruncateModifier() {
assertEquals("Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science",
BracketedPattern.expandBrackets("[fulltitle:truncate]", ';', dbentry, database));
}

@Test
void expandBracketsWithAuthorStartingWithBrackets() {
// Issue https://github.com/JabRef/jabref/issues/3920
BibEntry bibEntry = new BibEntry()
.withField(StandardField.AUTHOR, "Patrik {\\v{S}}pan{\\v{e}}l and Kseniya Dryahina and David Smith");
assertEquals("ŠpanělEtAl", BracketedPattern.expandBrackets("[authEtAl:latex_to_unicode]", null, bibEntry, null));
}
}
Loading

0 comments on commit efdd24c

Please sign in to comment.