1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
# Class added or removed from `bs_theme` when a theme includes a preset
THEME_PRESET_CLASS <- "bs_theme_with_preset"
resolve_bs_preset <- function(
preset = NULL,
bootswatch = NULL,
version = version_default()
) {
if (is.null(preset) && is.null(bootswatch)) return(NULL)
assert_preset_scalar_string(preset)
assert_preset_scalar_string(bootswatch)
assert_preset_only_one_name_arg(preset, bootswatch)
version <- switch_version(version, five = "5", four = "4", three = "3")
preset_name <- preset %||% bootswatch
if (preset_name %in% c("default", "bootstrap")) {
# "bootstrap" means no preset bundle, just bare default Bootstrap
return(new_bs_preset("bootstrap", version))
}
builtin_themes <- builtin_themes(version)
if (length(builtin_themes) > 0 && preset_name %in% builtin_themes) {
return(new_bs_preset(preset_name, version, type = "builtin"))
}
bootswatch_themes <- bootswatch_themes(version)
if (length(bootswatch_themes) > 0 && preset_name %in% bootswatch_themes) {
return(new_bs_preset(preset_name, version, type = "bootswatch"))
}
abort_preset_unknown_name(preset_name, version)
}
new_bs_preset <- function(name, version, type = NULL, ...) {
preset <- list(
version = version, # bootstrap version
name = name, # preset name
type = type, # preset type (e.g. "builtin", "bootswatch")
...
)
structure(dropNulls(preset), class = "bs_preset")
}
# The `bs_preset_bundle()` function is the main entry point for creating a SASS
# bundle for a theme preset. This calls out to the appropriate functions to
# create a bundle for a built-in theme or for a Bootswatch theme.
bs_preset_bundle <- function(preset) {
if (!inherits(preset, "bs_preset")) return(NULL)
if (is.null(preset$type)) return(NULL)
switch(
preset$type,
bootswatch = bootswatch_bundle(preset$name, version = preset$version),
builtin = builtin_bundle(preset$name, version = preset$version),
stop("Unknown preset type: ", preset$type)
)
}
theme_preset_info <- function(theme) {
if (!is_bs_theme(theme)) return(NULL)
info <- bs_get_variables(
theme,
c("bslib-preset-type", "bslib-preset-name", "bootstrap-version")
)
name <- info[["bslib-preset-name"]]
type <- info[["bslib-preset-type"]]
new_bs_preset(
name = if (!is.na(name)) name else "bootstrap",
version = info[["bootstrap-version"]],
type = if (!is.na(type)) type
)
}
assert_preset_scalar_string <- function(var, .frame = rlang::caller_env()) {
var_name <- deparse(substitute(var))
if (is.null(var) || rlang::is_string(var)) {
return(invisible())
}
msg <- c(
sprintf(
"The preset theme `%s` must be a single character string.",
var_name
),
"x" = sprintf('Bad: `%s = c("flatly", "darkly")`', var_name),
"v" = sprintf('Good: `%s = "flatly"`', var_name)
)
rlang::abort(msg, .frame = .frame)
}
assert_preset_only_one_name_arg <- function(
preset,
bootswatch,
.frame = rlang::caller_env()
) {
both_provided <- !is.null(preset) && !is.null(bootswatch)
if (!both_provided) {
return(invisible())
}
msg <- c(
"Only one of `preset` or `bootswatch` may be provided (`preset` is the preferred choice).",
"i" = "Did you mean one of the following options?",
"*" = sprintf('`preset = "%s"`', preset),
"*" = sprintf('`preset = "%s"`', bootswatch),
"*" = sprintf('`bootswatch = "%s"`', bootswatch)
)
rlang::abort(msg, .frame = .frame)
}
abort_preset_unknown_name <- function(
name,
version,
.frame = rlang::caller_env()
) {
msg <- c(
sprintf(
"'%s' is not a known preset theme name for Bootstrap version %s.",
name,
version
),
"i" = "You can list available preset themes:",
"*" = sprintf("Built-in: `builtin_themes(%s)`", version),
"*" = sprintf("Bootswatch: `bootswatch_themes(%s)`.", version)
)
rlang::abort(msg, .frame = .frame)
}
|