😎 Give your xaringan slides some style
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

51 line
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. css_files_head <- purrr::map(css_files, readLines, n = 5)
  23. is_xt <- grepl(pattern = "generated by xaringanthemer", css_files_head, fixed = TRUE)
  24. css_files[is_xt]
  25. }
  26. css_get_root <- function(file) {
  27. x <- readLines(file, warn = FALSE)
  28. x <- paste(x, collapse = "\n")
  29. where <- regexpr(":root\\s*\\{[^}]+\\}", x)
  30. if (where < 0) return(NULL)
  31. x <- substr(x, where, where + attr(where, "match.length"))
  32. x <- strsplit(x, "\n")[[1]]
  33. m <- regexec("--(.+):\\s*(.+?);", x)
  34. x <- regmatches(x, m)
  35. x <- purrr::compact(x)
  36. vars <- gsub("-", "_", purrr::map_chr(x, `[`, 2))
  37. values <- purrr::map(x, `[`, 3)
  38. names(values) <- vars
  39. for (font_type in c("text", "header", "code")) {
  40. font_is_google <- paste0(font_type, "_font_is_google")
  41. values[[font_is_google]] <- if (!is.null(values[[font_is_google]])) {
  42. values[[font_is_google]] %in% c("1", "TRUE", "true", "yes")
  43. } else FALSE
  44. }
  45. values
  46. }