Skip to content

Commit

Permalink
Add onSave action for pages fields #131
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Oct 27, 2015
1 parent d02db9d commit 80a071f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/net/sf/jabref/logic/cleanup/AutoFormatter.java
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 src/test/java/net/sf/jabref/logic/cleanup/AutoFormatterTest.java
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"));
}
}

0 comments on commit 80a071f

Please sign in to comment.