Skip to content

Commit

Permalink
Introduce prose() function
Browse files Browse the repository at this point in the history
For now, it prepends `#'` but in a way that might hold up if we use Rmd for the template.
  • Loading branch information
jennybc committed Sep 11, 2018
1 parent 6f82ada commit edd53f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
16 changes: 6 additions & 10 deletions R/reprex-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions R/reprex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ inject_file <- function(path, inject_path, pre_process = enfence, ...) {
writeLines(lines, path)
path
}

prose <- function(x) {
paste0("#' ", x)
}
13 changes: 8 additions & 5 deletions R/whisker.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -20,13 +23,13 @@ apply_template <- function(x, reprex_data = NULL) {
}

if (reprex_data$venue == "gh") {
data$si_start <- "#'<details><summary>Session info</summary>"
data$si_end <- "#'</details>"
data$si_start <- prose("<details><summary>Session info</summary>")
data$si_end <- prose("</details>")
}

if (reprex_data$venue == "so") {
data$yaml <- yaml_md("md")
data$so_syntax_highlighting <- "#'<!-- language-all: lang-r -->"
data$so_syntax_highlighting <- prose("<!-- language-all: lang-r -->")
## empty line between html comment re: syntax highlighting and reprex code
x <- c("", x)
}
Expand Down Expand Up @@ -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")
}

0 comments on commit edd53f0

Please sign in to comment.