Skip to content

Commit 64a389e

Browse files
committed
Fix quotes and comment charactes in glue
Closes #383.
1 parent 979e2f4 commit 64a389e

File tree

6 files changed

+87
-6
lines changed

6 files changed

+87
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ BugReports: https://github.com/r-lib/cli/issues
1919
RoxygenNote: 7.1.2.9000
2020
Depends: R (>= 2.10)
2121
Imports:
22-
glue,
22+
glue (>= 1.6.0),
2323
utils
2424
Suggests:
2525
asciicast,

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
* You can set the new `ESS_BACKGROUND_MODE` environment variable to
3333
`dark` to indicate dark mode.
3434

35+
* cli now handles quotes and comment characters better in the semantion
36+
`cli_*()` functions that perform glue string interpolation (#383).
37+
3538
# cli 3.1.1
3639

3740
* `style_hyperlink()` gains a `params=` argument (#384).

R/inline.R

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ inline_transformer <- function(code, envir) {
136136
.transformer = inline_transformer,
137137
.open = paste0("{", envir$marker),
138138
.close = paste0(envir$marker, "}"),
139-
.trim = TRUE
139+
.trim = TRUE,
140+
.literal = TRUE
140141
)
141142

142143
# If we don't have a brace expression, then (non-inherited) styling was
@@ -216,7 +217,8 @@ clii__inline <- function(app, text, .list) {
216217
.transformer = inline_transformer,
217218
.open = paste0("{", t$values$marker),
218219
.close = paste0(t$values$marker, "}"),
219-
.trim = TRUE
220+
.trim = TRUE,
221+
.literal = TRUE
220222
)
221223
})
222224
paste(out, collapse = "")
@@ -262,7 +264,13 @@ make_cmd_transformer <- function(values) {
262264
funname <- captures[[1]]
263265
text <- captures[[2]]
264266

265-
out <- glue::glue(text, .envir = envir, .transformer = sys.function(), .trim = TRUE)
267+
out <- glue::glue(
268+
text,
269+
.envir = envir,
270+
.transformer = sys.function(),
271+
.trim = TRUE,
272+
.literal = TRUE
273+
)
266274
paste0("{", values$marker, ".", funname, " ", out, values$marker, "}")
267275
}
268276
}
@@ -272,7 +280,13 @@ glue_cmd <- function(..., .envir) {
272280
str <- paste0(unlist(list(...), use.names = FALSE), collapse = "")
273281
values <- new.env(parent = emptyenv())
274282
transformer <- make_cmd_transformer(values)
275-
pstr <- glue::glue(str, .envir = .envir, .transformer = transformer, .trim = TRUE)
283+
pstr <- glue::glue(
284+
str,
285+
.envir = .envir,
286+
.transformer = transformer,
287+
.trim = TRUE,
288+
.literal = TRUE
289+
)
276290
glue_delay(
277291
str = post_process_plurals(pstr, values),
278292
values = values

R/pluralize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,6 @@ pluralize <- function(..., .envir = parent.frame(),
183183
}
184184
}
185185

186-
raw <- glue::glue(..., .envir = .envir, .transformer = tf)
186+
raw <- glue::glue(..., .envir = .envir, .transformer = tf, .literal = TRUE)
187187
post_process_plurals(raw, values)
188188
}

tests/testthat/_snaps/glue.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# glue quotes and comments
2+
3+
Code
4+
cli_dl(c(test_1 = "all good", test_2 = "not #good"))
5+
Message <cliMessage>
6+
test_1: all good
7+
test_2: not #good
8+
Code
9+
cli::cli_dl(c(test_3 = "no' good either"))
10+
Message <cliMessage>
11+
test_3: no' good either
12+
Code
13+
cli::cli_dl(c(test_4 = "no\" good also"))
14+
Message <cliMessage>
15+
test_4: no" good also
16+
Code
17+
cli::cli_text("{.url https://example.com/#section}")
18+
Message <cliMessage>
19+
<https://example.com/#section>
20+
Code
21+
cli::cli_alert_success("Qapla'")
22+
Message <cliMessage>
23+
v Qapla'
24+
25+
# quotes, etc. within expressions are still OK
26+
27+
Code
28+
cli::cli_text("{.url URL} {x <- 'foo'; nchar(x)}")
29+
Message <cliMessage>
30+
<URL> 3
31+
Code
32+
cli::cli_text("{.url URL} {x <- \"foo\"; nchar(x)}")
33+
Message <cliMessage>
34+
<URL> 3
35+
Code
36+
cli::cli_text("{.url URL} {1 + 1 # + 1} {1 + 1}")
37+
Message <cliMessage>
38+
<URL> 2 2
39+

tests/testthat/test-glue.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# https://github.com/r-lib/cli/issues/370
3+
4+
test_that("glue quotes and comments", {
5+
expect_snapshot({
6+
cli_dl(
7+
c(
8+
"test_1" = "all good",
9+
"test_2" = "not #good"
10+
)
11+
)
12+
cli::cli_dl(c("test_3" = "no' good either"))
13+
cli::cli_dl(c("test_4" = "no\" good also"))
14+
cli::cli_text("{.url https://example.com/#section}")
15+
cli::cli_alert_success("Qapla'")
16+
})
17+
})
18+
19+
test_that("quotes, etc. within expressions are still OK", {
20+
expect_snapshot({
21+
cli::cli_text("{.url URL} {x <- 'foo'; nchar(x)}")
22+
cli::cli_text("{.url URL} {x <- \"foo\"; nchar(x)}")
23+
cli::cli_text("{.url URL} {1 + 1 # + 1} {1 + 1}")
24+
})
25+
})

0 commit comments

Comments
 (0)