glue icon indicating copy to clipboard operation
glue copied to clipboard

Consider using doubling for escaping in `.literal = TRUE`

Open gaborcsardi opened this issue 3 years ago • 2 comments

I am wondering if .literal = TRUE should still allow double delimiters for escaping. I.e. this currently fails:

❯ glue::glue("{'{'}", .literal = TRUE)
Error in glue_data(.x = NULL, ..., .sep = .sep, .envir = .envir, .open = .open,  :
  Expecting '}'

and AFAICT there is no easy way of writing a (non well formed) literal brace in a glue string, apart from using substitution:

❯ glue::glue("{'{{'}", .literal = TRUE)
Error in glue_data(.x = NULL, ..., .sep = .sep, .envir = .envir, .open = .open,  :
  Expecting '}'

As you might suspect this affects cli, because cli uses doubling as escaping currently, so it cannot switch to .literal = TRUE. On the other hand .literal = TRUE would be much better for the cases when people include literal quotes in the string, e.g. https://github.com/r-lib/cli/issues/370

If doubling is still accepted for escaping, then .literal = TRUE does not break cli, and I think it is also beneficial in general to be able to escape the delimiter.

gaborcsardi avatar Feb 10 '22 12:02 gaborcsardi

Can you point me to some of the cli revdep failures that inspired this? I realize you've given minimal examples above, but some even more realistic ones would be helpful for tackling this. So what sort of glue expressions are we talking about?

jennybc avatar Feb 10 '22 17:02 jennybc

It is this from rlang:

mark_emph("{foo {}")
#> [1] "{.emph {\"{foo {}\"}}"

cli::format_message(mark_emph("{foo {}"))
#> Error in glue_data(.x = NULL, ..., .sep = .sep, .envir = .envir, .open = .open,  :
#>   Expecting '}'

But I am not concerned about the revdep failures too much. I am more concerned that with .literal = TRUE we cannot write (non well-formed) brace literals any more.

gaborcsardi avatar Feb 10 '22 17:02 gaborcsardi

Closing since it looks like you handled this in cli.

hadley avatar Jan 26 '23 21:01 hadley