From edd53f02f7498c9fa0bcf7e40dc0e24c836c0a57 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Tue, 11 Sep 2018 12:48:37 -0700 Subject: [PATCH] Introduce prose() function For now, it prepends `#'` but in a way that might hold up if we use Rmd for the template. --- R/reprex-undo.R | 16 ++++++---------- R/reprex.R | 6 +----- R/utils.R | 4 ++++ R/whisker.R | 13 ++++++++----- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/R/reprex-undo.R b/R/reprex-undo.R index 0a59e7b5..a1b61b5c 100644 --- a/R/reprex-undo.R +++ b/R/reprex-undo.R @@ -149,15 +149,15 @@ reprex_undo <- function(input = NULL, src <- switch( where, clipboard = ingest_clipboard(), - path = read_lines(input), - input = escape_newlines(sub("\n$", "", input)), + path = read_lines(input), + input = escape_newlines(sub("\n$", "", input)), NULL ) comment <- arg_option(comment) + outfile_given <- !is.null(outfile) infile <- if (where == "path") input else NULL - outfile_requested <- !is.null(outfile) - if (outfile_requested) { + if (outfile_given) { files <- make_filenames(make_filebase(outfile, infile), suffix = "clean") r_file <- files[["r_file"]] if (would_clobber(r_file)) { @@ -171,11 +171,7 @@ reprex_undo <- function(input = NULL, } else { line_info <- classify_lines(src, comment = comment) } - x_out <- ifelse( - line_info == "prose" & nzchar(src), - paste("#'", src), - src - ) + x_out <- ifelse(line_info == "prose" & nzchar(src), prose(src), src) x_out <- x_out[!line_info %in% c("output", "bt", "so_header") & nzchar(src)] x_out <- sub("^ ", "", x_out) } else if (is.null(prompt)) { ## reprex_clean @@ -190,7 +186,7 @@ reprex_undo <- function(input = NULL, clipr::write_clip(x_out) message("Clean code is on the clipboard.") } - if (outfile_requested) { + if (outfile_given) { writeLines(x_out, r_file) message("Writing clean code as R script:\n * ", r_file) } diff --git a/R/reprex.R b/R/reprex.R index 019e2975..546d66c3 100644 --- a/R/reprex.R +++ b/R/reprex.R @@ -394,11 +394,7 @@ reprex_render <- function(input, std_out_err = NULL) { convert_md_to_r <- function(lines, comment = "#>") { line_info <- classify_lines_bt(lines, comment = comment) - lines <- ifelse( - line_info == "prose" & nzchar(lines), - paste("#'", lines), - lines - ) + lines <- ifelse(line_info == "prose" & nzchar(lines), prose(lines), lines) lines[line_info != "bt"] } diff --git a/R/utils.R b/R/utils.R index 3bfd79d9..bbc4aa1a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -85,3 +85,7 @@ inject_file <- function(path, inject_path, pre_process = enfence, ...) { writeLines(lines, path) path } + +prose <- function(x) { + paste0("#' ", x) +} diff --git a/R/whisker.R b/R/whisker.R index 08f1148e..ed7ce372 100644 --- a/R/whisker.R +++ b/R/whisker.R @@ -8,7 +8,10 @@ apply_template <- function(x, reprex_data = NULL) { )) if (!is.null(reprex_data$std_file)) { - data$std_file_stub <- paste0("#' `", reprex_data$std_file, "`\n#'") + data$std_file_stub <- prose(c( + encodeString(reprex_data$std_file, quote = "`"), + "\n" + )) } if (isTRUE(reprex_data$si)) { @@ -20,13 +23,13 @@ apply_template <- function(x, reprex_data = NULL) { } if (reprex_data$venue == "gh") { - data$si_start <- "#'
Session info" - data$si_end <- "#'
" + data$si_start <- prose("
Session info") + data$si_end <- prose("
") } if (reprex_data$venue == "so") { data$yaml <- yaml_md("md") - data$so_syntax_highlighting <- "#'" + data$so_syntax_highlighting <- prose("") ## empty line between html comment re: syntax highlighting and reprex code x <- c("", x) } @@ -73,5 +76,5 @@ yaml_md <- function(flavor = c("gfm", "md"), ) ## prepend with `#' ` in a separate step because ## https://github.com/klutometis/roxygen/issues/668 - paste0("#' ", yaml, collapse = "\n") + paste0(prose(yaml), collapse = "\n") }