diff --git a/jabgui/src/main/java/org/jabref/gui/preview/ClipboardContentGenerator.java b/jabgui/src/main/java/org/jabref/gui/preview/ClipboardContentGenerator.java index ce5a61f5def..b3c2ddb774b 100644 --- a/jabgui/src/main/java/org/jabref/gui/preview/ClipboardContentGenerator.java +++ b/jabgui/src/main/java/org/jabref/gui/preview/ClipboardContentGenerator.java @@ -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; @@ -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 selectedEntries, CitationStyleOutputFormat outputFormat, BibDatabaseContext bibDatabaseContext) throws IOException { List citations = generateCitations(selectedEntries, outputFormat, bibDatabaseContext); @@ -77,7 +68,7 @@ private List generateCitations(List 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 @@ -100,7 +91,7 @@ static ClipboardContent processText(List 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 citations) { @@ -123,25 +114,22 @@ static ClipboardContent processHtml(List 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. + *

+ * 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 citations) { - String result = "" + OS.NEWLINE + - "" + OS.NEWLINE + - " " + OS.NEWLINE + - " " + OS.NEWLINE + - " " + OS.NEWLINE + - " " + OS.NEWLINE + OS.NEWLINE; - - result += String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations); - result += OS.NEWLINE + - " " + OS.NEWLINE + - "" + 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); diff --git a/jabgui/src/test/java/org/jabref/gui/preview/ClipboardContentGeneratorTest.java b/jabgui/src/test/java/org/jabref/gui/preview/ClipboardContentGeneratorTest.java index 4b3c5bee02e..6f506f84349 100644 --- a/jabgui/src/test/java/org/jabref/gui/preview/ClipboardContentGeneratorTest.java +++ b/jabgui/src/test/java/org/jabref/gui/preview/ClipboardContentGeneratorTest.java @@ -19,16 +19,14 @@ void processPreviewText() { "Smith, B.; Jones, B. & 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 + "
" + OS.NEWLINE + "Article (Smith2016)" + OS.NEWLINE + "Smith, B.; Jones, B. & 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 + @@ -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 = "

" + OS.NEWLINE + - "
[1]
B. Smith, B. Jones, and J. Williams, “Title of the test entry,” BibTeX Journal, vol. 34, no. 3, pp. 45–67, Jul. 2016.
" + OS.NEWLINE + - "
" + 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); } } diff --git a/jablib/src/test/java/org/jabref/logic/citationstyle/CitationStyleGeneratorTest.java b/jablib/src/test/java/org/jabref/logic/citationstyle/CitationStyleGeneratorTest.java index 132fba7f605..bf69d25016e 100644 --- a/jablib/src/test/java/org/jabref/logic/citationstyle/CitationStyleGeneratorTest.java +++ b/jablib/src/test/java/org/jabref/logic/citationstyle/CitationStyleGeneratorTest.java @@ -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(); @@ -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 = "
\n" + - "
[1]
F. Last and J. Doe,
\n" + - "
\n"; + String expected = """ +
+
[1]
F. Last and J. Doe,
+
+ """; String citation = CitationStyleGenerator.generateBibliography(List.of(entry), DEFAULT_STYLE, ENTRY_TYPES_MANAGER); assertEquals(expected, citation); } @@ -168,9 +171,11 @@ void missingCitationStyle() { @Test void htmlFormat() { - String expectedCitation = "
\n" + - "
[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.
\n" + - "
\n"; + 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.
+
+ """; String actualCitation = CitationStyleGenerator.generateBibliography(List.of(testEntry), DEFAULT_STYLE, HTML_OUTPUT_FORMAT, testEntryContext, ENTRY_TYPES_MANAGER).getFirst(); assertEquals(expectedCitation, actualCitation); @@ -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\\.
\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(); @@ -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 = "
\n" + - "
[1]
F. Läst and J. Doe,
\n" + - "
\n"; + String expected = """ +
+
[1]
F. Läst and J. Doe,
+
+ """; String citation = CitationStyleGenerator.generateBibliography(List.of(entry), DEFAULT_STYLE, ENTRY_TYPES_MANAGER); assertEquals(expected, citation); }