From 58b61da9c65d3881a93bda0c30714d1fe480464f Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Thu, 9 May 2024 16:18:41 -0600 Subject: [PATCH] Escape Rmarkdown errors before passing to cli (#2524) --- R/rmarkdown.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/rmarkdown.R b/R/rmarkdown.R index 7b1e7e720..c1bc552a9 100644 --- a/R/rmarkdown.R +++ b/R/rmarkdown.R @@ -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}}.", @@ -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)) {