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

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