Skip to content

Commit

Permalink
Support CrossRef in CSL
Browse files Browse the repository at this point in the history
Fixes #7378

Add new test
Fix existing test
  • Loading branch information
Siedlerchr committed Jan 23, 2022
1 parent 95a7274 commit acbeafd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 194 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Changed

- The CSL preview styles now also support displaying data from cross references entries that are linked via the `crossref` field [#7378](https://github.com/JabRef/jabref/issues/7378)

### Fixed

- We fixed an issue where an exception could occur when saving the preferences [#7614](https://github.com/JabRef/jabref/issues/7614)
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ private static Menu createCopySubMenu(ActionFactory factory,
if (previewPreferences.getSelectedPreviewLayout() instanceof CitationStylePreviewLayout) {
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)));
Menu copyCitationMenu = factory.createMenu(StandardActions.COPY_CITATION_MORE);
copyCitationMenu.getItems().addAll(
factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)),
factory.createMenuItem(StandardActions.COPY_CITATION_RTF, new CopyCitationAction(CitationStyleOutputFormat.RTF, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)),
factory.createMenuItem(StandardActions.COPY_CITATION_ASCII_DOC, new CopyCitationAction(CitationStyleOutputFormat.ASCII_DOC, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)),
factory.createMenuItem(StandardActions.COPY_CITATION_XSLFO, new CopyCitationAction(CitationStyleOutputFormat.XSL_FO, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)));
copyCitationMenu.getItems().add(
factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)));
copySpecialMenu.getItems().add(copyCitationMenu);
} else {
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, taskExecutor, previewPreferences)));
Expand Down
44 changes: 1 addition & 43 deletions src/main/java/org/jabref/gui/preview/CopyCitationAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,46 +126,6 @@ protected static ClipboardContent processText(List<String> citations) {
return content;
}

/**
* Converts the citations into the RTF format.
*/
protected static ClipboardContent processRtf(List<String> citations) {
String result = "{\\rtf" + OS.NEWLINE +
String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) +
"}";
ClipboardContent content = new ClipboardContent();
content.putString(result);
content.putRtf(result);
return content;
}

/**
* Inserts each citation into a XLSFO body and copies it to the clipboard
*/
protected static ClipboardContent processXslFo(List<String> citations) {
String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + OS.NEWLINE +
"<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">" + OS.NEWLINE +
" <fo:layout-master-set>" + OS.NEWLINE +
" <fo:simple-page-master master-name=\"citations\">" + OS.NEWLINE +
" <fo:region-body/>" + OS.NEWLINE +
" </fo:simple-page-master>" + OS.NEWLINE +
" </fo:layout-master-set>" + OS.NEWLINE +
" <fo:page-sequence master-reference=\"citations\">" + OS.NEWLINE +
" <fo:flow flow-name=\"xsl-region-body\">" + OS.NEWLINE + OS.NEWLINE;

result += String.join(CitationStyleOutputFormat.XSL_FO.getLineSeparator(), citations);

result += OS.NEWLINE +
" </fo:flow>" + OS.NEWLINE +
" </fo:page-sequence>" + OS.NEWLINE +
"</fo:root>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.putString(result);
content.put(ClipBoardManager.XML, result);
return content;
}

