Skip to content

Commit a44cd07

Browse files
tushar-2320tushar-pantokopporcalixtus
authored
Add Copy markdown to copy citation (#13387)
* fix:Added copy Markdown to copy citation #12552. * fix:Added copy Markdown to copy citation #12552. * fix:Added copy Markdown to copy citation #12552. * fix:Added @VisibleForTesting in method processMarkdown #12552. * fix:submodule removal * fix:Added @VisibleForTesting in method processMarkdown #12552. * Revert submodule changes to match main * Reset submodule pointers to match main branch * refactor:removed unecessary debugging statements and whitespace stripping. * refactor:used List.of instead of Array.asList * Add CHANGELOG.md entry --------- Co-authored-by: Tushar <[email protected]> Co-authored-by: Oliver Kopp <[email protected]> Co-authored-by: Carl Christian Snethlage <[email protected]>
1 parent f8544c8 commit a44cd07

File tree

7 files changed

+54
-2
lines changed

7 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
2121
- We added the field `monthfiled` to the default list of fields to resolve BibTeX-Strings for [#13375](https://github.com/JabRef/jabref/issues/13375)
2222
- We added a new ID based fetcher for [EuropePMC](https://europepmc.org/). [#13389](https://github.com/JabRef/jabref/pull/13389)
2323
- We added an initial [cite as you write](https://retorque.re/zotero-better-bibtex/citing/cayw/) endpoint. [#13187](https://github.com/JabRef/jabref/issues/13187)
24+
- We added "copy preview as markdown" feature. [#12552](https://github.com/JabRef/jabref/issues/12552)
2425
- In case no citation relation information can be fetched, we show the data providers reason. [#13549](https://github.com/JabRef/jabref/pull/13549)
2526

2627
### Changed

jabgui/src/main/java/org/jabref/gui/actions/StandardActions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public enum StandardActions implements Action {
1919
COPY_KEY_AND_LINK(Localization.lang("Copy citation key and link"), KeyBinding.COPY_CITATION_KEY_AND_LINK),
2020
COPY_CITATION_HTML(Localization.lang("Copy citation (html)"), KeyBinding.COPY_PREVIEW),
2121
COPY_CITATION_TEXT(Localization.lang("Copy citation (text)")),
22+
COPY_CITATION_MARKDOWN(Localization.lang("Copy citation (markdown)")),
2223
COPY_CITATION_PREVIEW(Localization.lang("Copy preview"), KeyBinding.COPY_PREVIEW),
2324
EXPORT_TO_CLIPBOARD(Localization.lang("Export to clipboard"), IconTheme.JabRefIcons.EXPORT_TO_CLIPBOARD),
2425
EXPORT_SELECTED_TO_CLIPBOARD(Localization.lang("Export selected entries to clipboard"), IconTheme.JabRefIcons.EXPORT_TO_CLIPBOARD),

jabgui/src/main/java/org/jabref/gui/maintable/RightClickMenu.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ private static Menu createCopySubMenu(ActionFactory factory,
192192
if (previewPreferences.getSelectedPreviewLayout() instanceof CitationStylePreviewLayout) {
193193
copySpecialMenu.getItems().addAll(
194194
factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)),
195-
factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)));
195+
factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)),
196+
factory.createMenuItem(StandardActions.COPY_CITATION_MARKDOWN, new CopyCitationAction(CitationStyleOutputFormat.MARKDOWN, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)));
196197
} else {
197198
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, taskExecutor, preferences, abbreviationRepository)));
198199
}

jabgui/src/main/java/org/jabref/gui/preview/ClipboardContentGenerator.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.airhacks.afterburner.injection.Injector;
2525
import com.google.common.annotations.VisibleForTesting;
26+
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
2627

2728
public class ClipboardContentGenerator {
2829

@@ -45,6 +46,7 @@ public ClipboardContent generate(List<BibEntry> selectedEntries, CitationStyleOu
4546
return switch (outputFormat) {
4647
case HTML -> processHtml(citations);
4748
case TEXT -> processText(citations);
49+
case MARKDOWN -> processMarkdown(citations);
4850
};
4951
} else {
5052
// if it is not a citation style take care of the preview
@@ -120,6 +122,32 @@ static ClipboardContent processHtml(List<String> citations) {
120122
return content;
121123
}
122124

125+
/**
126+
* Insert each citation into HTML.
127+
* convert HTML to markdown using flexmark.
128+
*/
129+
@VisibleForTesting
130+
static ClipboardContent processMarkdown(List<String> citations) {
131+
String result = "<!DOCTYPE html>" + OS.NEWLINE +
132+
"<html>" + OS.NEWLINE +
133+
" <head>" + OS.NEWLINE +
134+
" <meta charset=\"utf-8\">" + OS.NEWLINE +
135+
" </head>" + OS.NEWLINE +
136+
" <body>" + OS.NEWLINE + OS.NEWLINE;
137+
138+
result += String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations);
139+
result += OS.NEWLINE +
140+
" </body>" + OS.NEWLINE +
141+
"</html>" + OS.NEWLINE;
142+
143+
FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
144+
String markdown = converter.convert(result);
145+
146+
ClipboardContent content = new ClipboardContent();
147+
content.putString(markdown);
148+
return content;
149+
}
150+
123151
private List<String> generateTextBasedPreviewLayoutCitations(List<BibEntry> selectedEntries, BibDatabaseContext bibDatabaseContext) throws IOException {
124152
TextBasedPreviewLayout customPreviewLayout = previewPreferences.getCustomPreviewLayout();
125153
Reader customLayoutReader = Reader.of(customPreviewLayout.getText().replace("__NEWLINE__", "\n"));

jabgui/src/test/java/org/jabref/gui/preview/ClipboardContentGeneratorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,22 @@ void processHtmlAsHtml() {
138138
Object actual = htmlTransferable.getHtml();
139139
assertEquals(expected, actual);
140140
}
141+
142+
@Test
143+
void processMarkdownAsMarkdown() {
144+
String expected = "\\[1\\] \n" +
145+
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n" +
146+
"\n" +
147+
"\\[1\\] \n" +
148+
"B. Smith, B. Jones, and J. Williams, \"Title of the test entry,\" *BibTeX Journal*, vol. 34, no. 3, pp. 45--67, Jul. 2016.\n";
149+
150+
String citation = " <div class=\"csl-entry\">" + OS.NEWLINE +
151+
" <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 +
152+
" </div>" + OS.NEWLINE;
153+
154+
ClipboardContent markdown = ClipboardContentGenerator.processMarkdown(List.of(citation, citation));
155+
String actual = markdown.getString();
156+
assertEquals(expected, actual);
157+
}
141158
}
159+

jablib/src/main/java/org/jabref/logic/citationstyle/CitationStyleOutputFormat.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
public enum CitationStyleOutputFormat {
66

77
HTML("html", OS.NEWLINE + "<br>" + OS.NEWLINE),
8-
TEXT("text", "");
8+
TEXT("text", ""),
9+
MARKDOWN("markdown", "");
910

1011
private final String format;
1112
private final String lineSeparator;

jablib/src/main/resources/l10n/JabRef_en.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Copy=Copy
171171
Copy\ title=Copy title
172172
Copy\ citation\ (html)=Copy citation (html)
173173
Copy\ citation\ (text)=Copy citation (text)
174+
Copy\ citation\ (markdown)=Copy citation (markdown)
175+
174176
Copy\ citation\ key=Copy citation key
175177
Copy\ citation\ key\ and\ link=Copy citation key and link
176178
Copy\ citation\ key\ and\ title=Copy citation key and title

0 commit comments

Comments
 (0)