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

Fixes #3648: Chained modifiers work again #3670

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ For more details refer to the [field mapping help page](http://help.jabref.org/e
- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576)
- Autocompletion in the search bar can now be disabled via the preferences. [#3598](https://github.com/JabRef/jabref/issues/3598)
- We fixed and extended the RIS import functionality to cover more fields. [#3634](https://github.com/JabRef/jabref/issues/3634) [#2607](https://github.com/JabRef/jabref/issues/2607)
- Chaining modifiers in BibTeX key pattern now works as described in the documentation. [#3648](https://github.com/JabRef/jabref/issues/3648)

### Removed
- We removed the [Look and Feels from JGoodies](http://www.jgoodies.com/freeware/libraries/looks/), because the open source version is not compatible with Java 9.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Objects;
import java.util.Optional;

import org.jabref.logic.util.BracketedPattern;
import org.jabref.model.FieldChange;
import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
Expand Down Expand Up @@ -46,13 +45,13 @@ public BibtexKeyGenerator(AbstractBibtexKeyPattern citeKeyPattern, BibDatabase d
this.bibtexKeyPatternPreferences = Objects.requireNonNull(bibtexKeyPatternPreferences);
}

static String generateKey(BibEntry entry, String value) {
return generateKey(entry, value, new BibDatabase());
static String generateKey(BibEntry entry, String pattern) {
return generateKey(entry, pattern, new BibDatabase());
}

static String generateKey(BibEntry entry, String value, BibDatabase database) {
static String generateKey(BibEntry entry, String pattern, BibDatabase database) {
GlobalBibtexKeyPattern keyPattern = new GlobalBibtexKeyPattern(Collections.emptyList());
keyPattern.setDefaultValue("[" + value + "]");
keyPattern.setDefaultValue("[" + pattern + "]");
return new BibtexKeyGenerator(keyPattern, database, new BibtexKeyPatternPreferences("", "", false, true, true, keyPattern, ','))
.generateKey(entry);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.logic.util;
package org.jabref.logic.bibtexkeypattern;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -338,40 +338,33 @@ else if (val.matches("edtr\\d+")) {
* @param offset The number of initial items in the modifiers array to skip.
* @return The modified label.
*/
public static String applyModifiers(final String label, final List<String> parts, final int offset) {
static String applyModifiers(final String label, final List<String> parts, final int offset) {
String resultingLabel = label;
if (parts.size() > offset) {
for (int j = offset; j < parts.size(); j++) {
String modifier = parts.get(j);

if ("abbr".equals(modifier)) {
// Abbreviate - that is,
StringBuilder abbreviateSB = new StringBuilder();
String[] words = resultingLabel.replaceAll("[\\{\\}']", "")
.split("[\\(\\) \r\n\"]");
for (String word : words) {
if (!word.isEmpty()) {
abbreviateSB.append(word.charAt(0));
}
for (int j = offset; j < parts.size(); j++) {
String modifier = parts.get(j);

if ("abbr".equals(modifier)) {
// Abbreviate - that is,
StringBuilder abbreviateSB = new StringBuilder();
String[] words = resultingLabel.replaceAll("[\\{\\}']", "")
.split("[\\(\\) \r\n\"]");
for (String word : words) {
if (!word.isEmpty()) {
abbreviateSB.append(word.charAt(0));
}
resultingLabel = abbreviateSB.toString();
} else {
Optional<Formatter> formatter = Formatters.getFormatterForModifier(modifier);
if (formatter.isPresent()) {
resultingLabel = formatter.get().format(label);
} else if (!modifier.isEmpty() && (modifier.length() >= 2) && (modifier.charAt(0) == '(') && modifier.endsWith(")")) {
// Alternate text modifier in parentheses. Should be inserted if
// the label is empty:
if (label.isEmpty() && (modifier.length() > 2)) {
resultingLabel = modifier.substring(1, modifier.length() - 1);
} else {
resultingLabel = label;
}
} else {
// LOGGER.info("Key generator warning: unknown modifier '"
// + modifier + "'.");
resultingLabel = label;
}
resultingLabel = abbreviateSB.toString();
} else {
Optional<Formatter> formatter = Formatters.getFormatterForModifier(modifier);
if (formatter.isPresent()) {
resultingLabel = formatter.get().format(resultingLabel);
} else if (!modifier.isEmpty() && (modifier.length() >= 2) && (modifier.charAt(0) == '(') && modifier.endsWith(")")) {
// Alternate text modifier in parentheses. Should be inserted if the label is empty
if (label.isEmpty() && (modifier.length() > 2)) {
resultingLabel = modifier.substring(1, modifier.length() - 1);
}
} else {
LOGGER.warn("Key generator warning: unknown modifier '" + modifier + "'.");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jabref.logic.bibtexkeypattern.BracketedPattern;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.layout.LayoutHelper;
import org.jabref.logic.util.BracketedPattern;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.util.OptionalUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jabref.logic.util.BracketedPattern;
import org.jabref.logic.bibtexkeypattern.BracketedPattern;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,18 @@ public void generateKeyStripsApostropheFromTitle() throws Exception {
entry.setField("title", "Green Scheduling of `Whatever`");
assertEquals("GreenSchedulingofWhatever", BibtexKeyGenerator.generateKey(entry, "title"));
}

@Test
public void generateKeyWithOneModifier() throws Exception {
BibEntry entry = new BibEntry();
entry.setField("title", "The Interesting Title");
assertEquals("theinterestingtitle", BibtexKeyGenerator.generateKey(entry, "title:lower"));
}

@Test
public void generateKeyWithTwoModifiers() throws Exception {
BibEntry entry = new BibEntry();
entry.setField("title", "The Interesting Title");
assertEquals("theinterestingtitle", BibtexKeyGenerator.generateKey(entry, "title:lower:(_)"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.util;

import org.jabref.logic.bibtexkeypattern.BracketedPattern;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
Expand All @@ -9,7 +10,8 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class BracketedPatternTest {
private BibEntry bibentry;
Expand Down