From 09a0d5dcf16ad3c01d08f22b44ced8a62586fcfe Mon Sep 17 00:00:00 2001 From: "Mathew W. McLean" Date: Tue, 24 Apr 2018 19:00:13 +1000 Subject: [PATCH] Add key when warning for missing fields; closes #48 --- R/04InternalFunctions.R | 40 ++++++++++++++++++++++++---------------- inst/NEWS.md | 7 +++++++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/R/04InternalFunctions.R b/R/04InternalFunctions.R index e51a0e9..d3cc213 100755 --- a/R/04InternalFunctions.R +++ b/R/04InternalFunctions.R @@ -14,20 +14,20 @@ if (length(rfields) > 0L) { ok <- vapply(rfields, function(f) any(f %in% fields), FALSE) if (any(!ok)){ - if (check == 'warn'){ - warning(sprintf(ngettext(sum(!ok), - "A bibentry of bibtype %s has to specify the field: %s", + key <- attr(x, "key") + if (is.null(key)) + key <- "" + else + key <- paste0(key, ": ") + msg <- sprintf(ngettext(sum(!ok), + "%sA bibentry of bibtype %s has to specify the field: %s", "A bibentry of bibtype %s has to specify the fields: %s"), - sQuote(bibtype), paste(rfields[!ok], collapse = ", ")), - domain = NA) + key, sQuote(bibtype), paste(rfields[!ok], collapse = ", ")) + if (check == 'warn'){ + warning(msg, domain = NA, call. = FALSE) return(NULL) - }else{ - stop(sprintf(ngettext(sum(!ok), - "A bibentry of bibtype %s has to specify the field: %s", - "A bibentry of bibtype %s has to specify the fields: %s"), - sQuote(bibtype), paste(rfields[!ok], collapse = ", ")), - domain = NA) - } + }else + stop(msg, domain = NA) } } } @@ -940,14 +940,22 @@ MakeBibEntry <- function(x, to.person = TRUE){ if (type != 'set') tdate <- ProcessDates(y) - tryCatch(BibEntry(bibtype = type, key = key, dateobj = tdate, other = y), + withCallingHandlers(tryCatch(BibEntry(bibtype = type, key = key, + dateobj = tdate, other = y), error = function(e){ - message(sprintf("Ignoring entry '%s' %sbecause:\n\t%s\n", + message(sprintf("Ignoring entry '%s' %sbecause:\n\t%s", key, line.no, - conditionMessage(e))) + conditionMessage(e)), domain = NA) NULL - }) + }), + warning = function(w){ + warning(sprintf("%s %s:\n\t%s", + key, + line.no, + conditionMessage(w)), domain = NA, call. = FALSE) + invokeRestart("muffleWarning") + }) } #' @keywords internal diff --git a/inst/NEWS.md b/inst/NEWS.md index 63b8c68..811b44c 100755 --- a/inst/NEWS.md +++ b/inst/NEWS.md @@ -8,7 +8,14 @@ which defaults to `BibOptions()$ignore.case` (`TRUE`) so that case is ignore when checking for duplicate fields (h/t Justin Calabrese #47) * Improved warning message when printing entries with unknown LaTeX macros (the entry key is now included). (h/t Justin Calabrese #49) +* The entry key is now included in warning messages when entries are +missing fields and `BibOptions()$check.entries == "warn"` (h/t Justin +Calabrese #48) +BUG FIXES + +* Entries are now only checked once to ensure all required + fields are present in `ReadBib` Changes in Version 1.1.0 (2018-04-02) --------------------------------------------------------