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

Auto trim url field #4355

Merged
merged 4 commits into from
Sep 19, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
## [Unreleased]

### Changed
- URL field formatting is updated. All whitespace chars, located at the beginning/ending of the url, are trimmed automatically
- We changed the behavior of the field formatting dialog such that the `bibtexkey` is not changed when formatting all fields or all text fields.
- We added a "Move file to file directory and rename file" option for simultaneously moving and renaming of document file. [#4166](https://github.com/JabRef/jabref/issues/4166)
- Use integrated graphics card instead of discrete on macOS [#4070](https://github.com/JabRef/jabref/issues/4070)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;
Expand All @@ -37,7 +38,7 @@ public UrlEditor(String fieldName, DialogService dialogService, AutoCompleteSugg

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


new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) {
}

/**
* The default context menu with a specific menu item to cleanup URL.
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;

/**
* Trim all whitespace characters(defined in java) in the string.
*/
public class TrimWhitespaceFormatter extends Formatter {

@Override
public String getName() {
return Localization.lang("Trim whitespace characters");
}

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

@Override
public String format(String value) {
Objects.requireNonNull(value);
return value.trim();
}

@Override
public String getDescription() {
return Localization.lang("Trim all whitespace characters in the field content.");
}

@Override
public String getExampleInput() {
return "\r\n InCDMA\n\r ";
}
}
5 changes: 5 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,11 @@ This\ operation\ requires\ one\ or\ more\ entries\ to\ be\ selected.=This operat

Toggle\ entry\ preview=Toggle entry preview
Toggle\ groups\ interface=Toggle groups interface

Trim\ all\ whitespace\ characters\ in\ the\ field\ content.=Trim all whitespace characters in the field content.

Trim\ whitespace\ characters=Trim whitespace characters

Try\ different\ encoding=Try different encoding

Unabbreviate\ journal\ names\ of\ the\ selected\ entries=Unabbreviate journal names of the selected entries
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

package org.jabref.logic.formatter.bibtexfields;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TrimWhitespaceFormatterTest {

private TrimWhitespaceFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new TrimWhitespaceFormatter();
}

@Test
public void removeHorizontalTabulations() {
assertEquals("whitespace", formatter.format("\twhitespace"));
assertEquals("whitespace", formatter.format("whitespace\t"));
assertEquals("whitespace", formatter.format("\twhitespace\t\t"));
}

@Test
public void removeLineFeeds() {
assertEquals("whitespace", formatter.format("\nwhitespace"));
assertEquals("whitespace", formatter.format("whitespace\n"));
assertEquals("whitespace", formatter.format("\nwhitespace\n\n"));
}

@Test
public void removeFormFeeds() {
assertEquals("whitespace", formatter.format("\fwhitespace"));
assertEquals("whitespace", formatter.format("whitespace\f"));
assertEquals("whitespace", formatter.format("\fwhitespace\f\f"));
}

@Test
public void removeCarriageReturnFeeds() {
assertEquals("whitespace", formatter.format("\rwhitespace"));
assertEquals("whitespace", formatter.format("whitespace\r"));
assertEquals("whitespace", formatter.format("\rwhitespace\r\r"));
}

@Test
public void removeSeparatorSpaces() {
assertEquals("whitespace", formatter.format(" whitespace"));
assertEquals("whitespace", formatter.format("whitespace "));
assertEquals("whitespace", formatter.format(" whitespace "));
}

@Test
public void removeMixedWhitespaceChars() {
assertEquals("whitespace", formatter.format(" \r\t\fwhitespace"));
assertEquals("whitespace", formatter.format("whitespace \n \r"));
assertEquals("whitespace", formatter.format(" \f\t whitespace \r \n"));
}


}