Skip to content

Commit

Permalink
Fall back to literal string if page field contains an illegal number
Browse files Browse the repository at this point in the history
Fixes #114
  • Loading branch information
michel-kraemer committed Jan 5, 2022
1 parent 2c6a4de commit c12a33c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public static PageRange parse(String pages) {
CommonTokenStream tokens = new CommonTokenStream(lexer);
InternalPageParser parser = new InternalPageParser(tokens);
parser.removeErrorListeners(); // do not output errors to console
PagesContext ctx = parser.pages();
if (ctx.literal == null || ctx.literal.isEmpty() ||
PagesContext ctx;
try {
ctx = parser.pages();
} catch (NumberFormatException e) {
ctx = null;
}
if (ctx == null || ctx.literal == null || ctx.literal.isEmpty() ||
ctx.exception != null || parser.getNumberOfSyntaxErrors() > 0) {
// unparsable fall back to literal string
return new PageRange(pages, null, null, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,29 @@ public void inBookSeries() throws ParseException {
assertEquals("978-3-030-83014-4", item.getISBN());
assertEquals("10.1007/978-3-030-83014-4_2", item.getDOI());
}

/**
* Check if the parser correctly falls back to a literal string if the
* 'pages' field contains an illegal number.
* See https://github.com/michel-kraemer/citeproc-java/issues/114
*/
@Test
public void invalidPageNumber() throws ParseException {
String entry = "@Article{baks-2021,\n" +
" author = {Sandipan Baksi},\n" +
" date = {2021},\n" +
" journaltitle = {The Indian Economic {\\&} Social History Review},\n" +
" pages = {001946462110645},\n" +
" title = {Science journalism in Hindi in pre-independence India: A study of Hindi periodicals},\n" +
" doi = {10.1177/00194646211064586},\n" +
" creationdate = {2022-01-03T11:59:38},\n" +
" modificationdate = {2022-01-03T12:01:49},\n" +
" publisher = {{SAGE} Publications},\n" +
"}";
BibTeXDatabase db = new BibTeXParser().parse(new StringReader(entry));
BibTeXConverter converter = new BibTeXConverter();
Map<String, CSLItemData> items = converter.toItemData(db);
CSLItemData item = items.get("baks-2021");
assertEquals("001946462110645", item.getPage());
}
}

0 comments on commit c12a33c

Please sign in to comment.