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

Add BS4 templates and assets (with help from @jayhesselberth). #1536

Merged
merged 67 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
ca23faf
start
maelle Feb 25, 2021
e3eeead
oops
maelle Feb 25, 2021
d7d0268
fix tests
maelle Feb 26, 2021
ece77a8
can't change current sites or it will break CSS rules
maelle Feb 26, 2021
5fe762e
fixes
maelle Feb 26, 2021
129f562
update toc
maelle Feb 26, 2021
4be62af
repair
maelle Feb 26, 2021
c036c52
fix class size
maelle Feb 26, 2021
3b69236
call get_bs_version only once
maelle Feb 26, 2021
4895fdf
oops
maelle Feb 26, 2021
fe6ca22
try passing logo
maelle Feb 26, 2021
ca122b9
logo + back to vanilla :cry:
maelle Feb 26, 2021
46a41c6
bye bye logo hack
maelle Feb 26, 2021
e225f98
a bit too big
maelle Feb 26, 2021
48e9194
Merge branch 'master' into null-or
maelle Feb 26, 2021
f3d5984
smaller logo
maelle Mar 8, 2021
33267e7
user-friendlier error when Bootswatch theme does not exist
maelle Mar 8, 2021
41842cb
README fixes
maelle Mar 8, 2021
0c0b6ff
fix for bs3
maelle Mar 8, 2021
8ff9de9
opt-out of highlighting css
maelle Mar 8, 2021
1fae4d7
colors from arrow theme
maelle Mar 8, 2021
556e7ae
clean
maelle Mar 8, 2021
d958c28
color contrast
maelle Mar 8, 2021
64cc128
add form label for accessibility
maelle Mar 8, 2021
ce32f45
update via gist
maelle Mar 8, 2021
10ff8cb
add logo to other mds
maelle Mar 8, 2021
0e476b1
rm logo stuff
maelle Mar 11, 2021
e5dd502
styling
maelle Mar 11, 2021
005545e
re-add header
maelle Mar 11, 2021
f32859d
indent
maelle Mar 11, 2021
91fe0e9
fix bg
maelle Mar 11, 2021
6f7e9a7
name 3d argument
maelle Mar 11, 2021
16bb529
less vertical space
maelle Mar 11, 2021
c3a0150
early return
maelle Mar 11, 2021
7450ce4
simplify code
maelle Mar 11, 2021
8043a9b
one function again
maelle Mar 11, 2021
23220cc
{}
maelle Mar 11, 2021
54b90a9
early return ok
maelle Mar 11, 2021
753b9e0
a bit less space
maelle Mar 11, 2021
c4cb7b0
font size for TOC
maelle Mar 11, 2021
caab045
navbar space
maelle Mar 11, 2021
62e2afc
active experiment
maelle Mar 11, 2021
35404e8
fix reference
maelle Mar 11, 2021
3a2406d
class='pb-2 mt-4 mb-2 border-bottom'
maelle Mar 11, 2021
0dae354
syntax highlighting tweaks
maelle Mar 11, 2021
9d28738
namespace
maelle Mar 11, 2021
6f34532
fix note
maelle Mar 11, 2021
f6cc38d
oops
maelle Mar 11, 2021
0c255ef
typo
maelle Mar 11, 2021
82ca3dd
h top
maelle Mar 11, 2021
23c6214
h
maelle Mar 11, 2021
24d2c72
less dramatic
maelle Mar 11, 2021
6aee7f2
update news
maelle Mar 12, 2021
8df0bc1
actual drop down fix
maelle Mar 12, 2021
d4c2346
Update NEWS.md
maelle Mar 12, 2021
eaec19e
Update NEWS.md
maelle Mar 12, 2021
5100517
toc
maelle Mar 12, 2021
0ee04a1
place-holder link
maelle Mar 12, 2021
c18764c
feedback + indent
maelle Mar 15, 2021
08c18a2
oops
maelle Mar 15, 2021
325d8b8
clean CSS
maelle Mar 15, 2021
aeba340
this is not supposed to have changed
maelle Mar 15, 2021
a27321c
refix indent
maelle Mar 15, 2021
34c2cfc
fix
maelle Mar 15, 2021
f150435
should not change
maelle Mar 15, 2021
6690d0a
shouldn't change
maelle Mar 15, 2021
39a0914
no
maelle Mar 15, 2021
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
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ BugReports: https://github.com/r-lib/pkgdown/issues
Depends:
R (>= 3.1.0)
Imports:
bslib,
maelle marked this conversation as resolved.
Show resolved Hide resolved
callr (>= 2.0.2),
crayon,
desc,
digest,
downlit,
fs (>= 1.3.0),
htmltools,
httr (>= 1.4.2),
magrittr,
memoise,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export(template_navbar)
export(template_reference)
import(fs)
import(rlang)
importFrom(htmltools,tagList)
importFrom(htmltools,tags)
importFrom(magrittr,"%>%")
importFrom(memoise,memoise)
importFrom(utils,installed.packages)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* pkgdown now support Bootstrap 4 (@jayhesselberth)
* `build_articles()` again sets the `theme` argument of the document format
to `NULL` when `as_is: true` but lets users override this via the `theme`
argument of the output format.
Expand Down
3 changes: 2 additions & 1 deletion R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ build_home_index <- function(pkg = ".", quiet = TRUE) {
dst_path,
tweak_homepage_html,
strip_header = strip_header,
sidebar = !isFALSE(pkg$meta$home$sidebar)
sidebar = !isFALSE(pkg$meta$home$sidebar),
bs_version = pkg$bs_version
)

