Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown Export Formatter #12220

Merged
merged 9 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -11,6 +11,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Added

- We added a Markdown export functionality [#12220](https://github.com/JabRef/jabref/pull/12220)
LinusDietz marked this conversation as resolved.
Show resolved Hide resolved
- We added a "view as BibTeX" option before importing an entry from the citation relation tab. [#11826](https://github.com/JabRef/jabref/issues/11826)
- We added support finding LaTeX-encoded special characters based on plain Unicode and vice versa. [#11542](https://github.com/JabRef/jabref/pull/11542)
- When a search hits a file, the file icon of that entry is changed accordingly. [#11542](https://github.com/JabRef/jabref/pull/11542)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/logic/exporter/ExporterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.metadata.SaveOrder.SortCriterion;
import org.jabref.model.metadata.SelfContainedSaveOrder;

public class ExporterFactory {
Expand All @@ -27,6 +30,7 @@ public static ExporterFactory create(CliPreferences preferences) {
List<TemplateExporter> customFormats = preferences.getExportPreferences().getCustomExporters();
LayoutFormatterPreferences layoutPreferences = preferences.getLayoutFormatterPreferences();
SelfContainedSaveOrder saveOrder = SelfContainedSaveOrder.of(preferences.getSelfContainedExportConfiguration().getSaveOrder());
SelfContainedSaveOrder saveMostRecentFirst = new SelfContainedSaveOrder(SaveOrder.OrderType.SPECIFIED, List.of(new SortCriterion(StandardField.YEAR, true)));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
XmpPreferences xmpPreferences = preferences.getXmpPreferences();
FieldPreferences fieldPreferences = preferences.getFieldPreferences();
BibDatabaseMode bibDatabaseMode = preferences.getLibraryPreferences().getDefaultBibDatabaseMode();
Expand All @@ -43,6 +47,7 @@ public static ExporterFactory create(CliPreferences preferences) {
exporters.add(new TemplateExporter(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs", StandardFileType.HTML, layoutPreferences, saveOrder));
exporters.add(new TemplateExporter(Localization.lang("HTML list"), "listrefs", "listrefs", "listrefs", StandardFileType.HTML, layoutPreferences, saveOrder));
exporters.add(new TemplateExporter(Localization.lang("HTML table (with Abstract & BibTeX)"), "tablerefsabsbib", "tablerefsabsbib", "tablerefsabsbib", StandardFileType.HTML, layoutPreferences, saveOrder));
exporters.add(new TemplateExporter(Localization.lang("Markdown titles"), "title-md", "title-md", "title-markdown", StandardFileType.MARKDOWN, layoutPreferences, saveMostRecentFirst));
exporters.add(new TemplateExporter("Harvard RTF", "harvard", "harvard", "harvard", StandardFileType.RTF, layoutPreferences, saveOrder));
exporters.add(new TemplateExporter("ISO 690 RTF", "iso690rtf", "iso690RTF", "iso690rtf", StandardFileType.RTF, layoutPreferences, saveOrder));
exporters.add(new TemplateExporter("ISO 690", "iso690txt", "iso690", "iso690txt", StandardFileType.TXT, layoutPreferences, saveOrder));
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ The\ marked\ area\ does\ not\ contain\ any\ legible\ text!=The marked area does

HTML\ table=HTML table
HTML\ table\ (with\ Abstract\ &\ BibTeX)=HTML table (with Abstract & BibTeX)
Markdown\ titles=Markdown titles

Icon=Icon

Ignore=Ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* \format[RemoveLatexCommands,HTMLChars]{\title}. \begin{journal}\format[RemoveLatexCommands,HTMLChars]{\journal}\end{journal}\begin{year} \format{\year}\end{year}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* \format[RemoveLatexCommands,HTMLChars]{\title}.\begin{publisher} \format[RemoveLatexCommands,HTMLChars]{\publisher}\end{publisher} \format{\year}\end{year}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* \format[RemoveLatexCommands,HTMLChars]{\title}. \begin{booktitle}\format[RemoveLatexCommands,HTMLChars]{\booktitle}\end{booktitle}\begin{publisher}, \format[RemoveLatexCommands,HTMLChars]{\publisher}\end{publisher} \format{\year}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* \format[RemoveLatexCommands,HTMLChars]{\title}. \begin{publisher}\format[RemoveLatexCommands,HTMLChars]{\publisher} \end{publisher}\begin{series}\format[RemoveLatexCommands,HTMLChars]{\series}\end{series}\begin{!series}\format[RemoveLatexCommands,HTMLChars]{\booktitle} \format{\year}\end{!series}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* \format[RemoveLatexCommands,HTMLChars]{\title}.\begin{year} \year\end{year}
190 changes: 190 additions & 0 deletions src/test/java/org/jabref/logic/exporter/MarkdownTitleExporterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package org.jabref.logic.exporter;

import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.metadata.SaveOrder;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Answers;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

class MarkdownTitleExporterTest {

private static Exporter htmlWebsiteExporter;
private static BibDatabaseContext databaseContext;

@BeforeAll
static void setUp() {
htmlWebsiteExporter = new TemplateExporter(
"Title-Markdown",
"title-md",
"title-md",
"title-markdown",
StandardFileType.MARKDOWN,
mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS),
SaveOrder.getDefaultSaveOrder(),
BlankLineBehaviour.DELETE_BLANKS);

databaseContext = new BibDatabaseContext();
}

@Test
final void exportForNoEntriesWritesNothing(@TempDir Path tempFile) throws Exception {
Path file = tempFile.resolve("ThisIsARandomlyNamedFile");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, tempFile, Collections.emptyList());
assertEquals(Collections.emptyList(), Files.readAllLines(file));
}

@Test
final void exportsCorrectContentArticle(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.Article)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.JOURNAL, "Journal of this \\& that")
.withField(StandardField.PUBLISHER, "THE PRESS")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. Journal of this &amp; that 2020");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentInCollection(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.InCollection)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.BOOKTITLE, "Test book")
.withField(StandardField.PUBLISHER, "PRESS")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. Test book, PRESS 2020");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentBook(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.Book)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.BOOKTITLE, "Test book")
.withField(StandardField.PUBLISHER, "PRESS")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. PRESS 2020");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentInProceeedingsPublisher(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.InProceedings)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.BOOKTITLE, "Test Conference")
.withField(StandardField.PUBLISHER, "ACM")
.withField(StandardField.SERIES, "CONF'20")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. ACM CONF'20");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentInProceeedingsNoPublisher(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.InProceedings)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.BOOKTITLE, "Test Conference")
.withField(StandardField.SERIES, "CONF'20")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. CONF'20");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentInProceeedingsNoSeries(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.InProceedings)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "Test Title")
.withField(StandardField.BOOKTITLE, "Test Conference")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* Test Title. Test Conference 2020");

assertEquals(expected, Files.readAllLines(file));
}

@Test
final void exportsCorrectContentBracketsInTitle(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.Article)
.withCitationKey("test")
.withField(StandardField.AUTHOR, "Test Author")
.withField(StandardField.TITLE, "This is {JabRef}")
.withField(StandardField.JOURNAL, "Journal of this \\& that")
.withField(StandardField.YEAR, "2020");

Path file = tempFile.resolve("RandomFileName");
Files.createFile(file);
htmlWebsiteExporter.export(databaseContext, file, Collections.singletonList(entry));

List<String> expected = List.of(
"* This is JabRef. Journal of this &amp; that 2020");

assertEquals(expected, Files.readAllLines(file));
}
}
Loading