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

Fix save actions #9890

Merged
merged 4 commits into from
May 15, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We moved the preferences option to open the last edited files on startup to the 'General' tab. [#9808](https://github.com/JabRef/jabref/pull/9808)
- We split the 'Import and Export' tab into 'Web Search' and 'Export'. [#9839](https://github.com/JabRef/jabref/pull/9839)
- We improved the recognition of DOIs when pasting a link containing a DOI on the maintable [#9864](https://github.com/JabRef/jabref/issues/9864s)
- The formatter `remove_unicode_ligatures` is now called `replace_unicode_ligatures`. [#9890](https://github.com/JabRef/jabref/pull/9890)

### Fixed

Expand All @@ -74,6 +75,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed the Save/save as file type shows BIBTEX_DB instead of "Bibtex library" [#9372](https://github.com/JabRef/jabref/issues/9372)
- We fixed an issue regarding recording redundant prefixes in search history. [#9685](https://github.com/JabRef/jabref/issues/9685)
- We fixed an issue where passing a URL containing a DOI led to a "No entry found" notification. [#9821](https://github.com/JabRef/jabref/issues/9821)
- The order of save actions is now retained. [#9890](https://github.com/JabRef/jabref/pull/9890)

### Removed

Expand Down
32 changes: 19 additions & 13 deletions src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -31,6 +30,9 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.strings.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FieldFormatterCleanups {

public static final List<FieldFormatterCleanup> DEFAULT_SAVE_ACTIONS;
Expand All @@ -40,25 +42,27 @@ public class FieldFormatterCleanups {
public static final String ENABLED = "enabled";
public static final String DISABLED = "disabled";

private static final Logger LOGGER = LoggerFactory.getLogger(FieldFormatterCleanups.class);

/**
* This parses the key/list map of fields and clean up actions for the field.
*
* <p>
* General format for one key/list map: <code>...[...]</code> - <code>field[formatter1,formatter2,...]</code>
* Multiple are written as <code>...[...]...[...]...[...]</code>
* <code>field1[formatter1,formatter2,...]field2[formatter3,formatter4,...]</code>
*
* <code>field1[formatter1,formatter2,...]field2[formatter3,formatter4,...]</code>
* <p>
* The idea is that characters are field names until <code>[</code> is reached and that formatter lists are terminated by <code>]</code>
*
* <p>
* Example: <code>pages[normalize_page_numbers]title[escapeAmpersands,escapeDollarSign,escapeUnderscores,latex_cleanup]</code>
*/
private static final Pattern FIELD_FORMATTER_CLEANUP_PATTERN = Pattern.compile("([^\\[]+)\\[([^]]+)]");

static {
DEFAULT_SAVE_ACTIONS = List.of(
new FieldFormatterCleanup(StandardField.PAGES, new NormalizePagesFormatter()),
new FieldFormatterCleanup(StandardField.DATE, new NormalizeDateFormatter()),
new FieldFormatterCleanup(StandardField.MONTH, new NormalizeMonthFormatter()),
new FieldFormatterCleanup(InternalField.INTERNAL_ALL_TEXT_FIELDS_FIELD, new ReplaceUnicodeLigaturesFormatter()));
new FieldFormatterCleanup(StandardField.PAGES, new NormalizePagesFormatter()),
new FieldFormatterCleanup(StandardField.DATE, new NormalizeDateFormatter()),
new FieldFormatterCleanup(StandardField.MONTH, new NormalizeMonthFormatter()),
new FieldFormatterCleanup(InternalField.INTERNAL_ALL_TEXT_FIELDS_FIELD, new ReplaceUnicodeLigaturesFormatter()));

List<FieldFormatterCleanup> recommendedBibtexFormatters = new ArrayList<>(DEFAULT_SAVE_ACTIONS);
recommendedBibtexFormatters.addAll(List.of(
Expand Down Expand Up @@ -87,8 +91,9 @@ public FieldFormatterCleanups(boolean enabled, List<FieldFormatterCleanup> actio
* Note: String parsing is done at {@link FieldFormatterCleanups#parse(String)}
*/
public static String getMetaDataString(List<FieldFormatterCleanup> actionList, String newLineSeparator) {
// first, group all formatters by the field for which they apply
Map<Field, List<String>> groupedByField = new TreeMap<>(Comparator.comparing(Field::getName));
// First, group all formatters by the field for which they apply
// Order of the list should be kept
Map<Field, List<String>> groupedByField = new LinkedHashMap<>();
for (FieldFormatterCleanup cleanup : actionList) {
Field key = cleanup.getField();

Expand Down Expand Up @@ -198,12 +203,13 @@ public static FieldFormatterCleanups parse(List<String> formatterMetaList) {
}
}

private static Formatter getFormatterFromString(String formatterName) {
static Formatter getFormatterFromString(String formatterName) {
for (Formatter formatter : Formatters.getAll()) {
if (formatterName.equals(formatter.getKey())) {
return formatter;
}
}
LOGGER.info("Formatter {} not found.", formatterName);
return new IdentityFormatter();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jabref.logic.formatter.minifier.MinifyNameListFormatter;
import org.jabref.logic.formatter.minifier.TruncateFormatter;
import org.jabref.logic.layout.format.LatexToUnicodeFormatter;
import org.jabref.logic.layout.format.ReplaceUnicodeLigaturesFormatter;

public class Formatters {
private static final Pattern TRUNCATE_PATTERN = Pattern.compile("\\Atruncate\\d+\\z");
Expand Down Expand Up @@ -78,6 +79,7 @@ public static List<Formatter> getOthers() {
new EscapeAmpersandsFormatter(),
new EscapeDollarSignFormatter(),
new ShortenDOIFormatter(),
new ReplaceUnicodeLigaturesFormatter(),
new UnprotectTermsFormatter()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ public String getName() {

@Override
public String getKey() {
return "remove_unicode_ligatures";
return "replace_unicode_ligatures";
}

@Override
public String format(String fieldText) {
String result = fieldText;

for (Pattern key : ligaturesMap.keySet()) {
result = key.matcher(result).replaceAll(ligaturesMap.get(key));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.jabref.logic.exporter;
package org.jabref.logic.cleanup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.formatter.IdentityFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeAmpersandsFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeDollarSignFormatter;
Expand All @@ -17,6 +15,7 @@
import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
import org.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import org.jabref.logic.layout.format.ReplaceUnicodeLigaturesFormatter;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
Expand Down Expand Up @@ -309,4 +308,29 @@ void parserTest() {
),
fieldFormatterCleanups);
}

@Test
void getMetaDataStringWorks() {
assertEquals("""
pages[normalize_page_numbers]
date[normalize_date]
month[normalize_month]
all-text-fields[replace_unicode_ligatures]
""", FieldFormatterCleanups.getMetaDataString(FieldFormatterCleanups.DEFAULT_SAVE_ACTIONS, "\n"));
}

@Test
void parsingOfDefaultSaveActions() {
assertEquals(FieldFormatterCleanups.DEFAULT_SAVE_ACTIONS, FieldFormatterCleanups.parse("""
pages[normalize_page_numbers]
date[normalize_date]
month[normalize_month]
all-text-fields[replace_unicode_ligatures]
"""));
}

@Test
void formatterFromString() {
assertEquals(new ReplaceUnicodeLigaturesFormatter(), FieldFormatterCleanups.getFormatterFromString("replace_unicode_ligatures"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,12 @@ void writeSaveActions() throws Exception {

databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList());

// The order should be kept (the cleanups are a list, not a set)
assertEquals("@Comment{jabref-meta: saveActions:enabled;"
+ OS.NEWLINE
+ "day[upper_case]" + OS.NEWLINE
+ "journal[title_case]" + OS.NEWLINE
+ "title[lower_case]" + OS.NEWLINE
+ "journal[title_case]" + OS.NEWLINE
+ "day[upper_case]" + OS.NEWLINE
+ ";}"
+ OS.NEWLINE, stringWriter.toString());
}
Expand Down