Skip to content

Commit

Permalink
Replaced regular expressions with plain strings in HTML/Unicode conve…
Browse files Browse the repository at this point in the history
…rsion map and fixed #405
  • Loading branch information
oscargus committed Jun 2, 2016
1 parent af16d0b commit 973a39d
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 417 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 [#405](https://github.com/JabRef/jabref/issues/405): Added more {} around capital letters in Unicode/HTML to LaTeX conversion to preserve them

### Removed
- Removed possibility to export entries/databases to an `.sql` file, as the logic cannot easily use the correct escape logic
Expand Down
20 changes: 10 additions & 10 deletions src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
@State(Scope.Thread)
public class Benchmarks {

String bibtexString;
BibDatabase database = new BibDatabase();
List<String> latexConversionStrings = new ArrayList<>();
List<String> htmlConversionStrings = new ArrayList<>();
private String bibtexString;
private final BibDatabase database = new BibDatabase();
private final List<String> latexConversionStrings = new ArrayList<>();
private final List<String> htmlConversionStrings = new ArrayList<>();

@Setup
public void init() throws IOException, SaveException {
Expand Down Expand Up @@ -70,21 +70,21 @@ public void init() throws IOException, SaveException {
int symbolcount = latexSymbols.size();
StringBuilder sb = new StringBuilder();
sb.append("{A} \\textbf{bold} ");
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append(" {\\it italic} {");
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append(latexSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append("} abc");
latexConversionStrings.add(sb.toString());

List<String> htmlSymbols = new ArrayList<>(HTMLUnicodeConversionMaps.HTML_LATEX_CONVERSION_MAP.keySet());
symbolcount = htmlSymbols.size();
sb = new StringBuilder();
sb.append("A <b>bold</b> ");
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append(" <it>italic</it> ");
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt()) % symbolcount));
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append(htmlSymbols.get(Math.abs(randomizer.nextInt() % symbolcount)));
sb.append("&#8211; abc");
htmlConversionStrings.add(sb.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public String format(String text) {
// Handle text based HTML entities
Set<String> patterns = HTMLUnicodeConversionMaps.HTML_LATEX_CONVERSION_MAP.keySet();
for (String pattern : patterns) {
result = result.replaceAll(pattern, HTMLUnicodeConversionMaps.HTML_LATEX_CONVERSION_MAP.get(pattern));
result = result.replace(pattern, HTMLUnicodeConversionMaps.HTML_LATEX_CONVERSION_MAP.get(pattern));
}

// Handle numerical HTML entities
Matcher m = ESCAPED_PATTERN.matcher(result);
while (m.find()) {
int num = Integer.decode(m.group(1).replace("x", "#") + m.group(3));
if (HTMLUnicodeConversionMaps.NUMERICAL_LATEX_CONVERSION_MAP.containsKey(num)) {
result = result.replaceAll("&#" + m.group(1) + m.group(2) + m.group(3) + ";",
result = result.replace("&#" + m.group(1) + m.group(2) + m.group(3) + ";",
HTMLUnicodeConversionMaps.NUMERICAL_LATEX_CONVERSION_MAP.get(num));
}
}
Expand All @@ -100,14 +100,14 @@ public String format(String text) {
int num = Integer.decode(m.group(2).replace("x", "#") + m.group(4));
if (HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.containsKey(num)) {
if ("i".equals(m.group(1))) {
result = result.replaceAll(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";",
"\\{\\\\" + HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "\\{\\\\i\\}\\}");
result = result.replace(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";",
"{\\" + HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "{\\i}}");
} else if ("j".equals(m.group(1))) {
result = result.replaceAll(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";",
"\\{\\\\" + HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "\\{\\\\j\\}\\}");
result = result.replace(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";",
"{\\" + HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "{\\j}}");
} else {
result = result.replaceAll(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";", "\\{\\\\"
+ HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "\\{" + m.group(1) + "\\}\\}");
result = result.replace(m.group(1) + "&#" + m.group(2) + m.group(3) + m.group(4) + ";",
"{\\" + HTMLUnicodeConversionMaps.ESCAPED_ACCENTS.get(num) + "{" + m.group(1) + "}}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String format(String text) {
// Standard symbols
Set<Character> chars = HTMLUnicodeConversionMaps.UNICODE_LATEX_CONVERSION_MAP.keySet();
for (Character character : chars) {
result = result.replaceAll(character.toString(),
result = result.replace(character.toString(),
HTMLUnicodeConversionMaps.UNICODE_LATEX_CONVERSION_MAP.get(character));
}

Expand Down
Loading

0 comments on commit 973a39d

Please sign in to comment.