Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow - in citaton keys #12144

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- When importing a file using "Find Unlinked Files", when one or more file directories are available, the file path will be relativized where possible [koppor#549](https://github.com/koppor/jabref/issues/549)
- We added minimum window sizing for windows dedicated to creating new entries [#11944](https://github.com/JabRef/jabref/issues/11944)
- We changed the name of the library-based file directory from 'General File Directory' to 'Library-specific File Directory' per issue. [#571](https://github.com/koppor/jabref/issues/571)
- We changed the defualt [unwanted charachters](https://docs.jabref.org/setup/citationkeypatterns#removing-unwanted-characters) in the citation key generator and allow a dash (`-`) and colon (`:`) being part of a citation key. [#12144](https://github.com/JabRef/jabref/pull/12144)
- The CitationKey column is now a default shown column for the entry table. [#10510](https://github.com/JabRef/jabref/issues/10510)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@
* This is the utility class of the LabelPattern package.
*/
public class CitationKeyGenerator extends BracketedPattern {

/**
* All single characters that we can use for extending a key to make it unique.
*/
public static final String APPENDIX_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";

/**
* List of unwanted characters. These will be removed at the end.
* Note that <code>+</code> is a wanted character to indicate "et al." in authorsAlpha.
* Example: "ABC+". See {@link org.jabref.logic.citationkeypattern.BracketedPatternTest#authorsAlpha()} for examples.
*/
public static final String DEFAULT_UNWANTED_CHARACTERS = "-`ʹ:!;?^";
/// List of unwanted characters. These will be removed at the end.
/// Note that `+` is a wanted character to indicate "et al." in authorsAlpha.
/// Example: `ABC+`. See {@link org.jabref.logic.citationkeypattern.BracketedPatternTest#authorsAlpha()} for examples.
///
/// See also #DISALLOWED_CHARACTERS
public static final String DEFAULT_UNWANTED_CHARACTERS = "?!;^`ʹ";

private static final Logger LOGGER = LoggerFactory.getLogger(CitationKeyGenerator.class);

// Source of disallowed characters : https://tex.stackexchange.com/a/408548/9075
/// Source of disallowed characters: <https://tex.stackexchange.com/a/408548/9075>
/// These characters are disallowed in BibTeX keys.
private static final List<Character> DISALLOWED_CHARACTERS = Arrays.asList('{', '}', '(', ')', ',', '=', '\\', '"', '#', '%', '~', '\'');

private static final Logger LOGGER = LoggerFactory.getLogger(CitationKeyGenerator.class);

private final AbstractCitationKeyPatterns citeKeyPattern;
private final BibDatabase database;
private final CitationKeyPatternPreferences citationKeyPatternPreferences;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class CitationKeyGeneratorTest {
private static final String AUTHNOFMTH = "[auth%d_%d]";
private static final String AUTHFOREINI = "[authForeIni]";
private static final String AUTHFIRSTFULL = "[authFirstFull]";
private static final String AUTHORS = "[authors]";
private static final String AUTHORSALPHA = "[authorsAlpha]";
private static final String AUTHORLAST = "[authorLast]";
private static final String AUTHORLASTFOREINI = "[authorLastForeIni]";
Expand Down Expand Up @@ -192,14 +191,15 @@ void specialLatexCharacterInAuthorName() throws ParseException {
"@ARTICLE{kohn, author={Andreas Ùöning}, year={2000}}", "Uoe",

# Special cases
"@ARTICLE{kohn, author={Oraib Al-Ketan}, year={2000}}", "AlK",
# We keep "-" in citation keys, thus Al-Ketan with three letters is "Al-"
"@ARTICLE{kohn, author={Oraib Al-Ketan}, year={2000}}", "Al-",
"@ARTICLE{kohn, author={Andrés D'Alessandro}, year={2000}}", "DAl",
"@ARTICLE{kohn, author={Andrés Aʹrnold}, year={2000}}", "Arn"
"""
)
void makeLabelAndCheckLegalKeys(String bibtexString, String expectedResult) throws ParseException {
Optional<BibEntry> bibEntry = BibtexParser.singleFromString(bibtexString, importFormatPreferences);
String citationKey = generateKey(bibEntry.orElse(null), "[auth3]", new BibDatabase());
BibEntry bibEntry = BibtexParser.singleFromString(bibtexString, importFormatPreferences).get();
String citationKey = generateKey(bibEntry, "[auth3]", new BibDatabase());

String cleanedKey = CitationKeyGenerator.cleanKey(citationKey, DEFAULT_UNWANTED_CHARACTERS);

Expand Down Expand Up @@ -493,14 +493,19 @@ void firstAuthorVonAndLastNoVonInName() {
assertEquals("Newton", generateKey(AUTHOR_FIRSTNAME_FULL_LASTNAME_FULL_COUNT_2, AUTHFIRSTFULL));
}

/**
* Tests [authors]
*/
@Test
void allAuthors() {
assertEquals("Newton", generateKey(AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_1, AUTHORS));
assertEquals("NewtonMaxwell", generateKey(AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_2, AUTHORS));
assertEquals("NewtonMaxwellEinstein", generateKey(AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_3, AUTHORS));
@ParameterizedTest
@MethodSource
void authors(String expectedKey, BibEntry entry, String pattern) {
assertEquals(expectedKey, generateKey(entry, pattern));
}

static Stream<Arguments> authors() {
return Stream.of(
Arguments.of("Newton", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_1, "[authors]"),
Arguments.of("NewtonMaxwell", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_2, "[authors]"),
Arguments.of("NewtonMaxwellEinstein", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_3, "[authors]"),
Arguments.of("Newton-Maxwell-Einstein", AUTHOR_FIRSTNAME_INITIAL_LASTNAME_FULL_COUNT_3, "[authors:regex(\"(.)([A-Z])\",\"$1-$2\")]")
);
}

static Stream<Arguments> authorsAlpha() {
Expand Down Expand Up @@ -894,7 +899,7 @@ void crossrefShorttitleInitials() {
@Test
void generateKeyStripsColonFromTitle() {
BibEntry entry = new BibEntry().withField(StandardField.TITLE, "Green Scheduling of: Whatever");
assertEquals("GreenSchedulingOfWhatever", generateKey(entry, "[title]"));
assertEquals("GreenSchedulingOf:Whatever", generateKey(entry, "[title]"));
}

@Test
Expand Down Expand Up @@ -971,15 +976,15 @@ void generateKeyWithMinusInCitationStyleOutsideAField() {
.withField(StandardField.AUTHOR, AUTHOR_STRING_FIRSTNAME_FULL_LASTNAME_FULL_COUNT_1)
.withField(StandardField.YEAR, "2019");

assertEquals("Newton2019", generateKey(entry, "[auth]-[year]"));
assertEquals("Newton-2019", generateKey(entry, "[auth]-[year]"));
}

@Test
void generateKeyWithWithFirstNCharacters() {
BibEntry entry = new BibEntry().withField(StandardField.AUTHOR, "Newton, Isaac")
.withField(StandardField.YEAR, "2019");

assertEquals("newt2019", generateKey(entry, "[auth4:lower]-[year]"));
assertEquals("newt-2019", generateKey(entry, "[auth4:lower]-[year]"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void generateKeyEmptyFieldNoColonInDefaultText() {
bibtexKeyPattern.setDefaultValue("[author:(Problem:No Author Provided)]");
entry.clearField(StandardField.AUTHOR);
new CitationKeyGenerator(bibtexKeyPattern, database, preferences).generateAndSetKey(entry);
assertEquals(Optional.of("ProblemNoAuthorProvided"), entry.getCitationKey());
assertEquals(Optional.of("Problem:NoAuthorProvided"), entry.getCitationKey());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ void extractFirstWord() {
formatter = new RegexFormatter("(\"(\\w+).*\", \"$1\")");
assertEquals("First", formatter.format("First Second Third"));
}

@Test
void addDash() {
formatter = new RegexFormatter("(\"(.)([A-Z])\", \"$1-$2\")");
assertEquals("First-Second-Third", formatter.format("FirstSecondThird"));
}
}