Просмотр исходного кода

Read and load css variables from xaringan css files

tags/v0.3.0
Garrick Aden-Buie 6 лет назад
Родитель
Сommit
86f096b56c
5 измененных файлов: 144 добавлений и 18 удалений
  1. +51
    -0
      R/css.R
  2. +53
    -17
      R/ggplot2.R
  3. +8
    -0
      man/theme_xaringan.Rd
  4. +8
    -1
      man/theme_xaringan_get_value.Rd
  5. +24
    -0
      tests/testthat/test-css.R

+ 51
- 0
R/css.R Просмотреть файл

@@ -0,0 +1,51 @@
read_css_vars <- function(file = NULL) {
if (is.null(file)) {
css_candidates <- find_xaringan_themer_css()
if (!length(css_candidates)) {
stop("Unable to locate a xaringanthemer css file.")
} else if (length(css_candidates) == 1) {
file <- css_candidates
} else if (length(css_candidates) > 1) {
is_xaringan_themer_css <- grepl("xaringan-themer.css", css_candidates, fixed = TRUE)
if (any(is_xaringan_themer_css)) {
file <- css_candidates[is_xaringan_themer_css][1]
} else {
file <- css_candidates[1]
message(glue::glue("Using xaringanthemer theme in {file}"))
}
}
}

css_get_root(file)
}

find_xaringan_themer_css <- function() {
css_files <- list.files(pattern = "css$", recursive = TRUE, full.names = TRUE)
is_xt <- css_files %>%
purrr::map(readLines, n = 5) %>%
grepl(pattern = "generated by xaringanthemer", fixed = TRUE)

css_files[is_xt]
}

css_get_root <- function(file) {
x <- readLines(file, warn = FALSE)
x <- paste(x, collapse = "\n")
where <- regexpr(":root\\s*\\{[^}]+\\}", x)
if (where < 0) return(NULL)
x <- substr(x, where, where + attr(where, "match.length"))
x <- strsplit(x, "\n")[[1]]
m <- regexec("--(.+):\\s*(.+?);", x)
x <- regmatches(x, m)
x <- purrr::compact(x)
vars <- gsub("-", "_", purrr::map_chr(x, `[`, 2))
values <- purrr::map(x, `[`, 3)
names(values) <- vars
for (font_type in c("text", "header", "code")) {
font_is_google <- paste0(font_type, "_font_is_google")
values[[font_is_google]] <- if (!is.null(values[[font_is_google]])) {
values[[font_is_google]] %in% c("1", "TRUE", "true", "yes")
} else FALSE
}
values
}

+ 53
- 17
R/ggplot2.R Просмотреть файл

