Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.CleanupTabSelection;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;

import com.airhacks.afterburner.views.ViewLoader;
import org.jspecify.annotations.NonNull;
Expand Down Expand Up @@ -37,7 +37,7 @@ private void bindProperties() {

@FXML
private void onApply() {
FieldFormatterCleanups selectedFormatters = viewModel.getSelectedFormatters();
FieldFormatterCleanupActions selectedFormatters = viewModel.getSelectedFormatters();
CleanupTabSelection selectedTab = CleanupTabSelection.ofFormatters(selectedFormatters);
dialogViewModel.apply(selectedTab);
getScene().getWindow().hide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import javafx.collections.FXCollections;

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;

public class CleanupSingleFieldViewModel {
public final BooleanProperty cleanupsEnabled = new SimpleBooleanProperty(true);
public final ListProperty<FieldFormatterCleanup> cleanups = new SimpleListProperty<>(FXCollections.observableArrayList());

public CleanupSingleFieldViewModel(FieldFormatterCleanups initialCleanups) {
public CleanupSingleFieldViewModel(FieldFormatterCleanupActions initialCleanups) {
cleanupsEnabled.set(initialCleanups.isEnabled());
cleanups.setAll(initialCleanups.getConfiguredActions());
}

public FieldFormatterCleanups getSelectedFormatters() {
return new FieldFormatterCleanups(cleanupsEnabled.get(), cleanups.get());
public FieldFormatterCleanupActions getSelectedFormatters() {
return new FieldFormatterCleanupActions(cleanupsEnabled.get(), cleanups.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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.FieldFormatterCleanupActions;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.model.entry.field.Field;
Expand All @@ -38,9 +38,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 @@ -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 @@ -22,7 +22,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 @@ -541,9 +541,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 @@ -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 @@ -7,7 +7,7 @@
public record CleanupTabSelection(
EnumSet<CleanupPreferences.CleanupStep> allJobs,
EnumSet<CleanupPreferences.CleanupStep> selectedJobs,
Optional<FieldFormatterCleanups> formatters) {
Optional<FieldFormatterCleanupActions> formatters) {

public CleanupTabSelection {
allJobs = allJobs == null ? EnumSet.noneOf(CleanupPreferences.CleanupStep.class) : EnumSet.copyOf(allJobs);
Expand All @@ -19,7 +19,7 @@ public static CleanupTabSelection ofJobs(EnumSet<CleanupPreferences.CleanupStep>
return new CleanupTabSelection(allJobs, selectedJobs, Optional.empty());
}

public static CleanupTabSelection ofFormatters(FieldFormatterCleanups cleanups) {
public static CleanupTabSelection ofFormatters(FieldFormatterCleanupActions cleanups) {
return new CleanupTabSelection(EnumSet.noneOf(CleanupPreferences.CleanupStep.class), EnumSet.noneOf(CleanupPreferences.CleanupStep.class), Optional.of(cleanups));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
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;
Expand All @@ -25,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 @@ -46,20 +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);

/**
* 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>
* <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("([^\\[]+)\\[([^]]+)]");
private static final Logger LOGGER = LoggerFactory.getLogger(FieldFormatterCleanupActions.class);

static {
DEFAULT_SAVE_ACTIONS = List.of(
Expand Down Expand Up @@ -88,41 +66,14 @@ 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 [FieldFormatterCleanupMapper#parseActions(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 FieldFormatterCleanupMapper.serializeActions(actionList, newLineSeparator);
}

public boolean isEnabled() {
Expand Down Expand Up @@ -151,33 +102,6 @@ private List<FieldChange> applyAllActions(BibEntry entry) {
return result;
}

public static List<FieldFormatterCleanup> parse(String formatterString) {
if ((formatterString == null) || formatterString.isEmpty()) {
Comment thread
calixtus marked this conversation as resolved.
// 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;
}

static Formatter getFormatterFromString(String formatterName) {
return Formatters
.getFormatterForKey(formatterName)
Expand All @@ -201,15 +125,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