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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javafx.scene.input.ClipboardContent;

import org.jabref.logic.bst.BstPreviewLayout;
import org.jabref.logic.citationstyle.CitationStyleGenerator;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
Expand All @@ -23,21 +24,11 @@

import com.airhacks.afterburner.injection.Injector;
import com.google.common.annotations.VisibleForTesting;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;

public class ClipboardContentGenerator {

private PreviewPreferences previewPreferences;
private final LayoutFormatterPreferences layoutFormatterPreferences;
private final JournalAbbreviationRepository abbreviationRepository;

public ClipboardContentGenerator(PreviewPreferences previewPreferences,
LayoutFormatterPreferences layoutFormatterPreferences,
JournalAbbreviationRepository abbreviationRepository) {
this.previewPreferences = previewPreferences;
this.layoutFormatterPreferences = layoutFormatterPreferences;
this.abbreviationRepository = abbreviationRepository;
}
public record ClipboardContentGenerator(
PreviewPreferences previewPreferences,
LayoutFormatterPreferences layoutFormatterPreferences,
JournalAbbreviationRepository abbreviationRepository) {

public ClipboardContent generate(List<BibEntry> selectedEntries, CitationStyleOutputFormat outputFormat, BibDatabaseContext bibDatabaseContext) throws IOException {
List<String> citations = generateCitations(selectedEntries, outputFormat, bibDatabaseContext);
Expand Down Expand Up @@ -77,7 +68,7 @@ private List<String> generateCitations(List<BibEntry> selectedEntries, CitationS
}

/**
* Generates a plain text string out of the preview (based on {@link org.jabref.logic.layout.TextBasedPreviewLayout} or {@link org.jabref.logic.bst.BstPreviewLayout})
* Generates a plain text string out of the preview (based on {@link TextBasedPreviewLayout} or {@link BstPreviewLayout})
* and copies it additionally to the html to the clipboard (WYSIWYG Editors use the HTML, plain text editors the text)
*/
@VisibleForTesting
Expand All @@ -100,7 +91,7 @@ static ClipboardContent processText(List<String> citations) {

/**
* Inserts each citation into a HTML body and copies it to the clipboard.
* The given preview is based on {@link org.jabref.logic.citationstyle.CitationStylePreviewLayout}.
* The given preview is based on {@link CitationStylePreviewLayout}.
*/
@VisibleForTesting
static ClipboardContent processHtml(List<String> citations) {
Expand All @@ -123,25 +114,22 @@ static ClipboardContent processHtml(List<String> citations) {
}

/**
* Insert each citation into HTML.
* convert HTML to markdown using flexmark.
*/
* Joins each citation using the platform-specific newline into a single Markdown string (from citeproc) and copies it to the clipboard.
* <p>
* A trailing newline is appended if missing to keep the behavior consistent with other output formats
* and to satisfy tests expecting a newline-terminated string.
*
* @param citations the list of already-formatted citation strings to be combined as Markdown
* @return clipboard content containing the Markdown representation in its plain string flavor
*/
@VisibleForTesting
static ClipboardContent processMarkdown(List<String> citations) {
String result = "<!DOCTYPE html>" + OS.NEWLINE +
"<html>" + OS.NEWLINE +
" <head>" + OS.NEWLINE +
" <meta charset=\"utf-8\">" + OS.NEWLINE +
" </head>" + OS.NEWLINE +
" <body>" + OS.NEWLINE + OS.NEWLINE;

result += String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations);
result += OS.NEWLINE +
" </body>" + OS.NEWLINE +
"</html>" + OS.NEWLINE;
String markdown = String.join(OS.NEWLINE, citations);

FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
String markdown = converter.convert(result);
// Ensure trailing newline at end for consistency with other output formats/tests
if (!markdown.endsWith(OS.NEWLINE)) {
markdown = markdown + OS.NEWLINE;
}

ClipboardContent content = new ClipboardContent();
content.putString(markdown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ void processPreviewText() {
"Smith, B.; Jones, B. &amp; Williams, J." + OS.NEWLINE +
"Taylor, P. (Ed.)" + OS.NEWLINE +
"Title of the test entry " + OS.NEWLINE +
"BibTeX Journal, JabRef Publishing, 2016, 34, 45-67 " + OS.NEWLINE +
"" + OS.NEWLINE +
"BibTeX Journal, JabRef Publishing, 2016, 34, 45-67 " + OS.NEWLINE + OS.NEWLINE +
"Abstract: This entry describes a test scenario which may be useful in JabRef. By providing a test entry it is possible to see how certain things will look in this graphical BIB-file mananger. " + OS.NEWLINE +
"<br>" + OS.NEWLINE +
"Article (Smith2016)" + OS.NEWLINE +
"Smith, B.; Jones, B. &amp; Williams, J." + OS.NEWLINE +
"Taylor, P. (Ed.)" + OS.NEWLINE +
"Title of the test entry " + OS.NEWLINE +
"BibTeX Journal, JabRef Publishing, 2016, 34, 45-67 " + OS.NEWLINE +
"" + OS.NEWLINE +
"BibTeX Journal, JabRef Publishing, 2016, 34, 45-67 " + OS.NEWLINE + OS.NEWLINE +
"Abstract: This entry describes a test scenario which may be useful in JabRef. By providing a test entry it is possible to see how certain things will look in this graphical BIB-file mananger. ";

String citation = "Article (Smith2016)" + OS.NEWLINE +
Expand Down Expand Up @@ -140,19 +138,15 @@ void processHtmlAsHtml() {
}

@Test
void processMarkdownAsMarkdown() {
String expected = "\\[1\\] \n" +
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n" +
"\n" +
"\\[1\\] \n" +
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n";
void processCslMarkdownAsMarkdown() {
String citationMd = "[1] " + OS.NEWLINE +
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.";

String citation = " <div class=\"csl-entry\">" + OS.NEWLINE +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <i>BibTeX Journal</i>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</div>" + OS.NEWLINE +
" </div>" + OS.NEWLINE;
String expected = citationMd + OS.NEWLINE + citationMd + OS.NEWLINE;

ClipboardContent markdown = ClipboardContentGenerator.processMarkdown(List.of(citation, citation));
ClipboardContent markdown = ClipboardContentGenerator.processMarkdown(List.of(citationMd, citationMd));
String actual = markdown.getString();

assertEquals(expected, actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CitationStyleGeneratorTest {
private static final String DEFAULT_STYLE = CSLStyleLoader.getDefaultStyle().getSource();
private static final CitationStyleOutputFormat HTML_OUTPUT_FORMAT = CitationStyleOutputFormat.HTML;
private static final CitationStyleOutputFormat TEXT_OUTPUT_FORMAT = CitationStyleOutputFormat.TEXT;
private static final CitationStyleOutputFormat MARKDOWN_OUTPUT_FORMAT = CitationStyleOutputFormat.MARKDOWN;
private static final BibEntryTypesManager ENTRY_TYPES_MANAGER = new BibEntryTypesManager();

private final BibEntry testEntry = TestEntry.getTestEntry();
Expand Down Expand Up @@ -150,9 +151,11 @@ void ignoreCarriageReturnNewLine() {
entry.setField(StandardField.AUTHOR, "Last, First and\r\nDoe, Jane");

// if the default citation style changes this has to be modified
String expected = " <div class=\"csl-entry\">\n" +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">F. Last and J. Doe, </div>\n" +
" </div>\n";
String expected = """
<div class="csl-entry">
<div class="csl-left-margin">[1]</div><div class="csl-right-inline">F. Last and J. Doe, </div>
</div>
""";
String citation = CitationStyleGenerator.generateBibliography(List.of(entry), DEFAULT_STYLE, ENTRY_TYPES_MANAGER);
assertEquals(expected, citation);
}
Expand All @@ -168,9 +171,11 @@ void missingCitationStyle() {

@Test
void htmlFormat() {
String expectedCitation = " <div class=\"csl-entry\">\n" +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">B. Smith, B. Jones, and J. Williams, &ldquo;Title of the test entry,&rdquo; <span style=\"font-style: italic\">BibTeX Journal</span>, vol. 34, no. 3, pp. 45&ndash;67, July 2016, doi: 10.1001/bla.blubb.</div>\n" +
" </div>\n";
String expectedCitation = """
<div class="csl-entry">
<div class="csl-left-margin">[1]</div><div class="csl-right-inline">B. Smith, B. Jones, and J. Williams, &ldquo;Title of the test entry,&rdquo; <span style="font-style: italic">BibTeX Journal</span>, vol. 34, no. 3, pp. 45&ndash;67, July 2016, doi: 10.1001/bla.blubb.</div>
</div>
""";

String actualCitation = CitationStyleGenerator.generateBibliography(List.of(testEntry), DEFAULT_STYLE, HTML_OUTPUT_FORMAT, testEntryContext, ENTRY_TYPES_MANAGER).getFirst();
assertEquals(expectedCitation, actualCitation);
Expand All @@ -184,6 +189,14 @@ void textFormat() {
assertEquals(expectedCitation, actualCitation);
}

@Test
void markdownFormat() {
String expectedCitation = "\\[1\\]B\\. Smith\\, B\\. Jones\\, and J\\. Williams\\, “Title of the test entry\\,” *BibTeX Journal*\\, vol\\. 34\\, no\\. 3\\, pp\\. 45–67\\, July 2016\\, doi\\: 10\\.1001\\/bla\\.blubb\\.<br />\n";

String actualCitation = CitationStyleGenerator.generateBibliography(List.of(testEntry), DEFAULT_STYLE, MARKDOWN_OUTPUT_FORMAT, testEntryContext, ENTRY_TYPES_MANAGER).getFirst();
assertEquals(expectedCitation, actualCitation);
}

@Test
void handleDiacritics() {
BibEntry entry = new BibEntry();
Expand All @@ -192,9 +205,11 @@ void handleDiacritics() {

// if the default citation style changes this has to be modified.
// in this case ä was added to check if it is formatted appropriately
String expected = " <div class=\"csl-entry\">\n" +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">F. L&auml;st and J. Doe, </div>\n" +
" </div>\n";
String expected = """
<div class="csl-entry">
<div class="csl-left-margin">[1]</div><div class="csl-right-inline">F. L&auml;st and J. Doe, </div>
</div>
""";
String citation = CitationStyleGenerator.generateBibliography(List.of(entry), DEFAULT_STYLE, ENTRY_TYPES_MANAGER);
assertEquals(expected, citation);
}
Expand Down
Loading