Skip to content
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 @@ -207,6 +207,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed LibreOffice/OpenOffice citation updates modifying citations marked for deletion through Track Changes after change recording was disabled. [#17673](https://github.com/JabRef/jabref/pull/17673)
- We fixed edits to selected JStyle files not taking effect when inserting or refreshing citations in LibreOffice/OpenOffice. [#17673](https://github.com/JabRef/jabref/pull/17673)
- We fixed an issue where the generate citation keys toolbar button stopped working after canceling an overwrite. [#17748](https://github.com/JabRef/jabref/issues/17748)
- We fixed an issue where RIS export split organization authors containing "and" into multiple authors. [#17821](https://github.com/JabRef/jabref/issues/17821)

### Removed

Expand Down
52 changes: 41 additions & 11 deletions jablib/src/main/java/org/jabref/logic/layout/format/RisAuthors.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
package org.jabref.logic.layout.format;

import java.util.stream.Collectors;

import org.jabref.logic.layout.ParamLayoutFormatter;
import org.jabref.logic.os.OS;
import org.jabref.model.entry.AuthorList;

import org.jspecify.annotations.Nullable;

public class RisAuthors implements ParamLayoutFormatter {

private String arg = "";

/// Formats each author as a separate RIS line, e.g. `AU - Smith, John`.
///
/// The authors are parsed before any LaTeX conversion so that brace-protected
/// organization names such as `{Research and Development Institute}` stay a single author.
@Override
public String format(String s) {
if (s == null) {
public String format(@Nullable String fieldText) {
if (fieldText == null) {
return "";
}
StringBuilder sb = new StringBuilder();
String[] authors = AuthorList.fixAuthorLastNameFirst(s).split(" and ");
for (int i = 0; i < authors.length; i++) {
sb.append(arg);
sb.append(" - ");
sb.append(authors[i]);
if (i < authors.length - 1) {
sb.append(OS.NEWLINE);
return AuthorList.parse(normalizeWhitespaceInsideBraces(fieldText))
.latexFree()
.getAuthors()
.stream()
.map(author -> arg + " - " + author.getFamilyGiven(false))
.collect(Collectors.joining(OS.NEWLINE));
}

/// Collapses line breaks and repeated whitespace inside braces to a single space.
///
/// Whitespace outside braces is kept, because `AuthorListParser` treats line breaks and
/// multiple spaces between names as author separators.
private static String normalizeWhitespaceInsideBraces(String fieldText) {
StringBuilder result = new StringBuilder(fieldText.length());
int braceLevel = 0;
boolean previousWasWhitespace = false;
for (char c : fieldText.toCharArray()) {
if (c == '{') {
braceLevel++;
} else if ((c == '}') && (braceLevel > 0)) {
braceLevel--;
}
if ((braceLevel > 0) && Character.isWhitespace(c)) {
if (!previousWasWhitespace) {
result.append(' ');
}
previousWasWhitespace = true;
} else {
result.append(c);
previousWasWhitespace = false;
}
}
return sb.toString();
return result.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TY - JOUR
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{journal}JO - \format[FormatChars]{\journal}\end{journal}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
Expand Down
4 changes: 2 additions & 2 deletions jablib/src/main/resources/resource/layout/ris/ris.book.layout
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - BOOK
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{publisher}PB - \format[FormatChars]{\publisher}\end{publisher}
\begin{address}AD - \format[FormatChars]{\address}\end{address}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - CONF
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{booktitle}T2 - \format[FormatChars]{\booktitle}\end{booktitle}
\begin{publisher}PB - \format[FormatChars]{\publisher}\end{publisher}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - CHAP
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{booktitle}T2 - \format[FormatChars]{\booktitle}\end{booktitle}
\begin{publisher}PB - \format[FormatChars]{\publisher}\end{publisher}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - CONF
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{booktitle}T2 - \format[FormatChars]{\booktitle}\end{booktitle}
\begin{publisher}PB - \format[FormatChars]{\publisher}\end{publisher}
Expand Down
4 changes: 2 additions & 2 deletions jablib/src/main/resources/resource/layout/ris/ris.layout
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - GEN
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{journal}JO - \format[FormatChars]{\journal}\end{journal}
\begin{publisher}PB - \format[FormatChars]{\publisher}\end{publisher}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TY - THES
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
\begin{school}PB - \format[FormatChars]{\school}\end{school}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - PAT
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{assignee}\format[FormatChars,RisAuthors(A2)]{\assignee}\end{assignee}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{assignee}\format[RisAuthors(A2)]{\assignee}\end{assignee}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
\begin{number}IS - \format[FormatChars]{\number}\end{number}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TY - THES
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
\begin{school}PB - \format[FormatChars]{\school}\end{school}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - RPRT
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{journal}JO - \format[FormatChars]{\journal}\end{journal}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - UNPB
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
\begin{pages}SP - \format[FormatChars,FirstPage]{\pages}
Expand Down
4 changes: 2 additions & 2 deletions jablib/src/main/resources/resource/layout/ris/ris.www.layout
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TY - ELEC
\begin{author}\format[FormatChars,RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[FormatChars,RisAuthors(A2)]{\editor}\end{editor}
\begin{author}\format[RisAuthors(AU)]{\author}\end{author}
\begin{editor}\format[RisAuthors(A2)]{\editor}\end{editor}
\begin{title}T1 - \format[FormatChars]{\title}\end{title}
\begin{year}Y1 - \format[FormatChars]{\year}\end{year}\begin{month}/\format[FormatChars,RisMonth]{\month}\end{month}
\begin{url}UR - \url\end{url}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.jabref.logic.layout.format;

import org.jabref.logic.os.OS;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class RisAuthorsTest {

private RisAuthors formatter;

@BeforeEach
void setUp() {
formatter = new RisAuthors();
formatter.setArgument("AU");
}

@Test
void nullReturnsEmptyString() {
assertEquals("", formatter.format(null));
}

@Test
void singlePerson() {
assertEquals("AU - Smith, John", formatter.format("John Smith"));
}

@Test
void multiplePeople() {
assertEquals("AU - Smith, John" + OS.NEWLINE + "AU - Doe, Jane",
formatter.format("John Smith and Jane Doe"));
}

@Test
void organizationContainingAndIsSingleAuthor() {
assertEquals("AU - Research and Development Institute",
formatter.format("{Research and Development Institute}"));
}

@Test
void organizationAndPeopleMixed() {
assertEquals("AU - Food and Agriculture Organization" + OS.NEWLINE + "AU - Smith, John",
formatter.format("{Food and Agriculture Organization} and John Smith"));
}

@Test
void latexInNameIsConvertedToUnicode() {
assertEquals("AU - Müller, Hans", formatter.format("Hans M{\\\"u}ller"));
}

@Test
void editorTagIsUsed() {
formatter.setArgument("A2");
assertEquals("A2 - Smith, John", formatter.format("John Smith"));
}

@Test
void multilineOrganizationNameIsPreserved() {
assertEquals("AU - World Health Organization",
formatter.format("{World Health\n Organization}"));
}

@Test
void multilineAuthorListIsSplitCorrectly() {
assertEquals("AU - World Health Organization" + OS.NEWLINE + "AU - Smith, John",
formatter.format("{World Health Organization}\n and John Smith"));
}

@Test
void namesSeparatedByMultipleSpacesAreSplit() {
assertEquals("AU - Smith, John" + OS.NEWLINE + "AU - Doe, Jane",
formatter.format("John Smith Jane Doe"));
}

@Test
void namesSeparatedByLineBreakAreSplit() {
assertEquals("AU - Smith, John" + OS.NEWLINE + "AU - Doe, Jane",
formatter.format("John Smith\nJane Doe"));
}
}
Loading