😎 Give your xaringan slides some style
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

77 rindas
2.4KB

  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. #' @inheritParams write_xaringan_theme
  14. #' @export
  15. write_extra_css <- function(css, outfile = "xaringan-themer.css") {
  16. cat("\n\n/* Extra CSS */", list2css(css), file = outfile,
  17. append = TRUE, sep = "\n")
  18. }
  19. #' @inheritParams write_extra_css
  20. #' @keywords internal
  21. list2css <- function(css) {
  22. `%.%` <- function(x, y) paste0(x, y)
  23. error <- NULL
  24. if (is.null(names(css))) {
  25. stop("All elements in `css` list must be named", call. = FALSE)
  26. }
  27. if (purrr::vec_depth(css) != 3) {
  28. stop("`css` list must be a named list within a named list, e.g.:\n",
  29. ' list(".class-id" = list("css-property" = "value"))')
  30. }
  31. if (any(names(css) == "")) {
  32. not_named <- which(names(css) == "")
  33. if (length(not_named) > 1) stop(call. = FALSE,
  34. "All elements in `css` list must be named. Items ",
  35. paste(not_named, collapse = ", "), " are unnamed."
  36. ) else stop(call. = FALSE,
  37. "All elements in `css` list must be named. Item ", not_named, " is not named.")
  38. }
  39. child_unnamed <- purrr::map_lgl(purrr::map(css, ~ {is.null(names(.)) || names(.) == ""}), ~ any(.))
  40. if (any(child_unnamed)) {
  41. has_unnamed <- names(css)[child_unnamed]
  42. msg <- paste(
  43. "All properties of elements in `css` list must be named.",
  44. if (length(has_unnamed) > 1) "Elements" else "Element",
  45. paste(has_unnamed, collapse = ", "),
  46. if (length(has_unnamed) > 1) "have" else "has",
  47. "unnamed property or properties."
  48. )
  49. stop(msg, call. = FALSE)
  50. }
  51. purrr::map_chr(names(css), function(el) {
  52. paste(sep = "\n",
  53. el %.% " {",
  54. paste(
  55. purrr::map_chr(names(css[[el]]), function(prop) {
  56. " " %.% prop %.% ': ' %.% css[[el]][[prop]] %.% ';'
  57. }),
  58. collapse = "\n"
  59. ),
  60. "}"
  61. )
  62. })
  63. }
  64. list2fonts <- function(fonts) {
  65. fonts <- purrr::map_chr(fonts, function(f) {
  66. if (inherits(f, "google_font")) {
  67. f$url
  68. } else if (inherits(f, "character")) {
  69. f
  70. } else NA_character_
  71. })
  72. paste0("@import url(", fonts[!is.na(fonts)], ");")
  73. }