Last active
December 14, 2015 23:29
-
-
Save chris-martin/5165717 to your computer and use it in GitHub Desktop.
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
import java.util.List; | |
import com.google.common.collect.ImmutableList; | |
import com.google.common.collect.Lists; | |
class LatexEscaper { | |
private class Replacement { | |
final String toReplace; | |
final String replaceWith; | |
Replacement(final String toReplace, final String replaceWith) { | |
this.toReplace = toReplace; | |
this.replaceWith = replaceWith; | |
} | |
String applyTo(final String input) { | |
return input.replace(toReplace, replaceWith); | |
} | |
} | |
private final List<Replacement> texReplacements = Lists.newArrayList(); | |
{ | |
texReplacements.add(new Replacement("\\", "\\textbackslash ")); | |
for (final String x : ImmutableList.of("_", "^", "~", "$", "%", "#", "&", "{", "}")) { | |
texReplacements.add(new Replacement(x, "\\" + x)); | |
} | |
} | |
public String escape(final String input) { | |
String escaped = input; | |
for (final Replacement replacement : texReplacements) { | |
escaped = replacement.applyTo(escaped); | |
} | |
return escaped; | |
} | |
} |
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
import org.testng.Assert; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
public class LatexEscaperTest { | |
private LatexEscaper escaper; | |
@BeforeMethod | |
public void init() { | |
escaper = new LatexEscaper(); | |
} | |
@Test | |
public void testBackslash() { | |
Assert.assertEquals(escaper.escape("abc\\def"), "abc\\textbackslash def"); | |
} | |
@Test | |
public void testAandB() { | |
Assert.assertEquals(escaper.escape("A&B"), "A\\&B"); | |
} | |
@Test | |
public void testTexCommand() { | |
Assert.assertEquals(escaper.escape("a \\ghi{jkl} z"), "a \\textbackslash ghi\\{jkl\\} z"); | |
} | |
} |
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
public final class LatexUtils { | |
private LatexUtils() { } | |
private static final LatexEscaper escaper = new LatexEscaper(); | |
public static String escape(final String input) { | |
return escaper.escape(input); | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>???</groupId> | |
<artifactId>latex-util</artifactId> | |
<packaging>jar</packaging> | |
<version>???</version> | |
<name>LaTeX Util</name> | |
<description>LaTeX-related utilities</description> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>12.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.google.code.findbugs</groupId> | |
<artifactId>jsr305</artifactId> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment