Skip to content

Commit

Permalink
Citation keygen: Return vonPart if lastName is empty
Browse files Browse the repository at this point in the history
Fixes #8601
  • Loading branch information
Siedlerchr committed Apr 1, 2022
1 parent 2bf53cb commit 853834b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,16 @@ private static String keepLettersAndDigitsOnly(String in) {
* Gets the last name of the first author/editor
*
* @param authorList an {@link AuthorList}
* @return the surname of an author/editor or "" if no author was found This method is guaranteed to never return
* null.
* @return the surname of an author/editor or the von part if no lastname is prsent or "" if no author was found or both firstname+lastname are empty
* This method is guaranteed to never return null.
*/
private static String firstAuthor(AuthorList authorList) {
return authorList.getAuthors().stream()
.findFirst()
.flatMap(Author::getLast).orElse("");
.flatMap(author -> author.getLast().isPresent() ? author.getLast() : author.getVon())
.orElse("");


}

/**
Expand Down Expand Up @@ -967,7 +970,7 @@ private static String authshort(AuthorList authorList) {
if (numberOfAuthors == 1) {
author.append(authorList.getAuthor(0).getLast().orElse(""));
} else if (numberOfAuthors >= 2) {
for (int i = 0; i < numberOfAuthors && i < 3; i++) {
for (int i = 0; (i < numberOfAuthors) && (i < 3); i++) {
author.append(authNofMth(authorList, 1, i + 1));
}
if (numberOfAuthors > 3) {
Expand Down Expand Up @@ -1004,7 +1007,7 @@ private static String authshort(AuthorList authorList) {
* to "" be returned.
*/
private static String authIniN(AuthorList authorList, int n) {
if (n <= 0 || authorList.isEmpty()) {
if ((n <= 0) || authorList.isEmpty()) {
return "";
}

Expand Down Expand Up @@ -1233,7 +1236,7 @@ private static String generateInstitutionKey(String content) {
}
} else if ((tokenTypes.contains(Institution.SCHOOL)
|| tokenTypes.contains(Institution.DEPARTMENT))
&& institutionNameTokens.length > 1) {
&& (institutionNameTokens.length > 1)) {
// School is an abbreviation of all the words beginning with a
// capital letter excluding: department, school and faculty words.
StringBuilder schoolSB = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,18 @@ void generateKeyWithFallbackField() {

assertEquals("2021", generateKey(bibEntry, "[title:([EPRINT:([YEAR])])]"));
}

@Test
void generateKeyWithLowercaseAuthorLastnameUseVonPart() {
BibEntry entry = createABibEntryAuthor("Stéphane d'Ascoli");
entry.setField(StandardField.YEAR, "2021");
assertEquals("dAscoli2021", generateKey(entry, "[auth][year]"));
}

@Test
void generateKeyWithLowercaseAuthorWithVonAndLastname() {
BibEntry entry = createABibEntryAuthor("Michiel van den Brekel");
entry.setField(StandardField.YEAR, "2021");
assertEquals("Brekel2021", generateKey(entry, "[auth][year]"));
}
}

0 comments on commit 853834b

Please sign in to comment.