Skip to content

Commit

Permalink
Introduce BS version, move templates to a subfolder by version (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Feb 12, 2021
1 parent 0c8f604 commit f58b8d2
Show file tree
Hide file tree
Showing 23 changed files with 63 additions and 8 deletions.
8 changes: 7 additions & 1 deletion R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ build_docsearch_json <- function(pkg = ".") {
url = pkg$meta$url
)

template <- find_template("config", "docsearch", ext = ".json")
template <- find_template(
"config",
"docsearch",
ext = ".json",
bs_version = get_bs_version(pkg)
)

json <- render_template(template, data)

json_path <- path(pkg$dst_path, "docsearch.json")
Expand Down
52 changes: 45 additions & 7 deletions R/render.r
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ render_page <- function(pkg = ".", name, data, path = "", depth = NULL, quiet =
# render template components
pieces <- c("head", "navbar", "header", "content", "docsearch", "footer")

templates <- purrr::map_chr(pieces, find_template, name, template_path = template_path(pkg))
templates <- purrr::map_chr(
pieces, find_template, name,
template_path = template_path(pkg),
bs_version = get_bs_version(pkg)
)
components <- purrr::map(templates, render_template, data = data)
components <- purrr::set_names(components, pieces)
components$template <- name

# render complete layout
template <- find_template("layout", name, template_path = template_path(pkg))
template <- find_template(
"layout", name,
template_path = template_path(pkg),
bs_version = get_bs_version(pkg)
)
rendered <- render_template(template, components)
write_if_different(pkg, rendered, path, quiet = quiet)
}
Expand Down Expand Up @@ -162,6 +170,31 @@ check_open_graph <- function(og) {
og[intersect(supported_fields, names(og))]
}

get_bs_version <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

template <- pkg$meta[["template"]]

if (is.null(template$bootstrap)) {
return(3)
}
if (template$bootstrap %in% c(3, 4)) {
return(template$bootstrap)
}

abort(
message = c(
"Boostrap version must be 3 or 4.",
x = sprintf(
"You specified a value of %s in %s.",
template$bootstrap,
pkgdown_field(pkg = pkg, "template", "bootstrap")
)
)
)

}

template_path <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

Expand All @@ -171,11 +204,15 @@ template_path <- function(pkg = ".") {
path <- path_abs(template$path, start = pkg$src_path)

if (!file_exists(path))
stop("Can not find template path ", src_path(path), call. = FALSE)
abort(paste0("Can not find template path ", src_path(path)))

path
} else if (!is.null(template$package)) {
path_package_pkgdown(template$package, "templates")
path_package_pkgdown(
template$package,
"templates",
paste0("BS", get_bs_version(pkg))
)
} else {
character()
}
Expand All @@ -189,10 +226,11 @@ render_template <- function(path, data) {
whisker::whisker.render(template, data)
}

find_template <- function(type, name, ext = ".html", template_path = NULL) {
find_template <- function(type, name, ext = ".html", template_path = NULL,
bs_version) {
paths <- c(
template_path,
path_pkgdown("templates")
path_pkgdown("templates", paste0("BS", bs_version))
)
names <- c(
paste0(type, "-", name, ext),
Expand All @@ -202,7 +240,7 @@ find_template <- function(type, name, ext = ".html", template_path = NULL) {
locations <- path(all$path, all$name)

Find(file_exists, locations, nomatch =
stop("Can't find template for ", type, "-", name, ".", call. = FALSE))
stop("Can't find template for ", type, "-", name, ".", call. = FALSE))
}


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/testthat/_snaps/get_bs_version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# get_bs_version gives an informative error message

Boostrap version must be 3 or 4.
x You specified a value of 5 in template.bootstrap in '_pkgdown.yml'.

6 changes: 6 additions & 0 deletions tests/testthat/test-get_bs_version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("get_bs_version gives an informative error message", {
pkg <- test_path("assets/sidebar")
pkg <- as_pkgdown(pkg)
pkg$meta$template$bootstrap <- 5
expect_snapshot_error(get_bs_version(pkg))
})

0 comments on commit f58b8d2

Please sign in to comment.