😎 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.

190 lines
9.7KB

  1. template_variables <- tibble::tribble(
  2. ~ variable, ~ default, ~ element, ~ description
  3. , "text_color", "#000", "body", "Text Color"
  4. , "header_color", "#000", "h1, h2, h3", "Header Color"
  5. , "background_color", "#FFF", ".remark-slide-content", "Slide Background Color"
  6. , "link_color", "rgb(249, 38, 114)", "a, a > code", "Link Color"
  7. , "text_bold_color", NA_character_, "strong", "Bold Text Color"
  8. , "text_slide_number_color", "{inverse_background_color}", ".remark-slide-number", "Slide Number Color"
  9. , "code_highlight_color", "#ffff88", ".remark-code-line-highlighted", "Code Line Highlight"
  10. , "code_inline_color", "#000", ".remark-inline-code", "Inline Code Color"
  11. , "code_inline_background_color", NA_character_, ".remark-inline-code", "Inline Code Background Color"
  12. , "inverse_background_color", "#272822", ".inverse", "Inverse Background Color"
  13. , "inverse_text_color", "#d6d6d6", ".inverse", "Inverse Text Color"
  14. , "inverse_text_shadow", "{FALSE}", ".inverse", "Enables Shadow on text of inverse slides"
  15. , "inverse_header_color", "#f3f3f3", ".inverse h1, .inverse h2, .inverse h3", "Inverse Header Color"
  16. , "title_slide_text_color", "{inverse_text_color}", ".title-slide", "Title Slide Text Color"
  17. , "title_slide_background_color", "{inverse_background_color}", ".title-slide", "Title Slide Background Color"
  18. , "title_slide_background_image", NA_character_, ".title-slide", "Title Slide Background Image URL"
  19. , "left_column_subtle_color", "#777", ".left-column", "Left Column Text (not last)"
  20. , "left_column_selected_color", "#000", ".left-column h2:last-of-type, .left-column h3:last-child", "Left Column Current Selection"
  21. , "blockquote_left_color", "lightgray", "blockquote", "Blockquote Left Border Color"
  22. , "table_border_color", "#666", "table: border-top, border-bottom", "Table top/bottom border"
  23. , "table_row_border_color", "#ddd", "table thead th: border-bottom", "Table row inner bottom border"
  24. , "table_row_even_background_color", "#eee", "thead, tfoot, tr:nth-child(even)", "Table Even Row Background Color"
  25. , "text_font_google", NULL, "body", "Use `google_font()` to specify body font"
  26. , "text_font_family", "'Droid Serif'", "body", "Body Text Font Family"
  27. , "text_font_weight", "normal", "body", "Body Text Font Weight"
  28. , "text_font_url", "https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic", "@import url()", "Body Text Font URL(s)"
  29. , "text_font_family_fallback", "'Palatino Linotype', 'Book Antiqua', Palatino, 'Microsoft YaHei', 'Songti SC'", "body", "Body Text Font Fallbacks"
  30. , "text_font_base", "serif", "body", "Body Text Base Font (Total Failure Fallback)"
  31. , "header_font_google", NULL, "body", "Use `google_font()` to specify header font"
  32. , "header_font_family", "'Yanone Kaffeesatz'", "h1, h2, h3", "Header Font Family"
  33. , "header_font_weight", "normal", "h1, h2, h3", "Header Font Weight"
  34. , "header_font_url", "https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz", "@import url", "Header Font URL"
  35. , "code_font_google", NULL, "body", "Use `google_font()` to specify code font"
  36. , "code_font_family", "'Source Code Pro'", ".remark-code, .remark-inline-code", "Code Font Family"
  37. , "code_font_url", "https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700", "@import url", "Code Font URL"
  38. , "code_font_family_fallback", "'Lucida Console', Monaco", ".remark-code, .remark-inline-code", "Code Font Fallback"
  39. )
  40. #' @keywords internal
  41. setup_theme_function <- function(
  42. f_name = "write_xaringan_theme",
  43. to_clipboard = TRUE,
  44. ...
  45. ) {
  46. `%,%` <- function(x, y) c(x, y)
  47. null_default <- purrr::map_lgl(template_variables$default, is.null)
  48. tv <- xaringanthemer::template_variables
  49. tv[null_default, 'default'] <- "{NULL}"
  50. x <-
  51. as.character(
  52. glue::glue_data(tv,
  53. "#' @param {variable} {description}")) %,%
  54. "#' @param outfile Customized xaringan CSS output file name" %,%
  55. c(...) %,%
  56. glue("{f_name} <- function(") %,%
  57. as.character(glue::glue_data(tv,
  58. " {variable} = {ifelse(!stringr::str_detect(default, '^[{].+[}]$'), paste0('\"', default, '\"'), stringr::str_replace_all(default, '[{}]', ''))},")) %,%
  59. " outfile = \"xaringan-themed.css\"" %,%
  60. ") {"
  61. cat(x, sep = "\n",
  62. file = if(to_clipboard) pipe("pbcopy", "w") else "")
  63. if (to_clipboard) message("Wrote function signature to clipboard.")
  64. }
  65. #' @keywords internal
  66. call_write_xaringan_theme <- function() {
  67. paste0("write_xaringan_theme(",
  68. paste(template_variables$variable, collapse = ", "),
  69. ")")
  70. }
  71. #' Write Customized Xaringan Theme
  72. #'
  73. #' @param text_color Text Color
  74. #' @param header_color Header Color
  75. #' @param background_color Slide Background Color
  76. #' @param link_color Link Color
  77. #' @param text_bold_color Bold Text Color
  78. #' @param text_slide_number_color Slide Number Color
  79. #' @param code_highlight_color Code Line Highlight
  80. #' @param code_inline_color Inline Code Color
  81. #' @param code_inline_background_color Inline Code Background Color
  82. #' @param inverse_background_color Inverse Background Color
  83. #' @param inverse_text_color Inverse Text Color
  84. #' @param inverse_text_shadow Enables Shadow on text of inverse slides
  85. #' @param inverse_header_color Inverse Header Color
  86. #' @param title_slide_text_color Title Slide Text Color
  87. #' @param title_slide_background_color Title Slide Background Color
  88. #' @param title_slide_background_image Title Slide Background Image URL
  89. #' @param left_column_subtle_color Left Column Text (not last)
  90. #' @param left_column_selected_color Left Column Current Selection
  91. #' @param blockquote_left_color Blockquote Left Border Color
  92. #' @param table_border_color Table top/bottom border
  93. #' @param table_row_border_color Table row inner bottom border
  94. #' @param table_row_even_background_color Table Even Row Background Color
  95. #' @param text_font_google Use `google_font()` to specify body font
  96. #' @param text_font_family Body Text Font Family
  97. #' @param text_font_weight Body Text Font Weight
  98. #' @param text_font_url Body Text Font URL(s)
  99. #' @param text_font_family_fallback Body Text Font Fallbacks
  100. #' @param text_font_base Body Text Base Font (Total Failure Fallback)
  101. #' @param header_font_google Use `google_font()` to specify header font
  102. #' @param header_font_family Header Font Family
  103. #' @param header_font_weight Header Font Weight
  104. #' @param header_font_url Header Font URL
  105. #' @param code_font_google Use `google_font()` to specify code font
  106. #' @param code_font_family Code Font Family
  107. #' @param code_font_url Code Font URL
  108. #' @param code_font_family_fallback Code Font Fallback
  109. #' @param outfile Customized xaringan CSS output file name
  110. write_xaringan_theme <- function(
  111. text_color = "#000",
  112. header_color = "#000",
  113. background_color = "#FFF",
  114. link_color = "rgb(249, 38, 114)",
  115. text_bold_color = NA,
  116. text_slide_number_color = color_inverse_bg,
  117. code_highlight_color = "#ffff88",
  118. code_inline_color = "#000",
  119. code_inline_background_color = NA,
  120. inverse_background_color = "#272822",
  121. inverse_text_color = "#d6d6d6",
  122. inverse_text_shadow = FALSE,
  123. inverse_header_color = "#f3f3f3",
  124. title_slide_text_color = color_inverse_text,
  125. title_slide_background_color = color_inverse_bg,
  126. title_slide_background_image = NA,
  127. left_column_subtle_color = "#777",
  128. left_column_selected_color = "#000",
  129. blockquote_left_color = "lightgray",
  130. table_border_color = "#666",
  131. table_row_border_color = "#ddd",
  132. table_row_even_background_color = "#eee",
  133. text_font_google = NULL,
  134. text_font_family = "'Droid Serif'",
  135. text_font_weight = "normal",
  136. text_font_url = "https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic",
  137. text_font_family_fallback = "'Palatino Linotype', 'Book Antiqua', Palatino, 'Microsoft YaHei', 'Songti SC'",
  138. text_font_base = "serif",
  139. header_font_google = NULL,
  140. header_font_family = "'Yanone Kaffeesatz'",
  141. header_font_weight = "normal",
  142. header_font_url = "https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz",
  143. code_font_google = NULL,
  144. code_font_family = "'Source Code Pro'",
  145. code_font_url = "https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700",
  146. code_font_family_fallback = "'Lucida Console', Monaco",
  147. outfile = "xaringan-themed.css"
  148. ) {
  149. # Make sure font names are wrapped in quotes if they have spaces
  150. f_args <- names(formals(sys.function()))
  151. for (var in f_args[grepl("font_family$", f_args)]) {
  152. eval(parse(text = paste0(
  153. var, "<-quote_elements_w_spaces(", var, ")"
  154. )))
  155. }
  156. # Use font_..._google args to overwrite font args
  157. for (var in f_args[grepl("font_google$", f_args)]) {
  158. gf <- eval(parse(text = var))
  159. if (is.null(gf)) next
  160. if (!inherits(gf, "google_font")) stop(
  161. "`", var, "` must be set using `google_font()`."
  162. )
  163. group <- stringr::str_split(var, "_")[[1]][1]
  164. if (group == "text") {
  165. text_font_family <- gf$family
  166. text_font_weight <- gf$weights %||% "normal"
  167. text_font_weight <- substr(text_font_weight, 1, regexpr(",", text_font_weight)[1]-1)
  168. text_font_url <- gf$url
  169. } else {
  170. for (thing in c("family", "url")) {
  171. eval(parse(text = paste0(
  172. group, "_font_", thing, " <- gf$", thing
  173. )))
  174. }
  175. }
  176. }
  177. tf <- system.file("resources", "template.css", package = "xaringanthemer")
  178. template <- readLines(tf, warn = FALSE)
  179. template <- paste(template, collapse = "\n")
  180. x <- glue::glue(template, .open = "{{", .close = "}}")
  181. cat(x, file = outfile)
  182. outfile
  183. }