| colorspace, | colorspace, | ||||
| glue, | glue, | ||||
| purrr, | purrr, | ||||
| stringr, | |||||
| tibble, | tibble, | ||||
| utils, | utils, | ||||
| whisker | whisker |
| xaringan_coolors_accent <- function(coolors_url, order = "12345", ...) { | xaringan_coolors_accent <- function(coolors_url, order = "12345", ...) { | ||||
| stopifnot(length(coolors_url) == 1) | stopifnot(length(coolors_url) == 1) | ||||
| colors <- coolors2colors(coolors_url) | colors <- coolors2colors(coolors_url) | ||||
| order <- stringr::str_split(order, "")[1] | |||||
| order <- strplit(order, "")[1] | |||||
| order <- unlist(purrr::map(order, as.integer)) | order <- unlist(purrr::map(order, as.integer)) | ||||
| colors <- colors[order] | colors <- colors[order] | ||||
| } | } | ||||
| coolors2colors <- function(url) { | coolors2colors <- function(url) { | ||||
| colors <- stringr::str_extract(url, "([a-f0-9]{6}-?){5}") | |||||
| colors <- stringr::str_split(colors, "-") | |||||
| colors <- sub(".*(([a-f0-9]{6}-?){5}).*", "\\1", url) | |||||
| colors <- strsplit(colors, "-") | |||||
| colors <- purrr::map(colors, ~ paste0("#", .)) | colors <- purrr::map(colors, ~ paste0("#", .)) | ||||
| if (length(colors) == 1) colors[[1]] | if (length(colors) == 1) colors[[1]] | ||||
| else colors | else colors |
| if (!inherits(gf, "google_font")) stop( | if (!inherits(gf, "google_font")) stop( | ||||
| "`", var, "` must be set using `google_font()`." | "`", var, "` must be set using `google_font()`." | ||||
| ) | ) | ||||
| group <- stringr::str_split(var, "_")[[1]][1] | |||||
| group <- strsplit(var, "_")[[1]][1] | |||||
| if (group == "text") { | if (group == "text") { | ||||
| text_font_family <- gf$family | text_font_family <- gf$family | ||||
| text_font_weight <- gf$weights %||% "normal" | text_font_weight <- gf$weights %||% "normal" |
| weights = weights, | weights = weights, | ||||
| languages = languages, | languages = languages, | ||||
| url = paste0( | url = paste0( | ||||
| base, stringr::str_replace_all(family, " ", "+"), | |||||
| base, gsub(" ", "+", family), | |||||
| if (!is.null(weights)) paste0(":", weights), | if (!is.null(weights)) paste0(":", weights), | ||||
| if (!is.null(languages)) paste0("&subset=", languages) | if (!is.null(languages)) paste0("&subset=", languages) | ||||
| ) | ) | ||||
| } | } | ||||
| quote_elements_w_spaces <- function(x) { | quote_elements_w_spaces <- function(x) { | ||||
| x <- stringr::str_split(x, ", ?")[[1]] | |||||
| has_space <- stringr::str_detect(x, "\\w \\w") | |||||
| not_quoted <- stringr::str_detect(x, "^\\w.+\\w$") | |||||
| x <- strsplit(x, ", ?")[[1]] | |||||
| has_space <- grepl("\\w \\w", x) | |||||
| not_quoted <- grepl("^\\w.+\\w$", x) | |||||
| x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'") | x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'") | ||||
| paste(x, collapse = ", ") | paste(x, collapse = ", ") | ||||
| } | } |
| glue::glue_data( | glue::glue_data( | ||||
| tv, | tv, | ||||
| "#' @param {variable} {description}. ", | "#' @param {variable} {description}. ", | ||||
| "Defaults to {stringr::str_replace_all(default, '[{{}}]', '`')}. ", | |||||
| "Defaults to {gsub('[{{}}]', '`', default)}. ", | |||||
| "{element_description(element)}") | "{element_description(element)}") | ||||
| ), | ), | ||||
| "#' @template extra_css", | "#' @template extra_css", | ||||
| as.character( | as.character( | ||||
| glue::glue_data( | glue::glue_data( | ||||
| tv, | tv, | ||||
| " {variable} = {ifelse(!stringr::str_detect(default, '^[{].+[}]$'), paste0('\"', default, '\"'), stringr::str_replace_all(default, '[{}]', ''))},") | |||||
| " {variable} = {ifelse(!grepl('^[{].+[}]$', default), paste0('\"', default, '\"'), gsub('[{}]', '', default))},") | |||||
| ), | ), | ||||
| " extra_css = NULL,", | " extra_css = NULL,", | ||||
| " extra_fonts = NULL,", | " extra_fonts = NULL,", |
| if (!inherits(gf, "google_font")) stop( | if (!inherits(gf, "google_font")) stop( | ||||
| "`", var, "` must be set using `google_font()`." | "`", var, "` must be set using `google_font()`." | ||||
| ) | ) | ||||
| group <- stringr::str_split(var, "_")[[1]][1] | |||||
| group <- strsplit(var, "_")[[1]][1] | |||||
| if (group == "text") { | if (group == "text") { | ||||
| text_font_family <- gf$family | text_font_family <- gf$family | ||||
| text_font_weight <- gf$weights %||% "normal" | text_font_weight <- gf$weights %||% "normal" |