Skip to content
Snippets Groups Projects
Commit e3477f75 authored by David Dorchies's avatar David Dorchies
Browse files

feat(pkgdown): report list in navbar

Refs #22
parent 8c4dc2ca
No related branches found
No related tags found
1 merge request!3Resolve "Add a menu with the list of reports in pkgdown"
......@@ -10,6 +10,8 @@
#'
#' @inheritParams pkgdown::build_site
#' @inheritParams render_reports
#' @param navbar_reports Add the report list in the navbar of the pkgdown
#' website
#' @param ... Further parameters to pass to [render_reports]
#'
#' @return Used for side effects
......@@ -26,11 +28,15 @@ build_site <- function(pkg = pkgload::pkg_path(),
new_process = !devel,
install = !devel,
reports_dir = file.path(pkg, "reports"),
navbar_reports = TRUE,
...) {
if (file.exists(file.path(pkg, "README.Rmd"))) {
devtools::build_readme(path = pkg)
}
unlink(file.path(pkg, "public"), recursive = TRUE)
if (navbar_reports) {
add_reports_pkgdown(pkg, reports_dir)
}
pkgdown::build_site(pkg = pkg,
examples = examples,
run_dont_run = run_dont_run,
......@@ -44,3 +50,39 @@ build_site <- function(pkg = pkgload::pkg_path(),
render_reports(reports_dir = reports_dir, ...)
invisible(TRUE)
}
add_reports_pkgdown <- function(pkg, reports_dir) {
f <- file.path(pkg, "_pkgdown.yml")
if (!file.exists(f)) {
warning("File not found: ", f, "\nReport list won't be visible in the website navigation bar")
invisible()
}
cpd <- yaml::read_yaml(f)
cpd$navbar <- add_ifnull(cpd, "navbar")
cpd$navbar$structure <- add_ifnull(cpd$navbar, "structure")
cpd$navbar$structure$left <-
add_ifnull(
cpd$navbar$structure,
"left",
c("intro", "reference", "articles", "tutorials", "news")
)
if (!"reports" %in% cpd$navbar$structure$left) {
cpd$navbar$structure$left <- c("reports", cpd$navbar$structure$left)
}
cpd$navbar$components <- add_ifnull(cpd$navbar, "components")
cpd$navbar$components$reports <- list(text = "Reports")
dfr <- list_reports(reports_dir)
pkg <- gsub("/$", "", pkg)
lr <- apply(dfr, 1, function(r) {
list(text = r["title"],
href = file.path(sub(paste0(pkg, "/"), "", r["path"], fixed = TRUE), "index.html"))
})
names(lr) <- NULL
cpd$navbar$components$reports$menu <- lr
yaml::write_yaml(cpd, f)
}
add_ifnull <- function(l, i, v = list()) {
if(is.null(l[[i]])) l[[i]] <- v
return(l[[i]])
}
......@@ -16,6 +16,7 @@ build_site(
new_process = !devel,
install = !devel,
reports_dir = file.path(pkg, "reports"),
navbar_reports = TRUE,
...
)
}
......@@ -61,6 +62,9 @@ so it is available for vignettes.}
\item{reports_dir}{\link{character} path for the reports}
\item{navbar_reports}{Add the report list in the navbar of the pkgdown
website}
\item{...}{Further parameters to pass to \link{render_reports}}
}
\value{
......
......@@ -8,5 +8,8 @@ test_that("build_site works", {
report_path <- file.path(path, "public/reports/my_report")
expect_true(dir.exists(report_path))
expect_true(file.exists(file.path(report_path, "index.html")))
y <- yaml::read_yaml(file.path(path, "_pkgdown.yml"))
expect_equal(y$navbar$components$reports$menu[[1]]$href,
"reports/my_report/index.html")
unlink(path, recursive = TRUE)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment