😎 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.

206 Zeilen
5.7KB

  1. source(here::here("R/utils_theme-gen.R"))
  2. load(here::here("R/sysdata.rda"))
  3. # R/theme_settings.R contains element_description() and plural_elements()
  4. setup_theme_function <- function(
  5. f_name = "style_xaringan",
  6. template = template_variables,
  7. ...,
  8. file = "",
  9. body = " eval(parse(text = call_style_xaringan()))",
  10. theme_colors = NULL
  11. ) {
  12. if (file == "clip" && !requireNamespace("clipr", quietly = TRUE)) file <- ""
  13. f_body_theme_colors <- include_theme_colors(theme_colors)
  14. f_body <- c(
  15. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  16. f_body_theme_colors,
  17. body
  18. )
  19. tv <- template
  20. f_def <- c(
  21. "# Generated by inst/scripts/generate_theme_functions.R: do not edit by hand\n",
  22. as.character(
  23. glue::glue_data(
  24. tv,
  25. "#' @param {variable} {description}. ",
  26. "Defaults to {gsub('[{{}}]', '`', default)}. ",
  27. "{element_description(element)}",
  28. "{describe_css_variable(css_variable)}"
  29. )
  30. ),
  31. "#' @template theme_params",
  32. "#' @template style-usage",
  33. ...,
  34. glue::glue("{f_name} <- function("),
  35. as.character(
  36. glue::glue_data(
  37. tv,
  38. " {variable} = {ifelse(!grepl('^[{].+[}]$', default), paste0('\"', default, '\"'), gsub('[{}]', '', default))},"
  39. )
  40. ),
  41. " colors = NULL,",
  42. " extra_css = NULL,",
  43. " extra_fonts = NULL,",
  44. " outfile = \"xaringan-themer.css\"",
  45. ") {"
  46. )
  47. if (!is.null(f_body)) f_def <- c(f_def, f_body, "}")
  48. if (file == "clip") {
  49. clipr::write_clip(f_def)
  50. message("Wrote ", f_name, " function signature to clipboard.")
  51. } else {
  52. cat(reflow_roxygen(f_def), sep = "\n", file = file)
  53. message("Wrote ", f_name, " to ", file)
  54. }
  55. invisible()
  56. }
  57. reflow_roxygen <- function(x) {
  58. is_roxy_tag <- grepl("^#' @", x)
  59. roxy_tags <- x[is_roxy_tag]
  60. roxy_tags <- sub("^#' ", "", roxy_tags)
  61. roxy_tags <- purrr::map_chr(
  62. roxy_tags,
  63. ~ paste(
  64. "#'", strwrap(
  65. pack_inline_code(.x),
  66. width = 77,
  67. exdent = 2
  68. ), collapse = "\n")
  69. )
  70. roxy_tags <- gsub("\u00A0", " ", roxy_tags)
  71. x[is_roxy_tag] <- roxy_tags
  72. x
  73. }
  74. pack_inline_code <- function(x) {
  75. stopifnot(length(x) == 1, is.character(x))
  76. x <- strsplit(x, "")[[1]]
  77. inline_code <- FALSE
  78. for (i in seq_along(x)) {
  79. if (identical(x[i], "`")) {
  80. inline_code <- !inline_code
  81. } else if (inline_code && identical(x[i], " ")) {
  82. x[i] <- "\u00A0"
  83. }
  84. }
  85. paste(x, collapse = "")
  86. }
  87. include_theme_colors <- function(theme_colors = NULL) {
  88. if (is.null(theme_colors)) return(NULL)
  89. x <- glue::glue('{names(theme_colors)} = {theme_colors}')
  90. x <- paste(x, collapse = ", ")
  91. glue::glue(" colors <- c({x}, colors)")
  92. }
  93. # ---- Write Xaringan Theme Function ----
  94. setup_theme_function(
  95. "style_xaringan",
  96. template_variables,
  97. "#' @template style_xaringan",
  98. "#' @export",
  99. body = paste0(" ", readLines(here::here("inst/scripts/style_xaringan_body.R"))),
  100. file = here::here("R/style_xaringan.R")
  101. )
  102. # ---- Monotone Light ----
  103. setup_theme_function(
  104. "style_mono_light",
  105. template_mono_light,
  106. "#' @template style_mono_light",
  107. "#' @family Monotone themes",
  108. "#' @export",
  109. file = here::here("R/style_mono_light.R"),
  110. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  111. )
  112. # ---- Monotone Dark ----
  113. setup_theme_function(
  114. "style_mono_dark",
  115. template_mono_dark,
  116. "#' @template style_mono_dark",
  117. "#' @family Monotone themes",
  118. "#' @export",
  119. file = here::here("R/style_mono_dark.R"),
  120. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  121. )
  122. # ---- Monotone Accent ----
  123. setup_theme_function(
  124. "style_mono_accent",
  125. template_mono_accent,
  126. "#' @template style_mono_accent",
  127. "#' @family Monotone themes",
  128. "#' @export",
  129. file = here::here("R/style_mono_accent.R"),
  130. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  131. )
  132. # ---- Monotone Accent Inverse ----
  133. setup_theme_function(
  134. "style_mono_accent_inverse",
  135. template_mono_accent_inverse,
  136. "#' @template style_mono_accent_inverse",
  137. "#' @family Monotone themes",
  138. "#' @export",
  139. file = here::here("R/style_mono_accent_inverse.R"),
  140. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  141. )
  142. # ---- Duotone ----
  143. setup_theme_function(
  144. "style_duo",
  145. template_duo,
  146. "#' @template style_duo",
  147. "#' @family Duotone themes",
  148. "#' @export",
  149. file = here::here("R/style_duo.R"),
  150. theme_colors = c(primary = "primary_color", secondary = "secondary_color")
  151. )
  152. # ---- Duotone Accent ----
  153. setup_theme_function(
  154. "style_duo_accent",
  155. template_duo_accent,
  156. "#' @template style_duo_accent",
  157. "#' @family Duotone themes",
  158. "#' @export",
  159. file = here::here("R/style_duo_accent.R"),
  160. theme_colors = c(primary = "primary_color", secondary = "secondary_color",
  161. white = "white_color", black = "black_color")
  162. )
  163. # ---- Duotone Accent Inverse ----
  164. setup_theme_function(
  165. "style_duo_accent_inverse",
  166. template_duo_accent_inverse,
  167. "#' @template style_duo_accent_inverse",
  168. "#' @family Duotone themes",
  169. "#' @export",
  170. file = here::here("R/style_duo_accent_inverse.R"),
  171. theme_colors = c(primary = "primary_color", secondary = "secondary_color",
  172. white = "white_color", black = "black_color")
  173. )
  174. # ---- Solarized Light ----
  175. setup_theme_function(
  176. "style_solarized_light",
  177. template_solarized_light,
  178. "#' @template style_solarized_light",
  179. "#' @family Solarized themes",
  180. "#' @export",
  181. file = here::here("R/style_solarized_light.R")
  182. )
  183. # ---- Solarized Dark ----
  184. setup_theme_function(
  185. "style_solarized_dark",
  186. template_solarized_dark,
  187. "#' @template style_solarized_dark",
  188. "#' @family Solarized themes",
  189. "#' @export",
  190. file = here::here("R/style_solarized_dark.R")
  191. )