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

Implementation of new modifiers for issue #11367 #12067

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
27 changes: 26 additions & 1 deletion src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
import org.jabref.logic.formatter.bibtexfields.ShortenDOIFormatter;
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter;
import org.jabref.logic.formatter.casechanger.CamelFormatter;
import org.jabref.logic.formatter.casechanger.CamelNFormatter;
import org.jabref.logic.formatter.casechanger.CapitalizeFormatter;
import org.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import org.jabref.logic.formatter.casechanger.SentenceCaseFormatter;
import org.jabref.logic.formatter.casechanger.ShortTitleFormatter;
import org.jabref.logic.formatter.casechanger.TitleCaseFormatter;
import org.jabref.logic.formatter.casechanger.UnprotectTermsFormatter;
import org.jabref.logic.formatter.casechanger.UpperCaseFormatter;
import org.jabref.logic.formatter.casechanger.VeryShortTitleFormatter;
import org.jabref.logic.formatter.minifier.MinifyNameListFormatter;
import org.jabref.logic.formatter.minifier.TruncateFormatter;
import org.jabref.logic.layout.format.LatexToUnicodeFormatter;
Expand Down Expand Up @@ -96,11 +100,20 @@ public static List<Formatter> getOthers() {
);
}

public static List<Formatter> getTitleChangers() {
return Arrays.asList(
new VeryShortTitleFormatter(),
new ShortTitleFormatter(),
new CamelFormatter()
);
}

public static List<Formatter> getAll() {
List<Formatter> all = new ArrayList<>();
all.addAll(getConverters());
all.addAll(getCaseChangers());
all.addAll(getOthers());
all.addAll(getTitleChangers());
return all;
}

Expand All @@ -123,9 +136,21 @@ public static Optional<Formatter> getFormatterForModifier(String modifier) {
return Optional.of(new TitleCaseFormatter());
case "sentencecase":
return Optional.of(new SentenceCaseFormatter());
case "veryshorttitle":
return Optional.of(new VeryShortTitleFormatter());
case "shorttitle":
return Optional.of(new ShortTitleFormatter());
}

if (modifier.startsWith(RegexFormatter.KEY)) {
if (modifier.contains("camel")) {
modifier = modifier.replace("camel", "");
if (modifier.isEmpty()) {
return Optional.of(new CamelFormatter());
} else {
int length = Integer.parseInt(modifier);
return Optional.of(new CamelNFormatter(length));
}
} else if (modifier.startsWith(RegexFormatter.KEY)) {
String regex = modifier.substring(RegexFormatter.KEY.length());
return Optional.of(new RegexFormatter(regex));
} else if (TRUNCATE_PATTERN.matcher(modifier).matches()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jabref.logic.formatter.casechanger;

import java.util.stream.Collectors;

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

public class CamelFormatter extends Formatter {

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

@Override
public String getKey() {
return "camel";
Mtjpp marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String format(String input) {
Title title = new Title(input);

return title.getWords().stream()
.map(Word -> {
Word.toUpperFirst();
return Word.toString();
})
.collect(Collectors.joining(""));
}

@Override
public String getDescription() {
return Localization.lang(
"Returns capitalized and concatenated title.");
}

@Override
public String getExampleInput() {
return "N";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jabref.logic.formatter.casechanger;

import java.util.stream.Collectors;

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

public class CamelNFormatter extends Formatter {
private final int length;

public CamelNFormatter(int length) {
this.length = length;
}

@Override
public String getName() {
return Localization.lang("Cameln");
Mtjpp marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
public String format(String input) {
Title title = new Title(input);

return title.getWords().stream()
.map(Word -> {
Word.toUpperFirst();
return Word.toString();
})
.limit(length)
.collect(Collectors.joining(""));
}

@Override
public String getDescription() {
return Localization.lang(
"Returns capitalized and concatenated title to N length.");
}

@Override
public String getExampleInput() {
return "N";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jabref.logic.formatter.casechanger;

import java.util.function.Predicate;
import java.util.stream.Collectors;

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

public class ShortTitleFormatter extends Formatter {

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

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

@Override
public String format(String input) {
Title title = new Title(input);

return title.getWords().stream()
.filter(Predicate.not(
Word::isSmallerWord))
.map(Word::toString)
.limit(3)
.collect(Collectors.joining(" "));
}

@Override
public String getDescription() {
return Localization.lang(
"Returns first 3 words of the title ignoring any function words.");
}

@Override
public String getExampleInput() {
return "N";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jabref.logic.formatter.casechanger;

import java.util.Optional;
import java.util.function.Predicate;

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

public class VeryShortTitleFormatter extends Formatter {

@Override
public String getName() {
return Localization.lang("Very short title");
}

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

@Override
public String format(String input) {
Title title = new Title(input);

Optional<Word> resultTitle = title.getWords().stream()
.filter(Predicate.not(
Word::isSmallerWord))
.findFirst();

if (resultTitle.isEmpty()) {
return "";
}
return resultTitle.toString();
}

@Override
public String getDescription() {
return Localization.lang(
"Returns first word of the title ignoring any function words.");
}

@Override
public String getExampleInput() {
return "N";
}
}
8 changes: 8 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,14 @@ HTML\ to\ LaTeX=HTML to LaTeX
LaTeX\ cleanup=LaTeX cleanup
LaTeX\ to\ Unicode=LaTeX to Unicode
lower\ case=lower case
Camel=Camel
Cameln=Cameln
Very\ short\ title=Very short title
Short\ title=Short title
Returns\ first\ word\ of\ the\ title\ ignoring\ any\ function\ words.=Returns first word of the title ignoring any function words.
Returns\ first\ 3\ words\ of\ the\ title\ ignoring\ any\ function\ words.=Returns first 3 words of the title ignoring any function words.
Returns\ capitalized\ and\ concatenated\ title\ to\ N\ length.=Returns capitalized and concatenated title to N length.
Returns\ capitalized\ and\ concatenated\ title.=Returns capitalized and concatenated title.
Minify\ list\ of\ person\ names=Minify list of person names
Normalize\ date=Normalize date
Normalize\ en\ dashes=Normalize en dashes
Expand Down
Loading