😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

52 lines
1.7KB

  1. read_css_vars <- function(file = NULL) {
  2. if (is.null(file)) {
  3. css_candidates <- find_xaringan_themer_css()
  4. if (!length(css_candidates)) {
  5. stop("Unable to locate a xaringanthemer css file.")
  6. } else if (length(css_candidates) == 1) {
  7. file <- css_candidates
  8. } else if (length(css_candidates) > 1) {
  9. is_xaringan_themer_css <- grepl("xaringan-themer.css", css_candidates, fixed = TRUE)
  10. if (any(is_xaringan_themer_css)) {
  11. file <- css_candidates[is_xaringan_themer_css][1]
  12. } else {
  13. file <- css_candidates[1]
  14. message(glue::glue("Using xaringanthemer theme in {file}"))
  15. }
  16. }
  17. }
  18. css_get_root(file)
  19. }
  20. find_xaringan_themer_css <- function() {
  21. css_files <- list.files(pattern = "css$", recursive = TRUE, full.names = TRUE)
  22. is_xt <- css_files %>%
  23. purrr::map(readLines, n = 5) %>%
  24. grepl(pattern = "generated by xaringanthemer", fixed = TRUE)
  25. css_files[is_xt]
  26. }
  27. css_get_root <- function(file) {
  28. x <- readLines(file, warn = FALSE)
  29. x <- paste(x, collapse = "\n")
  30. where <- regexpr(":root\\s*\\{[^}]+\\}", x)
  31. if (where < 0) return(NULL)
  32. x <- substr(x, where, where + attr(where, "match.length"))
  33. x <- strsplit(x, "\n")[[1]]
  34. m <- regexec("--(.+):\\s*(.+?);", x)
  35. x <- regmatches(x, m)
  36. x <- purrr::compact(x)
  37. vars <- gsub("-", "_", purrr::map_chr(x, `[`, 2))
  38. values <- purrr::map(x, `[`, 3)
  39. names(values) <- vars
  40. for (font_type in c("text", "header", "code")) {
  41. font_is_google <- paste0(font_type, "_font_is_google")
  42. values[[font_is_google]] <- if (!is.null(values[[font_is_google]])) {
  43. values[[font_is_google]] %in% c("1", "TRUE", "true", "yes")
  44. } else FALSE
  45. }
  46. values
  47. }