😎 Give your xaringan slides some style
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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