-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for exporting to YAML format (#7007)
- Loading branch information
Showing
11 changed files
with
201 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/org/jabref/logic/exporter/BlankLineBehaviour.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
/** | ||
* This enum represents the behaviour for blank lines in {@link TemplateExporter} | ||
*/ | ||
public enum BlankLineBehaviour { | ||
KEEP_BLANKS, | ||
DELETE_BLANKS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.jabref.logic.layout.format; | ||
|
||
import org.jabref.logic.layout.LayoutFormatter; | ||
import org.jabref.model.entry.types.StandardEntryType; | ||
|
||
public class CSLType implements LayoutFormatter { | ||
|
||
@Override | ||
public String format(String value) { | ||
return switch (StandardEntryType.valueOf(value)) { | ||
case Article -> "article"; | ||
case Book -> "book"; | ||
case Conference -> "paper-conference"; | ||
case Report, TechReport -> "report"; | ||
case Thesis, MastersThesis, PhdThesis -> "thesis"; | ||
case WWW, Online -> "webpage"; | ||
|
||
default -> "no-type"; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
references: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
- id: \citationkey | ||
\begin{entrytype} type: \format[CSLType]{\entrytype}\end{entrytype} | ||
\begin{author} | ||
author: | ||
- literal: "\author" | ||
\end{author} | ||
\begin{title} title: "\title"\end{title} | ||
\begin{shorttitle} title-short: "\shorttitle"\end{shorttitle} | ||
\begin{date} issued: \date\end{date} | ||
\begin{url} url: \url\end{url} | ||
\begin{doi} doi: \doi\end{doi} | ||
\begin{volume} volume: \volume\end{volume} | ||
\begin{number} number: \number\end{number} | ||
\begin{urldate} accessed: \urldate\end{urldate} |
108 changes: 108 additions & 0 deletions
108
src/test/java/org/jabref/logic/exporter/YamlExporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.jabref.logic.layout.LayoutFormatterPreferences; | ||
import org.jabref.logic.xmp.XmpPreferences; | ||
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.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.mockito.Answers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class YamlExporterTest { | ||
|
||
private static Charset charset; | ||
private static Exporter yamlExporter; | ||
private static BibDatabaseContext databaseContext; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
List<TemplateExporter> customFormats = new ArrayList<>(); | ||
LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); | ||
SavePreferences savePreferences = mock(SavePreferences.class); | ||
XmpPreferences xmpPreferences = mock(XmpPreferences.class); | ||
ExporterFactory exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences, xmpPreferences); | ||
|
||
databaseContext = new BibDatabaseContext(); | ||
charset = StandardCharsets.UTF_8; | ||
yamlExporter = exporterFactory.getExporterByName("yaml").get(); | ||
} | ||
|
||
@Test | ||
public final void exportForNoEntriesWritesNothing(@TempDir Path tempFile) throws Exception { | ||
Path file = tempFile.resolve("ThisIsARandomlyNamedFile"); | ||
Files.createFile(file); | ||
yamlExporter.export(databaseContext, tempFile, charset, Collections.emptyList()); | ||
assertEquals(Collections.emptyList(), Files.readAllLines(file)); | ||
} | ||
|
||
@Test | ||
public final void exportsCorrectContent(@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.URL, "http://example.com") | ||
.withField(StandardField.DATE, "2020-10-14"); | ||
|
||
Path file = tempFile.resolve("RandomFileName"); | ||
Files.createFile(file); | ||
yamlExporter.export(databaseContext, file, charset, Collections.singletonList(entry)); | ||
|
||
List<String> expected = List.of( | ||
"---", | ||
"references:", | ||
"- id: test", | ||
" type: article", | ||
" author:", | ||
" - literal: \"Test Author\"", | ||
" title: \"Test Title\"", | ||
" issued: 2020-10-14", | ||
" url: http://example.com", | ||
"---"); | ||
|
||
assertEquals(expected, Files.readAllLines(file)); | ||
} | ||
|
||
@Test | ||
public final void formatsContentCorrect(@TempDir Path tempFile) throws Exception { | ||
BibEntry entry = new BibEntry(StandardEntryType.Misc) | ||
.withCitationKey("test") | ||
.withField(StandardField.AUTHOR, "Test Author") | ||
.withField(StandardField.TITLE, "Test Title") | ||
.withField(StandardField.URL, "http://example.com") | ||
.withField(StandardField.DATE, "2020-10-14"); | ||
|
||
Path file = tempFile.resolve("RandomFileName"); | ||
Files.createFile(file); | ||
yamlExporter.export(databaseContext, file, charset, Collections.singletonList(entry)); | ||
|
||
List<String> expected = List.of( | ||
"---", | ||
"references:", | ||
"- id: test", | ||
" type: no-type", | ||
" author:", | ||
" - literal: \"Test Author\"", | ||
" title: \"Test Title\"", | ||
" issued: 2020-10-14", | ||
" url: http://example.com", | ||
"---"); | ||
|
||
assertEquals(expected, Files.readAllLines(file)); | ||
} | ||
} |