@@ -16,6 +16,12 @@
#' `header_color`
#' @param accent_secondary_color Color for secondary accents, inherits from
#' `text_bold_color`
#' @param css_file Path to a \pkg{xaringanthemer} CSS file, from which the
#' theme variables and values will be inferred. In general, if you use the
#' \pkg{xaringathemer} defaults, you will not need to set this. This feature
#' lets you create a \pkg{ggplot2} theme for your \pkg{xaringan} slides, even
#' if you have only saved your theme CSS file and you aren't creating your
#' CSS theme with \pkg{xaringanthemer} in your slides' source file.
#' @inheritDotParams theme_xaringan_base
#'
#' @examples
@@ -37,9 +43,10 @@ theme_xaringan <- function(
background_color = NULL,
accent_color = NULL,
accent_secondary_color = NULL,
css_file = NULL,
...
) {
requires_xaringanthemer_env()
requires_xaringanthemer_env(css_file = css_file, try_css = TRUE)
requires_package(fn = "xaringan_theme")

background_color <- background_color %||% xaringanthemer_env$background_color
@@ -72,6 +79,7 @@ theme_xaringan <- function(
#' `header_color`
#' @param accent_secondary_color Color for secondary accents, inherits from
#' `text_bold_color`
#' @inheritParams theme_xaringan
#' @inheritDotParams theme_xaringan_base
#'
#' @examples
@@ -634,14 +642,17 @@ register_font <- function(
family <- gsub("['\"]", "", family)

if (!identical(xaringanthemer_env$showtext_auto, TRUE)) {
if (requires_package(pkg = "showtext", fn, required = FALSE)) {
showtext::showtext_auto()
} else {
if (!requires_package(pkg = "showtext", fn, required = FALSE)) {
return(family)
}
showtext::showtext_auto()
xaringanthemer_env$showtext_auto <- TRUE
}

if (family %in% theme_xaringan_get_value("registered_font_families") %||% "") {
return(family)
}

if (!requires_package(pkg = "sysfonts", fn, required = FALSE)) {
return(family)
} else if (!family %in% sysfonts::font_families()) {
@@ -650,19 +661,34 @@ register_font <- function(
"Source Code Pro",
"Yanone Kaffeesatz"
)
if (identical(google, TRUE) || is_default_font) {
font_found <- family %in% sysfonts::font_families()
is_google_font <- identical(google, TRUE) || is_default_font
if (is_google_font) {
tryCatch(
sysfonts::font_add_google(family, ...),
error = function(e) warning(e$message),
warning = function(w) warning(w$message)
{
sysfonts::font_add_google(family, ...)
font_found <- TRUE
},
error = function(e) {},
warning = function(w) {}
)
} else {
warning(paste(
"Please manually register fonts not served by Google Fonts.",
"See `sysfonts::font_add()` for more information."
))
}
if (!font_found) { # warn user if font still not found
msg <- if (is_google_font) glue::glue(
"Font '{family}' not found in Google Fonts. ",
"Please manually register the font using `sysfonts::font_add()`."
) else {
glue::glue(
"Font '{family}' must be manually registered using `sysfonts::font_add()`."
)
}
warning(msg, call. = FALSE)
}
}
xaringanthemer_env[["registered_font_families"]] <- c(
xaringanthemer_env[["registered_font_families"]],
family
)
family
}

@@ -683,11 +709,20 @@ requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
invisible(TRUE)
}

requires_xaringanthemer_env <- function() {
if (!exists("xaringanthemer_env") || is.null(xaringanthemer_env$header_color)) {
requires_xaringanthemer_env <- function(css_file = NULL, try_css = TRUE) {
reload <- !is.null(css_file) && isTRUE(try_css)
if (reload || !exists("xaringanthemer_env") || is.null(xaringanthemer_env$header_color)) {
if (try_css) {
css_vars <- read_css_vars(css_file)
for (css_var in names(css_vars)) {
xaringanthemer_env[[css_var]] <- css_vars[[css_var]]
}
return(requires_xaringanthemer_env(try_css = FALSE))
} else {
stop("Please call a xaringanthemer theme function first.")
}
}
}

#' Get the Value of xaringanthemer Style Setting
#'
@@ -759,9 +794,10 @@ requires_xaringanthemer_env <- function() {
#' - `title_slide_text_color`
#'
#' @param setting A xaringanthemer style setting
#' @inheritParams theme_xaringan
#' @export
theme_xaringan_get_value <- function(setting) {
requires_xaringanthemer_env()
theme_xaringan_get_value <- function(setting, css_file = NULL) {
requires_xaringanthemer_env(css_file = css_file)
if (length(setting) > 1) {
xaringanthemer_env[setting]
} else {

+ 8
- 0
man/theme_xaringan.Rd Просмотреть файл

@@ -9,6 +9,7 @@ theme_xaringan(
background_color = NULL,
accent_color = NULL,
accent_secondary_color = NULL,
css_file = NULL,
...
)
}
@@ -24,6 +25,13 @@ theme_xaringan(
\item{accent_secondary_color}{Color for secondary accents, inherits from
\code{text_bold_color}}

\item{css_file}{Path to a \pkg{xaringanthemer} CSS file, from which the
theme variables and values will be inferred. In general, if you use the
\pkg{xaringathemer} defaults, you will not need to set this. This feature
lets you create a \pkg{ggplot2} theme for your \pkg{xaringan} slides, even
if you have only saved your theme CSS file and you aren't creating your
CSS theme with \pkg{xaringanthemer} in your slides' source file.}

\item{...}{
Arguments passed on to \code{\link[=theme_xaringan_base]{theme_xaringan_base}}
\describe{

+ 8
- 1
man/theme_xaringan_get_value.Rd Просмотреть файл

@@ -4,10 +4,17 @@
\alias{theme_xaringan_get_value}
\title{Get the Value of xaringanthemer Style Setting}
\usage{
theme_xaringan_get_value(setting)
theme_xaringan_get_value(setting, css_file = NULL)
}
\arguments{
\item{setting}{A xaringanthemer style setting}

\item{css_file}{Path to a \pkg{xaringanthemer} CSS file, from which the
theme variables and values will be inferred. In general, if you use the
\pkg{xaringathemer} defaults, you will not need to set this. This feature
lets you create a \pkg{ggplot2} theme for your \pkg{xaringan} slides, even
if you have only saved your theme CSS file and you aren't creating your
CSS theme with \pkg{xaringanthemer} in your slides' source file.}
}
\description{
A helper function to retrieve the value of style settings as set by a

+ 24
- 0
tests/testthat/test-css.R Просмотреть файл

@@ -0,0 +1,24 @@
test_that("read theme settings from css variables", {
expected_vars <- list(
background_color = "#E9EBEE",
text_color = "#18273F",
header_color = "#23395b",
text_bold_color = "#23395b",
inverse_background_color = "#23395b",
inverse_text_color = "#E9EBEE",
inverse_header_color = "#E9EBEE",
text_font_size = "20px",
header_h3_font_size = "35px",
text_font_family = "'Noto Serif'",
text_font_is_google = TRUE,
header_font_family = "'Yanone Kaffeesatz'",
header_font_is_google = FALSE,
code_font_family = "'Source Code Pro'",
code_font_is_google = FALSE
)

css_vars <- read_css_vars(testthat::test_path("css/mono_light.css"))
for (var in names(expected_vars)) {
expect_equal(!!css_vars[[var]], !!expected_vars[[var]])
}
})

Загрузка…
Отмена
Сохранить