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

118 lines
3.1KB

  1. #' Add Extra CSS Styles
  2. #'
  3. #' Adds css elements to target `outfile`, typically a xaringanthemer css file.
  4. #' The `css` argument takes a list of CSS classes and definitions (see examples below)
  5. #' and appends CSS rules to `outfile`.
  6. #'
  7. #' @section css list:
  8. #' The `css` input must be a named list of css properties and values within a
  9. #' named list of class identifiers, for example
  10. #' `list(".class-id" = list("css-property" = "value"))`.
  11. #'
  12. #' @param css A named list of CSS definitions each containing a named list
  13. #' of CSS property-value pairs, i.e.
  14. #' `list(".class-id" = list("css-property" = "value"))`
  15. #'
  16. #' @examples
  17. #' style_extra_css(
  18. #' outfile = stdout(),
  19. #' css = list(
  20. #' ".red" = list(color = "red"),
  21. #' ".small" = list("font-size" = "90%"),
  22. #' ".full-width" = list(
  23. #' display = "flex",
  24. #' width = "100%",
  25. #' flex = "1 1 auto"
  26. #' )
  27. #' )
  28. #' )
  29. #' @inheritParams style_xaringan
  30. #' @export
  31. style_extra_css <- function(css, outfile = "xaringan-themer.css") {
  32. cat(
  33. "\n\n/* Extra CSS */",
  34. list2css(css),
  35. file = outfile,
  36. append = TRUE,
  37. sep = "\n"
  38. )
  39. }
  40. #' @inheritParams style_extra_css
  41. #' @keywords internal
  42. list2css <- function(css) {
  43. `%.%` <- function(x, y) paste0(x, y)
  44. error <- NULL
  45. if (is.null(names(css))) {
  46. stop("All elements in `css` list must be named", call. = FALSE)
  47. }
  48. if (purrr::vec_depth(css) != 3) {
  49. stop(
  50. "`css` list must be a named list within a named list, e.g.:\n",
  51. ' list(".class-id" = list("css-property" = "value"))'
  52. )
  53. }
  54. if (any(names(css) == "")) {
  55. not_named <- which(names(css) == "")
  56. if (length(not_named) > 1) {
  57. stop(
  58. call. = FALSE,
  59. "All elements in `css` list must be named. Items ",
  60. paste(not_named, collapse = ", "),
  61. " are unnamed."
  62. )
  63. } else {
  64. stop(
  65. call. = FALSE,
  66. "All elements in `css` list must be named. Item ",
  67. not_named,
  68. " is not named."
  69. )
  70. }
  71. }
  72. child_unnamed <- purrr::map_lgl(purrr::map(css, ~ {
  73. is.null(names(.)) || names(.) == ""
  74. }), ~ any(.))
  75. if (any(child_unnamed)) {
  76. has_unnamed <- names(css)[child_unnamed]
  77. msg <- paste(
  78. "All properties of elements in `css` list must be named.",
  79. if (length(has_unnamed) > 1) "Elements" else "Element",
  80. paste(has_unnamed, collapse = ", "),
  81. if (length(has_unnamed) > 1) "have" else "has",
  82. "unnamed property or properties."
  83. )
  84. stop(msg, call. = FALSE)
  85. }
  86. purrr::map_chr(names(css), function(el) {
  87. paste(
  88. sep = "\n",
  89. el %.% " {",
  90. paste(
  91. purrr::map_chr(names(css[[el]]), function(prop) {
  92. " " %.% prop %.% ": " %.% css[[el]][[prop]] %.% ";"
  93. }),
  94. collapse = "\n"
  95. ),
  96. "}"
  97. )
  98. })
  99. }
  100. list2fonts <- function(fonts) {
  101. if (inherits(fonts, "google_font")) {
  102. fonts <- list(fonts)
  103. }
  104. fonts <- purrr::map_chr(fonts, function(f) {
  105. if (inherits(f, "google_font")) {
  106. f$url
  107. } else if (inherits(f, "character")) {
  108. f
  109. } else {
  110. NA_character_
  111. }
  112. })
  113. paste0("@import url(", fonts[!is.na(fonts)], ");")
  114. }