Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for bslib > 0.5.1 #2395

Merged
merged 13 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Description: Generate an attractive and useful website from a source
License: MIT + file LICENSE
URL: https://pkgdown.r-lib.org, https://github.com/r-lib/pkgdown
BugReports: https://github.com/r-lib/pkgdown/issues
Depends:
Depends:
R (>= 3.6)
Imports:
bslib (>= 0.3.1),
Imports:
bslib (>= 0.5.1),
callr (>= 3.7.3),
cli (>= 3.6.1),
desc (>= 1.4.0),
Expand All @@ -40,7 +40,7 @@ Imports:
withr (>= 2.4.3),
xml2 (>= 1.3.1),
yaml
Suggests:
Suggests:
covr,
diffviewer,
evaluate,
Expand All @@ -58,7 +58,7 @@ Suggests:
sass,
testthat (>= 3.1.3),
tools
VignetteBuilder:
VignetteBuilder:
knitr
Config/Needs/website: usethis, servr
Config/potools/style: explicit
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Add Catalan translation (@jmaspons, #2333)
* Set RNG seed for htmlwidgets IDs. This reduces noise in pkgdown reference HTML output when examples generate htmlwidgets (@salim-b, #2294).
* Fix BS5 navbar template to get `navbar.type: dark` to work with bslib 0.6+ / Bootstrap 5.3+ (@tanho63, #2388)
* pkgdown now requires `{bslib}` >= 0.5.1 (@gadenbuie, #2395)

# pkgdown 2.0.7

Expand Down
4 changes: 2 additions & 2 deletions R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data_navbar <- function(pkg = ".", depth = 0L) {

style <- navbar_style(
navbar = navbar,
theme = get_bootswatch_theme(pkg),
theme = get_bslib_theme(pkg),
bs_version = pkg$bs_version
)

Expand All @@ -21,7 +21,7 @@ navbar_style <- function(navbar = list(), theme = "_default", bs_version = 3) {
list(type = navbar$type %||% "default")
} else {
# bg is usually light, dark, or primary, but can use any .bg-*
bg <- navbar$bg %||% bootswatch_bg[[theme]]
bg <- navbar$bg %||% purrr::pluck(bootswatch_bg, theme, .default = "light")
type <- navbar$type %||% if (bg == "light") "light" else "dark"

list(bg = bg, type = type)
Expand Down
77 changes: 50 additions & 27 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ data_deps_path <- function(pkg) {
bs_theme <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

theme <- get_bootswatch_theme(pkg)
theme <- check_bootswatch_theme(theme, pkg$bs_version, pkg)
bs_theme_args <- pkg$meta$template$bslib %||% list()
bs_theme_args[["version"]] <- pkg$bs_version
bs_theme_args[["preset"]] <- get_bslib_theme(pkg)
hadley marked this conversation as resolved.
Show resolved Hide resolved

bs_theme <- exec(bslib::bs_theme, !!!bs_theme_args)

bs_theme <- exec(bslib::bs_theme,
version = pkg$bs_version,
bootswatch = theme,
!!!pkg$meta$template$bslib
)
# Drop bs3 compat files added for shiny/RMarkdown
bs_theme <- bslib::bs_remove(bs_theme, "bs3compat")

Expand Down Expand Up @@ -92,30 +90,55 @@ highlight_styles <- function() {
path_ext_remove(path_file(paths))
}

get_bootswatch_theme <- function(pkg) {
pkg$meta[["template"]]$bootswatch %||%
pkg$meta[["template"]]$params$bootswatch %||%
"_default"
get_bslib_theme <- function(pkg) {
preset <-
pkg$meta[["template"]]$bslib$preset %||%
pkg$meta[["template"]]$params$bslib$preset

if (!is.null(preset)) {
check_bslib_theme(preset, pkg, c("template", "bslib", "preset"))
return(preset)
}

bootswatch <-
pkg$meta[["template"]]$bootswatch %||%
pkg$meta[["template"]]$params$bootswatch

if (!is.null(bootswatch)) {
check_bslib_theme(bootswatch, pkg, c("template", "bootswatch"))
return(bootswatch)
}

"default"
}

check_bootswatch_theme <- function(bootswatch_theme, bs_version, pkg) {
if (bootswatch_theme == "_default") {
NULL
} else if (bootswatch_theme %in% bslib::bootswatch_themes(bs_version)) {
bootswatch_theme
} else {
cli::cli_abort(c(
sprintf(
"Can't find Bootswatch theme {.val %s} ({.field %s}) for Bootstrap version {.val %s} ({.field %s}).",
bootswatch_theme,
pkgdown_field(pkg, c("template", "bootswatch")),
bs_version,
pkgdown_field(pkg, c("template", "bootstrap"))
),
x = "Edit settings in {.file {pkgdown_config_relpath(pkg)}}"
), call = caller_env())
check_bslib_theme <- function(theme, pkg, field = c("template", "bootswatch"), bs_version = pkg$bs_version) {
if (theme %in% c("_default", "default")) {
return("default")
}

bslib_themes <- c(
bslib::bootswatch_themes(bs_version),
bslib::builtin_themes(bs_version),
# bs_theme() recognizes both below as bare bootstrap
"default",
"bootstrap"
)

if (theme %in% bslib_themes) {
return(theme)
}

cli::cli_abort(c(
sprintf(
"Can't find Bootswatch or bslib theme preset {.val %s} ({.field %s}) for Bootstrap version {.val %s} ({.field %s}).",
theme,
pkgdown_field(pkg, field),
bs_version,
pkgdown_field(pkg, c("template", "bootstrap"))
),
x = "Edit settings in {.file {pkgdown_config_relpath(pkg)}}"
), call = caller_env())
}

bs_theme_deps_suppress <- function(deps = list()) {
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/_snaps/render.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# check_bootswatch_theme() works
# check_bslib_theme() works

Can't find Bootswatch theme "paper" (template.bootswatch) for Bootstrap version "4" (template.bootstrap).
Can't find Bootswatch or bslib theme preset "paper" (template.bootswatch) for Bootstrap version "4" (template.bootstrap).
x Edit settings in '_pkgdown.yml'

---

Can't find Bootswatch or bslib theme preset "paper" (template.preset) for Bootstrap version "4" (template.bootstrap).
x Edit settings in '_pkgdown.yml'

# capture data_template()
Expand Down
33 changes: 29 additions & 4 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
test_that("check_bootswatch_theme() works", {
test_that("check_bslib_theme() works", {
pkg <- as_pkgdown(test_path("assets/reference"))
expect_equal(check_bootswatch_theme("_default", 4, pkg), NULL)
expect_equal(check_bootswatch_theme("lux", 4, pkg), "lux")
expect_snapshot_error(check_bootswatch_theme("paper", 4, pkg))
expect_equal(check_bslib_theme("_default", pkg, bs_version = 4), "default")
expect_equal(check_bslib_theme("lux", pkg, bs_version = 4), "lux")
expect_snapshot_error(check_bslib_theme("paper", pkg, bs_version = 4))
expect_snapshot_error(check_bslib_theme("paper", pkg, bs_version = 4, field = c("template", "preset")))
})

test_that("get_bslib_theme() works with template.bslib.preset", {
pkg <- local_pkgdown_site(test_path("assets/site-empty"), '
template:
bootstrap: 5
bslib:
preset: shiny
enable-shadows: true
')

expect_equal(get_bslib_theme(pkg), "shiny")
expect_no_error(bs_theme(pkg))

pkg <- local_pkgdown_site(test_path("assets/site-empty"), '
template:
bootstrap: 5
bslib:
preset: lux
enable-shadows: true
')

expect_equal(get_bslib_theme(pkg), "lux")
expect_no_error(bs_theme(pkg))
})

test_that("capture data_template()", {
Expand Down
Loading