Skip to content

Commit

Permalink
Fix quotes and comment charactes in glue
Browse files Browse the repository at this point in the history
Closes #383.
  • Loading branch information
gaborcsardi committed Feb 9, 2022
1 parent 979e2f4 commit 64a389e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BugReports: https://github.com/r-lib/cli/issues
RoxygenNote: 7.1.2.9000
Depends: R (>= 2.10)
Imports:
glue,
glue (>= 1.6.0),
utils
Suggests:
asciicast,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* You can set the new `ESS_BACKGROUND_MODE` environment variable to
`dark` to indicate dark mode.

* cli now handles quotes and comment characters better in the semantion
`cli_*()` functions that perform glue string interpolation (#383).

# cli 3.1.1

* `style_hyperlink()` gains a `params=` argument (#384).
Expand Down
22 changes: 18 additions & 4 deletions R/inline.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ inline_transformer <- function(code, envir) {
.transformer = inline_transformer,
.open = paste0("{", envir$marker),
.close = paste0(envir$marker, "}"),
.trim = TRUE
.trim = TRUE,
.literal = TRUE
)

# If we don't have a brace expression, then (non-inherited) styling was
Expand Down Expand Up @@ -216,7 +217,8 @@ clii__inline <- function(app, text, .list) {
.transformer = inline_transformer,
.open = paste0("{", t$values$marker),
.close = paste0(t$values$marker, "}"),
.trim = TRUE
.trim = TRUE,
.literal = TRUE
)
})
paste(out, collapse = "")
Expand Down Expand Up @@ -262,7 +264,13 @@ make_cmd_transformer <- function(values) {
funname <- captures[[1]]
text <- captures[[2]]

out <- glue::glue(text, .envir = envir, .transformer = sys.function(), .trim = TRUE)
out <- glue::glue(
text,
.envir = envir,
.transformer = sys.function(),
.trim = TRUE,
.literal = TRUE
)
paste0("{", values$marker, ".", funname, " ", out, values$marker, "}")
}
}
Expand All @@ -272,7 +280,13 @@ glue_cmd <- function(..., .envir) {
str <- paste0(unlist(list(...), use.names = FALSE), collapse = "")
values <- new.env(parent = emptyenv())
transformer <- make_cmd_transformer(values)
pstr <- glue::glue(str, .envir = .envir, .transformer = transformer, .trim = TRUE)
pstr <- glue::glue(
str,
.envir = .envir,
.transformer = transformer,
.trim = TRUE,
.literal = TRUE
)
glue_delay(
str = post_process_plurals(pstr, values),
values = values
Expand Down
2 changes: 1 addition & 1 deletion R/pluralize.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ pluralize <- function(..., .envir = parent.frame(),
}
}

raw <- glue::glue(..., .envir = .envir, .transformer = tf)
raw <- glue::glue(..., .envir = .envir, .transformer = tf, .literal = TRUE)
post_process_plurals(raw, values)
}
39 changes: 39 additions & 0 deletions tests/testthat/_snaps/glue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# glue quotes and comments

Code
cli_dl(c(test_1 = "all good", test_2 = "not #good"))
Message <cliMessage>
test_1: all good
test_2: not #good
Code
cli::cli_dl(c(test_3 = "no' good either"))
Message <cliMessage>
test_3: no' good either
Code
cli::cli_dl(c(test_4 = "no\" good also"))
Message <cliMessage>
test_4: no" good also
Code
cli::cli_text("{.url https://example.com/#section}")
Message <cliMessage>
<https://example.com/#section>
Code
cli::cli_alert_success("Qapla'")
Message <cliMessage>
v Qapla'

# quotes, etc. within expressions are still OK

Code
cli::cli_text("{.url URL} {x <- 'foo'; nchar(x)}")
Message <cliMessage>
<URL> 3
Code
cli::cli_text("{.url URL} {x <- \"foo\"; nchar(x)}")
Message <cliMessage>
<URL> 3
Code
cli::cli_text("{.url URL} {1 + 1 # + 1} {1 + 1}")
Message <cliMessage>
<URL> 2 2

25 changes: 25 additions & 0 deletions tests/testthat/test-glue.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# https://github.com/r-lib/cli/issues/370

test_that("glue quotes and comments", {
expect_snapshot({
cli_dl(
c(
"test_1" = "all good",
"test_2" = "not #good"
)
)
cli::cli_dl(c("test_3" = "no' good either"))
cli::cli_dl(c("test_4" = "no\" good also"))
cli::cli_text("{.url https://example.com/#section}")
cli::cli_alert_success("Qapla'")
})
})

test_that("quotes, etc. within expressions are still OK", {
expect_snapshot({
cli::cli_text("{.url URL} {x <- 'foo'; nchar(x)}")
cli::cli_text("{.url URL} {x <- \"foo\"; nchar(x)}")
cli::cli_text("{.url URL} {1 + 1 # + 1} {1 + 1}")
})
})

0 comments on commit 64a389e

Please sign in to comment.