invisible()
Expand Down
44 changes: 36 additions & 8 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ data_news <- function(pkg = ".") {
}

html <- sections %>%
purrr::walk2(versions, tweak_news_heading, timeline = timeline) %>%
purrr::walk2(
versions,
tweak_news_heading,
timeline = timeline,
bs_version = pkg$bs_version
) %>%
purrr::map_chr(as.character) %>%
purrr::map_chr(repo_auto_link, pkg = pkg)

Expand Down Expand Up @@ -256,28 +261,51 @@ pkg_timeline <- function(package) {
)
}

tweak_news_heading <- function(x, versions, timeline) {
x %>%
tweak_news_heading <- function(x, versions, timeline, bs_version) {

x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_set_attr("class", "page-header")
xml2::xml_set_attr(
"class",
switch(
as.character(bs_version),
'3' = "page-header",
'4' = "pb-2 mt-4 mb-2 border-bottom"
)
)
maelle marked this conversation as resolved.
Show resolved Hide resolved

x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_set_attr("data-toc-text", versions)

if (is.null(timeline))
if (is.null(timeline)) {
return(x)
}

date <- timeline$date[match(versions, timeline$version)]
date_str <- ifelse(is.na(date), "Unreleased", as.character(date))

date_nodes <- paste(" <small>", date_str, "</small>", collapse = "") %>%
if (bs_version == 3) {
date_nodes <- paste(" <small>", date_str, "</small>", collapse = "") %>%
xml2::read_html() %>%
xml2::xml_find_all(".//small")

x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_add_child(date_nodes, .where = 1)
} else {
cran_release_string <- sprintf(
"<h6 class='text-muted' data-toc-skip> CRAN release: %s</h6>",
date_str
)
maelle marked this conversation as resolved.
Show resolved Hide resolved
date_nodes <- cran_release_string %>%
xml2::read_html() %>%
xml2::xml_find_all(".//small")
xml2::xml_find_all(".//h6")

x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_add_child(date_nodes, .where = 1)
xml2::xml_add_sibling(date_nodes, .where = "after")
}

invisible()
}
Expand Down
2 changes: 1 addition & 1 deletion R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build_docsearch_json <- function(pkg = ".") {
"config",
"docsearch",
ext = ".json",
bs_version = get_bs_version(pkg)
bs_version = pkg$bs_version
)

json <- render_template(template, data)
Expand Down
11 changes: 9 additions & 2 deletions R/html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ tweak_rmarkdown_html <- function(html, input_path, pkg = pkg) {
invisible()
}

tweak_homepage_html <- function(html, strip_header = FALSE, sidebar = TRUE) {
tweak_homepage_html <- function(
html, strip_header = FALSE, sidebar = TRUE,
bs_version
) {
maelle marked this conversation as resolved.
Show resolved Hide resolved

html <- tweak_sidebar_html(html, sidebar = sidebar)

Expand All @@ -172,7 +175,11 @@ tweak_homepage_html <- function(html, strip_header = FALSE, sidebar = TRUE) {
if (strip_header) {
xml2::xml_remove(header, free = TRUE)
} else {
page_header_text <- paste0("<div class='page-header'>", header, "</div>")
page_header_text <- if (bs_version == 3) {
paste0("<div class='page-header'>", header, "</div>")
maelle marked this conversation as resolved.
Show resolved Hide resolved
} else {
sprintf("<div class='pb-2 mt-4 mb-2 border-bottom'>%s</div>", header)
}
page_header <- xml2::read_html(page_header_text) %>% xml2::xml_find_first("//div")
xml2::xml_replace(header, page_header)
}
Expand Down
8 changes: 6 additions & 2 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ copy_assets <- function(pkg = ".") {

# Copy default assets
if (!identical(template$default_assets, FALSE)) {
copy_asset_dir(pkg, path_pkgdown("assets"))
copy_asset_dir(pkg, path_pkgdown("assets", paste0("BS", pkg$bs_version)))
}

# Copy extras
Expand All @@ -74,8 +74,12 @@ copy_assets <- function(pkg = ".") {
if (!is.null(template$package)) {
copy_asset_dir(
pkg,
path_package_pkgdown(template$package, bs_version = NULL, "assets")
path_package_pkgdown(
template$package,
bs_version = pkg$bs_version,
"assets"
)
)
}
}

Expand Down
131 changes: 121 additions & 10 deletions R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ data_navbar <- function(pkg = ".", depth = 0L) {
left <- navbar$left %||% components[left_comp]
right <- navbar$right %||% components[right_comp]

if (pkg$bs_version == 3) {
return(
list(
type = navbar$type %||% "default",
left = render_navbar_links(left, depth = depth, bs_version = pkg$bs_version),
right = render_navbar_links(right, depth = depth, bs_version = pkg$bs_version)
)
maelle marked this conversation as resolved.
Show resolved Hide resolved
)
}

list(
type = navbar$type %||% "default",
left = render_navbar_links(left, depth = depth),
right = render_navbar_links(right, depth = depth)
type = navbar$type %||% "light",
bg = navbar$bg %||% "light",
left = render_navbar_links(left, depth = depth, pkg$bs_version),
right = render_navbar_links(right, depth = depth, pkg$bs_version)
)
}

render_navbar_links <- function(x, depth = 0L) {
render_navbar_links <- function(x, depth = 0L, bs_version) {
stopifnot(is.integer(depth), depth >= 0L)

tweak <- function(x) {
Expand All @@ -43,7 +54,13 @@ render_navbar_links <- function(x, depth = 0L) {
if (depth != 0L) {
x <- lapply(x, tweak)
}
rmarkdown::navbar_links_html(x)

if (bs_version == 3) {
return(rmarkdown::navbar_links_html(x))
}

bs4_navbar_links_html(x)

}

# Default navbar ----------------------------------------------------------
Expand Down Expand Up @@ -146,15 +163,15 @@ menu_text <- function(text) {
list(text = text)
}
menu_spacer <- function() {
menu_text("---------")
menu_text("---------")
}


# Testing helpers ---------------------------------------------------------
# Simulate minimal package structure so we can more easily test

pkg_navbar <- function(meta = NULL, vignettes = pkg_navbar_vignettes(),
github_url = NULL) {
github_url = NULL) {
structure(
list(
package = "test",
Expand All @@ -168,11 +185,105 @@ pkg_navbar <- function(meta = NULL, vignettes = pkg_navbar_vignettes(),
}

pkg_navbar_vignettes <- function(
name = character(),
title = NULL,
file_out = NULL) {
name = character(),
maelle marked this conversation as resolved.
Show resolved Hide resolved
title = NULL,
file_out = NULL) {
title <- title %||% paste0("Title ", name)
file_out <- file_out %||% paste0(name, ".html")

tibble::tibble(name = name, title = title, file_out)
}

bs4_navbar_links_html <- function(links) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
as.character(bs4_navbar_links_tags(links))
}

#' @importFrom htmltools tags tagList
bs4_navbar_links_tags <- function(links, depth = 0L) {

if (is.null(links)) {
return(tagList())
}

# sub-menu
is_submenu <- (depth > 0L)

# function for links
tackle_link <- function(x, is_submenu, depth) {

if (!is.null(x$menu)) {

if (is_submenu) {
menu_class <- "dropdown-item"
link_text <- bs4_navbar_link_text(x)
} else {
menu_class <- "nav-item dropdown"
link_text <- bs4_navbar_link_text(x)
}

submenuLinks <- bs4_navbar_links_tags(x$menu, depth = depth + 1L)

return(
tags$li(
class = menu_class,
tags$a(
href = "#", class = "nav-link dropdown-toggle",
`data-toggle` = "dropdown", role = "button",
`aria-expanded` = "false", `aria-haspopup` = "true",
link_text
),
tags$div(
class = "dropdown-menu",
`aria-labelledby` ="navbarDropdown",
submenuLinks
)
)
)

}

if (!is.null(x$text) && grepl("^\\s*-{3,}\\s*$", x$text)) {
# divider
return(tags$div(class = "dropdown-divider"))
}

if (!is.null(x$text) && is.null(x$href)) {
# header
return(tags$h6(class = "dropdown-header", `data-toc-skip` = NA, x$text))
}

# standard menu item
textTags <- bs4_navbar_link_text(x)

if (is_submenu) {
return(tags$a(class = "dropdown-item", href = x$href, textTags))
}

tags$li(
class = "nav-item",
tags$a(class = "nav-link", href = x$href, textTags)
)

}

tags <- lapply(links, tackle_link, is_submenu = is_submenu, depth = depth)
tagList(tags)

}

bs4_navbar_link_text <- function(x, ...) {

if (!is.null(x$icon)) {
# find the iconset
split <- strsplit(x$icon, "-")
if (length(split[[1]]) > 1) {
iconset <- split[[1]][[1]]
}
else {
iconset <- ""
}
tagList(tags$span(class = paste(iconset, x$icon)), " ", x$text, ...)
}
else
tagList(x$text, ...)
}
10 changes: 7 additions & 3 deletions R/package.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ as_pkgdown <- function(pkg = ".", override = list()) {

install_metadata <- meta$deploy$install_metadata %||% FALSE

structure(
list(
pkg_list <- list(
package = package,
version = version,

Expand All @@ -62,7 +61,12 @@ as_pkgdown <- function(pkg = ".", override = list()) {
topics = package_topics(pkg, package),
tutorials = package_tutorials(pkg, meta),
vignettes = package_vignettes(pkg)
),
)

pkg_list$bs_version <- get_bs_version(pkg_list)

structure(
pkg_list,
class = "pkgdown"
)
}
Expand Down
Loading