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 3 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
4 changes: 3 additions & 1 deletion 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 Expand Up @@ -65,5 +67,5 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.1.1.9001
SystemRequirements: pandoc
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)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pkgdown (development version)

* pkgdown now support Bootstrap 4 (@jayhesselberth)

* Authors configuration is more flexible. Users can now:
* Choose the roles used for filtering authors for the sidebar and footer.
* Choose the text before authors in the footer.
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 = get_bs_version(pkg)
maelle marked this conversation as resolved.
Show resolved Hide resolved
)

invisible()
Expand Down
12 changes: 8 additions & 4 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pkg_timeline <- function(package) {
tweak_news_heading <- function(x, versions, timeline) {
x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_set_attr("class", "page-header")
xml2::xml_set_attrs(c("class" = "pb-2 mt-4 mb-2 border-bottom"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only difference? If so, I think it would be better to keep a single function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no but there are only two differences (I had actually hesitated at the time) so I made it one single function again.


x %>%
xml2::xml_find_all(".//h1") %>%
Expand All @@ -271,13 +271,17 @@ tweak_news_heading <- function(x, versions, timeline) {
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 = "") %>%
cran_release_string <- sprintf(
"<h6 class='text-muted' data-toc-skip> CRAN release: %s</h6>",
date_str
)
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
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
14 changes: 12 additions & 2 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ copy_assets <- function(pkg = ".") {

# Copy default assets
if (!identical(template$default_assets, FALSE)) {
copy_asset_dir(pkg, path_pkgdown("assets"))
copy_asset_dir(
maelle marked this conversation as resolved.
Show resolved Hide resolved
pkg,
path_pkgdown(
"assets",
paste0("BS", get_bs_version(pkg))
)
)
}

# Copy extras
Expand All @@ -74,8 +80,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 = get_bs_version(pkg),
"assets"
)
)
}
}

Expand Down
121 changes: 116 additions & 5 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 (get_bs_version(pkg) == 3) {
return(
list(
type = navbar$type %||% "default",
left = render_navbar_links(left, depth = depth, get_bs_version(pkg)),
right = render_navbar_links(right, depth = depth, get_bs_version(pkg))
)
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, get_bs_version(pkg)),
right = render_navbar_links(right, depth = depth, get_bs_version(pkg))
)
}

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 @@ -176,3 +193,97 @@ pkg_navbar_vignettes <- function(

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) {

# sub-menu
is_submenu <- depth > 0L

if (!is.null(links)) {
maelle marked this conversation as resolved.
Show resolved Hide resolved

tags <- lapply(links, function(x) {

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)

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
)
)

} else if (!is.null(x$text) && grepl("^\\s*-{3,}\\s*$", x$text)) {

# divider
tags$div(class = "dropdown-divider")

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

} else {
# standard menu item
textTags <- bs4_navbar_link_text(x)

if (is_submenu) {
tags$a(
class = "dropdown-item",
href = x$href,
textTags
)
} else {
tags$li(
class = "nav-item",
tags$a(
class = "nav-link",
href = x$href,
textTags
)
)
}
}
})
tagList(tags)
} else {
tagList()
}
}

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

if (!is.null(x$icon)) {
# find the iconset
split <- strsplit(x$icon, "-")
if (length(split[[1]]) > 1)
maelle marked this conversation as resolved.
Show resolved Hide resolved
iconset <- split[[1]][[1]]
else
iconset <- ""
tagList(tags$span(class = paste(iconset, x$icon)), " ", x$text, ...)
}
else
tagList(x$text, ...)
}
57 changes: 55 additions & 2 deletions R/render.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Render page with template
#'
#' Each page is composed of four templates: "head", "header", "content", and
#' Each page is composed of four templates: "head", "content", and
#' "footer". Each of these templates is rendered using the `data`, and
#' then assembled into an overall page using the "layout" template.
#'
Expand Down Expand Up @@ -42,9 +42,12 @@ render_page <- function(pkg = ".", name, data, path = "", depth = NULL, quiet =

data$footer <- pkgdown_footer(data, pkg)

# Dependencies for head
data$headdeps <- data_deps(pkg = pkg, depth = depth)

# render template components
pieces <- c(
"head", "navbar", "header", "content", "docsearch", "footer",
maelle marked this conversation as resolved.
Show resolved Hide resolved
"head", "navbar", "content", "docsearch", "footer",
"in-header", "after-head", "before-body", "after-body"
)

Expand Down Expand Up @@ -370,3 +373,53 @@ footer_pkgdown <- function(data) {
data$pkgdown$version, "."
)
}

data_deps <- function(pkg, depth) {

# theme variables from configuration
bs_version <- get_bs_version(pkg)
bootswatch_theme <- pkg$meta[["template"]]$bootswatch %||% NULL
bs_theme <- do.call(
bslib::bs_theme,
c(
list(
version = bs_version,
bootswatch = bootswatch_theme
),
maelle marked this conversation as resolved.
Show resolved Hide resolved
pkg$meta[["template"]]$bslib
)
)
deps <- bslib::bs_theme_dependencies(bs_theme)
# Add other dependencies - TODO: more of those?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related to #1541

# Even font awesome had a too old version in R Markdown (no ORCID)

# Dependencies files end up at the website root in a deps folder
deps <- lapply(
deps,
htmltools::copyDependencyToDir,
file.path(pkg$dst_path, "deps")
)

# Function needed for indicating where that deps folder is compared to here
transform_path <- function(x) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and the navbar link creation might need to share logic? not sure. in any case both involve depth.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an fs function for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now read fs docs again, and I don't see any good fit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs::path_rel() doesn't help?


x <- gsub(pkg$dst_path, "", x)

if (depth == 0) {
return(sub("/", "", x))
}

paste0(
paste0(rep("..", depth), collapse = "/"), # as many levels up as depth
x
)

}

# Tags ready to be added in heads
htmltools::renderDependencies(
maelle marked this conversation as resolved.
Show resolved Hide resolved
deps,
srcType = "file",
hrefFilter = transform_path
)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
2 changes: 1 addition & 1 deletion inst/assets/pkgdown.js → inst/assets/BS3/pkgdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
$(document).ready(function() {
var copyButton = "<button type='button' class='btn btn-primary btn-copy-ex' type = 'submit' title='Copy to clipboard' aria-label='Copy to clipboard' data-toggle='tooltip' data-placement='left auto' data-trigger='hover' data-clipboard-copy><i class='fa fa-copy'></i></button>";

$(".examples, div.sourceCode").wrap("<div class='hasCopyButton'></div>");
maelle marked this conversation as resolved.
Show resolved Hide resolved
$(".examples, div.sourceCode").addClass("hasCopyButton");

// Insert copy buttons:
$(copyButton).prependTo(".hasCopyButton");
Expand Down
Loading