Skip to content

Commit

Permalink
Escape Rmarkdown errors before passing to cli (r-lib#2524)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeehio authored and SebKrantz committed Jun 1, 2024
1 parent 41a44c5 commit 84b055f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions R/rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =
callr::r_safe(rmarkdown_render_with_seed, args = args, show = !quiet),
error = function(cnd) {
lines <- strsplit(gsub("^\r?\n", "", cnd$stderr), "\r?\n")[[1]]
lines <- escape_cli(lines)
cli::cli_abort(
c(
"!" = "Failed to render {.path {input}}.",
Expand Down Expand Up @@ -97,6 +98,17 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =
invisible(path)
}

#' Escapes a cli msg
#'
#' Removes empty lines and escapes braces
#' @param msg A character vector with messages to be escaped
#' @noRd
escape_cli <- function(msg) {
msg <- msg[nchar(msg) >0]
msg <- gsub("{", "{{", msg, fixed = TRUE)
msg <- gsub("}", "}}", msg, fixed = TRUE)
msg
}

rmarkdown_render_with_seed <- function(..., seed = NULL) {
if (!is.null(seed)) {
Expand Down

0 comments on commit 84b055f

Please sign in to comment.