From 94422a649b140a82f5c75afdb5f1811169029ec9 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 22 Dec 2022 23:47:50 -0600 Subject: [PATCH] amend 973ed84ead38aad22896712cec6dd019ef4c2252: store the original marked up code in `code.src` when paring 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() --- DESCRIPTION | 2 +- R/block.R | 4 ++++ R/hooks.R | 4 +--- R/parser.R | 12 +++++++----- R/template.R | 6 +++--- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c8c590636b..381536a60d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Abhraneel", "Sarma", role = "ctb"), diff --git a/R/block.R b/R/block.R index 96ed20a924..a53949b2d9 100644 --- a/R/block.R +++ b/R/block.R @@ -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) { diff --git a/R/hooks.R b/R/hooks.R index d63e5f9ff2..d0410167ac 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -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 diff --git a/R/parser.R b/R/parser.R index d56dd12484..e7b8efafe4 100644 --- a/R/parser.R +++ b/R/parser.R @@ -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' ) } diff --git a/R/template.R b/R/template.R index db4e51d84e..77221942e3 100644 --- a/R/template.R +++ b/R/template.R @@ -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) }