Skip to content

Commit

Permalink
Fix % sign cleanup (#2521)
Browse files Browse the repository at this point in the history
* Fix repeated escaping of % sign
Extracted Regexe in Patterns
Fix for #2451

* Fix stackoverflow on regex
  • Loading branch information
Siedlerchr authored Feb 5, 2017
1 parent 020f1c4 commit a52d657
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fix import of journal title in ris format. [#2506](https://github.com/JabRef/jabref/issues/2506)
- We fixed the export of the `number` field in MS-Office XML export. [#2509](https://github.com/JabRef/jabref/issues/2509)
- The field `issue` is now always exported to the corresponding `issue` field in MS-Office XML.
- We fixed an issue with repeated escaping of the %-sign when running the LaTeXCleanup more than once. [#2451](https://github.com/JabRef/jabref/issues/2451)
### Removed


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package net.sf.jabref.logic.formatter.bibtexfields;

import java.util.regex.Pattern;

import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.cleanup.Formatter;

public class LatexCleanupFormatter implements Formatter {

private static final Pattern REMOVE_REDUNDANT = Pattern
.compile("(?<!\\\\[\\p{Alpha}]{0,100}\\{[^\\}]{0,100})\\}([-/ ]?)\\{");

private static final Pattern REPLACE_WITH_AT = Pattern.compile("(^|[^\\\\$])\\$");
private static final Pattern REPLACE_EVERY_OTHER_AT = Pattern.compile("([^@]*)@@([^@]*)@@");
private static final Pattern MOVE_NUMBERS_WITH_OPERATORS = Pattern.compile("([0-9\\(\\.]+[ ]?[-+/]?[ ]?)\\$");
private static final Pattern MOVE_NUMBERS_RIGHT_INTO_EQUATION = Pattern.compile("@@([ ]?[-+/]?[ ]?[0-9\\)\\.]+)");
private static final Pattern ESCAPE_PERCENT_SIGN_ONCE = Pattern.compile("(^|[^\\\\%])%");

@Override
public String getName() {
return Localization.lang("LaTeX cleanup");
Expand All @@ -20,19 +31,25 @@ public String format(String oldString) {
String newValue = oldString;

// Remove redundant $, {, and }, but not if the } is part of a command argument: \mbox{-}{GPS} should not be adjusted
newValue = newValue.replace("$$", "").replaceAll("(?<!\\\\[\\p{Alpha}]{0,100}\\{[^\\}]{0,100})\\}([-/ ]?)\\{",
"$1");
newValue = newValue.replace("$$", "");
newValue = REMOVE_REDUNDANT.matcher(newValue).replaceAll("$1");

// Move numbers, +, -, /, and brackets into equations
newValue = newValue.replaceAll("(([^$]|\\\\\\$)*)\\$", "$1@@"); // Replace $, but not \$ with @@
newValue = newValue.replaceAll("([^@]*)@@([^@]*)@@", "$1\\$$2@@"); // Replace every other @@ with $
newValue = REPLACE_WITH_AT.matcher(newValue).replaceAll("$1@@"); // Replace $, but not \$ with @@

newValue = REPLACE_EVERY_OTHER_AT.matcher(newValue).replaceAll("$1\\$$2@@"); // Replace every other @@ with $
//newValue = newValue.replaceAll("([0-9\\(\\.]+) \\$","\\$$1\\\\ "); // Move numbers followed by a space left of $ inside the equation, e.g., 0.35 $\mu$m
newValue = newValue.replaceAll("([0-9\\(\\.]+[ ]?[-+/]?[ ]?)\\$", "\\$$1"); // Move numbers, possibly with operators +, -, or /, left of $ into the equation
newValue = newValue.replaceAll("@@([ ]?[-+/]?[ ]?[0-9\\)\\.]+)", " $1@@"); // Move numbers right of @@ into the equation

newValue = MOVE_NUMBERS_WITH_OPERATORS.matcher(newValue).replaceAll("\\$$1"); // Move numbers, possibly with operators +, -, or /, left of $ into the equation
newValue = MOVE_NUMBERS_RIGHT_INTO_EQUATION.matcher(newValue).replaceAll(" $1@@"); // Move numbers right of @@ into the equation

newValue = newValue.replace("@@", "$"); // Replace all @@ with $
newValue = newValue.replace(" ", " "); // Clean up
newValue = newValue.replace("$$", "");
newValue = newValue.replace(" )$", ")$");
newValue = newValue.replace("%", "\\%"); // escape % used for comments in TeX

newValue = ESCAPE_PERCENT_SIGN_ONCE.matcher(newValue).replaceAll("$1\\\\%"); // escape %, but do not escapee \% again, used for comments in TeX

return newValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@ public void preservePercentSign() {
assertEquals("\\%", formatter.format("%"));
}

@Test
public void escapePercentSignOnlyOnce() {
assertEquals("\\%", formatter.format("\\%"));
}

@Test
public void escapePercentSignOnlnyOnceWithNumber() {
assertEquals("50\\%", formatter.format("50\\%"));
}

@Test
public void formatExample() {
assertEquals("{VLSI DSP}", formatter.format(formatter.getExampleInput()));
}


}

0 comments on commit a52d657

Please sign in to comment.