Skip to content

Commit

Permalink
Remove umlauts from word export title #421 (#1465)
Browse files Browse the repository at this point in the history
* Split MsBib functionality into Converter classes
* Put all mappings inside a mapping class
* Fixes #421 Remove LaTeX commands from all BibTeX fields when exporting to Word Bibliography
  • Loading branch information
stefan-kolb committed Jun 3, 2016
1 parent 34e8c94 commit a6769dd
Show file tree
Hide file tree
Showing 28 changed files with 644 additions and 1,594 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1457](https://github.com/JabRef/jabref/issues/1457): Support multiple words inside LaTeX commands to RTF export
- Entries retain their groupmembership when undoing their cut/deletion
- Fixed [#1450](https://github.com/JabRef/jabref/issues/1450): EntryEditor is restored in the correct size after preference changes
- Fixed [#421](https://github.com/JabRef/jabref/issues/421): Remove LaTeX commands from all BibTeX fields when exporting to Word Bibliography

### Removed
- Removed possibility to export entries/databases to an `.sql` file, as the logic cannot easily use the correct escape logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String

try (VerifyingWriter ps = session.getWriter()) {
try {
DOMSource source = new DOMSource(msBibDatabase.getDOMrepresentation());
DOMSource source = new DOMSource(msBibDatabase.getDOM());
StreamResult result = new StreamResult(ps);
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public String format(String inField) {
return "";
}
int i;
// TODO: document what does this do
String field = inField.replaceAll("&|\\\\&", "&amp;").replaceAll("[\\n]{1,}", "<p>").replace("\\$", "&dollar;") // Replace \$ with &dollar;
.replaceAll("\\$([^\\$]*)\\$", "\\{$1\\}");

Expand Down
18 changes: 6 additions & 12 deletions src/main/java/net/sf/jabref/logic/mods/PageNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,24 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* @author Michael Wrighton
* @author S M Mahbub Murshed
*
*/
public class PageNumbers {

private String freeform;
private int start;
private int end;

private static final Pattern PAGE_PATTERN = Pattern.compile("\\s*(\\d+)\\s*-{1,2}\\s*(\\d+)\\s*");

public PageNumbers(String s) {
parsePageNums(s);
public PageNumbers(String pages) {
parsePageNums(pages);
}

private void parsePageNums(String numberString) {
Matcher matcher = PAGE_PATTERN.matcher(numberString);
private void parsePageNums(String pages) {
Matcher matcher = PAGE_PATTERN.matcher(pages);
if (matcher.matches()) {
start = Integer.parseInt(matcher.group(1));
end = Integer.parseInt(matcher.group(2));
} else {
freeform = numberString;
freeform = pages;
}
}

Expand Down Expand Up @@ -75,7 +69,7 @@ public String toString(String separator) {

@Override
public String toString() {
return toString("--");
return toString("-");
}

}
112 changes: 112 additions & 0 deletions src/main/java/net/sf/jabref/logic/msbib/BibTeXConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.sf.jabref.logic.msbib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import net.sf.jabref.importer.fileformat.ImportFormat;
import net.sf.jabref.logic.mods.PersonName;
import net.sf.jabref.model.entry.BibEntry;

public class BibTeXConverter {
private static final String MSBIB_PREFIX = "msbib-";

public static BibEntry convert(MSBibEntry entry) {
BibEntry result;
Map<String, String> fieldValues = new HashMap<>();

if (entry.getCiteKey() == null) {
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, MSBibMapping.getBibTeXEntryType(entry.getType()));
} else {
// TODO: the cite key should not be the ID?!
// id assumes an existing database so don't
result = new BibEntry(entry.getCiteKey(), MSBibMapping.getBibTeXEntryType(entry.getType()));
}

// add String fields
for (Map.Entry<String, String> field : entry.fields.entrySet()) {
String msField = field.getKey();
String value = field.getValue();

if (value != null && MSBibMapping.getBibTeXField(msField) != null) {
fieldValues.put(MSBibMapping.getBibTeXField(msField), value);
}
}

// Value must be converted
if (fieldValues.containsKey("language")) {
int lcid = Integer.valueOf(fieldValues.get("language"));
fieldValues.put("language", MSBibMapping.getLanguage(lcid));
}

addAuthor(fieldValues, "author", entry.authors);
addAuthor(fieldValues, MSBIB_PREFIX + "bookauthor", entry.bookAuthors);
addAuthor(fieldValues, "editor", entry.editors);
addAuthor(fieldValues, MSBIB_PREFIX + "translator", entry.translators);
addAuthor(fieldValues, MSBIB_PREFIX + "producername", entry.producerNames);
addAuthor(fieldValues, MSBIB_PREFIX + "composer", entry.composers);
addAuthor(fieldValues, MSBIB_PREFIX + "conductor", entry.conductors);
addAuthor(fieldValues, MSBIB_PREFIX + "performer", entry.performers);
addAuthor(fieldValues, MSBIB_PREFIX + "writer", entry.writers);
addAuthor(fieldValues, MSBIB_PREFIX + "director", entry.directors);
addAuthor(fieldValues, MSBIB_PREFIX + "compiler", entry.compilers);
addAuthor(fieldValues, MSBIB_PREFIX + "interviewer", entry.interviewers);
addAuthor(fieldValues, MSBIB_PREFIX + "interviewee", entry.interviewees);
addAuthor(fieldValues, MSBIB_PREFIX + "inventor", entry.inventors);
addAuthor(fieldValues, MSBIB_PREFIX + "counsel", entry.counsels);

if (entry.pages != null) {
fieldValues.put("pages", entry.pages.toString("--"));
}

parseStandardNumber(entry.standardNumber, fieldValues);

if (entry.address != null) {
fieldValues.put("address", entry.address);
}
// TODO: ConferenceName is saved as booktitle when converting from MSBIB to BibTeX
if (entry.conferenceName != null) {
fieldValues.put("organization", entry.conferenceName);
}

if (entry.dateAccessed != null) {
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed);
}

// set all fields
result.setField(fieldValues);

return result;
}

private static void addAuthor(Map<String, String> map, String type, List<PersonName> authors) {
if (authors == null) {
return;
}
String allAuthors = authors.stream().map(PersonName::getFullname).collect(Collectors.joining(" and "));

map.put(type, allAuthors);
}

private static void parseSingleStandardNumber(String type, String bibtype, String standardNum, Map<String, String> map) {
Pattern pattern = Pattern.compile(':' + type + ":(.[^:]+)");
Matcher matcher = pattern.matcher(standardNum);
if (matcher.matches()) {
map.put(bibtype, matcher.group(1));
}
}

private static void parseStandardNumber(String standardNum, Map<String, String> map) {
if (standardNum == null) {
return;
}
parseSingleStandardNumber("ISBN", "isbn", standardNum, map);
parseSingleStandardNumber("ISSN", "issn", standardNum, map);
parseSingleStandardNumber("LCCN", "lccn", standardNum, map);
parseSingleStandardNumber("MRN", "mrnumber", standardNum, map);
parseSingleStandardNumber("DOI", "doi", standardNum, map);
}
}
143 changes: 143 additions & 0 deletions src/main/java/net/sf/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package net.sf.jabref.logic.msbib;

import java.util.ArrayList;
import java.util.List;

import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.logic.layout.format.RemoveBrackets;
import net.sf.jabref.logic.mods.PageNumbers;
import net.sf.jabref.logic.mods.PersonName;
import net.sf.jabref.model.entry.BibEntry;

public class MSBibConverter {
private static final String MSBIB_PREFIX = "msbib-";
private static final String BIBTEX_PREFIX = "BIBTEX_";

public static MSBibEntry convert(BibEntry entry) {
MSBibEntry result = new MSBibEntry();

// memorize original type
result.fields.put(BIBTEX_PREFIX + "Entry", entry.getType());
// define new type
String msbibType = result.fields.put("SourceType", MSBibMapping.getMSBibEntryType(entry.getType()).name());

for (String field : entry.getFieldNames()) {
// clean field
String unicodeField = removeLaTeX(entry.getField(field));

if (MSBibMapping.getMSBibField(field) != null) {
result.fields.put(MSBibMapping.getMSBibField(field), unicodeField);
}
}

// Duplicate: also added as BookTitle
if (entry.hasField("booktitle")) {
result.conferenceName = entry.getField("booktitle");
}

if (entry.hasField("pages")) {
result.pages = new PageNumbers(entry.getField("pages"));
}

if (entry.hasField(MSBIB_PREFIX + "accessed")) {
result.dateAccessed = entry.getField(MSBIB_PREFIX + "accessed");
}

// TODO: currently this can never happen
if ("SoundRecording".equals(msbibType) && (entry.hasField("title"))) {
result.albumTitle = entry.getField("title");
}

// TODO: currently this can never happen
if ("Interview".equals(msbibType) && (entry.hasField("title"))) {
result.broadcastTitle = entry.getField("title");
}

// Value must be converted
if (entry.hasField("language")) {
result.fields.put("LCID", String.valueOf(MSBibMapping.getLCID(entry.getField("language"))));
}

result.standardNumber = "";
if (entry.hasField("isbn")) {
result.standardNumber += " ISBN: " + entry.getField("isbn");
}
if (entry.hasField("issn")) {
result.standardNumber += " ISSN: " + entry.getField("issn");
}
if (entry.hasField("lccn")) {
result.standardNumber += " LCCN: " + entry.getField("lccn");
}
if (entry.hasField("mrnumber")) {
result.standardNumber += " MRN: " + entry.getField("mrnumber");
}
if (entry.hasField("doi")) {
result.standardNumber += " DOI: " + entry.getField("doi");
}
if (result.standardNumber.isEmpty()) {
result.standardNumber = null;
}

if (entry.hasField("address")) {
result.address = entry.getField("address");
}

if (entry.hasField("type")) {
result.thesisType = entry.getField("type");
} else {
if ("techreport".equalsIgnoreCase(entry.getType())) {
result.thesisType = "Tech. rep.";
} else if ("mastersthesis".equalsIgnoreCase(entry.getType())) {
result.thesisType = "Master's thesis";
} else if ("phdthesis".equalsIgnoreCase(entry.getType())) {
result.thesisType = "Ph.D. dissertation";
} else if ("unpublished".equalsIgnoreCase(entry.getType())) {
result.thesisType = "unpublished";
}
}

// TODO: currently this can never happen
if (("InternetSite".equals(msbibType) || "DocumentFromInternetSite".equals(msbibType))
&& (entry.hasField("title"))) {
result.internetSiteTitle = entry.getField("title");
}

// TODO: currently only Misc can happen
if (("ElectronicSource".equals(msbibType) || "Art".equals(msbibType) || "Misc".equals(msbibType))
&& (entry.hasField("title"))) {
result.publicationTitle = entry.getField("title");
}

if (entry.hasField("author")) {
result.authors = getAuthors(entry.getField("author"));
}
if (entry.hasField("editor")) {
result.editors = getAuthors(entry.getField("editor"));
}

return result;
}

private static List<PersonName> getAuthors(String authors) {
List<PersonName> result = new ArrayList<>();

// TODO: case-insensitive?!
if (authors.contains(" and ")) {
String[] names = authors.split(" and ");
for (String name : names) {
result.add(new PersonName(name));
}
} else {
result.add(new PersonName(authors));
}
return result;
}

private static String removeLaTeX(String text) {
// TODO: just use latex free version everywhere in the future
String result = new RemoveBrackets().format(text);
result = new LatexToUnicodeFormatter().format(result);

return result;
}
}
Loading

0 comments on commit a6769dd

Please sign in to comment.