Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup: Escape $ signs #8698

Merged
merged 14 commits into from
Apr 20, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ This section explains how you get the JabRef code onto your machine in a form al

### Clone your forked repository on your local machine

* In a command line, navigate to the folder where you want to place the source code (parent folder of `jabref/`). To prevent issues along the way, it is strongly recommend to choose a path that does not contain any special (non-ASCII or whitespace) characters.
* In a command line, navigate to the folder where you want to place the source code (parent folder of `jabref/`). To prevent issues along the way, it is strongly recommend to choose a path that does not contain any special (non-ASCII or whitespace) characters. Note that placing jabref folder directly in the disk root directory may cause an error.
* Run `git clone --depth=10 https://github.com/YOUR_USERNAME/jabref.git`. The `--depth--10` is used to limit the download to \~20 MB instead of downloading the complete history (\~800 MB). If you want to dig in our commit history, feel free to download everything.
* Go to the newly created jabref folder: `cd jabref`
* Generate additional source code: `./gradlew assemble`
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeAmpersandsFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeCurrencySymbolsFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeUnderscoresFormatter;
import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.HtmlToUnicodeFormatter;
Expand Down Expand Up @@ -75,6 +76,7 @@ public static List<Formatter> getOthers() {
new UnitsToLatexFormatter(),
new EscapeUnderscoresFormatter(),
new EscapeAmpersandsFormatter(),
new EscapeCurrencySymbolsFormatter(),
new ShortenDOIFormatter(),
new UnprotectTermsFormatter()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;
import java.util.regex.Matcher;

import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.l10n.Localization;

public class EscapeCurrencySymbolsFormatter extends Formatter {
koppor marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String getName() {
return Localization.lang("Escape currency symbols");
}

@Override
public String getKey() {
return "escapeCurrencySymbols";
}

@Override
public String format(String value) {
Objects.requireNonNull(value);
String replacement = "\\$";
return value.replaceAll("(?<!\\\\)\\$", Matcher.quoteReplacement(replacement));
koppor marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String getDescription() {
return Localization.lang("Escape currency symbols");
}

@Override
public String getExampleInput() {
return "Text$with$currency$symbols";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jabref.logic.formatter.bibtexfields;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class EscapeCurrencySymbolsFormatterTest {

private EscapeCurrencySymbolsFormatter formatter;

@BeforeEach
void setUp() {
formatter = new EscapeCurrencySymbolsFormatter();
}

@Test
void formatReturnsSameTextIfNoCurrencySymbolsPresent() throws Exception {
assertEquals("Lorem ipsum", formatter.format("Lorem ipsum"));
}

@Test
void formatEscapesCurrencySymbolsIfPresent() throws Exception {
assertEquals("Lorem\\\\$ipsum", formatter.format("Lorem$ipsum"));
koppor marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void formatExample() {
assertEquals("Text\\\\$with\\\\$currency\\\\$symbols", formatter.format(formatter.getExampleInput()));
}
}