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 all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We implemented an option to download fulltext files while importing. [#6381](https://github.com/JabRef/jabref/pull/6381)
- We added a progress-indicator showing the average progress of background tasks to the toolbar. Clicking it reveals a pop-over with a list of running background tasks. [6443](https://github.com/JabRef/jabref/pull/6443)
- We fixed the bug when strike the delete key in the text field. [#6421](https://github.com/JabRef/jabref/issues/6421)
- We added a BibTex key modifier for truncating strings. [#3915](https://github.com/JabRef/jabref/issues/3915)
- We added support for jumping to target entry when typing letter/digit after sorting a column in maintable [#6146](https://github.com/JabRef/jabref/issues/6146)

### Changed
Expand Down Expand Up @@ -57,6 +58,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where JabRef switched to discrete graphics under macOS [#5935](https://github.com/JabRef/jabref/issues/5935)
- We fixed an issue where the Preferences entry preview will be unexpected modified leads to Value too long exception [#6198](https://github.com/JabRef/jabref/issues/6198)
- We fixed an issue where custom jstyles for Open/LibreOffice would only be valid if a layout line for the entry type `default` was at the end of the layout section [#6303](https://github.com/JabRef/jabref/issues/6303)
- We fixed an issue where long directory names created from patterns could create an exception. [#3915](https://github.com/JabRef/jabref/issues/3915)

- We fixed an issue where sort on numeric cases was broken. [#6349](https://github.com/JabRef/jabref/issues/6349)
- We fixed an issue where an "Not on FX thread" exception occured when saving on linux [#6453](https://github.com/JabRef/jabref/issues/6453)
- We fixed an issue where the library sort order was lost. [#6091](https://github.com/JabRef/jabref/issues/6091)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
Expand All @@ -29,10 +30,12 @@
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;

public class Formatters {
private static final Pattern TRUNCATE_PATTERN = Pattern.compile("\\Atruncate\\d+\\z");

private Formatters() {
}
Expand Down Expand Up @@ -102,6 +105,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 (TRUNCATE_PATTERN.matcher(modifier).matches()) {
int truncateAfter = Integer.parseInt(modifier.substring(8));
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,51 @@
package org.jabref.logic.formatter.minifier;

import java.util.Objects;

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

public class TruncateFormatter extends Formatter {
private final int TRUNCATE_AFTER;
private final String KEY;

/**
* The TruncateFormatter truncates a string after the given index and removes trailing whitespaces.
*
* @param truncateIndex truncate a string after this index.
*/
public TruncateFormatter(final int truncateIndex) {
TRUNCATE_AFTER = (truncateIndex >= 0) ? truncateIndex : Integer.MAX_VALUE;
KEY = "truncate" + TRUNCATE_AFTER;
}

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

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

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

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

@Override
public String getExampleInput() {
return "Truncate this sentence.";
}
}
15 changes: 8 additions & 7 deletions src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,23 @@ public static String createFileNameFromPattern(BibDatabase database, BibEntry en
}

/**
* Determines filename provided by an entry in a database
* Determines directory name provided by an entry in a database
*
* @param database the database, where the entry is located
* @param entry the entry to which the file should be linked to
* @param fileNamePattern the filename pattern
* @return a suggested fileName
* @param entry the entry to which the directory should be linked to
* @param directoryNamePattern the dirname pattern
* @return a suggested dirName
*/
public static String createDirNameFromPattern(BibDatabase database, BibEntry entry, String fileNamePattern) {
String targetName = BracketedPattern.expandBrackets(fileNamePattern, ';', entry, database);
public static String createDirNameFromPattern(BibDatabase database, BibEntry entry, String directoryNamePattern) {
String targetName = BracketedPattern.expandBrackets(directoryNamePattern, ';', entry, database);

if (targetName.isEmpty()) {
targetName = entry.getCiteKeyOptional().orElse("default");
}

// Removes illegal characters from filename
// Removes illegal characters from directory name
targetName = FileNameCleaner.cleanDirectoryName(targetName);

return targetName;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2241,5 +2241,6 @@ 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

Remove\ the\ following\ characters\:=Remove the following characters:
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 All @@ -21,7 +20,7 @@ class BracketedPatternTest {
private BibEntry dbentry;

@BeforeEach
void setUp() throws Exception {
void setUp() {
bibentry = new BibEntry();
bibentry.setField(StandardField.AUTHOR, "O. Kitsune");
bibentry.setField(StandardField.YEAR, "2017");
Expand Down Expand Up @@ -253,4 +252,19 @@ void testEmptyBrackets() {
assertEquals("2003-Organization Science",
BracketedPattern.expandBrackets("[year][]-[journal]", ';', dbentry, database));
}

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

@Test
void expandBracketsDoesNotTruncateWithoutAnArgumentToTruncateModifier() {
assertEquals("Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science",
BracketedPattern.expandBrackets("[fulltitle:truncate]", ';', dbentry, database));
}
}
7 changes: 5 additions & 2 deletions src/test/java/org/jabref/logic/formatter/FormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.stream.Stream;

import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
import org.jabref.logic.formatter.minifier.TruncateFormatter;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.logic.protectedterms.ProtectedTermsPreferences;
import org.jabref.model.cleanup.Formatter;
Expand Down Expand Up @@ -111,10 +112,12 @@ public static Stream<Formatter> getFormatters() {
Formatters.getAll().stream(),
// following formatters are not contained in the list of all formatters, because
// - the IdentityFormatter is not offered to the user,
// - the ProtectTermsFormatter needs more configuration
// - the ProtectTermsFormatter needs more configuration,
// - the TruncateFormatter needs setup,
Stream.of(
new IdentityFormatter(),
new ProtectTermsFormatter(protectedTermsLoader)));
new ProtectTermsFormatter(protectedTermsLoader),
new TruncateFormatter(0)));
// @formatter:on
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.jabref.logic.formatter.minifier;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests in addition to the general tests from {@link org.jabref.logic.formatter.FormatterTest}
*/
public class TruncateFormatterTest {
private final String TITLE = "A Title";

@Test
void formatWorksWith0Index() {
TruncateFormatter formatter = new TruncateFormatter(0);
assertEquals("", formatter.format(TITLE));
}

@Test
void formatRemovesTrailingWhitespace() {
TruncateFormatter formatter = new TruncateFormatter(2);
assertEquals("A", formatter.format(TITLE));
}

@Test
void formatKeepsInternalWhitespace() {
TruncateFormatter formatter = new TruncateFormatter(3);
assertEquals("A T", formatter.format(TITLE));
}

@Test
void formatWorksWith9999Length() {
TruncateFormatter formatter = new TruncateFormatter(9999);
assertEquals(TITLE, formatter.format(TITLE));
}

@Test
void formatIgnoresNegativeIndex() {
TruncateFormatter formatter = new TruncateFormatter(-1);
assertEquals(TITLE, formatter.format(TITLE));
}

@Test
void formatWorksWithEmptyString() {
TruncateFormatter formatter = new TruncateFormatter(9999);
assertEquals("", formatter.format(""));
}

@Test
void formatThrowsExceptionNullString() {
TruncateFormatter formatter = new TruncateFormatter(9999);
assertThrows(NullPointerException.class, () -> formatter.format(null));
}
}