diff --git a/DESCRIPTION b/DESCRIPTION index 7f3e4c316..15ffad606 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -75,5 +75,5 @@ Config/testthat/start-first: build-article, build-quarto-article, Config/usethis/last-upkeep: 2025-09-07 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.3 +RoxygenNote: 7.3.3.9000 SystemRequirements: pandoc (>= 2.10.1) diff --git a/NAMESPACE b/NAMESPACE index 853ed814f..658772940 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -132,6 +132,7 @@ export(deploy_site_github) export(deploy_to_branch) export(fig_settings) export(in_pkgdown) +export(in_pkgdown_pkg) export(init_site) export(pkgdown_print) export(pkgdown_sitrep) diff --git a/NEWS.md b/NEWS.md index 29315dae5..2d97f127c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,10 @@ * do not autolink code that is in a link (href) in Rd files (#2972) +* New `in_pkgdown_pkg()` function and new `IN_PKGDOWN_PKG` variable, to go with + `in_pkgdown()` and `IN_PKGDOWN`. Returns/contains the name of the package + whose site is being built (#2976). + # pkgdown 2.2.0 * Make `build_llm_docs()` more robust to the use of old Pandoc (@nanxstats, @galachad, #2952, #2954) diff --git a/R/pkgdown.R b/R/pkgdown.R index d61a3cecf..311b19e2e 100644 --- a/R/pkgdown.R +++ b/R/pkgdown.R @@ -10,9 +10,19 @@ in_pkgdown <- function() { identical(Sys.getenv("IN_PKGDOWN"), "true") } +#' @rdname in_pkgdown +#' @param pkg Package name (a string) +#' @export +#' @examples +#' in_pkgdown_pkg("pkgdown") +in_pkgdown_pkg <- function(pkg) { + identical(Sys.getenv("IN_PKGDOWN_PKG"), pkg) +} + local_envvar_pkgdown <- function(pkg, scope = parent.frame()) { withr::local_envvar( IN_PKGDOWN = "true", + IN_PKGDOWN_PKG = pkg$package, LANGUAGE = pkg$lang, .local_envir = scope ) diff --git a/man/in_pkgdown.Rd b/man/in_pkgdown.Rd index a1e8b8831..e259dd469 100644 --- a/man/in_pkgdown.Rd +++ b/man/in_pkgdown.Rd @@ -2,9 +2,15 @@ % Please edit documentation in R/pkgdown.R \name{in_pkgdown} \alias{in_pkgdown} +\alias{in_pkgdown_pkg} \title{Determine if code is executed by pkgdown} \usage{ in_pkgdown() + +in_pkgdown_pkg(pkg) +} +\arguments{ +\item{pkg}{Package name (a string)} } \description{ This is occasionally useful when you need different behaviour by @@ -12,4 +18,5 @@ pkgdown and regular documentation. } \examples{ in_pkgdown() +in_pkgdown_pkg("pkgdown") } diff --git a/tests/testthat/test-pkgdown.R b/tests/testthat/test-pkgdown.R new file mode 100644 index 000000000..e2849332f --- /dev/null +++ b/tests/testthat/test-pkgdown.R @@ -0,0 +1,11 @@ +test_that("in_pkgdown_pkg() works", { + expect_false(in_pkgdown()) + expect_false(in_pkgdown_pkg("testpackage")) + + pkg <- local_pkgdown_site() + local_envvar_pkgdown(pkg) + + expect_true(in_pkgdown()) + expect_false(in_pkgdown_pkg("pkgdown")) + expect_true(in_pkgdown_pkg("testpackage")) +})