😎 Give your xaringan slides some style
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

200 lines
5.3KB

  1. source(here::here("R/theme_settings.R"))
  2. plural_element <- function(css_name) {
  3. is_mult <- grepl(",|and|or", css_name)
  4. is_class <- grepl("^\\.", css_name)
  5. ifelse(is_class,
  6. ifelse(is_mult, "classes", "class"),
  7. ifelse(is_mult, "elements", "element")
  8. )
  9. }
  10. element_description <- function(element) {
  11. ifelse(
  12. grepl("multiple", element),
  13. "Modifies multiple CSS classes or elements.",
  14. glue::glue("Modifies the `{element}` {plural_element(element)}.")
  15. )
  16. }
  17. setup_theme_function <- function(
  18. f_name = "write_xaringan_theme",
  19. template = template_variables,
  20. ...,
  21. file = "",
  22. body = NULL
  23. ) {
  24. if (file == "clip" && !requireNamespace("clipr", quietly = TRUE)) file <- ""
  25. tv <- template
  26. null_default <- purrr::map_lgl(tv$default, is.null)
  27. tv[null_default, 'default'] <- "{NULL}"
  28. x <- c(
  29. as.character(
  30. glue::glue_data(
  31. tv,
  32. "#' @param {variable} {description}. ",
  33. "Defaults to {stringr::str_replace_all(default, '[{{}}]', '`')}. ",
  34. "{element_description(element)}")
  35. ),
  36. "#' @template extra_css",
  37. "#' @param outfile Customized xaringan CSS output file name, default is \"xaringan-themer.css\"",
  38. "#' @family themes",
  39. ...,
  40. glue::glue("{f_name} <- function("),
  41. as.character(
  42. glue::glue_data(
  43. tv,
  44. " {variable} = {ifelse(!stringr::str_detect(default, '^[{].+[}]$'), paste0('\"', default, '\"'), stringr::str_replace_all(default, '[{}]', ''))},")
  45. ),
  46. " extra_css = NULL,",
  47. " extra_fonts = NULL,",
  48. " outfile = \"xaringan-themer.css\"",
  49. ") {"
  50. )
  51. if (!is.null(body)) x <- c(x, body, "}")
  52. if (file == "clip") {
  53. clipr::write_clip(x)
  54. message("Wrote ", f_name, " function signature to clipboard.")
  55. } else {
  56. cat(x, sep = "\n", file = file)
  57. message("Wrote ", f_name, " to ", file)
  58. }
  59. invisible()
  60. }
  61. # ---- Write Xaringan Theme Function ----
  62. setup_theme_function(
  63. "write_xaringan_theme",
  64. template_variables,
  65. "#' @template write_xaringan_theme",
  66. "#' @export",
  67. body = paste0(" ", readLines(here::here("inst/scripts/write_xaringan_theme_body.R"))),
  68. file = here::here("R/write_theme.R")
  69. )
  70. # ---- Monotone Light ----
  71. setup_theme_function(
  72. "mono_light",
  73. template_mono_light,
  74. "#' @template mono_light",
  75. "#' @family Monotone themes",
  76. "#' @export",
  77. body = c(
  78. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  79. " eval(parse(text = call_write_xaringan_theme()))"
  80. ),
  81. file = here::here("R/mono_light.R")
  82. )
  83. # ---- Monotone Dark ----
  84. setup_theme_function(
  85. "mono_dark",
  86. template_mono_dark,
  87. "#' @template mono_dark",
  88. "#' @family Monotone themes",
  89. "#' @export",
  90. body = c(
  91. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  92. " eval(parse(text = call_write_xaringan_theme()))"
  93. ),
  94. file = here::here("R/mono_dark.R")
  95. )
  96. # ---- Monotone Accent ----
  97. setup_theme_function(
  98. "mono_accent",
  99. template_mono_accent,
  100. "#' @template mono_accent",
  101. "#' @family Monotone themes",
  102. "#' @export",
  103. body = c(
  104. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  105. " eval(parse(text = call_write_xaringan_theme()))"
  106. ),
  107. file = here::here("R/mono_accent.R")
  108. )
  109. # ---- Monotone Accent Inverse ----
  110. setup_theme_function(
  111. "mono_accent_inverse",
  112. template_mono_accent_inverse,
  113. "#' @template mono_accent_inverse",
  114. "#' @family Monotone themes",
  115. "#' @export",
  116. body = c(
  117. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  118. " eval(parse(text = call_write_xaringan_theme()))"
  119. ),
  120. file = here::here("R/mono_accent_inverse.R")
  121. )
  122. # ---- Duotone ----
  123. setup_theme_function(
  124. "duo",
  125. template_duo,
  126. "#' @template duo",
  127. "#' @family Duotone themes",
  128. "#' @export",
  129. body = c(
  130. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  131. " eval(parse(text = call_write_xaringan_theme()))"
  132. ),
  133. file = here::here("R/duo.R")
  134. )
  135. # ---- Duotone Accent ----
  136. setup_theme_function(
  137. "duo_accent",
  138. template_duo_accent,
  139. "#' @template duo_accent",
  140. "#' @family Duotone themes",
  141. "#' @export",
  142. body = c(
  143. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  144. " eval(parse(text = call_write_xaringan_theme()))"
  145. ),
  146. file = here::here("R/duo_accent.R")
  147. )
  148. # ---- Duotone Accent Inverse ----
  149. setup_theme_function(
  150. "duo_accent_inverse",
  151. template_duo_accent_inverse,
  152. "#' @template duo_accent_inverse",
  153. "#' @family Duotone themes",
  154. "#' @export",
  155. body = c(
  156. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  157. " eval(parse(text = call_write_xaringan_theme()))"
  158. ),
  159. file = here::here("R/duo_accent_inverse.R")
  160. )
  161. # ---- Solarized Light ----
  162. setup_theme_function(
  163. "solarized_light",
  164. template_solarized_light,
  165. "#' @template solarized_light",
  166. "#' @family Solarized themes",
  167. "#' @export",
  168. body = c(
  169. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  170. " eval(parse(text = call_write_xaringan_theme()))"
  171. ),
  172. file = here::here("R/solarized_light.R")
  173. )
  174. # ---- Solarized Dark ----
  175. setup_theme_function(
  176. "solarized_dark",
  177. template_solarized_dark,
  178. "#' @template solarized_dark",
  179. "#' @family Solarized themes",
  180. "#' @export",
  181. body = c(
  182. " # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R",
  183. " eval(parse(text = call_write_xaringan_theme()))"
  184. ),
  185. file = here::here("R/solarized_dark.R")
  186. )