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 footer flexibility & markdown for custom components #1502

Merged
merged 23 commits into from
Feb 19, 2021
Merged
10 changes: 8 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# pkgdown (development version)

* Make footer specification more flexible: users can now
* change the placement of elements on the left and right
* add sentences to the left and right
(#1502)

* pkgdown can now use the templates "in-header.html"/"after-head.html", "before-body.html" and
"after-body.html" whose content will be placed
(similarly to bookdown options `in_header`, `before_body` and `after_body`),
Expand All @@ -9,11 +14,12 @@ right below the opening `<body>` tag; and before the closing tag `</body>` (#148
* Links for GitHub Enterprise and GitLab Enterprise repositories are detected
by assuming such host address begin with `github.` or `gitlab.`
(@ijlyttle, #1452).

* Make sidebar specification more flexible: users can now
* change the order of sidebar elements
* add custom sidebar sections (title, text that has to be HTML)
* add custom sidebar sections (title, text that can be Markdown or HTML)
* completely suppress the navbar (even "Dev status")
* provide their own HTML for the navbar. (#1443, #1488)
* provide their own HTML for the navbar. (#1443, #1488, #1502)


* Protect the rules drawn by the CLI (as for example, in `build_site()`) against
Expand Down
23 changes: 4 additions & 19 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,7 @@ data_home_sidebar <- function(pkg = ".") {
missing <- setdiff(sidebar_structure, names(sidebar_components))

if (length(missing) > 0) {
missing_components <- lapply(
missing, append,
c("home", "sidebar", "components"),
after = 0
)
missing_fields <- pkgdown_fields(pkg = pkg, fields = missing_components)

abort(
sprintf(
"Can't find component%s %s.",
if (length(missing) > 1) "s" else "",
paste0(
missing_fields, collapse = " nor "
)
)
)
abort_missing(missing, where = c("home", "sidebar", "components"), pkg)
maelle marked this conversation as resolved.
Show resolved Hide resolved
}

sidebar_final_components <- purrr::compact(
Expand All @@ -135,20 +120,20 @@ default_sidebar_structure <- function() {

data_home_component <- function(component, component_name, pkg) {

if (!all(c("title", "html") %in% names(component))) {
if (!all(c("title", "text") %in% names(component))) {
maelle marked this conversation as resolved.
Show resolved Hide resolved
abort(
sprintf(
"Can't find %s for the component %s",
paste0(
c("title", "html")[!c("title", "html") %in% names(component)],
c("title", "text")[!c("title", "text") %in% names(component)],
collapse = " nor "
),
pkgdown_field(pkg = pkg, "home", "sidebar", "components", component_name)
)
)
}

sidebar_section(component$title, bullets = component$html)
sidebar_section(component$title, bullets = markdown_text2(component$text))
}

data_home_sidebar_links <- function(pkg = ".") {
Expand Down
3 changes: 2 additions & 1 deletion R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
#' The example below creates a sidebar whose only components will be the
#' authors section, a custom section, and a Dev Status section if there are
#' badges.
#' The `text` can be Markdown or HTML.
maelle marked this conversation as resolved.
Show resolved Hide resolved
#'
#' ```
#' home:
Expand All @@ -135,7 +136,7 @@
#' components:
#' custom:
#' title: Funding
#' html: We are grateful for funding!
#' text: 'We are *grateful* for funding!'
maelle marked this conversation as resolved.
Show resolved Hide resolved
#' ```
#'
#' You can provide a ready-made sidebar HTML:
Expand Down
17 changes: 17 additions & 0 deletions R/build.r
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@
#' deploy:
#' install_metadata: true
#' ```
#' @section YAML config - footer:
#' By default, the footer is automatically populated with:
#' * the names of the authors `authors`, on the left;
maelle marked this conversation as resolved.
Show resolved Hide resolved
#' * a reference to pkgdown `pkgdown`, on the right.
maelle marked this conversation as resolved.
Show resolved Hide resolved
#'
#' The example below puts the authors information on the right together with
#' a legal disclaimer, and puts pkgdown on the left.
#'
#' ```
#' footer:
#' left:
#' structure: [pkgdown]
#' right:
#' structure: [authors, legal]
#' components:
#' legal: 'Provided without ***any warranty***.'
#' ```
#'
#' @section Options:
#' Users with limited internet connectivity can disable CRAN checks by setting
Expand Down
12 changes: 12 additions & 0 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@ markdown_text <- function(text, ...) {
write_lines(text, path = tmp)
markdown(tmp, ...)
}


markdown_text2 <- function(text, ...) {
maelle marked this conversation as resolved.
Show resolved Hide resolved
html <- markdown_text(text, ...)
html %>%
xml2::read_html() %>%
xml2::xml_child() %>% # body
xml2::xml_child() %>% # p
xml2::xml_contents() %>%
as.character() %>%
paste(collapse = "")
}
63 changes: 63 additions & 0 deletions R/render.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ render_page <- function(pkg = ".", name, data, path = "", depth = NULL, quiet =
data$site$root <- paste0(pkg$meta$url, "/")
}

data$footer <- pkgdown_footer(data, pkg)

# render template components
pieces <- c(
"head", "navbar", "header", "content", "docsearch", "footer",
Expand Down Expand Up @@ -301,3 +303,64 @@ check_made_by <- function(first) {
if (length(first) == 0L) return(FALSE)
grepl("<!-- Generated by pkgdown", first, fixed = TRUE)
}

pkgdown_footer <- function(data, pkg) {
footer_components <- list(
authors = footer_authors(data),
pkgdown = footer_pkgdown(data)
)

# footer left
left_structure <- pkg$meta$footer$left$structure %||% c("authors")

left_components <- utils::modifyList(
maelle marked this conversation as resolved.
Show resolved Hide resolved
footer_components,
purrr::map(pkg$meta$footer$left$components, markdown_text2) %>%
set_names(names(pkg$meta$footer$left$components))
)

missing <- setdiff(left_structure, names(left_components))

if (length(missing) > 0) {
maelle marked this conversation as resolved.
Show resolved Hide resolved
abort_missing(missing, where = c("footer", "left", "components"), pkg)
}

left_final_components <- purrr::compact(
maelle marked this conversation as resolved.
Show resolved Hide resolved
paste0(left_components[left_structure], collapse = " ")
)

# footer right
right_structure <- pkg$meta$footer$right$structure %||% c("pkgdown")

right_components <- utils::modifyList(
footer_components,
purrr::map(pkg$meta$footer$right$components, markdown_text2) %>%
set_names(names(pkg$meta$footer$right$components))
)

missing <- setdiff(right_structure, names(right_components))

if (length(missing) > 0) {
abort_missing(missing, where = c("footer", "right", "components"), pkg)
}

right_final_components <- purrr::compact(
paste0(right_components[right_structure], collapse = " ")
)

list(left = left_final_components, right = right_final_components)
}

footer_authors <- function(data) {
whisker::whisker.render(
maelle marked this conversation as resolved.
Show resolved Hide resolved
"{{#package}}Developed by {{{authors}}}.{{/package}}",
data
)
}

footer_pkgdown <- function(data) {
whisker::whisker.render(
'Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> {{#pkgdown}}{{version}}{{/pkgdown}}.',
data
)
}
19 changes: 19 additions & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ pkgdown_fields <- function(pkg, fields) {
pkgdown_field(pkg, fields)
}

abort_missing <- function(missing, where, pkg) {
missing_components <- lapply(
missing, append,
where,
after = 0
)
missing_fields <- pkgdown_fields(pkg = pkg, fields = missing_components)

abort(
sprintf(
"Can't find component%s %s.",
if (length(missing) > 1) "s" else "",
paste0(
missing_fields, collapse = " nor "
)
)
maelle marked this conversation as resolved.
Show resolved Hide resolved
)
}

#' @export
print.print_yaml <- function(x, ...) {
cat(yaml::as.yaml(x), "\n", sep = "")
Expand Down
4 changes: 2 additions & 2 deletions inst/templates/BS3/footer.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="copyright">
<p>{{#package}}Developed by {{{authors}}}.{{/package}}</p>
<p>{{#footer}}{{#left}}{{{.}}}{{/left}}{{/footer}}</p>
maelle marked this conversation as resolved.
Show resolved Hide resolved
</div>

<div class="pkgdown">
<p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> {{#pkgdown}}{{version}}{{/pkgdown}}.</p>
<p>{{#footer}}{{#right}}{{{.}}}{{/right}}{{/footer}}</p>
</div>
7 changes: 4 additions & 3 deletions man/build_home.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/build_site.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/_snaps/data_home_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
# data_home_sidebar() can get a custom component

Code
xml2::xml_find_first(result, ".//div[@class='fancy-section']")
xml2::xml_find_first(xml2::xml_find_first(result,
maelle marked this conversation as resolved.
Show resolved Hide resolved
".//div[@class='fancy-section']"), ".//ul")
Output
{html_node}
<div class="fancy-section">
[1] <h2>Fancy section</h2>
[2] <ul class="list-unstyled">\n<li>How cool is pkgdown?!</li>\n</ul>
<ul class="list-unstyled">
[1] <li>How <em>cool</em> is pkgdown?!</li>

# data_home_sidebar() outputs informative error messages

Expand All @@ -56,5 +56,5 @@

---

Can't find title nor html for the component home.sidebar.components.fancy in '_pkgdown.yml'
Can't find title nor text for the component home.sidebar.components.fancy in '_pkgdown.yml'

44 changes: 44 additions & 0 deletions tests/testthat/_snaps/pkgdown_footer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pkgdown_footer() works by default

Code
pkgdown_footer(data, pkg)
Output
$left
[1] "Developed by bla."

$right
[1] "Site built with <a href=\"https://pkgdown.r-lib.org/\">pkgdown</a> 42."


# pkgdown_footer() can use custom components

Code
pkgdown_footer(data, pkg)
Output
$left
[1] "Developed by bla. <strong><em>Wow</em></strong>"

$right
[1] "Site built with <a href=\"https://pkgdown.r-lib.org/\">pkgdown</a> 42."


---

Code
pkgdown_footer(data, pkg)
Output
$left
[1] "<strong><em>Wow</em></strong>"

$right
[1] "Site built with <a href=\"https://pkgdown.r-lib.org/\">pkgdown</a> 42."


# pkgdown_footer() throws informative error messages

Can't find component footer.left.components.pof in '_pkgdown.yml'.

---

Can't find component footer.right.components.bof in '_pkgdown.yml'.