Skip to content

Commit

Permalink
fix(template.bslib): Support bootswatch item (#2483)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Apr 22, 2024
1 parent 910bb1d commit 79e159e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* New translation for "Search site", the label applied to the search box for screenreaders. This was previously incorrectly labelled as "Toggle navigation" (#2320).
* You can now choose where the search box is placed with the "search" navbar component. This has been documented for a very long time, but as far as I can tell, never worked (#2320). If you have made your own template with a custom `navbar`, you will need to remove the `<form>` with `role="search"` to avoid getting two search boxes.
* The mobile version of pkgdown sites no longer has a scrollburglar (a small amount of horizontal scroll) (#2179, @netique).
* The `template.bslib` item now also accepts a `bootswatch` key (@gadenbuie, #2483).

# pkgdown 2.0.9

Expand Down
40 changes: 23 additions & 17 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ bs_theme <- function(pkg = ".") {

bs_theme_args <- pkg$meta$template$bslib %||% list()
bs_theme_args[["version"]] <- pkg$bs_version
# In bslib >= 0.5.1, bs_theme() takes bootstrap preset theme via `preset`
bs_theme_args[["preset"]] <- get_bslib_theme(pkg)
bs_theme_args[["bootswatch"]] <- NULL

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

Expand Down Expand Up @@ -91,31 +93,35 @@ highlight_styles <- function() {
}

get_bslib_theme <- function(pkg) {
preset <- pkg$meta[["template"]]$bslib$preset
themes <- list(
"template.bslib.preset" = pkg$meta[["template"]]$bslib$preset,
"template.bslib.bootswatch" = pkg$meta[["template"]]$bslib$bootswatch,
"template.bootswatch" = pkg$meta[["template"]]$bootswatch,
# Historically (< 0.2.0), bootswatch wasn't a top-level template field
"template.params.bootswatch" = pkg$meta[["template"]]$params$bootswatch
)

if (!is.null(preset)) {
check_bslib_theme(preset, pkg, c("template", "bslib", "preset"))
return(preset)
}
is_present <- !purrr::map_lgl(themes, is.null)
n_present <- sum(is_present)
n_unique <- length(unique(themes[is_present]))

bootswatch <-
pkg$meta[["template"]]$bootswatch %||%
# Historically (< 0.2.0), bootswatch wasn't a top-level template field
pkg$meta[["template"]]$params$bootswatch
if (n_present == 0) {
return("default")
}

if (!is.null(bootswatch)) {
check_bslib_theme(bootswatch, pkg, c("template", "bootswatch"))
return(bootswatch)
if (n_present > 1 && n_unique > 1) {
cli::cli_warn(c(
"Multiple Bootstrap preset themes were set. Using {.val {themes[is_present][[1]]}} from {.field {names(themes)[is_present][1]}}.",
x = "Found {.and {.field {names(themes)[is_present]}}}.",
i = "Remove extraneous theme declarations to avoid this warning."
))
}

"default"
field <- names(themes)[which(is_present)[1]]
check_bslib_theme(themes[[field]], pkg, strsplit(field, ".")[[1]])
}

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),
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/_snaps/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
Error in `as_pkgdown()`:
! Both template.bootstrap and template.bslib.version are set.

# Warns when Bootstrap theme is specified in multiple locations

Multiple Bootstrap preset themes were set. Using "flatly" from template.bslib.preset.
x Found template.bslib.preset, template.bslib.bootswatch, template.bootswatch, and template.params.bootswatch.
i Remove extraneous theme declarations to avoid this warning.

2 changes: 1 addition & 1 deletion tests/testthat/test-render.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("check_bslib_theme() works", {
pkg <- as_pkgdown(test_path("assets/reference"))
expect_equal(check_bslib_theme("_default", pkg, bs_version = 4), "default")
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")))
Expand Down
64 changes: 64 additions & 0 deletions tests/testthat/test-templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,67 @@ test_that("Valid local Bootstrap version masks invalid template package", {
)))
)
})

# Bootstrap theme resolution ----------------------------------------------
test_that("Finds Bootstrap theme in all the places", {
pkg_sketchy <- local_pkgdown_site(meta = '
template:
bslib:
preset: sketchy
version: 5
')
pkg_superhero <- local_pkgdown_site(meta = '
template:
bslib:
preset: superhero
version: 5
')
pkg_cosmo <- local_pkgdown_site(meta = '
template:
bootstrap: 5
bootswatch: cosmo
')

pkg_yeti <- local_pkgdown_site(meta = '
template:
bootstrap: 5
params:
bootswatch: yeti
')

expect_equal(get_bslib_theme(pkg_sketchy), "sketchy")
expect_equal(get_bslib_theme(pkg_superhero), "superhero")
expect_equal(get_bslib_theme(pkg_cosmo), "cosmo")
expect_equal(get_bslib_theme(pkg_yeti), "yeti")
})

test_that("Warns when Bootstrap theme is specified in multiple locations", {
pkg <- local_pkgdown_site(meta = '
template:
bootstrap: 5
bootswatch: cerulean
bslib:
preset: flatly
bootswatch: lux
params:
bootswatch: darkly
')

expect_snapshot_warning(
get_bslib_theme(pkg)
)
})

test_that("Doesn't warn when the same Bootstrap theme is specified in multiple locations", {
pkg <- local_pkgdown_site(meta = '
template:
bootswatch: cerulean
bslib:
preset: cerulean
')

expect_equal(
expect_silent(get_bslib_theme(pkg)),
"cerulean"
)
})

0 comments on commit 79e159e

Please sign in to comment.