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

Add truncate as a BibTex key modifier #6427

Merged
merged 38 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6ae850a
Add modifier to truncate a BibTex key
k3KAW8Pnf7mkmdSMPHz27 May 5, 2020
19fbbb3
Add tests for the truncate BibTex key modifier
k3KAW8Pnf7mkmdSMPHz27 May 5, 2020
efb5e72
Revert "Add tests for the truncate BibTex key modifier"
k3KAW8Pnf7mkmdSMPHz27 May 5, 2020
87b4bad
Add tests for the truncate bracketed pattern modifier
k3KAW8Pnf7mkmdSMPHz27 May 5, 2020
8fb49e3
Remove unnecessary imports in the test for the truncate bracketed pat…
k3KAW8Pnf7mkmdSMPHz27 May 5, 2020
b39ec98
Fix code style of local variable
k3KAW8Pnf7mkmdSMPHz27 May 6, 2020
572f724
Refactor BracketedPatternTest to maintain a parallel directory structure
k3KAW8Pnf7mkmdSMPHz27 May 6, 2020
7e52aff
Refactor test for the truncate BibTex key modifier
k3KAW8Pnf7mkmdSMPHz27 May 6, 2020
f23e888
Drop unused and unnecessary imports from BracketedPatternTest.java
k3KAW8Pnf7mkmdSMPHz27 May 6, 2020
895f302
Refactor truncate modifier to be compatible with Formatters
k3KAW8Pnf7mkmdSMPHz27 May 7, 2020
4e54476
Fix type in truncate modifier test
k3KAW8Pnf7mkmdSMPHz27 May 7, 2020
9ace66b
Add check for Null in format
k3KAW8Pnf7mkmdSMPHz27 May 7, 2020
cceb55d
Refactor tests for truncate modifier
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
e35e79d
Add truncation for long directory names
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
b48bc46
Fix name and documentation of createDirNameFromPattern
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
d6f2bf9
Update src/main/java/org/jabref/logic/formatter/minifier/TruncateForm…
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
2a04bf2
Fix matching the modifier String to a TruncateFormatter
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
5f8cc56
Fix encapsulation and naming in TruncateFormatter
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
544a05d
Fix JavaDoc in TruncateFormatter
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
30a8688
Merge branch 'fix-for-issue-3915' of https://github.com/k3KAW8Pnf7mkm…
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
deeaf2b
Add changes to CHANGELOG.md
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
c518562
Merge branch 'master' into fix-for-issue-3915
k3KAW8Pnf7mkmdSMPHz27 May 8, 2020
d1677b7
Fix createDirNameFromPattern to verify names of subfolders
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
7d1c54b
Add tests for long directory names
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
d667856
Fix truncate with negative integer
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
93470e2
Add test with negative index
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
5d97d35
Add test with invalid data
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
47be136
Refactor names of truncate tests
k3KAW8Pnf7mkmdSMPHz27 May 9, 2020
b1af777
Add TruncateFormatter to general Formatter tests
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
20487a5
Fix truncate with negative index
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
d501d5d
Merge remote-tracking branch 'origin/fix-for-issue-3915' into fix-for…
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
1f7f2ee
Drop truncation of long directory names
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
59c2465
Fix mating truncate in Formatters.java
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
452ad96
Fix matching `truncateN` in Formatters.java
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
9d9caa4
Merge branch 'fix-for-issue-3915' of https://github.com/k3KAW8Pnf7mkm…
k3KAW8Pnf7mkmdSMPHz27 May 11, 2020
7d96513
Merge branch 'master' into fix-for-issue-3915
k3KAW8Pnf7mkmdSMPHz27 May 12, 2020
6a7a211
Resolve conflicts
k3KAW8Pnf7mkmdSMPHz27 May 12, 2020
859493c
Refactor truncate tests in BracketedPatternTest
k3KAW8Pnf7mkmdSMPHz27 May 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jabref.logic.formatter.casechanger.TitleCaseFormatter;
import org.jabref.logic.formatter.casechanger.UpperCaseFormatter;
import org.jabref.logic.formatter.minifier.MinifyNameListFormatter;
import org.jabref.logic.formatter.minifier.TruncateFormatter;
import org.jabref.logic.layout.format.LatexToUnicodeFormatter;
import org.jabref.model.cleanup.Formatter;

