diff --git a/src/main/java/org/jabref/logic/formatter/Formatters.java b/src/main/java/org/jabref/logic/formatter/Formatters.java index fb827f9ab8e..a52c4437848 100644 --- a/src/main/java/org/jabref/logic/formatter/Formatters.java +++ b/src/main/java/org/jabref/logic/formatter/Formatters.java @@ -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; @@ -96,11 +100,20 @@ public static List getOthers() { ); } + public static List getTitleChangers() { + return Arrays.asList( + new VeryShortTitleFormatter(), + new ShortTitleFormatter(), + new CamelFormatter() + ); + } + public static List getAll() { List all = new ArrayList<>(); all.addAll(getConverters()); all.addAll(getCaseChangers()); all.addAll(getOthers()); + all.addAll(getTitleChangers()); return all; } @@ -123,9 +136,21 @@ public static Optional 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()) { diff --git a/src/main/java/org/jabref/logic/formatter/casechanger/CamelFormatter.java b/src/main/java/org/jabref/logic/formatter/casechanger/CamelFormatter.java new file mode 100644 index 00000000000..2d314fc440e --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/casechanger/CamelFormatter.java @@ -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 case"); + } + + @Override + public String getKey() { + return "camel_case"; + } + + @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 "this is example input"; + } +} diff --git a/src/main/java/org/jabref/logic/formatter/casechanger/CamelNFormatter.java b/src/main/java/org/jabref/logic/formatter/casechanger/CamelNFormatter.java new file mode 100644 index 00000000000..d3e2efe86ca --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/casechanger/CamelNFormatter.java @@ -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("Camel case - n letters max"); + } + + @Override + public String getKey() { + return "camel_case_n"; + } + + @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 "this is camel formatter"; + } +} diff --git a/src/main/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatter.java b/src/main/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatter.java new file mode 100644 index 00000000000..6e9da157286 --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatter.java @@ -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 "This is a short title"; + } +} diff --git a/src/main/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatter.java b/src/main/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatter.java new file mode 100644 index 00000000000..20332909f13 --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatter.java @@ -0,0 +1,43 @@ +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 resultTitle = title.getWords().stream() + .filter(Predicate.not( + Word::isSmallerWord)) + .findFirst(); + + return resultTitle.map(Word::toString).orElse(""); + } + + @Override + public String getDescription() { + return Localization.lang( + "Returns first word of the title ignoring any function words."); + } + + @Override + public String getExampleInput() { + return "A very short title"; + } +} diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 94119350460..00902dc045b 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -1486,6 +1486,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 diff --git a/src/test/java/org/jabref/logic/formatter/casechanger/CamelFormatterTest.java b/src/test/java/org/jabref/logic/formatter/casechanger/CamelFormatterTest.java new file mode 100644 index 00000000000..c5def702547 --- /dev/null +++ b/src/test/java/org/jabref/logic/formatter/casechanger/CamelFormatterTest.java @@ -0,0 +1,44 @@ +package org.jabref.logic.formatter.casechanger; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest} + */ +class CamelFormatterTest { + + private CamelFormatter formatter; + + @BeforeEach + void setUp() { + formatter = new CamelFormatter(); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat") + void test(String expected, String input) { + assertEquals(expected, formatter.format(input)); + } + + private static Stream provideStringsForFormat() { + return Stream.of( + Arguments.of("CamelTitleFormatter", "Camel Title Formatter"), + Arguments.of("CamelTitleFormatter", "CAMEL TITLE FORMATTER"), + Arguments.of("CamelTitleFormatter", "camel title formatter"), + Arguments.of("CamelTitleFormatter", "cAMEL tITLE fORMATTER"), + Arguments.of("C", "c")); + } + + @Test + void formatExample() { + assertEquals("ThisIsExampleInput", formatter.format(formatter.getExampleInput())); + } +} diff --git a/src/test/java/org/jabref/logic/formatter/casechanger/CamelNFormatterTest.java b/src/test/java/org/jabref/logic/formatter/casechanger/CamelNFormatterTest.java new file mode 100644 index 00000000000..379d277ea7d --- /dev/null +++ b/src/test/java/org/jabref/logic/formatter/casechanger/CamelNFormatterTest.java @@ -0,0 +1,90 @@ +package org.jabref.logic.formatter.casechanger; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest} + */ +class CamelNFormatterTest { + + private CamelNFormatter formatter_3; + private CamelNFormatter formatter_1; + private CamelNFormatter formatter_0; + + @BeforeEach + void setUp() { + formatter_3 = new CamelNFormatter(3); + formatter_1 = new CamelNFormatter(1); + formatter_0 = new CamelNFormatter(0); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat_3") + void test_3(String expected, String input) { + assertEquals(expected, formatter_3.format(input)); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat_1") + void test_1(String expected, String input) { + assertEquals(expected, formatter_1.format(input)); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat_0") + void test_0(String expected, String input) { + assertEquals(expected, formatter_0.format(input)); + } + + private static Stream provideStringsForFormat_3() { + return Stream.of( + Arguments.of("CamelTitleFormatter", "Camel Title Formatter"), + Arguments.of("CamelTitleFormatter", "CAMEL TITLE FORMATTER"), + Arguments.of("CamelTitleFormatter", "camel title formatter"), + Arguments.of("CamelTitleFormatter", "cAMEL tITLE fORMATTER"), + Arguments.of("CamelTitleFormatter", "cAMEL tITLE fORMATTER test"), + Arguments.of("CamelTitleFormatter", "cAMEL tITLE fORMATTER tEST"), + Arguments.of("CamelTitleFormatter", "camel title formatter test"), + Arguments.of("", ""), + Arguments.of("C", "c")); + } + + private static Stream provideStringsForFormat_1() { + return Stream.of( + Arguments.of("Camel", "Camel Title Formatter"), + Arguments.of("Camel", "CAMEL TITLE FORMATTER"), + Arguments.of("Camel", "camel title formatter"), + Arguments.of("Camel", "cAMEL tITLE fORMATTER"), + Arguments.of("", ""), + Arguments.of("C", "c")); + } + + private static Stream provideStringsForFormat_0() { + return Stream.of( + Arguments.of("", "Camel Title Formatter"), + Arguments.of("", "")); + } + + @Test + void formatExample_3() { + assertEquals("ThisIsCamel", formatter_3.format(formatter_3.getExampleInput())); + } + + @Test + void formatExample_1() { + assertEquals("This", formatter_1.format(formatter_1.getExampleInput())); + } + + @Test + void formatExample_0() { + assertEquals("", formatter_0.format(formatter_0.getExampleInput())); + } +} diff --git a/src/test/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatterTest.java b/src/test/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatterTest.java new file mode 100644 index 00000000000..8e308a9da42 --- /dev/null +++ b/src/test/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatterTest.java @@ -0,0 +1,45 @@ +package org.jabref.logic.formatter.casechanger; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest} + */ +class ShortTitleFormatterTest { + + private ShortTitleFormatter formatter; + + @BeforeEach + void setUp() { + formatter = new ShortTitleFormatter(); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat") + void test(String expected, String input) { + assertEquals(expected, formatter.format(input)); + } + + private static Stream provideStringsForFormat() { + return Stream.of( + Arguments.of("Very short title", "Very short title"), + Arguments.of("This is very", "This is very short title"), + Arguments.of("This is", "This is"), + Arguments.of("This", "This"), + Arguments.of("", ""), + Arguments.of("very short title", "A very short title")); + } + + @Test + void formatExample() { + assertEquals("This is short", formatter.format(formatter.getExampleInput())); + } +} diff --git a/src/test/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatterTest.java b/src/test/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatterTest.java new file mode 100644 index 00000000000..e9892c741db --- /dev/null +++ b/src/test/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatterTest.java @@ -0,0 +1,42 @@ +package org.jabref.logic.formatter.casechanger; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest} + */ +class VeryShortTitleFormatterTest { + + private VeryShortTitleFormatter formatter; + + @BeforeEach + void setUp() { + formatter = new VeryShortTitleFormatter(); + } + + @ParameterizedTest + @MethodSource("provideStringsForFormat") + void test(String expected, String input) { + assertEquals(expected, formatter.format(input)); + } + + private static Stream provideStringsForFormat() { + return Stream.of( + Arguments.of("Very", "Very short title"), + Arguments.of("", ""), + Arguments.of("very", "A very short title")); + } + + @Test + void formatExample() { + assertEquals("very", formatter.format(formatter.getExampleInput())); + } +}