-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onSave action for pages fields #131
- Loading branch information
1 parent
d02db9d
commit 80a071f
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/net/sf/jabref/logic/cleanup/AutoFormatter.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,55 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import net.sf.jabref.model.entry.BibtexEntry; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* This class includes sensible defaults for consistent formatting of BibTex entries. | ||
*/ | ||
public class AutoFormatter { | ||
private BibtexEntry entry; | ||
|
||
public AutoFormatter(BibtexEntry entry) { | ||
this.entry = entry; | ||
} | ||
|
||
/** | ||
* Runs all default cleanups for the BibTex entry. | ||
*/ | ||
public void runDefaultCleanups() { | ||
formatPageNumbers(); | ||
} | ||
|
||
/** | ||
* 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. | ||
* Keeps the existing String if the field does not match the expected Regex. | ||
* | ||
* <example> | ||
* 1-2 -> 1--2 | ||
* 1,2,3 -> 1,2,3 | ||
* Invalid -> Invalid | ||
* </example> | ||
*/ | ||
public void formatPageNumbers() { | ||
final String field = "pages"; | ||
final Pattern pattern = Pattern.compile("\\A\\s*(\\d+)\\s*-{1,2}\\s*(\\d+)\\s*\\Z"); | ||
final String replace = "$1--$2"; | ||
|
||
String value = entry.getField(field); | ||
|
||
// nothing to do | ||
if (value == null || value.isEmpty()) { | ||
return; | ||
} | ||
|
||
Matcher matcher = pattern.matcher(value); | ||
// replace | ||
String newValue = matcher.replaceFirst(replace); | ||
|
||
// write field | ||
entry.setField(field, newValue); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/net/sf/jabref/logic/cleanup/AutoFormatterTest.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,76 @@ | ||
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 AutoFormatterTest { | ||
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 AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("1--2", entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersCommaSeparated() { | ||
entry.setField("pages", "1,2,3"); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("1,2,3", entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void ignoreWhitespaceInPageNumbers() { | ||
entry.setField("pages", " 1 - 2 "); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("1--2", entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void onlyFormatPageNumbersField() { | ||
entry.setField("otherfield", "1-2"); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("1-2", entry.getField("otherfield")); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersEmptyFields() { | ||
entry.setField("pages", ""); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("", entry.getField("pages")); | ||
|
||
entry.setField("pages", null); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals(null, entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersRegexNotMatching() { | ||
entry.setField("pages", "12"); | ||
new AutoFormatter(entry).formatPageNumbers(); | ||
|
||
Assert.assertEquals("12", entry.getField("pages")); | ||
} | ||
} |