Skip to content

Commit

Permalink
Export no empty lines in RIS format (#3661)
Browse files Browse the repository at this point in the history
* Do no longer export empty lines in RIS format

* Fix typos

* Extract and explain regex

* Turn regex static final
  • Loading branch information
lenhard authored and Siedlerchr committed Jan 24, 2018
1 parent cf8e6aa commit ca6bc18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We improved the export of the `address` and `location` field to the MS-Office XML fields. If the address field does not contain a comma, it is treated as single value and exported to the field `city`. [#1750, comment](https://github.com/JabRef/jabref/issues/1750#issuecomment-357539167)
For more details refer to the [field mapping help page](http://help.jabref.org/en/MsOfficeBibFieldMapping)
- We added Facebook and Twitter icons in the toolbar to link to our [Facebook](https://www.facebook.com/JabRef/) and [Twitter](https://twitter.com/jabref_org) pages.
- We no longer print empty lines when exporting an entry in RIS format [#3634](https://github.com/JabRef/jabref/issues/3634)

### Fixed
- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static ExporterFactory create(Map<String, TemplateExporter> customFormats
exporters.add(new TemplateExporter("iso690txt", "iso690", "iso690txt", FileType.ISO_690_TXT, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("endnote", "EndNote", "endnote", FileType.ENDNOTE_TXT, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("oocsv", "openoffice-csv", "openoffice", FileType.OO_LO, layoutPreferences, savePreferences));
exporters.add(new TemplateExporter("ris", "ris", "ris", FileType.RIS, layoutPreferences, savePreferences).withEncoding(StandardCharsets.UTF_8));
exporters.add(new TemplateExporter("ris", "ris", "ris", FileType.RIS, layoutPreferences, savePreferences, true).withEncoding(StandardCharsets.UTF_8));
exporters.add(new TemplateExporter("misq", "misq", "misq", FileType.MIS_QUARTERLY, layoutPreferences, savePreferences));
exporters.add(new BibTeXMLExporter());
exporters.add(new OpenOfficeDocumentCreator());
Expand Down
37 changes: 36 additions & 1 deletion src/main/java/org/jabref/logic/exporter/TemplateExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import org.jabref.JabRefMain;
import org.jabref.logic.layout.Layout;
Expand All @@ -33,13 +34,23 @@ 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 Logger LOGGER = LoggerFactory.getLogger(TemplateExporter.class);

private final String lfFileName;
private final String directory;
private final LayoutFormatterPreferences layoutPreferences;
private final SavePreferences savePreferences;
private Charset encoding; // If this value is set, it will be used to override the default encoding for the getCurrentBasePanel.
private boolean customExport;
private boolean deleteBlankLines;

/**
* Initialize another export format based on templates stored in dir with
Expand Down Expand Up @@ -93,6 +104,25 @@ public TemplateExporter(String consoleName, String lfFileName, String directory,
this(extension.getDescription(), consoleName, lfFileName, directory, extension, layoutPreferences, savePreferences);
}

/**
* Initialize another export format based on templates stored in dir with
* layoutFile lfFilename.
* The display name is automatically derived from the FileType
*
*
* @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)
*/
public TemplateExporter(String consoleName, String lfFileName, String directory, FileType extension, LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences, boolean deleteBlankLines) {
this(extension.getDescription(), consoleName, lfFileName, directory, extension, layoutPreferences, 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
Expand Down Expand Up @@ -257,7 +287,12 @@ public void export(final BibDatabaseContext databaseContext, final Path file,

// Write the entry
if (layout != null) {
ps.write(layout.doLayout(entry, databaseContext.getDatabase()));
if (deleteBlankLines) {
String withoutBlankLines = BLANK_LINE_MATCHER.matcher(layout.doLayout(entry, databaseContext.getDatabase())).replaceAll("");
ps.write(withoutBlankLines);
} else {
ps.write(layout.doLayout(entry, databaseContext.getDatabase()));
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/jabref/logic/layout/Layout.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,21 @@ public void setPostFormatter(LayoutFormatter formatter) {
* recursive string references are resolved.
*/
public String doLayout(BibEntry bibtex, BibDatabase database) {
StringBuilder sb = new StringBuilder(100);
StringBuilder builder = new StringBuilder(100);

for (LayoutEntry layoutEntry : layoutEntries) {
String fieldText = layoutEntry.doLayout(bibtex, database);

// 2005.05.05 M. Alver
// The following change means we treat null fields as "". This is to fix the
// problem of whitespace disappearing after missing fields. Hoping there are
// no side effects.
// problem of whitespace disappearing after missing fields.
if (fieldText == null) {
fieldText = "";
}

sb.append(fieldText);
builder.append(fieldText);
}

return sb.toString();
return builder.toString();
}

/**
Expand Down

0 comments on commit ca6bc18

Please sign in to comment.