-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Pagenumbers CleanupAction #273
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80a071f
Add onSave action for pages fields #131
stefan-kolb 99bfb77
Remove page number cleanup from CleanupAction as it is an onSave acti…
stefan-kolb 9a6d022
Revert "Remove page number cleanup from CleanupAction as it is an onS…
stefan-kolb 83ca0dc
Only replace old autocleanup action for now
stefan-kolb 6624485
Use new interface paradigma for all field formatters
stefan-kolb cc5db9f
Refactor PageNumbersFormatter test
stefan-kolb 60811a5
Only create the undo action if value was changed
stefan-kolb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/net/sf/jabref/logic/cleanup/PageNumbersCleanup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import net.sf.jabref.logic.formatter.FieldFormatters; | ||
import net.sf.jabref.logic.formatter.PageNumbersFormatter; | ||
import net.sf.jabref.model.entry.BibtexEntry; | ||
|
||
/** | ||
* This class includes sensible defaults for consistent formatting of BibTex page numbers. | ||
*/ | ||
public class PageNumbersCleanup { | ||
private BibtexEntry entry; | ||
|
||
public PageNumbersCleanup(BibtexEntry entry) { | ||
this.entry = entry; | ||
} | ||
|
||
/** | ||
* Format page numbers, separated either by commas or double-hyphens. | ||
* Converts the range number format of the <code>pages</code> field to page_number--page_number. | ||
* | ||
* @see{PageNumbersFormatter} | ||
*/ | ||
public void cleanup() { | ||
final String field = "pages"; | ||
|
||
String value = entry.getField(field); | ||
String newValue = FieldFormatters.PAGE_NUMBERS.format(value); | ||
entry.setField(field, newValue); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/sf/jabref/logic/formatter/FieldFormatters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FieldFormatters { | ||
public static final PageNumbersFormatter PAGE_NUMBERS = new PageNumbersFormatter(); | ||
|
||
public static final List<Formatter> ALL = Arrays.asList(PAGE_NUMBERS); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/net/sf/jabref/logic/formatter/Formatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
/** | ||
* Formatter Interface | ||
*/ | ||
public interface Formatter { | ||
/** | ||
* Returns a human readable name of the formatter usable for e.g. in the GUI | ||
* | ||
* @return the name of the formatter | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Formats a field value by with a particular formatter transformation. | ||
* | ||
* @param value the input String | ||
* @return the formatted output String | ||
*/ | ||
String format(String value); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/net/sf/jabref/logic/formatter/PageNumbersFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* This class includes sensible defaults for consistent formatting of BibTex page numbers. | ||
*/ | ||
public class PageNumbersFormatter implements Formatter { | ||
@Override | ||
public String getName() { | ||
return "Page numbers"; | ||
} | ||
|
||
/** | ||
* Format page numbers, separated either by commas or double-hyphens. | ||
* Converts the range number format of the <code>pages</code> field to page_number--page_number. | ||
* Removes all literals except [0-9,-]. | ||
* Keeps the existing String if the resulting field does not match the expected Regex. | ||
* | ||
* <example> | ||
* 1-2 -> 1--2 | ||
* 1,2,3 -> 1,2,3 | ||
* {1}-{2} -> 1--2 | ||
* Invalid -> Invalid | ||
* </example> | ||
*/ | ||
public String format(String value) { | ||
final String rejectLiterals = "[^0-9,-]"; | ||
final Pattern pagesPattern = Pattern.compile("\\A(\\d+)-{1,2}(\\d+)\\Z"); | ||
final String replace = "$1--$2"; | ||
|
||
// nothing to do | ||
if (value == null || value.isEmpty()) { | ||
return value; | ||
} | ||
|
||
// remove unwanted literals incl. whitespace | ||
String cleanValue = value.replaceAll(rejectLiterals, ""); | ||
// try to find pages pattern | ||
Matcher matcher = pagesPattern.matcher(cleanValue); | ||
// replace | ||
String newValue = matcher.replaceFirst(replace); | ||
// replacement? | ||
if(!newValue.equals(cleanValue)) { | ||
// write field | ||
return newValue; | ||
} | ||
return value; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/net/sf/jabref/logic/cleanup/PageNumbersCleanupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import junit.framework.Assert; | ||
import net.sf.jabref.model.entry.BibtexEntry; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class PageNumbersCleanupTest { | ||
private BibtexEntry entry; | ||
|
||
@Before | ||
public void setUp() { | ||
entry = new BibtexEntry(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
entry = null; | ||
} | ||
|
||
@Test | ||
public void formatPageNumbers() { | ||
entry.setField("pages", "1-2"); | ||
new PageNumbersCleanup(entry).cleanup(); | ||
|
||
Assert.assertEquals("1--2", entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void onlyFormatPageNumbersField() { | ||
entry.setField("otherfield", "1-2"); | ||
new PageNumbersCleanup(entry).cleanup(); | ||
|
||
Assert.assertEquals("1-2", entry.getField("otherfield")); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/net/sf/jabref/logic/formatter/PageNumbersFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import junit.framework.Assert; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class PageNumbersFormatterTest { | ||
private PageNumbersFormatter formatter; | ||
|
||
@Before | ||
public void setUp() { | ||
formatter = new PageNumbersFormatter(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
formatter = null; | ||
} | ||
|
||
@Test | ||
public void formatPageNumbers() { | ||
expectCorrect("1-2", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersCommaSeparated() { | ||
expectCorrect("1,2,3", "1,2,3"); | ||
} | ||
|
||
@Test | ||
public void ignoreWhitespaceInPageNumbers() { | ||
expectCorrect(" 1 - 2 ", "1--2"); | ||
} | ||
|
||
@Test | ||
public void keepCorrectlyFormattedPageNumbers() { | ||
expectCorrect("1--2", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersEmptyFields() { | ||
expectCorrect("", ""); | ||
expectCorrect(null, null); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersRemoveUnexpectedLiterals() { | ||
expectCorrect("{1}-{2}", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersRegexNotMatching() { | ||
expectCorrect("12", "12"); | ||
} | ||
|
||
private void expectCorrect(String input, String expected) { | ||
Assert.assertEquals(expected, formatter.format(input)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just return the new value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to return the initial value and not the cleaned value if page numbers regex does not match?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok. then leave this untouched.