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

Support for exporting to YAML format #7007

Merged
merged 16 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a query parser and mapping layer to enable conversion of queries formulated in simplified lucene syntax by the user into api queries. [#6799](https://github.com/JabRef/jabref/pull/6799)
- We added some basic functionality to customise the look of JabRef by importing a css theme file. [#5790](https://github.com/JabRef/jabref/issues/5790)
- We added connection check function in network preference setting [#6560](https://github.com/JabRef/jabref/issues/6560)
- We added support for exporting to YAML. [#6974](https://github.com/JabRef/jabref/issues/6974)

### Changed

Expand Down Expand Up @@ -48,6 +49,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where it was impossible to connect to OpenOffice/LibreOffice on Mac OSX. [#6970](https://github.com/JabRef/jabref/pull/6970)
- We fixed an issue with the python script used by browser plugins that failed to locate JabRef if not installed in its default location. [#6963](https://github.com/JabRef/jabref/pull/6963/files)
- We fixed an issue where identity column header had incorrect foreground color in the Dark theme. [#6796](https://github.com/JabRef/jabref/issues/6796)
- We fixed an issue where the RIS exporter added extra blank lines.[#7007](https://github.com/JabRef/jabref/pull/7007/files)

calixtus marked this conversation as resolved.
Show resolved Hide resolved
### Removed

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/exporter/ExporterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public static ExporterFactory create(List<TemplateExporter> customFormats,
exporters.add(new TemplateExporter("ISO 690", "iso690txt", "iso690", "iso690txt", StandardFileType.TXT, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("Endnote", "endnote", "EndNote", "endnote", StandardFileType.TXT, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("OpenOffice/LibreOffice CSV", "oocsv", "openoffice-csv", "openoffice", StandardFileType.CSV, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("RIS", "ris", "ris", "ris", StandardFileType.RIS, layoutPreferences, savePreferences).withEncoding(StandardCharsets.UTF_8));
exporters.add(new TemplateExporter("RIS", "ris", "ris", "ris", StandardFileType.RIS, layoutPreferences, savePreferences, true).withEncoding(StandardCharsets.UTF_8));
exporters.add(new TemplateExporter("MIS Quarterly", "misq", "misq", "misq", StandardFileType.RTF, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("CSL YAML", "yaml", "yaml", null, StandardFileType.YAML, layoutPreferences, savePreferences, true));
exporters.add(new BibTeXMLExporter());
exporters.add(new OpenOfficeDocumentCreator());
exporters.add(new OpenDocumentSpreadsheetCreator());
Expand Down
63 changes: 28 additions & 35 deletions src/main/java/org/jabref/logic/exporter/TemplateExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutFormatterPreferences;
Expand All @@ -34,14 +33,6 @@
public class TemplateExporter extends Exporter {

private static final String LAYOUT_PREFIX = "/resource/layout/";

/**
* A regular expression that matches blank lines
*
* ?m activates "multimode", which makes ^ match line starts/ends.
* \\s simply marks any whitespace character
*/
private static final Pattern BLANK_LINE_MATCHER = Pattern.compile("(?m)^\\s");
private static final String LAYOUT_EXTENSION = ".layout";
private static final String FORMATTERS_EXTENSION = ".formatters";
private static final String BEGIN_INFIX = ".begin";
Expand All @@ -58,8 +49,7 @@ public class TemplateExporter extends Exporter {
private boolean deleteBlankLines;

/**
* Initialize another export format based on templates stored in dir with
* layoutFile lfFilename.
* Initialize another export format based on templates stored in dir with layoutFile lfFilename.
*
* @param displayName Name to display to the user.
* @param consoleName Name to call this format in the console.
Expand All @@ -72,8 +62,7 @@ public TemplateExporter(String displayName, String consoleName, String lfFileNam
}

/**
* Initialize another export format based on templates stored in dir with
* layoutFile lfFilename.
* Initialize another export format based on templates stored in dir with layoutFile lfFilename.
*
* @param name to display to the user and to call this format in the console.
* @param lfFileName Name of the main layout file.
Expand All @@ -87,8 +76,7 @@ public TemplateExporter(String name, String lfFileName, String extension, Layout
}

/**
* Initialize another export format based on templates stored in dir with
* layoutFile lfFilename.
* Initialize another export format based on templates stored in dir with layoutFile lfFilename.
*
* @param displayName Name to display to the user.
* @param consoleName Name to call this format in the console.
Expand All @@ -112,27 +100,33 @@ public TemplateExporter(String displayName, String consoleName, String lfFileNam
}

/**
* Initialize another export format based on templates stored in dir with
* layoutFile lfFilename.
* The display name is automatically derived from the FileType
* Initialize another export format based on templates stored in dir with layoutFile lfFilename.
*
* @param displayName Name to display to the user.
* @param consoleName Name to call this format in the console.
* @param lfFileName Name of the main layout file.
* @param directory Directory in which to find the layout file.
* @param extension Should contain the . (for instance .txt).
* @param layoutPreferences Preferences for layout
* @param savePreferences Preferences for saving
* @param deleteBlankLines If blank lines should be remove (default: false)
* @param deleteBlankLines if blank lines should be removed (default: false)
joethei marked this conversation as resolved.
Show resolved Hide resolved
*/
public TemplateExporter(String consoleName, String lfFileName, String directory, StandardFileType extension, LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences, boolean deleteBlankLines) {
this(consoleName, consoleName, lfFileName, directory, extension, layoutPreferences, savePreferences);
public TemplateExporter(String displayName, String consoleName, String lfFileName, String directory, FileType extension,
LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences, boolean deleteBlankLines) {
calixtus marked this conversation as resolved.
Show resolved Hide resolved
super(consoleName, displayName, extension);
if (Objects.requireNonNull(lfFileName).endsWith(LAYOUT_EXTENSION)) {
this.lfFileName = lfFileName.substring(0, lfFileName.length() - LAYOUT_EXTENSION.length());
} else {
this.lfFileName = lfFileName;
}
this.directory = directory;
this.layoutPreferences = layoutPreferences;
this.savePreferences = savePreferences;
this.deleteBlankLines = deleteBlankLines;
}

/**
* Indicate whether this is a custom export. A custom export looks for its
* layout files using a normal file path, while a built-in export looks in
* the classpath.
* Indicate whether this is a custom export. A custom export looks for its layout files using a normal file path, while a built-in export looks in the classpath.
calixtus marked this conversation as resolved.
Show resolved Hide resolved
*
* @param custom true to indicate a custom export format.
*/
Expand All @@ -141,8 +135,7 @@ public void setCustomExport(boolean custom) {
}

/**
* Set an encoding which will be used in preference to the default value
* obtained from the basepanel.
* Set an encoding which will be used in preference to the default value obtained from the basepanel.
*
* @param encoding The name of the encoding to use.
*/
Expand All @@ -152,11 +145,9 @@ public TemplateExporter withEncoding(Charset encoding) {
}

/**
* This method should return a reader from which the given layout file can
* be read.
* This method should return a reader from which the given layout file can be read.
* <p>
* Subclasses of TemplateExporter are free to override and provide their own
* implementation.
* Subclasses of TemplateExporter are free to override and provide their own implementation.
*
* @param filename the filename
* @return a newly created reader
Expand Down Expand Up @@ -278,8 +269,12 @@ public void export(final BibDatabaseContext databaseContext, final Path file,
// Write the entry
if (layout != null) {
if (deleteBlankLines) {
String withoutBlankLines = BLANK_LINE_MATCHER.matcher(layout.doLayout(entry, databaseContext.getDatabase())).replaceAll("");
ps.write(withoutBlankLines);
String[] lines = layout.doLayout(entry, databaseContext.getDatabase()).split("\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check: Does this work on Windows and Linux equally well? Mayybe OS.NEW_LINE should be used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative implementation could have been to update the regular expression. I am really not sure why it stopped working.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly enough when using OS.NEW_LINE on Windows its breaks.
need to do an test on linux though, currently having problems running my vm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should check both? We don't know which line endings the user has in the template files (if modified)

for (String line : lines) {
if (!line.isBlank() && !line.isEmpty()) {
ps.write(line);
}
}
} else {
ps.write(layout.doLayout(entry, databaseContext.getDatabase()));
}
Expand Down Expand Up @@ -316,9 +311,7 @@ public void export(final BibDatabaseContext databaseContext, final Path file,
}

/**
* See if there is a name formatter file bundled with this export format. If so, read
* all the name formatters so they can be used by the filter layouts.
*
* See if there is a name formatter file bundled with this export format. If so, read all the name formatters so they can be used by the filter layouts.
*/
private void readFormatterFile() {
File formatterFile = new File(lfFileName + FORMATTERS_EXTENSION);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/util/StandardFileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum StandardFileType implements FileType {
JSON("json"),
XMP("xmp"),
ZIP("zip"),
CSS("css");
CSS("css"),
YAML("yaml");

private final List<String> extensions;

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/resource/layout/yaml.begin.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
references:
1 change: 1 addition & 0 deletions src/main/resources/resource/layout/yaml.end.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
14 changes: 14 additions & 0 deletions src/main/resources/resource/layout/yaml.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- id: \citationkey
\begin{type} type: \type\end{type}
\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}
koppor marked this conversation as resolved.
Show resolved Hide resolved
\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}
79 changes: 79 additions & 0 deletions src/test/java/org/jabref/logic/exporter/YamlExporterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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.BeforeEach;
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 {

public Charset charset;
private Exporter yamlExporter;
private BibDatabaseContext databaseContext;

@BeforeEach
public void setUp() throws Exception {
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 exportCorrectContent(@TempDir Path tempFile) throws Exception {
BibEntry entry = new BibEntry(StandardEntryType.Article);
entry.withCitationKey("test");
entry.withField(StandardField.AUTHOR, "Test Author");
entry.withField(StandardField.TITLE, "Test Title");
entry.withField(StandardField.URL, "http://example.com");
entry.withField(StandardField.YEAR, "2020");
joethei marked this conversation as resolved.
Show resolved Hide resolved

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

List<String> lines = new ArrayList<>();
lines.add("---");
lines.add("references:");
lines.add("- id: test");
lines.add(" author:");
lines.add(" - literal: \"Test Author\"");
lines.add(" title: \"Test Title\"");
lines.add(" issued: 2020");
lines.add(" url: http://example.com");
lines.add("---");
joethei marked this conversation as resolved.
Show resolved Hide resolved

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