Skip to content

Commit

Permalink
Add logo, when present, to the header of all pages (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Mar 25, 2021
1 parent ff86fa3 commit 9a5a8f5
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 4 deletions.
3 changes: 3 additions & 0 deletions R/build-home-citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ build_citation_authors <- function(pkg = ".") {
source = source
)

data$before <- markdown_block(pkg$meta$authors$before, pkg = pkg)
data$after <- markdown_block(pkg$meta$authors$after, pkg = pkg)

render_page(pkg, "citation-authors", data, "authors.html")
}
3 changes: 2 additions & 1 deletion R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ build_home_index <- function(pkg = ".", quiet = TRUE) {
tweak_homepage_html,
strip_header = strip_header,
sidebar = !isFALSE(pkg$meta$home$sidebar),
bs_version = pkg$bs_version
bs_version = pkg$bs_version,
logo = logo_path(pkg, depth = 0)
)

invisible()
Expand Down
7 changes: 6 additions & 1 deletion R/build-home-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ render_md <- function(pkg, filename) {
cat_line("Reading ", src_path(path_rel(filename, pkg$src_path)))

render_page(pkg, "title-body",
data = list(pagetitle = attr(body, "title"), body = body),
data = list(
pagetitle = attr(body, "title"),
body = body,
filename = filename,
source = repo_source(pkg, fs::path_rel(filename, pkg$src_path))
),
path = path_ext_set(basename(filename), "html")
)
}
21 changes: 20 additions & 1 deletion R/html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ tweak_rmarkdown_html <- function(html, input_path, pkg = pkg) {
tweak_homepage_html <- function(html,
strip_header = FALSE,
sidebar = TRUE,
bs_version = 3) {
bs_version = 3,
logo = NULL) {

html <- tweak_sidebar_html(html, sidebar = sidebar)

Expand All @@ -207,7 +208,25 @@ tweak_homepage_html <- function(html,
page_header <- xml2::read_html(page_header_text) %>% xml2::xml_find_first("//div")
xml2::xml_replace(header, page_header)
}
# logo
if (!is.null(logo) && bs_version > 3) {
# Remove logo if added to h1
xml2::xml_remove(
xml2::xml_find_first(
html,
".//h1/img[contains(@src, 'logo')]"
)
)

# Add logo
xml2::xml_find_first(html,".//div[contains(@class,'contents')]") %>%
xml2::xml_child() %>%
xml2::xml_add_sibling(
"img", src = "package-logo.png",
id = "logo-low", alt = "", width = "120",
.where = "before"
)
}
# Fix relative image links
imgs <- xml2::xml_find_all(html, ".//img")
urls <- xml2::xml_attr(imgs, "src")
Expand Down
8 changes: 8 additions & 0 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ copy_assets <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
template <- purrr::pluck(pkg$meta, "template", .default = list())

# Copy logo
if (pkg$has_logo) {
file.copy(
find_logo(pkg$src_path),
file.path(pkg$dst_path, "package-logo.png")
)
}

# Copy default assets
if (!identical(template$default_assets, FALSE)) {
copy_asset_dir(pkg, path_pkgdown("assets", paste0("BS", pkg$bs_version)))
Expand Down
1 change: 1 addition & 0 deletions R/package.r
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ as_pkgdown <- function(pkg = ".", override = list()) {
)

pkg_list$bs_version <- get_bs_version(pkg_list)
pkg_list$has_logo <- has_logo(pkg_list)

structure(
pkg_list,
Expand Down
22 changes: 22 additions & 0 deletions R/render.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ render_page <- function(pkg = ".", name, data, path = "", depth = NULL, quiet =
depth <- length(strsplit(path, "/")[[1]]) - 1L
}

data$logo <- list(
src = logo_path(pkg, depth = depth),
id = if (is.null(data$source)) "logo-low" else "logo-high"
)

data <- utils::modifyList(data, data_template(pkg, depth = depth))
data$pkgdown <- list(
version = utils::packageDescription("pkgdown", fields = "Version")
Expand Down Expand Up @@ -465,3 +470,20 @@ check_bootswatch_theme <- function(bootswatch_theme, bs_version, pkg) {
pkgdown_bslib_defaults <- function() {
list(`navbar-nav-link-padding-x` = "1rem")
}

logo_path <- function(pkg, depth) {
if (!has_logo(pkg)) {
return(NULL)
}
path <- "package-logo.png"

if (depth == 0) {
return(path)
}

paste0(
paste0(rep("..", depth), collapse = "/"), # as many levels up as depth
"/",
path
)
}
32 changes: 31 additions & 1 deletion inst/assets/BS4/pkgdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mark {


/* TOC heading */
h2[data-toc-skip] {
h2[data-toc-skip], #pkgdown-sidebar h2[data-toc-skip] {
font-size: x-large;
}

Expand Down Expand Up @@ -343,6 +343,7 @@ a:hover {

a.footnote-ref {
cursor: pointer;
}
/* Reference index & topics ----------------------------------------------- */

.ref-index th {font-weight: normal;}
Expand All @@ -365,3 +366,32 @@ table {
.fab {
font-family: "Font Awesome 5 Brands" !important;
}

/* logo homepage */
#logo-high {
float: right;
width: 100px;
margin-left: 30px;
margin-top: 25px;
}

@media (max-width: 767px) {
#logo-high {
float: none;
width: 100px;
}
}

#logo-low {
float: right;
width: 100px;
margin-left: 30px;
margin-top: 2px;
}

@media (max-width: 767px) {
#logo-low {
float: none;
width: 100px;
}
}
1 change: 1 addition & 0 deletions inst/templates/BS4/content-article-index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="col-md-9 contents">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">Articles</h1>
</div>
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-article.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<div class="row">
<div class="col-md-9 contents">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class="page-header pb-2 mt-4 mb-2 border-bottom toc-ignore">
<h1 class="display-4" data-toc-skip>$title$</h1>
$if(subtitle)$
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-authors.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">Authors</h1>
</div>
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-citation-authors.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class="page-header">
<h1 class="display-4">Citation</h1>
{{#source}}<small class="dont-index">{{{.}}}</small>{{/source}}
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-news-index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">Release notes</h1>
</div>
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-news.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="col-md-9 contents">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4" data-toc-skip>Changelog <small>{{version}}</small></h1>
{{#source}}<small>{{{.}}}</small>{{/source}}
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-reference-index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">Reference</h1>
</div>
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-reference-topic.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="col-md-9 contents">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class="page-header pb-2 mt-4 mb-2 border-bottom toc-ignore">
<h1 class="display-4">{{{title}}}</h1>
{{#source}}<small class="dont-index">{{{.}}}</small>{{/source}}
Expand Down
2 changes: 2 additions & 0 deletions inst/templates/BS4/content-title-body.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">{{pagetitle}}</h1>
{{#source}}<small class="dont-index">{{{.}}}</small>{{/source}}
</div>

{{{body}}}
Expand Down
1 change: 1 addition & 0 deletions inst/templates/BS4/content-tutorial-index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="row">
<div class="contents col-md-9">
{{#logo}}<img src="{{logo.src}}" id="{{logo.id}}" alt="" >{{/logo}}
<div class='pb-2 mt-4 mb-2 border-bottom'>
<h1 class="display-4">Tutorials</h1>
</div>
Expand Down

0 comments on commit 9a5a8f5

Please sign in to comment.