Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to harden the formatting of URLs? (issue with # encoding) #357

Closed
mjsteinbaugh opened this issue Sep 24, 2021 · 4 comments
Closed

How to harden the formatting of URLs? (issue with # encoding) #357

mjsteinbaugh opened this issue Sep 24, 2021 · 4 comments

Comments

@mjsteinbaugh
Copy link

I came across an edge case where some URLs are not rendering as expected.

Here's a minimal reprex:

## From googlesheets4 vignette:
## https://googlesheets4.tidyverse.org/articles/googlesheets4.html

url <- "https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077"

cli::cli_alert(sprintf("Downloading '%s'.", url))
## → Downloading 'https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077'.

cli::cli_alert(sprintf("Downloading {.file %s}.", url))
## → Downloading

cli::cli_alert(sprintf("Downloading {.url %s}.", url))
## → Downloading

cli::cli_alert(sprintf("Downloading {.val %s}.", url))
## → Downloading

cli::cli_alert(sprintf("Downloading {.var %s}.", url))
## → Downloading

Is there a way to harden the encoding of URLs to work better with the cli package?

@mjsteinbaugh
Copy link
Author

mjsteinbaugh commented Sep 24, 2021

It's the # character that's the problem, of course. Removal of this works, but I can't figure out the correct way to escape/encode:

cli::cli_alert(sprintf(
    fmt = "Downloading {.url %s}.",
    gsub(
        pattern = "#",
        replacement = "",
        x = url,
        fixed = TRUE
    )
))
## → Downloading <https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/editgid=780868077>.

cli::cli_alert(sprintf(
    fmt = "Downloading {.url %s}.",
    gsub(
        pattern = "#",
        replacement = "\\#",
        x = url,
        fixed = TRUE
    )
))
## → Downloading

@mjsteinbaugh mjsteinbaugh changed the title How to harden the formatting of URLs? How to harden the formatting of URLs? (issue with # encoding) Sep 24, 2021
@mjsteinbaugh
Copy link
Author

mjsteinbaugh commented Sep 24, 2021

Here's a simpler reprex that's the main issue:

cli::cli_alert("#")
## → #

cli::cli_alert("{.val #}")
## →

@gaborcsardi
Copy link
Member

This is a bug in glue: tidyverse/glue#224

But you don't need sprintf() in cli, it supports substitution natively:

url <- "https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077"
cli::cli_alert("Downloading {.url {url}}.")
#> → Downloading <https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077>.

@gaborcsardi
Copy link
Member

This is now "fixed" in dev glue, but unterminated # characters within {} will be errors now. (tidyverse/glue#227)

I am closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants