Skip to content

Commit 4c9dc4d

Browse files
committed
Improve warning when formatting unknown macros; closes #49
1 parent 84b9bbd commit 4c9dc4d

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

R/format.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,27 @@ format.BibEntry <- function(x, style = .BibOptions$style,
4444
on.exit(close(con))
4545
## macro.env <- tools::loadRdMacros(file.path(R.home("share"), "Rd",
4646
## "macros", "system.Rd"))
47+
warnWithKey <- function(w){
48+
msg <- sub("^.*[[:alnum:]][:] ", "", w)
49+
msg <- paste0(y$key, ": ", msg)
50+
warning(msg, call. = FALSE)
51+
invokeRestart("muffleWarning")
52+
}
4753
if (getRversion() >= "3.3.0"){
4854
## !!! prevent use of devtools::system.file
4955
macro.env <- tools::loadPkgRdMacros(base::system.file(package =
5056
"RefManageR"))
51-
f(con, fragment = TRUE, out = out, outputEncoding = 'UTF-8',
52-
macros = macro.env, ...)
57+
withCallingHandlers(f(con, fragment = TRUE, out = out,
58+
outputEncoding = 'UTF-8', macros = macro.env,
59+
warningCalls = FALSE, ...),
60+
warning = warnWithKey)
61+
## suppressWarnings(f(con, fragment = TRUE, out = out,
62+
## outputEncoding = 'UTF-8', macros = macro.env, ...))
5363
}else
54-
f(con, fragment = TRUE, out = out, outputEncoding = 'UTF-8', ...)
55-
64+
withCallingHandlers(f(con, fragment = TRUE, out = out,
65+
outputEncoding = 'UTF-8', warningCalls = FALSE, ...),
66+
warning = warnWithKey)
67+
5668
paste(readLines(out, encoding = 'UTF-8'), collapse = "\n")
5769
}, "")
5870
if (style == "html"){

inst/NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
Changes in Version 1.2.0 (2018-04-24)
22
--------------------------------------------------------
33

4+
NEW FEATURES
5+
46
* `+.BibEntry` and `merge.BibEntry` gain an argument ignore.case,
57
which defaults to `BibOptions()$ignore.case` (`TRUE`) so that case is
68
ignore when checking for duplicate fields (h/t Justin Calabrese #47)
9+
* Improved warning message when printing entries with unknown
10+
LaTeX macros (the entry key is now included). (h/t Justin Calabrese #49)
711

812

913
Changes in Version 1.1.0 (2018-04-02)

tests/testthat/test-methods.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ test_that("open", {
9090
open(testbib, entry = 1) # eprint
9191
})
9292

93+
test_that("Warning for unknown macro includes key",
94+
{
95+
bib <- BibEntry(key = "testkey", bibtype = "misc", author = "Smith, John",
96+
title = "Title with \\badmacro", year = 2018)
97+
expect_warning(print(bib), "^testkey: unknown macro")
98+
})
99+
93100
test_that("c", {
94101
expect_length(c(bib[1], bib[2]), 2L)
95102
expect_error(c(bib[1], unlist(bib[2])))

0 commit comments

Comments
 (0)