😎 Give your xaringan slides some style
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

209 lines
5.9KB

  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. unname <- glue::glue("{theme_colors} <- unname({theme_colors})")
  91. unname <- paste(unname, collapse = "\n ")
  92. x <- glue::glue('{names(theme_colors)} = {theme_colors}')
  93. x <- paste(x, collapse = ", ")
  94. glue::glue(" {unname}\n colors <- c({x}, colors)", .trim = FALSE)
  95. }
  96. # ---- Write Xaringan Theme Function ----
  97. setup_theme_function(
  98. "style_xaringan",
  99. template_variables,
  100. "#' @template style_xaringan",
  101. "#' @export",
  102. body = paste0(" ", readLines(here::here("inst/scripts/style_xaringan_body.R"))),
  103. file = here::here("R/style_xaringan.R")
  104. )
  105. # ---- Monotone Light ----
  106. setup_theme_function(
  107. "style_mono_light",
  108. template_mono_light,
  109. "#' @template style_mono_light",
  110. "#' @family Monotone themes",
  111. "#' @export",
  112. file = here::here("R/style_mono_light.R"),
  113. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  114. )
  115. # ---- Monotone Dark ----
  116. setup_theme_function(
  117. "style_mono_dark",
  118. template_mono_dark,
  119. "#' @template style_mono_dark",
  120. "#' @family Monotone themes",
  121. "#' @export",
  122. file = here::here("R/style_mono_dark.R"),
  123. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  124. )
  125. # ---- Monotone Accent ----
  126. setup_theme_function(
  127. "style_mono_accent",
  128. template_mono_accent,
  129. "#' @template style_mono_accent",
  130. "#' @family Monotone themes",
  131. "#' @export",
  132. file = here::here("R/style_mono_accent.R"),
  133. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  134. )
  135. # ---- Monotone Accent Inverse ----
  136. setup_theme_function(
  137. "style_mono_accent_inverse",
  138. template_mono_accent_inverse,
  139. "#' @template style_mono_accent_inverse",
  140. "#' @family Monotone themes",
  141. "#' @export",
  142. file = here::here("R/style_mono_accent_inverse.R"),
  143. theme_colors = c(base = "base_color", white = "white_color", black = "black_color")
  144. )
  145. # ---- Duotone ----
  146. setup_theme_function(
  147. "style_duo",
  148. template_duo,
  149. "#' @template style_duo",
  150. "#' @family Duotone themes",
  151. "#' @export",
  152. file = here::here("R/style_duo.R"),
  153. theme_colors = c(primary = "primary_color", secondary = "secondary_color")
  154. )
  155. # ---- Duotone Accent ----
  156. setup_theme_function(
  157. "style_duo_accent",
  158. template_duo_accent,
  159. "#' @template style_duo_accent",
  160. "#' @family Duotone themes",
  161. "#' @export",
  162. file = here::here("R/style_duo_accent.R"),
  163. theme_colors = c(primary = "primary_color", secondary = "secondary_color",
  164. white = "white_color", black = "black_color")
  165. )
  166. # ---- Duotone Accent Inverse ----
  167. setup_theme_function(
  168. "style_duo_accent_inverse",
  169. template_duo_accent_inverse,
  170. "#' @template style_duo_accent_inverse",
  171. "#' @family Duotone themes",
  172. "#' @export",
  173. file = here::here("R/style_duo_accent_inverse.R"),
  174. theme_colors = c(primary = "primary_color", secondary = "secondary_color",
  175. white = "white_color", black = "black_color")
  176. )
  177. # ---- Solarized Light ----
  178. setup_theme_function(
  179. "style_solarized_light",
  180. template_solarized_light,
  181. "#' @template style_solarized_light",
  182. "#' @family Solarized themes",
  183. "#' @export",
  184. file = here::here("R/style_solarized_light.R")
  185. )
  186. # ---- Solarized Dark ----
  187. setup_theme_function(
  188. "style_solarized_dark",
  189. template_solarized_dark,
  190. "#' @template style_solarized_dark",
  191. "#' @family Solarized themes",
  192. "#' @export",
  193. file = here::here("R/style_solarized_dark.R")
  194. )