diff --git a/CHANGELOG.md b/CHANGELOG.md index d9073b55d41..c3be908ef14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -75,6 +76,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue when overwriting the owner was disabled. [#9896](https://github.com/JabRef/jabref/pull/9896) - 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 diff --git a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java index 17e88ab6955..bb9651f9145 100644 --- a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java +++ b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java @@ -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; @@ -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 DEFAULT_SAVE_ACTIONS; @@ -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. - * + *

* General format for one key/list map: ...[...] - field[formatter1,formatter2,...] * Multiple are written as ...[...]...[...]...[...] - * field1[formatter1,formatter2,...]field2[formatter3,formatter4,...] - * + * field1[formatter1,formatter2,...]field2[formatter3,formatter4,...] + *

* The idea is that characters are field names until [ is reached and that formatter lists are terminated by ] - * + *

* Example: pages[normalize_page_numbers]title[escapeAmpersands,escapeDollarSign,escapeUnderscores,latex_cleanup] */ 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 recommendedBibtexFormatters = new ArrayList<>(DEFAULT_SAVE_ACTIONS); recommendedBibtexFormatters.addAll(List.of( @@ -87,8 +91,9 @@ public FieldFormatterCleanups(boolean enabled, List actio * Note: String parsing is done at {@link FieldFormatterCleanups#parse(String)} */ public static String getMetaDataString(List actionList, String newLineSeparator) { - // first, group all formatters by the field for which they apply - Map> 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> groupedByField = new LinkedHashMap<>(); for (FieldFormatterCleanup cleanup : actionList) { Field key = cleanup.getField(); @@ -198,12 +203,13 @@ public static FieldFormatterCleanups parse(List 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(); } diff --git a/src/main/java/org/jabref/logic/formatter/Formatters.java b/src/main/java/org/jabref/logic/formatter/Formatters.java index cc89cfcfb03..ad57b7f6202 100644 --- a/src/main/java/org/jabref/logic/formatter/Formatters.java +++ b/src/main/java/org/jabref/logic/formatter/Formatters.java @@ -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"); @@ -78,6 +79,7 @@ public static List getOthers() { new EscapeAmpersandsFormatter(), new EscapeDollarSignFormatter(), new ShortenDOIFormatter(), + new ReplaceUnicodeLigaturesFormatter(), new UnprotectTermsFormatter() ); } diff --git a/src/main/java/org/jabref/logic/layout/format/ReplaceUnicodeLigaturesFormatter.java b/src/main/java/org/jabref/logic/layout/format/ReplaceUnicodeLigaturesFormatter.java index 0824258087b..1d2db81c090 100644 --- a/src/main/java/org/jabref/logic/layout/format/ReplaceUnicodeLigaturesFormatter.java +++ b/src/main/java/org/jabref/logic/layout/format/ReplaceUnicodeLigaturesFormatter.java @@ -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)); } diff --git a/src/test/java/org/jabref/logic/exporter/FieldFormatterCleanupsTest.java b/src/test/java/org/jabref/logic/cleanup/FieldFormatterCleanupsTest.java similarity index 93% rename from src/test/java/org/jabref/logic/exporter/FieldFormatterCleanupsTest.java rename to src/test/java/org/jabref/logic/cleanup/FieldFormatterCleanupsTest.java index 9ac6e612423..b6737e8c924 100644 --- a/src/test/java/org/jabref/logic/exporter/FieldFormatterCleanupsTest.java +++ b/src/test/java/org/jabref/logic/cleanup/FieldFormatterCleanupsTest.java @@ -1,4 +1,4 @@ -package org.jabref.logic.exporter; +package org.jabref.logic.cleanup; import java.util.ArrayList; import java.util.Arrays; @@ -6,8 +6,6 @@ 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; @@ -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; @@ -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")); + } } diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java index 72740b6d6c7..d34f6f37ccc 100644 --- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java +++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java @@ -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()); }