-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a6f191
commit 03bbc8f
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/net/sf/jabref/logic/formatter/bibtexfields/RemoveLatexFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.sf.jabref.logic.formatter.bibtexfields; | ||
|
||
import net.sf.jabref.logic.formatter.Formatter; | ||
import net.sf.jabref.logic.l10n.Localization; | ||
|
||
public class RemoveLatexFormatter implements Formatter { | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("LaTeX remover"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "remove_latex"; | ||
} | ||
|
||
@Override | ||
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"); | ||
// Move numbers, +, -, /, and brackets into equations | ||
newValue = newValue.replaceAll("(([^$]|\\\\\\$)*)\\$", "$1@@"); // Replace $, but not \$ with @@ | ||
newValue = 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 = newValue.replace("@@", "$"); // Replace all @@ with $ | ||
newValue = newValue.replace(" ", " "); // Clean up | ||
newValue = newValue.replace("$$", ""); | ||
newValue = newValue.replace(" )$", ")$"); | ||
return newValue; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Removes LaTeX code."); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "\textit{Nucleus} {API}"; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/net/sf/jabref/logic/formatter/bibtexfields/RemoveLatexFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.sf.jabref.logic.formatter.bibtexfields; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Tests in addition to the general tests from {@link net.sf.jabref.logic.formatter.FormatterTest} | ||
*/ | ||
public class RemoveLatexFormatterTest { | ||
|
||
private final RemoveLatexFormatter formatter = new RemoveLatexFormatter(); | ||
|
||
@Test | ||
public void test() { | ||
assertEquals("$\\alpha\\beta$", formatter.format("$\\alpha$$\\beta$")); | ||
} | ||
|
||
@Test | ||
public void formatExample() { | ||
assertEquals("Nucleus API", formatter.format(formatter.getExampleInput())); | ||
} | ||
|
||
|
||
} |
03bbc8f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code looks similar to net.sf.jabref.logic.formatter.bibtexfields.LatexCleanupFormatter. I think, it is just a code start.
I wonder whether your desired functionality is achieved by net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter (old name:
FormatChars
)?Or is this code here obsolete? If not, could you add some comments on the relation to the other formatters? 😇