😎 Give your xaringan slides some style
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

72 líneas
2.3KB

  1. read_css_vars <- function(file = NULL) {
  2. if (is.null(file)) {
  3. css_candidates <- find_xaringan_themer_css()
  4. file <- choose_xaringan_themer_css(css_candidates)
  5. }
  6. css_get_root(file)
  7. }
  8. find_xaringan_themer_css <- function() {
  9. # finds xaringan themer files within or below current working directory
  10. # and is only ever intended to be called in that situation
  11. css_files <- list.files(pattern = "css$", recursive = TRUE, full.names = TRUE)
  12. css_files_head <- purrr::map(css_files, readLines, n = 5)
  13. is_xt <- grepl(pattern = "generated by xaringanthemer", css_files_head, fixed = TRUE)
  14. css_files[is_xt]
  15. }
  16. choose_xaringan_themer_css <- function(css_candidates = character(0)) {
  17. if (!length(css_candidates)) {
  18. stop("Unable to locate a xaringanthemer css file.", call. = FALSE)
  19. } else if (length(css_candidates) == 1) {
  20. file <- css_candidates
  21. } else if (length(css_candidates) > 1) {
  22. is_xaringan_themer_css <- grepl("xaringan-themer.css", css_candidates, fixed = TRUE)
  23. if (any(is_xaringan_themer_css)) {
  24. file <- css_candidates[is_xaringan_themer_css][1]
  25. } else {
  26. file <- css_candidates[1]
  27. message(glue::glue("Using xaringanthemer theme in {file}"))
  28. }
  29. }
  30. file
  31. }
  32. css_get_root <- function(file) {
  33. x <- readLines(file, warn = FALSE)
  34. x <- paste(x, collapse = "\n")
  35. where <- regexpr(":root\\s*\\{[^}]+\\}", x)
  36. if (where < 0) return(NULL)
  37. x <- substr(x, where, where + attr(where, "match.length"))
  38. x <- strsplit(x, "\n")[[1]]
  39. m <- regexec("--(.+):\\s*(.+?);", x)
  40. x <- regmatches(x, m)
  41. x <- purrr::compact(x)
  42. vars <- gsub("-", "_", purrr::map_chr(x, `[`, 2))
  43. values <- purrr::map(x, `[`, 3)
  44. names(values) <- vars
  45. for (font_type in c("text", "header", "code")) {
  46. font_is_google <- paste0(font_type, "_font_is_google")
  47. values[[font_is_google]] <- if (!is.null(values[[font_is_google]])) {
  48. values[[font_is_google]] %in% c("1", "TRUE", "true", "yes")
  49. } else FALSE
  50. }
  51. values
  52. }
  53. css_get_padding <- function(x) {
  54. stopifnot(length(x) == 1)
  55. x <- trimws(x)
  56. x <- as.list(strsplit(x, " ")[[1]])
  57. stopifnot(length(x) %in% c(1, 2, 4))
  58. names(x) <- c("top", "right", "bottom", "left")[seq_along(x)]
  59. list(
  60. top = x$top,
  61. right = x$right %||% x$top,
  62. bottom = x$bottom %||% x$top,
  63. left = x$left %||% x$right %||% x$top
  64. )
  65. }