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

Change latex command handling only for XML Chars #4379

Merged
merged 8 commits into from
Mar 27, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- We changed the latex command removal for docbook exporter. [#3838](https://github.com/JabRef/jabref/issues/3838)
- We changed the location of some fields in the entry editor (you might need to reset your preferences for these changes to come into effect)
- Journal/Year/Month in biblatex mode -> Deprecated (if filled)
- DOI/URL: General -> Optional
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/jabref/logic/layout/format/XMLChars.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public String format(String fieldText) {
return fieldText;
}

String formattedFieldText = firstFormat(fieldText);
String latexCommandFree = removeLatexCommands(fieldText);
String formattedFieldText = firstFormat(latexCommandFree);

for (Map.Entry<String, String> entry : XML_CHARS.entrySet()) {
String s = entry.getKey();
Expand All @@ -42,6 +43,11 @@ public String format(String fieldText) {
return restFormat(formattedFieldText);
}

private String removeLatexCommands(String fieldText) {
LatexToUnicodeFormatter latexToUnicode = new LatexToUnicodeFormatter();
return latexToUnicode.format(fieldText);
}

private static String firstFormat(String s) {
return s.replaceAll("&|\\\\&", "&#x0026;").replace("--", "&#x2013;");
}
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/jabref/logic/exporter/DocbookExporterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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.Arrays;
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.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 DocbookExporterTest {

public BibDatabaseContext databaseContext = new BibDatabaseContext();;
public Charset charset = StandardCharsets.UTF_8;

private Exporter exportFormat;

@BeforeEach
public 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);

exportFormat = exporterFactory.getExporterByName("docbook4").get();
}

@Test
public void testCorruptedTitleBraces(@TempDir Path testFolder) throws Exception {
Path tmpFile = testFolder.resolve("testBraces");

BibEntry entry = new BibEntry();
entry.setField("title", "Peptidomics of the larval {\\protect{{D}rosophila melanogaster}} central nervous system.");

List<BibEntry> entries = Arrays.asList(entry);

exportFormat.export(databaseContext, tmpFile, charset, entries);

List<String> lines = Files.readAllLines(tmpFile);
assertEquals(20, lines.size());
assertEquals(" <citetitle pubwork=\"article\">Peptidomics of the larval Drosophila melanogaster central nervous system.</citetitle>", lines.get(9));
}

@Test
public void testCorruptedTitleUnicode(@TempDir Path testFolder) throws Exception {
Path tmpFile = testFolder.resolve("testBraces");

BibEntry entry = new BibEntry();
entry.setField("title", "Insect neuropeptide bursicon homodimers induce innate immune and stress genes during molting by activating the {NF}-$\\kappa$B transcription factor Relish.");

List<BibEntry> entries = Arrays.asList(entry);

exportFormat.export(databaseContext, tmpFile, charset, entries);

List<String> lines = Files.readAllLines(tmpFile);
assertEquals(20, lines.size());
assertEquals(" <citetitle pubwork=\"article\">Insect neuropeptide bursicon homodimers induce innate immune and stress genes during molting by activating the NF&#45;&#954;B transcription factor Relish.</citetitle>", lines.get(9));
}

}