Skip to content

Commit

Permalink
Initial attempt at creating a regex modifier for key patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
RolfStarre committed Mar 20, 2017
1 parent 43cd266 commit 04cde60
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
import org.jabref.logic.formatter.bibtexfields.OrdinalsToSuperscriptFormatter;
import org.jabref.logic.formatter.bibtexfields.RegexFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class Formatters {
new NormalizeNamesFormatter(),
new NormalizePagesFormatter(),
new OrdinalsToSuperscriptFormatter(),
new RegexFormatter(),
new RemoveBracesFormatter(),
new UnitsToLatexFormatter(),
new EscapeUnderscoresFormatter()
Expand All @@ -68,7 +70,16 @@ private Formatters() {

public static Optional<Formatter> getFormatterForModifier(String modifier) {
Objects.requireNonNull(modifier);
Optional<Formatter> formatter = ALL.stream().filter(f -> f.getKey().equals(modifier)).findAny();
Optional<Formatter> formatter;

if (modifier.matches("regex.*")) {
String regex = modifier.substring(5);
RegexFormatter.setRegex(regex);
formatter = ALL.stream().filter(f -> f.getKey().equals("regex")).findAny();

} else {
formatter = ALL.stream().filter(f -> f.getKey().equals(modifier)).findAny();
}
if (formatter.isPresent()) {
return formatter;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;

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

public class RegexFormatter implements Formatter {

private static String[] rex;

@Override
public String getName() {
return Localization.lang("Regex");
}

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

@Override
public String format(String oldString) {
Objects.requireNonNull(oldString);
rex[0] = rex[0].replaceAll("\"", "");
rex[1] = rex[1].replaceAll("\"", "");

return oldString.replaceAll(rex[0], rex[1]);
}

@Override
public String getDescription() {
return Localization.lang("Add a regular expression for the key pattern.");
}

@Override
public String getExampleInput() {
return "Please replace the spaces";
}

public static void setRegex(String regex) {
regex = regex.substring(1, regex.length() - 1);
String[] parts = regex.split("\",\"");
parts[0] += "\"";
parts[1] = "\"" + parts[1];
rex = parts;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ public void generateKeyAuthIniMany() {
assertEquals(Optional.of("DoeSmiWon"), entry.getCiteKeyOptional());
}

@Test
public void generateKeyTitleRegexe() {
bibtexKeyPattern.setDefaultValue("[title:regex(\" \",\"-\")]");
entry.setField("title", "Please replace the spaces");
BibtexKeyPatternUtil.makeAndSetLabel(bibtexKeyPattern, database, entry, preferences);
assertEquals(Optional.of("Please-Replace-the-Spaces"), entry.getCiteKeyOptional());
}

@Test
public void generateKeyTitleTitleCase() {
bibtexKeyPattern.setDefaultValue("[title:title_case]");
Expand Down

0 comments on commit 04cde60

Please sign in to comment.