Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel;
import org.jabref.logic.FilePreferences;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.FieldTextMapper;
Expand Down Expand Up @@ -168,7 +168,7 @@ public CleanupPreferences getCleanupPreset() {

activeJobs.add(CleanupPreferences.CleanupStep.FIX_FILE_LINKS);

return new CleanupPreferences(activeJobs, new FieldFormatterCleanups(
return new CleanupPreferences(activeJobs, new FieldFormatterCleanupActions(
!formatterCleanupsPanel.cleanupsDisableProperty().getValue(),
formatterCleanupsPanel.cleanupsProperty()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldTextMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import org.jabref.gui.StateManager;
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
Expand All @@ -41,9 +41,9 @@ public FieldFormatterCleanupsPanelViewModel(StateManager stateManager) {
public void resetToRecommended() {
stateManager.getActiveDatabase().ifPresent(databaseContext -> {
if (databaseContext.isBiblatexMode()) {
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBLATEX_ACTIONS);
cleanupsListProperty.setAll(FieldFormatterCleanupActions.RECOMMEND_BIBLATEX_ACTIONS);
} else {
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBTEX_ACTIONS);
cleanupsListProperty.setAll(FieldFormatterCleanupActions.RECOMMEND_BIBTEX_ACTIONS);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextInputControl;

import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.strings.StringUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.icon.JabRefIcon;
import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
import org.jabref.logic.formatter.casechanger.UnprotectTermsFormatter;
import org.jabref.logic.l10n.Localization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.gui.libraryproperties.PropertiesTabViewModel;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.preferences.CliPreferences;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.Field;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void setValues() {

// FieldFormatterCleanupsPanel, included via <?import ...> in FXML

Optional<FieldFormatterCleanups> saveActions = initialMetaData.getSaveActions();
Optional<FieldFormatterCleanupActions> saveActions = initialMetaData.getSaveActions();
saveActions.ifPresentOrElse(value -> {
cleanupsDisableProperty.setValue(!value.isEnabled());
cleanupsProperty.setValue(FXCollections.observableArrayList(value.getConfiguredActions()));
Expand All @@ -113,18 +113,18 @@ public void storeSettings() {
newMetaData.markAsNotProtected();
}

FieldFormatterCleanups fieldFormatterCleanups = new FieldFormatterCleanups(
FieldFormatterCleanupActions fieldFormatterCleanupActions = new FieldFormatterCleanupActions(
!cleanupsDisableProperty().getValue(),
cleanupsProperty());

if (FieldFormatterCleanups.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanups.getConfiguredActions())) {
if (FieldFormatterCleanupActions.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanupActions.getConfiguredActions())) {
newMetaData.clearSaveActions();
} else {
// if all actions have been removed, remove the save actions from the MetaData
if (fieldFormatterCleanups.getConfiguredActions().isEmpty()) {
if (fieldFormatterCleanupActions.getConfiguredActions().isEmpty()) {
newMetaData.clearSaveActions();
} else {
newMetaData.setSaveActions(fieldFormatterCleanups);
newMetaData.setSaveActions(fieldFormatterCleanupActions);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jabref.gui.preferences.JabRefGuiPreferences;
import org.jabref.logic.citationkeypattern.GlobalCitationKeyPatterns;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.os.OS;
import org.jabref.logic.preferences.JabRefCliPreferences;
Expand Down Expand Up @@ -531,9 +531,9 @@ private static void upgradeCleanups(JabRefCliPreferences prefs) {
List<String> formatterCleanups = List.of(StringUtil.unifyLineBreaks(prefs.get(V5_8_CLEANUP_FIELD_FORMATTERS), "\n")
.split("\n"));
if (formatterCleanups.size() >= 2
&& (FieldFormatterCleanups.ENABLED.equals(formatterCleanups.getFirst())
|| FieldFormatterCleanups.DISABLED.equals(formatterCleanups.getFirst()))) {
prefs.putBoolean(V6_0_CLEANUP_FIELD_FORMATTERS_ENABLED, FieldFormatterCleanups.ENABLED.equals(formatterCleanups.getFirst())
&& (FieldFormatterCleanupActions.ENABLED.equals(formatterCleanups.getFirst())
|| FieldFormatterCleanupActions.DISABLED.equals(formatterCleanups.getFirst()))) {
prefs.putBoolean(V6_0_CLEANUP_FIELD_FORMATTERS_ENABLED, FieldFormatterCleanupActions.ENABLED.equals(formatterCleanups.getFirst())
? Boolean.TRUE
: Boolean.FALSE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.logic.formatter.bibtexfields.RemoveEnclosingBracesFormatter;
import org.jabref.logic.formatter.casechanger.Word;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
public class CleanupPreferences {

private final ObservableSet<CleanupStep> activeJobs;
private final ObjectProperty<FieldFormatterCleanups> fieldFormatterCleanups;
private final ObjectProperty<FieldFormatterCleanupActions> fieldFormatterCleanups;

public CleanupPreferences(EnumSet<CleanupStep> activeJobs) {
this(activeJobs, new FieldFormatterCleanups(false, new ArrayList<>()));
this(activeJobs, new FieldFormatterCleanupActions(false, new ArrayList<>()));
}

public CleanupPreferences(CleanupStep activeJob) {
this(EnumSet.of(activeJob));
}

public CleanupPreferences(FieldFormatterCleanups formatterCleanups) {
public CleanupPreferences(FieldFormatterCleanupActions formatterCleanups) {
this(EnumSet.noneOf(CleanupStep.class), formatterCleanups);
}

public CleanupPreferences(EnumSet<CleanupStep> activeJobs, FieldFormatterCleanups formatterCleanups) {
public CleanupPreferences(EnumSet<CleanupStep> activeJobs, FieldFormatterCleanupActions formatterCleanups) {
this.activeJobs = FXCollections.observableSet(activeJobs);
this.fieldFormatterCleanups = new SimpleObjectProperty<>(formatterCleanups);
}
Expand Down Expand Up @@ -60,15 +60,15 @@ public Boolean isActive(CleanupStep step) {
return activeJobs.contains(step);
}

public FieldFormatterCleanups getFieldFormatterCleanups() {
public FieldFormatterCleanupActions getFieldFormatterCleanups() {
return fieldFormatterCleanups.get();
}

public ObjectProperty<FieldFormatterCleanups> fieldFormatterCleanupsProperty() {
public ObjectProperty<FieldFormatterCleanupActions> fieldFormatterCleanupsProperty() {
return fieldFormatterCleanups;
}

public void setFieldFormatterCleanups(FieldFormatterCleanups fieldFormatters) {
public void setFieldFormatterCleanups(FieldFormatterCleanupActions fieldFormatters) {
fieldFormatterCleanups.setValue(fieldFormatters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Objects;
import java.util.Set;

import org.jabref.logic.formatter.Formatter;
import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntriesEventSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.jabref.logic.cleanup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.logic.formatter.IdentityFormatter;
import org.jabref.logic.formatter.bibtexfields.ConvertMSCCodesFormatter;
Expand All @@ -24,19 +19,16 @@
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.layout.format.LatexToUnicodeFormatter;
import org.jabref.logic.layout.format.ReplaceUnicodeLigaturesFormatter;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;

import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FieldFormatterCleanups {
public class FieldFormatterCleanupActions {

public static final List<FieldFormatterCleanup> DEFAULT_SAVE_ACTIONS;
public static final List<FieldFormatterCleanup> RECOMMEND_BIBTEX_ACTIONS;
Expand All @@ -45,7 +37,7 @@ public class FieldFormatterCleanups {
public static final String ENABLED = "enabled";
public static final String DISABLED = "disabled";

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

/**
* This parses the key/list map of fields and clean up actions for the field.
Expand All @@ -58,7 +50,6 @@ public class FieldFormatterCleanups {
* <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(
Expand Down Expand Up @@ -87,41 +78,16 @@ public class FieldFormatterCleanups {
private final boolean enabled;
private final List<FieldFormatterCleanup> actions;

public FieldFormatterCleanups(boolean enabled, @NonNull List<FieldFormatterCleanup> actions) {
public FieldFormatterCleanupActions(boolean enabled, @NonNull List<FieldFormatterCleanup> actions) {
this.enabled = enabled;
this.actions = actions;
}

/**
* Note: String parsing is done at {@link FieldFormatterCleanups#parse(String)}
* Note: String parsing is done at {@link FieldFormatterCleanupActions#parse(String)}
*/
public static String getMetaDataString(List<FieldFormatterCleanup> actionList, String newLineSeparator) {
// 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();
// add new list into the hashmap if needed
groupedByField.computeIfAbsent(key, k -> new ArrayList<>());

// add the formatter to the map if it is not already there
List<String> formattersForKey = groupedByField.get(key);
if (!formattersForKey.contains(cleanup.getFormatter().getKey())) {
formattersForKey.add(cleanup.getFormatter().getKey());
}
}

// convert the contents of the hashmap into the correct serialization
StringBuilder result = new StringBuilder();
for (Map.Entry<Field, List<String>> entry : groupedByField.entrySet()) {
result.append(entry.getKey().getName());

StringJoiner joiner = new StringJoiner(",", "[", "]" + newLineSeparator);
entry.getValue().forEach(joiner::add);
result.append(joiner);
}

return result.toString();
return FieldFormatterCleanupParser.serializeActions(actionList, newLineSeparator);
}

public boolean isEnabled() {
Expand Down Expand Up @@ -151,30 +117,7 @@ private List<FieldChange> applyAllActions(BibEntry entry) {
}

public static List<FieldFormatterCleanup> parse(String formatterString) {
if ((formatterString == null) || formatterString.isEmpty()) {
// no save actions defined in the meta data
return List.of();
}

List<FieldFormatterCleanup> result = new ArrayList<>();

// first remove all newlines for easier parsing
String formatterStringWithoutLineBreaks = StringUtil.unifyLineBreaks(formatterString, "");

Matcher matcher = FIELD_FORMATTER_CLEANUP_PATTERN.matcher(formatterStringWithoutLineBreaks);
while (matcher.find()) {
String fieldKey = matcher.group(1);
Field field = FieldFactory.parseField(fieldKey);

String fieldString = matcher.group(2);

List<FieldFormatterCleanup> fieldFormatterCleanups = Arrays.stream(fieldString.split(","))
.map(FieldFormatterCleanups::getFormatterFromString)
.map(formatter -> new FieldFormatterCleanup(field, formatter))
.toList();
result.addAll(fieldFormatterCleanups);
}
return result;
return FieldFormatterCleanupParser.parseAction(formatterString);
}

static Formatter getFormatterFromString(String formatterName) {
Expand All @@ -200,15 +143,15 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof FieldFormatterCleanups other) {
if (obj instanceof FieldFormatterCleanupActions other) {
return Objects.equals(actions, other.actions) && (enabled == other.enabled);
}
return false;
}

@Override
public String toString() {
return "FieldFormatterCleanups{" +
return "FieldFormatterCleanupActions{" +
"enabled=" + enabled + "," +
"actions=" + actions +
"}";
Expand Down
Loading
Loading