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 22 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We now show the number of items found and selected to import in the online search dialog. [#6248](https://github.com/JabRef/jabref/pull/6248)
- We created a new install screen for macOS. [#5759](https://github.com/JabRef/jabref/issues/5759)
- We implemented an option to download fulltext files while importing. [#6381](https://github.com/JabRef/jabref/pull/6381)
- We added a BibTex key modifier for truncating strings. [#3915](https://github.com/JabRef/jabref/issues/3915)

### Changed

Expand Down Expand Up @@ -50,6 +51,7 @@ 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)

### Removed

Expand Down
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("\\Atruncate\\d+\\z")) {
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;
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.";
}
}
19 changes: 12 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,27 @@ 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);

// Only use valid filenames for the directories in the path
targetName = Arrays.stream(targetName.split(Pattern.quote(File.separator)))
.map(FileUtil::getValidFileName)
.collect(Collectors.joining(File.separator));
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
return targetName;
}

Expand Down
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,19 @@ void regularExpressionReplace() {
assertEquals("2003-JabRef Science",
BracketedPattern.expandBrackets("[year]-[journal:regex(\"Organization\",\"JabRef\")]", ';', dbentry, database));
}

/**
* Test the [:truncate] modifier
*/
@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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jabref.logic.formatter.minifier;

import org.junit.jupiter.api.Test;

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

/**
* 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 truncateModifierWorksWith0Length() {
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
TruncateFormatter formatter = new TruncateFormatter(0);
assertEquals("", formatter.format(TITLE));
}

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

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

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