glue
glue copied to clipboard
Consider using doubling for escaping in `.literal = TRUE`
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.
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?
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.
Closing since it looks like you handled this in cli.