Skip to content

Commit

Permalink
amend 973ed84: store the original marked up code in code.src when p…
Browse files Browse the repository at this point in the history
…aring inline code; when the code can't be parsed, use `code.src` instead of reconstructing it from inline_expr()

also move the error message out of the `evaluate.inline` hook, and signal the error earlier in inline_exec()
  • Loading branch information
yihui committed Dec 23, 2022
1 parent 1545a7e commit 94422a6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.41.6
Version: 1.41.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
4 changes: 4 additions & 0 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,13 @@ inline_exec = function(
# run inline code and substitute original texts
code = block$code; input = block$input
if ((n <- length(code)) == 0) return(input) # untouched if no code is found
code.src = block$code.src

ans = character(n)
for (i in 1:n) {
tryCatch(parse_only(code[i]), error = function(e) {
stop2('Failed to parse the inline R code: ', code.src[i], '\nReason: ', e$message)
})
res = hook_eval(code[i], envir)
if (inherits(res, c('knit_asis', 'knit_asis_url'))) res = sew(res, inline = TRUE)
tryCatch(as.character(res), error = function(e) {
Expand Down
4 changes: 1 addition & 3 deletions R/hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
message = .out.hook, error = .out.hook, plot = .plot.hook,
inline = .inline.hook, chunk = .out.hook, text = identity,
evaluate.inline = function(code, envir = knit_global()) {
v = withVisible(eval(tryCatch(parse_only(code), error = function(e) {
stop2('Failed to parse the inline R code: ', inline_expr(code), '\nReason: ', e$message)
}), envir = envir))
v = withVisible(eval(parse_only(code), envir = envir))
if (v$visible) knit_print(v$value, inline = TRUE, options = opts_chunk$get())
},
evaluate = function(...) evaluate::evaluate(...), document = identity
Expand Down
12 changes: 7 additions & 5 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,17 @@ parse_inline = function(input, patterns) {

loc = cbind(start = numeric(0), end = numeric(0))
if (group_pattern(inline.code)) loc = str_locate(input, inline.code)[[1]]
code1 = code2 = character()
if (nrow(loc)) {
code = t(str_match(input, inline.code))
code = if (NCOL(code) >= 2L) {
apply(code[, -1L, drop = FALSE], 1, paste, collapse = '')
} else character(0)
} else code = character(0)
if (NCOL(code) >= 2L) {
code1 = code[, 1L]
code2 = apply(code[, -1L, drop = FALSE], 1, paste, collapse = '')
}
}

structure(
list(input = input, location = loc, code = code),
list(input = input, location = loc, code = code2, code.src = code1),
class = 'inline'
)
}
Expand Down
6 changes: 3 additions & 3 deletions R/template.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ knit_expand = function(file, ..., text = read_utf8(file), delim = c('{{', '}}')
loc = str_locate(txt, delim)[[1L]]
if (nrow(loc) == 0L) return(txt) # no match
mat = str_extract(txt, delim)[[1L]]
mat = sub(delim, '\\1', mat)
env = list(...)
env = if (length(env)) list2env(env, parent = parent.frame()) else parent.frame()
inline_exec(list(code = mat, input = txt, location = loc),
envir = env, hook = identity)
inline_exec(list(
code = sub(delim, '\\1', mat), code.src = mat, input = txt, location = loc
), envir = env, hook = identity)
}

0 comments on commit 94422a6

Please sign in to comment.