Skip to content

Commit

Permalink
Remove some UnicodeToLatex uses (JabRef#2132)
Browse files Browse the repository at this point in the history
* remove some UnicodeToLatex uses

* readd removeBracesFormatter
  • Loading branch information
chriba authored and zesaro committed Oct 27, 2016
1 parent e1410ae commit 48ba0fe
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import net.sf.jabref.logic.bibtex.comparator.EntryComparator;
import net.sf.jabref.logic.bibtex.comparator.FieldComparator;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
Expand Down Expand Up @@ -613,9 +612,7 @@ public Object getColumnValue(BibEntry entry, int column) {
}
else {
String field = FIELDS[column - PAD];

String fieldContent = entry.getField(field).orElse("");
fieldContent = new LatexToUnicodeFormatter().format(fieldContent);
String fieldContent = entry.getLatexFreeField(field).orElse("");

if (InternalBibtexFields.getFieldProperties(field).contains(FieldProperty.PERSON_NAMES)) {
// For name fields, tap into a MainTableFormat instance and use
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/net/sf/jabref/logic/importer/fetcher/CrossRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Optional;

import net.sf.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.logic.util.DOI;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
Expand All @@ -28,6 +27,7 @@
*/
public class CrossRef {
private static final Log LOGGER = LogFactory.getLog(CrossRef.class);
private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();

private static final String API_URL = "http://api.crossref.org";
private static final Levenshtein METRIC_DISTANCE = new Levenshtein();
Expand Down Expand Up @@ -85,8 +85,7 @@ private static String enhanceQuery(String query, BibEntry entry) {
}

private static boolean checkValidity(BibEntry entry, JSONArray result) {
// TODO: use latex-free version instead in the future
final String entryTitle = entry.getField(FieldName.TITLE).map(CrossRef::removeLaTeX).orElse("");
final String entryTitle = REMOVE_BRACES_FORMATTER.format(entry.getLatexFreeField(FieldName.TITLE).orElse(""));

// currently only title-based
// title: [ "How the Mind Hurts and Heals the Body." ]
Expand Down Expand Up @@ -114,16 +113,6 @@ private static boolean checkValidity(BibEntry entry, JSONArray result) {
}
}

private static String removeLaTeX(String text) {
String result;
// remove braces
result = new RemoveBracesFormatter().format(text);
// convert to unicode
result = new LatexToUnicodeFormatter().format(result);

return result;
}

private static double editDistanceIgnoreCase(String a, String b) {
// TODO: locale is dependent on the language of the strings?!
return METRIC_DISTANCE.distance(a.toLowerCase(Locale.ENGLISH), b.toLowerCase(Locale.ENGLISH));
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/net/sf/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Locale;

import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

Expand All @@ -24,7 +23,7 @@ public static MSBibEntry convert(BibEntry entry) {

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

if (MSBibMapping.getMSBibField(field) != null) {
result.fields.put(MSBibMapping.getMSBibField(field), unicodeField);
Expand Down Expand Up @@ -104,29 +103,24 @@ public static MSBibEntry convert(BibEntry entry) {
result.publicationTitle = entry.getField(FieldName.TITLE).orElse(null);
}

entry.getField(FieldName.AUTHOR).ifPresent(authors -> result.authors = getAuthors(authors));
entry.getField(FieldName.EDITOR).ifPresent(editors -> result.editors = getAuthors(editors));
entry.getLatexFreeField(FieldName.AUTHOR).ifPresent(authors -> result.authors = getAuthors(authors));
entry.getLatexFreeField(FieldName.EDITOR).ifPresent(editors -> result.editors = getAuthors(editors));

return result;
}

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

String cleanAuthors = removeLaTeX(authors);

if (cleanAuthors.toUpperCase(Locale.ENGLISH).contains(" AND ")) {
String[] names = cleanAuthors.split(" (?i)and ");
if (authors.toUpperCase(Locale.ENGLISH).contains(" AND ")) {
String[] names = authors.split(" (?i)and ");
for (String name : names) {
result.add(new PersonName(name));
}
} else {
result.add(new PersonName(cleanAuthors));
result.add(new PersonName(authors));
}
return result;
}

private static String removeLaTeX(String text) {
return new LatexToUnicodeFormatter().format(text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import java.util.List;

import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.strings.LatexToUnicode;

/**
* Search rule for contain-based search.
*/
public class ContainBasedSearchRule implements SearchRule {

private static final LatexToUnicode LATEX_TO_UNICODE_FORMATTER = new LatexToUnicode();

private final boolean caseSensitive;

public ContainBasedSearchRule(boolean caseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import java.util.regex.PatternSyntaxException;

import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.strings.LatexToUnicode;

/**
* Search rule for regex-based search.
*/
public class RegexBasedSearchRule implements SearchRule {

private static final LatexToUnicode LATEX_TO_UNICODE_FORMATTER = new LatexToUnicode();

private final boolean caseSensitive;

public RegexBasedSearchRule(boolean caseSensitive) {
Expand Down

0 comments on commit 48ba0fe

Please sign in to comment.