Expand Down Expand Up @@ -102,6 +103,9 @@ public static Optional<Formatter> getFormatterForModifier(String modifier) {
if (modifier.startsWith(RegexFormatter.KEY)) {
String regex = modifier.substring(RegexFormatter.KEY.length());
return Optional.of(new RegexFormatter(regex));
} else if (modifier.matches(TruncateFormatter.KEY + "\\d+")) {
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
int truncateAfter = Integer.parseInt(modifier.substring(TruncateFormatter.KEY.length()));
return Optional.of(new TruncateFormatter(truncateAfter));
} else {
return getAll().stream().filter(f -> f.getKey().equals(modifier)).findAny();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jabref.logic.formatter.minifier;

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

public class TruncateFormatter extends Formatter {
public static final String KEY = "truncate";
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
private static Integer truncateAfter;

/**
* The TruncateFormatter truncates a string after the given index.
*
* @param truncateIndex truncate a string after this index.
*/
public TruncateFormatter(int truncateIndex) {
truncateAfter = truncateIndex;
}

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

@Override
public String getKey() {
return KEY;
}

/**
* Truncates a string after a given index.
*/
@Override
public String format(final String input) {
final int truncateIndex = Math.min(truncateAfter, input.length());
return input.substring(0, truncateIndex).stripTrailing();
}

@Override
public String getDescription() {
return Localization.lang("Truncates a string after a given index.");
}

@Override
public String getExampleInput() {
return "Truncate this sentence.";
}
}
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2233,3 +2233,5 @@ Computer\ software.\ The\ standard\ styles\ will\ treat\ this\ entry\ type\ as\
A\ data\ set\ or\ a\ similar\ collection\ of\ (mostly)\ raw\ data.=A data set or a similar collection of (mostly) raw data.

Display\ count\ of\ items\ in\ group=Display count of items in group
Truncate=Truncate
Truncates\ a\ string\ after\ a\ given\ index.=Truncates a string after a given index.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.util;
package org.jabref.logic.bibtexkeypattern;

import org.jabref.logic.bibtexkeypattern.BracketedPattern;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexString;
Expand Down Expand Up @@ -241,4 +240,43 @@ void regularExpressionReplace() {
assertEquals("2003-JabRef Science",
BracketedPattern.expandBrackets("[year]-[journal:regex(\"Organization\",\"JabRef\")]", ';', dbentry, database));
}

/**
* Test the [:truncate] modifier
*/
@Test
void truncateModifierRemovesTrailingWhitespace() {
assertEquals("Open",
BracketedPattern.expandBrackets("[fulltitle:truncate5]", ';', dbentry, database));
}

@Test
void truncateModifierKeepsIntenalWhitespace() {
assertEquals("Open S",
BracketedPattern.expandBrackets("[fulltitle:truncate6]", ';', dbentry, database));
}

@Test
void truncateModifierWorksWith0Length() {
assertEquals("",
BracketedPattern.expandBrackets("[fulltitle:truncate0]", ';', dbentry, database));
}

@Test
void truncateModifierWorksWith9999Length() {
assertEquals("Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science",
BracketedPattern.expandBrackets("[fulltitle:truncate9999]", ';', dbentry, database));
}

@Test
void truncateModifiersWorksAppliedTwice() {
assertEquals("Open",
BracketedPattern.expandBrackets("[fulltitle:truncate6:truncate5]", ';', dbentry, database));
}

@Test
void truncateModifierIsIgnoredWithoutAnArgument() {
assertEquals("Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science",
BracketedPattern.expandBrackets("[fulltitle:truncate]", ';', dbentry, database));
}
}