😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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