From 146c2accf134dfc8c96890af08a91978c396a092 Mon Sep 17 00:00:00 2001 From: dmurdoch Date: Mon, 11 Mar 2024 11:33:18 -0400 Subject: [PATCH] Put selector functions etc. into a parent env (#2397) Fixes #2396 --- NEWS.md | 1 + R/topics.R | 19 +++++++------ .../testthat/_snaps/build-reference-index.md | 28 +++++++++++++++++++ .../assets/reference-selector/DESCRIPTION | 6 ++++ .../assets/reference-selector/NAMESPACE | 4 +++ .../assets/reference-selector/R/funs.R | 7 +++++ .../assets/reference-selector/_pkgdown.yml | 1 + .../assets/reference-selector/man/A.Rd | 11 ++++++++ .../assets/reference-selector/man/matches.Rd | 11 ++++++++ tests/testthat/test-build-reference-index.R | 10 +++++++ 10 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 tests/testthat/assets/reference-selector/DESCRIPTION create mode 100644 tests/testthat/assets/reference-selector/NAMESPACE create mode 100644 tests/testthat/assets/reference-selector/R/funs.R create mode 100644 tests/testthat/assets/reference-selector/_pkgdown.yml create mode 100644 tests/testthat/assets/reference-selector/man/A.Rd create mode 100644 tests/testthat/assets/reference-selector/man/matches.Rd diff --git a/NEWS.md b/NEWS.md index 169fd5c3e..11a8232b2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,7 @@ * Add Catalan translation (@jmaspons, #2333) * Set RNG seed for htmlwidgets IDs. This reduces noise in pkgdown reference HTML output when examples generate htmlwidgets (@salim-b, #2294). * Fix BS5 navbar template to get `navbar.type: dark` to work with bslib 0.6+ / Bootstrap 5.3+ (@tanho63, #2388) +* Topic names that conflict with selector functions can now be listed as references in `_pkgdown.yml` (@dmurdoch, #2397). # pkgdown 2.0.7 diff --git a/R/topics.R b/R/topics.R index b135eb3bc..9bf17c9fa 100644 --- a/R/topics.R +++ b/R/topics.R @@ -61,10 +61,11 @@ all_sign <- function(x, text) { } match_env <- function(topics) { - out <- env(empty_env(), + fns <- env(empty_env(), "-" = function(x) -x, "c" = function(...) c(...) ) + out <- env(fns) topic_index <- seq_along(topics$name) @@ -99,36 +100,36 @@ match_env <- function(topics) { is_public <- function(internal) { if (!internal) !topics$internal else rep(TRUE, nrow(topics)) } - out$starts_with <- function(x, internal = FALSE) { + fns$starts_with <- function(x, internal = FALSE) { any_alias(~ grepl(paste0("^", x), .), .internal = internal) } - out$ends_with <- function(x, internal = FALSE) { + fns$ends_with <- function(x, internal = FALSE) { any_alias(~ grepl(paste0(x, "$"), .), .internal = internal) } - out$matches <- function(x, internal = FALSE) { + fns$matches <- function(x, internal = FALSE) { any_alias(~ grepl(x, .), .internal = internal) } - out$contains <- function(x, internal = FALSE) { + fns$contains <- function(x, internal = FALSE) { any_alias(~ grepl(x, ., fixed = TRUE), .internal = internal) } - out$has_keyword <- function(x) { + fns$has_keyword <- function(x) { which(purrr::map_lgl(topics$keywords, ~ any(. %in% x))) } - out$has_concept <- function(x, internal = FALSE) { + fns$has_concept <- function(x, internal = FALSE) { match <- topics$concepts %>% purrr::map(~ str_trim(.) == x) %>% purrr::map_lgl(any) which(match & is_public(internal)) } - out$lacks_concepts <- function(x, internal = FALSE) { + fns$lacks_concepts <- function(x, internal = FALSE) { nomatch <- topics$concepts %>% purrr::map(~ match(str_trim(.), x, nomatch = FALSE)) %>% purrr::map_lgl(~ length(.) == 0L | all(. == 0L)) which(nomatch & is_public(internal)) } - out$lacks_concept <- out$lacks_concepts + fns$lacks_concept <- fns$lacks_concepts out } diff --git a/tests/testthat/_snaps/build-reference-index.md b/tests/testthat/_snaps/build-reference-index.md index 13a43f0e2..c19370cb5 100644 --- a/tests/testthat/_snaps/build-reference-index.md +++ b/tests/testthat/_snaps/build-reference-index.md @@ -157,3 +157,31 @@ has_icons: no +# can use a selector name as a topic name + + Code + data_reference_index(pkg) + Output + pagetitle: Function reference + rows: + - title: bla + slug: bla + desc: ~ + is_internal: no + - topics: + - path: matches.html + title: matches + aliases: matches() + icon: ~ + - path: A.html + title: A + aliases: A() + icon: ~ + names: + - matches + - A + row_has_icons: no + is_internal: no + has_icons: no + + diff --git a/tests/testthat/assets/reference-selector/DESCRIPTION b/tests/testthat/assets/reference-selector/DESCRIPTION new file mode 100644 index 000000000..49c062aca --- /dev/null +++ b/tests/testthat/assets/reference-selector/DESCRIPTION @@ -0,0 +1,6 @@ +Package: testpackage +Version: 1.0.0 +Title: A test package +Description: A test package +Authors@R: person("Hadley Wickham") +RoxygenNote: 7.3.1 diff --git a/tests/testthat/assets/reference-selector/NAMESPACE b/tests/testthat/assets/reference-selector/NAMESPACE new file mode 100644 index 000000000..7a3c4d864 --- /dev/null +++ b/tests/testthat/assets/reference-selector/NAMESPACE @@ -0,0 +1,4 @@ +# Generated by roxygen2: do not edit by hand + +export(A) +export(matches) diff --git a/tests/testthat/assets/reference-selector/R/funs.R b/tests/testthat/assets/reference-selector/R/funs.R new file mode 100644 index 000000000..201dcc993 --- /dev/null +++ b/tests/testthat/assets/reference-selector/R/funs.R @@ -0,0 +1,7 @@ +#' matches +#' @export +matches <- function() {} + +#' A +#' @export +A <- function() {} diff --git a/tests/testthat/assets/reference-selector/_pkgdown.yml b/tests/testthat/assets/reference-selector/_pkgdown.yml new file mode 100644 index 000000000..86939d57a --- /dev/null +++ b/tests/testthat/assets/reference-selector/_pkgdown.yml @@ -0,0 +1 @@ +url: http://test.org diff --git a/tests/testthat/assets/reference-selector/man/A.Rd b/tests/testthat/assets/reference-selector/man/A.Rd new file mode 100644 index 000000000..48ec60217 --- /dev/null +++ b/tests/testthat/assets/reference-selector/man/A.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/funs.R +\name{A} +\alias{A} +\title{A} +\usage{ +A() +} +\description{ +A +} diff --git a/tests/testthat/assets/reference-selector/man/matches.Rd b/tests/testthat/assets/reference-selector/man/matches.Rd new file mode 100644 index 000000000..693b742e9 --- /dev/null +++ b/tests/testthat/assets/reference-selector/man/matches.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/funs.R +\name{matches} +\alias{matches} +\title{matches} +\usage{ +matches() +} +\description{ +matches +} diff --git a/tests/testthat/test-build-reference-index.R b/tests/testthat/test-build-reference-index.R index 59ef31855..2899f2e6f 100644 --- a/tests/testthat/test-build-reference-index.R +++ b/tests/testthat/test-build-reference-index.R @@ -126,3 +126,13 @@ test_that("can use a topic from another package", { expect_snapshot(data_reference_index(pkg)) }) + +test_that("can use a selector name as a topic name", { + meta <- list(reference = list(list( + title = "bla", + contents = c("matches", "matches('A')") + ))) + pkg <- as_pkgdown(test_path("assets/reference-selector"), override = meta) + + expect_snapshot(data_reference_index(pkg)) +})