Skip to content

Instantly share code, notes, and snippets.

@jthomasmock
Last active May 10, 2022 13:26
Show Gist options
  • Save jthomasmock/85f6305b0d6ed1b045ac9e54d0f333f2 to your computer and use it in GitHub Desktop.
Save jthomasmock/85f6305b0d6ed1b045ac9e54d0f333f2 to your computer and use it in GitHub Desktop.
gist_to_carbon <- function(gist_id, file = "code.png", bg = "#4A90E2",
theme = "shades-of-purple", font = "Fira+Code",
lang = "auto", imgur = TRUE) {
fonts <- c("IBM+Plex+Mono", "Hack", "Fira+Code", "Source+Code+Pro")
langs <- c("python", "r", "yaml", "markdown", "text", "auto")
themes <- c("cobalt", "nord", "seti", "night-owl", "monokai", "material", "vscode", "verminal", "synthwave-84", "shades-of-purple")
if (!(nchar(bg) == 7 && grepl("#", bg))) stop("The background must be a 6 unit hex value preceded by #, like #4A90E2", call. = FALSE)
if (!(lang %in% langs)) stop(paste("Language must be one of", langs), call. = FALSE)
if (!(theme %in% themes)) stop(paste("Theme must be one of", themes), call. = FALSE)
if (!(font %in% fonts)) stop(paste("Font must be one of", fonts), call. = FALSE)
bcol <- col2rgb(bg)
bg_txt <- glue::glue("rgba%28{bcol[1]}%2C{bcol[2]}%2C{bcol[3]}%2C{1}%29")
carbon_query <- glue::glue("bg={bg_txt}&t={theme}&fm={font}&lang={r}")
carbon_url <- glue::glue("https://carbon.now.sh/embed/{gist_id}?{carbon_query}")
# save to disk
webshot2::webshot(url = carbon_url, file = file, zoom = 3)
# upload to imgur
if (imgur) {
imgur_url <- as.character(knitr::imgur_upload(img))
return(imgur_url)
}
}
# https://gist.github.com/85f6305b0d6ed1b045ac9e54d0f333f2
# Code image at: ![](https://i.imgur.com/rXkoukl.png)
@jthomasmock
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment