Skip to content

Commit

Permalink
Refine special pages handling
Browse files Browse the repository at this point in the history
Co-authored-by: ThiloteE <73715071+ThiloteE@users.noreply.github.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 28, 2022
1 parent d148a54 commit 8172766
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ private static CSLItemData bibEntryToCSLItemData(BibEntry originalBibEntry, BibD
bibEntry.getField(StandardField.PAGES).ifPresent(pages -> {
if (new PagesChecker(bibDatabaseContext).checkValue(pages).isPresent()) {
// pages field contains no valid pages range
if (!pages.startsWith("Article")) {
bibEntry.setField(StandardField.PAGES, "Article " + pages);
}
bibEntry.setField(StandardField.NUMBER, pages);
bibEntry.clearField(StandardField.PAGES);
}
});
}
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/org/jabref/logic/integrity/PagesChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
public class PagesChecker implements ValueChecker {

private static final String PAGES_EXP_BIBTEX = ""
+ "\\A" // begin String
+ "("
+ "[A-Za-z]?\\d*" // optional prefix and number
+ "\\A" // begin String
+ "[A-Za-z]?\\d*" // optional prefix and number
+ "("
+ "\\+|-{2}" // separator
+ "[A-Za-z]?\\d*" // optional prefix and number
+ "(\\+|-{2}|\u2013)" // separator, must contain exactly two dashes
+ "[A-Za-z]?\\d*" // optional prefix and number
+ ")?"
+ ",?" // page range separation
+ ")*"
+ "\\z"; // end String
+ "\\z"; // end String

// See https://packages.oth-regensburg.de/ctan/macros/latex/contrib/biblatex/doc/biblatex.pdf#subsubsection.3.15.3 for valid content
private static final String PAGES_EXP_BIBLATEX = ""
Expand Down Expand Up @@ -58,9 +55,7 @@ public Optional<String> checkValue(String value) {
return Optional.empty();
}

String[] split = value.split(",");

if (Arrays.stream(split)
if (Arrays.stream(value.split(","))
.map(String::trim)
.anyMatch(pageRange -> !isValidPageNumber.test(pageRange))) {
return Optional.of(Localization.lang("should contain a valid page number range"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void bibTexAcceptsOnePageNumber() {
assertEquals(Optional.empty(), checker.checkValue("12"));
}

@Test
void rejectsHexNumber() {
assertNotEquals(Optional.empty(), checker.checkValue("777e23"));
}

@Test
void bibTexDoesNotAcceptRangeOfNumbersWithSingleDash() {
assertNotEquals(Optional.empty(), checker.checkValue("1-2"));
Expand Down

0 comments on commit 8172766

Please sign in to comment.