From e3477f75421e9637224b26a50db3ea4b82deac53 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Tue, 5 Mar 2024 14:45:21 +0100 Subject: [PATCH 1/2] feat(pkgdown): report list in navbar Refs #22 --- R/build_site.R | 42 ++++++++++++++++++++++++++++++++ man/build_site.Rd | 4 +++ tests/testthat/test-build_site.R | 3 +++ 3 files changed, 49 insertions(+) diff --git a/R/build_site.R b/R/build_site.R index d319230..f9d66ab 100644 --- a/R/build_site.R +++ b/R/build_site.R @@ -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]]) +} diff --git a/man/build_site.Rd b/man/build_site.Rd index 2b67894..a3de07e 100644 --- a/man/build_site.Rd +++ b/man/build_site.Rd @@ -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{ diff --git a/tests/testthat/test-build_site.R b/tests/testthat/test-build_site.R index de03701..5181e7a 100644 --- a/tests/testthat/test-build_site.R +++ b/tests/testthat/test-build_site.R @@ -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) }) -- GitLab From b1f307d2003935092ba732246124ea734f275bc3 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Tue, 5 Mar 2024 14:46:22 +0100 Subject: [PATCH 2/2] docs(pkgdown): add gitlab repo link in navbar --- _pkgdown.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 85a26b1..c310185 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -2,3 +2,17 @@ url: ~ template: bootstrap: 5 destination: public +navbar: + structure: + left: + - intro + - reference + - articles + - reports + - news + right: + - gitlab + components: + gitlab: + icon: fab fa-gitlab fa-lg + href: https://forgemia.inra.fr/umr-g-eau/fairify -- GitLab