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

[WIP] Auto clean up url links(https://github.com/koppor/jabref/issues/254) #3394

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We increased performance and decreased the memory footprint of the entry editor drastically. [#3331](https://github.com/JabRef/jabref/pull/3331)
- Late initialization of the context menus in the entry editor. This improves performance and memory footprint further [#3340](https://github.com/JabRef/jabref/pull/3340)
- We added a dialog to show that JabRef is working on checking integrity. [#3358](https://github.com/JabRef/jabref/issues/3358)
- We added auto url formatting when user paste link to URL field in Entry editor [#254](https://github.com/koppor/jabref/issues/254)

### Fixed
- We fixed the translation of \textendash in the entry preview [#3307](https://github.com/JabRef/jabref/issues/3307)
Expand Down
54 changes: 48 additions & 6 deletions src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package org.jabref.gui.fieldeditors;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Supplier;

import com.sun.javafx.scene.control.skin.TextAreaSkin;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import com.sun.javafx.scene.control.skin.TextAreaSkin;
import javax.annotation.Nonnull;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Supplier;

public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable {

/**
* Variable that contains user-defined behavior for paste action.
* Set empty paste behavior by default
*/
@Nonnull
private PasteActionHandler pasteActionHandler = new EmptyPasteHandler();

public EditorTextArea() {
this("");
}
Expand Down Expand Up @@ -67,6 +74,41 @@ public void populateContextMenu(ContextMenu contextMenu) {

@Override
public void initialize(URL location, ResourceBundle resources) {
}

/**
* Set pasteActionHandler variable to passed handler
* @param handler an instance of PasteActionHandler that describes paste behavior
*/
public void setPasteActionHandler(@Nonnull PasteActionHandler handler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the non null annotation and use Objects.requireNonNulll

this.pasteActionHandler = handler;
}

/**
* Override javafx TextArea method applying TextArea.paste() and pasteActionHandler after
*/
@Override
public void paste() {
super.paste();
pasteActionHandler.handle();
}

/**
* Interface presents user-described paste behaviour applying to paste method
*/

@FunctionalInterface
public interface PasteActionHandler {
void handle();
}

/**
* Empty interface implementation to do nothing external on paste method
*/
private static class EmptyPasteHandler implements PasteActionHandler {
@Override
public void handle() {

}
}
}
17 changes: 16 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.HBox;

import org.jabref.gui.DialogService;
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.gui.util.ControlHelper;
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

import java.util.List;
import java.util.function.Supplier;

public class UrlEditor extends HBox implements FieldEditorFX {

@FXML private UrlEditorViewModel viewModel;
Expand All @@ -23,6 +28,15 @@ public UrlEditor(String fieldName, DialogService dialogService, AutoCompleteSugg
ControlHelper.loadFXMLForControl(this);

textArea.textProperty().bindBidirectional(viewModel.textProperty());
Supplier<List<MenuItem>> contextMenuSupplier = EditorMenus.getCleanupURLMenu(textArea);
textArea.addToContextMenu(contextMenuSupplier);
MenuItem cleanupURLMenuItem = contextMenuSupplier.get().get(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a bit ugly: you need to know that the cleanup url menu item is the first one returned. Thus the code fails if somebody changes the getCleanupURLMenu method. Everything related to the context-menu should actually go there. So much for the theory; I'm not sure if and how a menu item can be notified that it is displayed. If you don't find a good solution, I would remove the disable check for the moment.

textArea.setOnContextMenuRequested(event ->cleanupURLMenuItem.setDisable("".equals(textArea.getSelectedText())));

// init paste handler for URLEditor to format pasted url link in textArea
textArea.setPasteActionHandler(()->{
textArea.setText(new CleanupURLFormatter().format(textArea.getText()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a one liner lambda expression, you don't need to add curly braces

});

new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);
}
Expand All @@ -45,4 +59,5 @@ public Parent getNode() {
private void openExternalLink(ActionEvent event) {
viewModel.openExternalLink();
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package org.jabref.gui.fieldeditors.contextmenu;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import javax.swing.AbstractAction;
import javax.swing.Action;

import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;

import javafx.scene.control.*;
import org.jabref.gui.actions.CopyDoiUrlAction;
import org.jabref.gui.fieldeditors.EditorTextArea;
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.l10n.Localization;

import javax.swing.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no general imports with a star, because that loads all classes in the package.
Our code formatter for eclipse and intellij should have the correct orders

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
* Provides context menus for the text fields of the entry editor. Note that we use {@link Supplier} to prevent an early
* instantiation of the menus. Therefore, they are attached to each text field but instantiation happens on the first
* right-click of the user in that field. The late instantiation is done by {@link
* EditorTextArea#addToContextMenu(java.util.function.Supplier)}.
*/

public class EditorMenus {

/**
Expand Down Expand Up @@ -84,4 +77,26 @@ public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) {
return menuItems;
};
}
/**
* The default context menu with a specific menu item to cleanup URL.
*
* @param textArea text-area that this menu will be connected to
* @return menu containing items of the default menu and an item to cleanup a URL
*/
public static Supplier<List<MenuItem>> getCleanupURLMenu(TextArea textArea){
return () -> {

CustomMenuItem cleanupURL = new CustomMenuItem(new Label(Localization.lang("Cleanup URL Link")));
//cleanupURL.setDisable(true);
cleanupURL.setOnAction(event -> textArea.setText(new CleanupURLFormatter().format(textArea.getText())));

List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(cleanupURL);
menuItems.add(new SeparatorMenuItem());
menuItems.addAll(getDefaultMenu(textArea).get());

return menuItems;
};
}
}

16 changes: 2 additions & 14 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@
import java.util.Objects;
import java.util.Optional;

import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeUnderscoresFormatter;
import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.HtmlToUnicodeFormatter;
import org.jabref.logic.formatter.bibtexfields.LatexCleanupFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeDateFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
import org.jabref.logic.formatter.bibtexfields.OrdinalsToSuperscriptFormatter;
import org.jabref.logic.formatter.bibtexfields.RegexFormatter;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.*;
import org.jabref.logic.formatter.casechanger.CapitalizeFormatter;
import org.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
Expand Down Expand Up @@ -50,6 +37,7 @@ public class Formatters {

public static final List<Formatter> OTHERS = Arrays.asList(
new ClearFormatter(),
new CleanupURLFormatter(),
new LatexCleanupFormatter(),
new MinifyNameListFormatter(),
new NormalizeDateFormatter(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jabref.logic.formatter.bibtexfields;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Cleanup URL link
*/

public class CleanupURLFormatter implements Formatter {

private static final Log LOGGER = LogFactory.getLog(CleanupURLFormatter.class);

@Override
public String getName() {
return Localization.lang("Cleanup URL Link");
}

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

@Override
public String format(String value) {
URLDecoder urlDecoder = new URLDecoder();

String decodedLink = value;
String toDecode = value;

// This regexp find "url=" or "to=" parameter in full link and get text after them
String urlRegexp = "(?:url|to)=([^&]*)";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the pattern to a static Pattern and use Pattern.compile
This is more efficient, I think examples are in other integrity checker classes

Pattern urlExtruder = Pattern.compile(urlRegexp);
Matcher matcher = urlExtruder.matcher(value);
if(matcher.find())
toDecode = matcher.group(1);

try {
decodedLink = URLDecoder.decode(toDecode, StandardCharsets.UTF_8.name());
}
catch (UnsupportedEncodingException e){
LOGGER.warn("Used unsupported character encoding", e);
}
return decodedLink;
}

@Override
public String getDescription() {
return "Cleanup URL Link by removing special symbols and extracting simple link";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description should be localized, too

}

@Override
public String getExampleInput() {
return "https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=11&cad=" +
"rja&uact=8&ved=0ahUKEwjg3ZrB_ZPXAhVGuhoKHYdOBOg4ChAWCCYwAA&url=" +
"http%3A%2F%2Fwww.focus.de%2Fgesundheit%2Fratgeber%2Fherz%2Ftest%2" +
"Flebenserwartung-werden-sie-100-jahre-alt_aid_363828.html" + "&usg=AOvVaw1G6m2jf-pTHYkXceii4hXU";
}
}
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Anzeigen_der_Kons

Remove_line_breaks=Entfernen_der_Zeilenumbrüche
Removes_all_line_breaks_in_the_field_content.=Entfernen_aller_Zeilenumbrüche_im_Inhalt_des_Feldes.

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_el.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Show_console_outp

Remove_line_breaks=Remove_line_breaks
Removes_all_line_breaks_in_the_field_content.=Removes_all_line_breaks_in_the_field_content.

Cleanup_URL_Link=Cleanup_URL_Link

Checking_integrity...=Checking_integrity...
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
4 changes: 4 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,10 @@ Delete_from_disk=Supprimer_du_disque
Remove_from_entry=Effacer_de_l'entrée
The_group_name_contains_the_keyword_separator_"%0"_and_thus_probably_does_not_work_as_expected.=Le_nom_du_groupe_contient_le_séparateur_de_mot-clef_"%0"_et_ne_fonctionnera_probablement_donc_pas_comme_attendu.
There_exists_already_a_group_with_the_same_name.=Un_groupe_portant_ce_nom_existe_déjà.
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Copy_linked_files_to_folder...=
Copied_file_successfully=
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2347,4 +2347,7 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=

Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=

Cleanup_URL_Link=

Checking_integrity...=
Loading