Skip to content

Commit

Permalink
Implementation of new modifiers for issue #11367 (#12067)
Browse files Browse the repository at this point in the history
* Extended modifiers and added options for:

CamelN
Camel
ShortTitleFormatter
VeryShortTitleFormatter

* Extended modifiers and added options for:

CamelN
Camel
ShortTitleFormatter
VeryShortTitleFormatter

* Merge remote-tracking branch 'origin/main'

# Conflicts:
#	src/main/java/org/jabref/logic/formatter/casechanger/CamelFormatter.java
#	src/main/java/org/jabref/logic/formatter/casechanger/CamelNFormatter.java
#	src/main/java/org/jabref/logic/formatter/casechanger/ShortTitleFormatter.java
#	src/main/java/org/jabref/logic/formatter/casechanger/VeryShortTitleFormatter.java

* Small fix for get key and get name in camel formatters

* Changed tests to have only one location inside BracketedPatternTest.java
  • Loading branch information
Mtjpp authored Nov 25, 2024
1 parent b02b6ff commit a86adbb
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 1 deletion.
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 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";
}
}
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("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";
}
}
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 "This is a short title";
}
}
Original file line number Diff line number Diff line change
@@ -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<Word> 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";
}
}
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 @@ -1489,6 +1489,14 @@ HTML\ to\ LaTeX=HTML to LaTeX
LaTeX\ cleanup=LaTeX cleanup
LaTeX\ to\ Unicode=LaTeX to Unicode
lower\ case=lower case
Camel\ case=Camel case
Camel\ case\ -\ n\ letters\ max= Camel case - n letters max
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
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,75 @@ void expandBracketsDoesNotTruncateWithoutAnArgumentToTruncateModifier() {
BracketedPattern.expandBrackets("[fulltitle:truncate]", ';', dbentry, database));
}

/**
* Test the [:camel] modifier
*/
@ParameterizedTest
@CsvSource({
"'CamelTitleFormatter', 'Camel Title Formatter'",
"'CamelTitleFormatter', 'CAMEL TITLE FORMATTER'",
"'CamelTitleFormatter', 'camel title formatter'",
"'CamelTitleFormatter', 'cAMEL tITLE fORMATTER'",
"'C', 'c'"
})

void expandBracketsCamelTitleModifier(String expectedCitationKey, String title) {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.TITLE, title);
assertEquals(expectedCitationKey,
BracketedPattern.expandBrackets("[title:camel]", ';', bibEntry, null));
}

/**
* Test the [:veryshorttitle] modifier
*/
@ParameterizedTest
@CsvSource({
"'Very', 'A very short title'",
"'V', 'V'",
"'V', 'A v'"
})

void expandBracketsVeryShortTitleModifier(String expectedCitationKey, String title) {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.TITLE, title);
assertEquals(expectedCitationKey,
BracketedPattern.expandBrackets("[title:veryshorttitle]", ';', bibEntry, null));
}

/**
* Test the [:shorttitle] modifier
*/
@ParameterizedTest
@CsvSource({
"'Very Short Title', 'A very short title'",
"'Short Title', 'Short title'",
"'Title', 'A title'",
"'Title', 'A Title'"
})

void expandBracketsShortTitleModifier(String expectedCitationKey, String title) {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.TITLE, title);
assertEquals(expectedCitationKey,
BracketedPattern.expandBrackets("[title:shorttitle]", ';', bibEntry, null));
}

/**
* Test the [:camelN] modifier
*/
@Test
void expandBracketsCamelNModifier() {
BibEntry bibEntry = new BibEntry()
.withField(StandardField.TITLE, "Open Source Software And The Private Collective Innovation Model Issues");
assertEquals("Open",
BracketedPattern.expandBrackets("[title:camel1]", ';', bibEntry, null));
assertEquals("OpenSourceSoftwareAnd",
BracketedPattern.expandBrackets("[title:camel4]", ';', bibEntry, null));
assertEquals("OpenSourceSoftwareAndThePrivateCollectiveInnovationModelIssues",
BracketedPattern.expandBrackets("[title:camel10]", ';', bibEntry, null));
}

/**
* Test the [camelN] title marker.
*/
Expand Down

0 comments on commit a86adbb

Please sign in to comment.