😎 Give your xaringan slides some style
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

79 Zeilen
2.6KB

  1. #' Write Extra CSS arguments
  2. #'
  3. #' Manually adds css elements to target `outfile`.
  4. #'
  5. #' @section css list:
  6. #' The `css` input must be a named list of css properties and values within a
  7. #' named list of class identifiers, for example
  8. #' `list(".class-id" = list("css-property" = "value"))`.
  9. #'
  10. #' @param css A named list of CSS definitions each containing a named list
  11. #' of CSS property-value pairs, i.e.
  12. #' `list(".class-id" = list("css-property" = "value"))`
  13. #' @param append If `TRUE` output will be appended to `outfile`; otherwise,
  14. #' it will overwrite the contents of `outfile`.
  15. #' @inheritParams write_xaringan_theme
  16. #' @export
  17. write_extra_css <- function(css, outfile = "xaringan-themer.css", append = TRUE) {
  18. cat("\n\n/* Extra CSS */", list2css(css), file = outfile,
  19. append = append, sep = "\n")
  20. }
  21. #' @inheritParams write_extra_css
  22. #' @keywords internal
  23. list2css <- function(css) {
  24. `%.%` <- function(x, y) paste0(x, y)
  25. error <- NULL
  26. if (is.null(names(css))) {
  27. stop("All elements in `css` list must be named", call. = FALSE)
  28. }
  29. if (purrr::vec_depth(css) != 3) {
  30. stop("`css` list must be a named list within a named list, e.g.:\n",
  31. ' list(".class-id" = list("css-property" = "value"))')
  32. }
  33. if (any(names(css) == "")) {
  34. not_named <- which(names(css) == "")
  35. if (length(not_named) > 1) stop(call. = FALSE,
  36. "All elements in `css` list must be named. Items ",
  37. paste(not_named, collapse = ", "), " are unnamed."
  38. ) else stop(call. = FALSE,
  39. "All elements in `css` list must be named. Item ", not_named, " is not named.")
  40. }
  41. child_unnamed <- purrr::map_lgl(purrr::map(css, ~ {is.null(names(.)) || names(.) == ""}), ~ any(.))
  42. if (any(child_unnamed)) {
  43. has_unnamed <- names(css)[child_unnamed]
  44. msg <- paste(
  45. "All properties of elements in `css` list must be named.",
  46. if (length(has_unnamed) > 1) "Elements" else "Element",
  47. paste(has_unnamed, collapse = ", "),
  48. if (length(has_unnamed) > 1) "have" else "has",
  49. "unnamed property or properties."
  50. )
  51. stop(msg, call. = FALSE)
  52. }
  53. purrr::map_chr(names(css), function(el) {
  54. paste(sep = "\n",
  55. el %.% " {",
  56. paste(
  57. purrr::map_chr(names(css[[el]]), function(prop) {
  58. " " %.% prop %.% ': ' %.% css[[el]][[prop]] %.% ';'
  59. }),
  60. collapse = "\n"
  61. ),
  62. "}"
  63. )
  64. })
  65. }
  66. list2fonts <- function(fonts) {
  67. fonts <- purrr::map_chr(fonts, function(f) {
  68. if (inherits(f, "google_font")) {
  69. f$url
  70. } else if (inherits(f, "character")) {
  71. f
  72. } else NA_character_
  73. })
  74. paste0("@import url(", fonts[!is.na(fonts)], ");")
  75. }