/**
* Inserts each citation into a HTML body and copies it to the clipboard
*/
Expand Down Expand Up @@ -197,9 +157,7 @@ private void setClipBoardContent(List<String> citations) {
ClipboardContent content;
switch (outputFormat) {
case HTML -> content = processHtml(citations);
case RTF -> content = processRtf(citations);
case XSL_FO -> content = processXslFo(citations);
case ASCII_DOC, TEXT -> content = processText(citations);
case TEXT -> content = processText(citations);
default -> {
LOGGER.warn("unknown output format: '" + outputFormat + "', processing it via the default.");
content = processText(citations);
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ private static CSLItemData bibEntryToCSLItemData(BibEntry bibEntry, BibDatabase

// Not every field is already generated into latex free fields
RemoveNewlinesFormatter removeNewlinesFormatter = new RemoveNewlinesFormatter();

// adapted from TexBibEntriesResolver
bibDatabase.getReferencedEntry(bibEntry).ifPresent(refEntry ->
refEntry.getFields().forEach(field -> bibEntry.getFieldMap().putIfAbsent(field, refEntry.getFieldOrAlias(field).orElse(""))));

for (Field key : bibEntry.getFieldMap().keySet()) {
bibEntry.getResolvedFieldOrAlias(key, bibDatabase)
.map(removeNewlinesFormatter::format)
Expand All @@ -112,9 +117,6 @@ private static CSLItemData bibEntryToCSLItemData(BibEntry bibEntry, BibDatabase
}
bibTeXEntry.addField(new Key(key.getName()), new DigitStringValue(value));

if(StandardField.CROSSREF.equals(key)) {

}
});
}
return BIBTEX_CONVERTER.toItemData(bibTeXEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

public enum CitationStyleOutputFormat {

ASCII_DOC("asciidoc", ""),
HTML("html", OS.NEWLINE + "<br>" + OS.NEWLINE),
RTF("rtf", "\\line" + OS.NEWLINE),
TEXT("text", ""),
XSL_FO("fo", OS.NEWLINE);
TEXT("text", "");

private final String format;
private final String lineSeparator;
Expand Down
88 changes: 0 additions & 88 deletions src/test/java/org/jabref/gui/preview/CopyCitationActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import javafx.scene.input.ClipboardContent;

import org.jabref.gui.ClipBoardManager;
import org.jabref.logic.util.OS;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -109,93 +108,6 @@ void processText() throws Exception {
assertEquals(expected, actual);
}

@Test
void processRtf() throws Exception {
String expected = "{\\rtf" + OS.NEWLINE +
"[1]\\tab B. Smith, B. Jones, and J. Williams, \\uc0\\u8220{}Title of the test entry,\\uc0\\u8221{} {\\i{}BibTeX Journal}, vol. 34, no. 3, pp. 45\\uc0\\u8211{}67, Jul. 2016." + OS.NEWLINE +
"\\line" + OS.NEWLINE +
"[1]\\tab B. Smith, B. Jones, and J. Williams, \\uc0\\u8220{}Title of the test entry,\\uc0\\u8221{} {\\i{}BibTeX Journal}, vol. 34, no. 3, pp. 45\\uc0\\u8211{}67, Jul. 2016." + OS.NEWLINE +
"}";

String citation = "[1]\\tab B. Smith, B. Jones, and J. Williams, \\uc0\\u8220{}Title of the test entry,\\uc0\\u8221{} {\\i{}BibTeX Journal}, vol. 34, no. 3, pp. 45\\uc0\\u8211{}67, Jul. 2016." + OS.NEWLINE;
ClipboardContent content = CopyCitationAction.processRtf(Arrays.asList(citation, citation));

Object actual = content.getRtf();
assertEquals(expected, actual);
}

@Test
void processXslFo() throws Exception {
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + OS.NEWLINE +
"<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">" + OS.NEWLINE +
" <fo:layout-master-set>" + OS.NEWLINE +
" <fo:simple-page-master master-name=\"citations\">" + OS.NEWLINE +
" <fo:region-body/>" + OS.NEWLINE +
" </fo:simple-page-master>" + OS.NEWLINE +
" </fo:layout-master-set>" + OS.NEWLINE +
" <fo:page-sequence master-reference=\"citations\">" + OS.NEWLINE +
" <fo:flow flow-name=\"xsl-region-body\">" + OS.NEWLINE +
OS.NEWLINE +
"<fo:block id=\"Smith2016\">" + OS.NEWLINE +
" <fo:table table-layout=\"fixed\" width=\"100%\">" + OS.NEWLINE +
" <fo:table-column column-number=\"1\" column-width=\"2.5em\"/>" + OS.NEWLINE +
" <fo:table-column column-number=\"2\" column-width=\"proportional-column-width(1)\"/>" + OS.NEWLINE +
" <fo:table-body>" + OS.NEWLINE +
" <fo:table-row>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>[1]</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <fo:inline font-style=\"italic\">BibTeX Journal</fo:inline>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" </fo:table-row>" + OS.NEWLINE +
" </fo:table-body>" + OS.NEWLINE +
" </fo:table>" + OS.NEWLINE +
"</fo:block>" + OS.NEWLINE +
OS.NEWLINE +
"<fo:block id=\"Smith2016\">" + OS.NEWLINE +
" <fo:table table-layout=\"fixed\" width=\"100%\">" + OS.NEWLINE +
" <fo:table-column column-number=\"1\" column-width=\"2.5em\"/>" + OS.NEWLINE +
" <fo:table-column column-number=\"2\" column-width=\"proportional-column-width(1)\"/>" + OS.NEWLINE +
" <fo:table-body>" + OS.NEWLINE +
" <fo:table-row>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>[1]</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <fo:inline font-style=\"italic\">BibTeX Journal</fo:inline>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" </fo:table-row>" + OS.NEWLINE +
" </fo:table-body>" + OS.NEWLINE +
" </fo:table>" + OS.NEWLINE +
"</fo:block>" + OS.NEWLINE +
OS.NEWLINE +
" </fo:flow>" + OS.NEWLINE +
" </fo:page-sequence>" + OS.NEWLINE +
"</fo:root>" + OS.NEWLINE;

String citation = "<fo:block id=\"Smith2016\">" + OS.NEWLINE +
" <fo:table table-layout=\"fixed\" width=\"100%\">" + OS.NEWLINE +
" <fo:table-column column-number=\"1\" column-width=\"2.5em\"/>" + OS.NEWLINE +
" <fo:table-column column-number=\"2\" column-width=\"proportional-column-width(1)\"/>" + OS.NEWLINE +
" <fo:table-body>" + OS.NEWLINE +
" <fo:table-row>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>[1]</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" <fo:table-cell>" + OS.NEWLINE +
" <fo:block>B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <fo:inline font-style=\"italic\">BibTeX Journal</fo:inline>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</fo:block>" + OS.NEWLINE +
" </fo:table-cell>" + OS.NEWLINE +
" </fo:table-row>" + OS.NEWLINE +
" </fo:table-body>" + OS.NEWLINE +
" </fo:table>" + OS.NEWLINE +
"</fo:block>" + OS.NEWLINE;

ClipboardContent xmlTransferable = CopyCitationAction.processXslFo(Arrays.asList(citation, citation));

Object actual = xmlTransferable.get(ClipBoardManager.XML);
assertEquals(expected, actual);
}

@Test
void processHtmlAsHtml() throws Exception {
Expand Down
Loading

0 comments on commit acbeafd

Please sign in to comment.