Skip to content

Commit

Permalink
Fix custom name formatters (#4848)
Browse files Browse the repository at this point in the history
Fixes #3531.
  • Loading branch information
tobiasdiez authored Apr 4, 2019
1 parent a273eb1 commit 8095f46
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where the default icon of a group was not colored correctly.
- We fixed an issue where the first field in entry editor was not focused when adding a new entry. [#4024](https://github.com/JabRef/jabref/issues/4024)
- We reworked the "Edit file" dialog to make it resizeable and improved the workflow for adding and editing files https://github.com/JabRef/jabref/issues/2970
- We fixed an issue where custom name formatters were no longer found correctly. [#3531](https://github.com/JabRef/jabref/issues/3531)
- We fixed an issue where the month was not shown in the preview https://github.com/JabRef/jabref/issues/3239.
- Rewritten logic to detect a second jabref instance. [#4023](https://github.com/JabRef/jabref/issues/4023)
- We fixed an issue where the "Convert to BibTeX-Cleanup" moved the content of the `file` field to the `pdf` field [#4120](https://github.com/JabRef/jabref/issues/4120)
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ private void doOptionField(String s) {
}
}

private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {

private LayoutFormatter getLayoutFormatterByName(String name) {
switch (name) {
case "HTMLToLatexFormatter": // For backward compatibility
case "HtmlToLatex":
Expand Down Expand Up @@ -481,8 +480,7 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
case "Iso690NamesAuthors":
return new Iso690NamesAuthors();
case "JournalAbbreviator":
return new JournalAbbreviator(prefs.getJournalAbbreviationLoader(),
prefs.getJournalAbbreviationPreferences());
return new JournalAbbreviator(prefs.getJournalAbbreviationLoader(), prefs.getJournalAbbreviationPreferences());
case "LastPage":
return new LastPage();
case "FormatChars": // For backward compatibility
Expand Down Expand Up @@ -535,7 +533,7 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
case "WrapFileLinks":
return new WrapFileLinks(prefs.getFileLinkPreferences());
default:
return new NotFoundFormatter(name);
return null;
}
}

Expand All @@ -544,19 +542,13 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
* string (in order of appearance).
*/
private List<LayoutFormatter> getOptionalLayout(String formatterName) {

List<List<String>> formatterStrings = parseMethodsCalls(formatterName);

List<LayoutFormatter> results = new ArrayList<>(formatterStrings.size());

Map<String, String> userNameFormatter = NameFormatter.getNameFormatters(prefs.getNameFormatterPreferences());

for (List<String> strings : formatterStrings) {

String nameFormatterName = strings.get(0).trim();

// Check if this is a name formatter defined by this export filter:

Optional<String> contents = prefs.getCustomExportNameFormatter(nameFormatterName);
if (contents.isPresent()) {
NameFormatter nf = new NameFormatter();
Expand All @@ -566,22 +558,18 @@ private List<LayoutFormatter> getOptionalLayout(String formatterName) {
}

// Try to load from formatters in formatter folder
try {
LayoutFormatter f = getLayoutFormatterByName(nameFormatterName);
// If this formatter accepts an argument, check if we have one, and
// set it if so:
if ((f instanceof ParamLayoutFormatter) && (strings.size() >= 2)) {
((ParamLayoutFormatter) f).setArgument(strings.get(1));
LayoutFormatter formatter = getLayoutFormatterByName(nameFormatterName);
if (formatter != null) {
// If this formatter accepts an argument, check if we have one, and set it if so
if ((formatter instanceof ParamLayoutFormatter) && (strings.size() >= 2)) {
((ParamLayoutFormatter) formatter).setArgument(strings.get(1));
}
results.add(f);
results.add(formatter);
continue;
} catch (Exception ex) {
LOGGER.info("Problem with formatter", ex);
}

// Then check whether this is a user defined formatter
String formatterParameter = userNameFormatter.get(nameFormatterName);

if (formatterParameter != null) {
NameFormatter nf = new NameFormatter();
nf.setParameter(formatterParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ private static String format(String toFormat, AuthorList al, String[] formats) {
}

public String format(String toFormat, String inParameters) {

AuthorList al = AuthorList.parse(toFormat);
String parameters;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ public class NotFoundFormatter implements LayoutFormatter {

private final String notFound;


public NotFoundFormatter(String notFound) {

this.notFound = notFound;
}

Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/jabref/logic/layout/LayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;

import org.jabref.logic.layout.format.FileLinkPreferences;
import org.jabref.logic.layout.format.NameFormatterPreferences;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.CustomEntryType;
Expand Down Expand Up @@ -98,8 +99,10 @@ void HTMLCharsWithDotlessIAndTiled() throws IOException {
layoutText);
}

/**
* Test for http://discourse.jabref.org/t/the-wrapfilelinks-formatter/172 (the example in the help files)
*/
@Test
// Test for http://discourse.jabref.org/t/the-wrapfilelinks-formatter/172 (the example in the help files)
void wrapFileLinksExpandFile() throws IOException {
when(layoutFormatterPreferences.getFileLinkPreferences()).thenReturn(
new FileLinkPreferences(Collections.emptyList(), Collections.singletonList("src/test/resources/pdfs/")));
Expand All @@ -121,4 +124,15 @@ void expandCommandIfTerminatedByMinus() throws IOException {

assertEquals("2-th ed.-", layoutText);
}

@Test
void customNameFormatter() throws IOException {
when(layoutFormatterPreferences.getNameFormatterPreferences()).thenReturn(
new NameFormatterPreferences(Collections.singletonList("DCA"), Collections.singletonList("1@*@{ll}@@[email protected]@{ff}{ll}@2..2@ and {ff}{l}@@*@*@more")));
BibEntry entry = new BibEntry(BibtexEntryTypes.ARTICLE).withField("author", "Joe Doe and Mary Jane");

String layoutText = layout("\\begin{author}\\format[DCA]{\\author}\\end{author}", entry);

assertEquals("JoeDoe and MaryJ", layoutText);
}
}

0 comments on commit 8095f46

Please sign in to comment.