diff --git a/R/build-articles.R b/R/build-articles.R index d434233ad..b674e011f 100644 --- a/R/build-articles.R +++ b/R/build-articles.R @@ -372,7 +372,7 @@ data_articles_index <- function(pkg = ".") { if (length(missing) > 0) { cli::cli_abort( "{length(missing)} vignette{?s} missing from index in \\ - {pkgdown_config_href({pkg$src_path})}: {.val {missing}}.", + {.file {pkgdown_config_relpath(pkg)}}: {.val {missing}}.", call = caller_env() ) } @@ -392,7 +392,7 @@ data_articles_index_section <- function(section, pkg) { } # Match topics against any aliases - in_section <- select_vignettes(section$contents, pkg$vignettes) + in_section <- select_vignettes(section$contents, pkg$vignettes, pkg = pkg) section_vignettes <- pkg$vignettes[in_section, ] contents <- tibble::tibble( name = section_vignettes$name, @@ -411,13 +411,13 @@ data_articles_index_section <- function(section, pkg) { # Quick hack: create the same structure as for topics so we can use # the existing select_topics() -select_vignettes <- function(match_strings, vignettes) { +select_vignettes <- function(match_strings, vignettes, pkg) { topics <- tibble::tibble( name = vignettes$name, alias = as.list(vignettes$name), internal = FALSE ) - select_topics(match_strings, topics) + select_topics(match_strings, topics, pkg = pkg) } default_articles_index <- function(pkg = ".") { diff --git a/R/build-reference-index.R b/R/build-reference-index.R index 0abdd661a..9da8e2f56 100644 --- a/R/build-reference-index.R +++ b/R/build-reference-index.R @@ -48,7 +48,7 @@ data_reference_index_rows <- function(section, index, pkg) { if (has_name(section, "contents")) { check_all_characters(section$contents, index, pkg) - topics <- section_topics(section$contents, pkg$topics, pkg$src_path) + topics <- section_topics(section$contents, pkg$topics, pkg$src_path, pkg) names <- topics$name topics$name <- NULL @@ -136,7 +136,7 @@ check_missing_topics <- function(rows, pkg, error_call = caller_env()) { cli::cli_abort(c( "All topics must be included in reference index", "x" = "Missing topics: {pkg$topics$name[missing]}", - i = "Either add to {pkgdown_config_href({pkg$src_path})} or use @keywords internal" + i = "Either add to {.file {pkgdown_config_relpath(pkg)}} or use @keywords internal" ), call = error_call) } diff --git a/R/check.R b/R/check.R index 1858ab454..70fa2ba0b 100644 --- a/R/check.R +++ b/R/check.R @@ -13,6 +13,6 @@ check_pkgdown <- function(pkg = ".") { data_reference_index(pkg) cli::cli_inform(c( - "v" = "No problems found in {pkgdown_config_href({pkg$src_path})}" + "v" = "No problems found in {.file {pkgdown_config_relpath(pkg)}}" )) } diff --git a/R/navbar.R b/R/navbar.R index 4b67ed012..5214cc390 100644 --- a/R/navbar.R +++ b/R/navbar.R @@ -143,7 +143,7 @@ navbar_articles <- function(pkg = ".") { menu$articles <- menu_link(tr_("Articles"), "articles/index.html") } else { sections <- lapply(navbar, function(section) { - vig <- pkg$vignettes[select_vignettes(section$contents, pkg$vignettes), , drop = FALSE] + vig <- pkg$vignettes[select_vignettes(section$contents, pkg$vignettes, pkg), , drop = FALSE] vig <- vig[vig$name != pkg$package, , drop = FALSE] c( if (!is.null(section$navbar)) list(menu_spacer(), menu_text(section$navbar)), diff --git a/R/package.R b/R/package.R index dc7b5396a..9e0e940eb 100644 --- a/R/package.R +++ b/R/package.R @@ -130,12 +130,6 @@ pkgdown_config_path <- function(path) { ) ) } -pkgdown_config_href <- function(path) { - cli::style_hyperlink( - text = "_pkgdown.yml", - url = paste0("file://", pkgdown_config_path(path)) - ) -} read_meta <- function(path) { path <- pkgdown_config_path(path) diff --git a/R/topics.R b/R/topics.R index 9bf17c9fa..430d08af7 100644 --- a/R/topics.R +++ b/R/topics.R @@ -1,17 +1,19 @@ # @return An integer vector giving selected topics -select_topics <- function(match_strings, topics, check = FALSE) { +select_topics <- function(match_strings, topics, pkg = ".", check = FALSE) { n <- nrow(topics) if (length(match_strings) == 0) { return(integer()) } - indexes <- purrr::map(match_strings, match_eval, env = match_env(topics)) - + indexes <- purrr::map( + match_strings, + function(x) match_eval(x, env = match_env(topics), pkg = pkg) + ) # If none of the specified topics have a match, return no topics if (purrr::every(indexes, is_empty)) { if (check) { cli::cli_abort( - "No topics matched in {.file _pkgdown.yml}. No topics selected.", + "No topics matched in {.file {pkgdown_config_relpath(pkg)}}. No topics selected.", call = caller_env() ) } @@ -20,7 +22,7 @@ select_topics <- function(match_strings, topics, check = FALSE) { no_match <- match_strings[purrr::map_lgl(indexes, rlang::is_empty)] if (check && length(no_match) > 0) { - topic_must("match a function or concept", toString(no_match)) + topic_must("match a function or concept", toString(no_match), pkg = pkg) } indexes <- purrr::discard(indexes, is_empty) @@ -134,7 +136,7 @@ match_env <- function(topics) { } -match_eval <- function(string, env) { +match_eval <- function(string, env, pkg = ".") { # Early return in case string already matches symbol if (env_has(env, string)) { val <- env[[string]] @@ -145,7 +147,7 @@ match_eval <- function(string, env) { expr <- tryCatch(parse_expr(string), error = function(e) NULL) if (is.null(expr)) { - topic_must("be valid R code", string) + topic_must("be valid R code", string, pkg = pkg) } if (is_string(expr) || is_symbol(expr)) { @@ -154,7 +156,7 @@ match_eval <- function(string, env) { if (is.integer(val)) { val } else { - topic_must("be a known topic name or alias", string) + topic_must("be a known topic name or alias", string, pkg = pkg) } } else if (is_call(expr, "::")) { name <- paste0(expr[[2]], "::", expr[[3]]) @@ -162,34 +164,34 @@ match_eval <- function(string, env) { if (is.integer(val)) { val } else { - topic_must("be a known topic name or alias", string) + topic_must("be a known topic name or alias", string, pkg = pkg) } } else if (is_call(expr)) { tryCatch( eval(expr, env), error = function(e) { - topic_must("be a known selector function", string, parent = e) + topic_must("be a known selector function", string, parent = e, pkg = pkg) } ) } else { - topic_must("be a string or function call", string) + topic_must("be a string or function call", string, pkg = pkg) } } -topic_must <- function(message, topic, ..., call = NULL) { +topic_must <- function(message, topic, ..., call = NULL, pkg = ".") { cli::cli_abort( - "In {.file _pkgdown.yml}, topic must {message}, not {.val {topic}}.", + "In {.file {pkgdown_config_relpath(pkg)}}, topic must {message}, not {.val {topic}}.", ..., call = call ) } -section_topics <- function(match_strings, topics, src_path) { +section_topics <- function(match_strings, topics, src_path, pkg = ".") { # Add rows for external docs ext_strings <- match_strings[grepl("::", match_strings, fixed = TRUE)] topics <- rbind(topics, ext_topics(ext_strings)) - selected <- topics[select_topics(match_strings, topics), , ] + selected <- topics[select_topics(match_strings, topics, pkg), , ] tibble::tibble( name = selected$name, diff --git a/R/utils-fs.R b/R/utils-fs.R index 9e4445da0..5a9be0348 100644 --- a/R/utils-fs.R +++ b/R/utils-fs.R @@ -104,9 +104,13 @@ path_pkgdown <- function(...) { system_file(..., package = "pkgdown") } +# To avoid problems in tests, default to _pkgdown.yml pkgdown_config_relpath <- function(pkg) { - pkg <- as_pkgdown(pkg) - config_path <- pkgdown_config_path(pkg$src_path) + tryCatch({ + pkg <- as_pkgdown(pkg) + config_path <- pkgdown_config_path(pkg$src_path) - fs::path_rel(config_path, pkg$src_path) + fs::path_rel(config_path, pkg$src_path) + }, + error = function(e) fs::path("_pkgdown.yml")) } diff --git a/tests/testthat/_snaps/build-reference-index.md b/tests/testthat/_snaps/build-reference-index.md index 726ce3988..d3f6f6722 100644 --- a/tests/testthat/_snaps/build-reference-index.md +++ b/tests/testthat/_snaps/build-reference-index.md @@ -53,14 +53,14 @@ Error: ! All topics must be included in reference index x Missing topics: c, e, and ? - i Either add to _pkgdown.yml or use @keywords internal + i Either add to 'pkgdown/_pkgdown.yml' or use @keywords internal # errors well when a content entry is empty i In index: 1. Caused by error in `.f()`: ! Item 2 in section 1 is empty. - x Delete the empty line or add function name to reference in '_pkgdown.yml'. + x Delete the empty line or add function name to reference in 'pkgdown/_pkgdown.yml'. # errors well when a content entry is not a character @@ -71,7 +71,7 @@ i In index: 1. Caused by error in `.f()`: ! Item 2 in section 1 must be a character. - x You might need to add '' around e.g. - 'N' or - 'off' to reference in '_pkgdown.yml'. + x You might need to add '' around e.g. - 'N' or - 'off' to reference in 'pkgdown/_pkgdown.yml'. # errors well when a content entry refers to a not installed package diff --git a/tests/testthat/_snaps/check.md b/tests/testthat/_snaps/check.md index f1e4bb125..177f09cb3 100644 --- a/tests/testthat/_snaps/check.md +++ b/tests/testthat/_snaps/check.md @@ -6,7 +6,7 @@ Error in `check_pkgdown()`: ! All topics must be included in reference index x Missing topics: ? - i Either add to _pkgdown.yml or use @keywords internal + i Either add to 'pkgdown/_pkgdown.yml' or use @keywords internal # fails if article index incomplete @@ -14,12 +14,12 @@ check_pkgdown(pkg) Condition Error in `check_pkgdown()`: - ! 2 vignettes missing from index in _pkgdown.yml: "articles/nested" and "width". + ! 2 vignettes missing from index in '_pkgdown.yml': "articles/nested" and "width". # informs if everything is ok Code check_pkgdown(pkg) Message - v No problems found in _pkgdown.yml + v No problems found in 'pkgdown/_pkgdown.yml' diff --git a/tests/testthat/_snaps/render.md b/tests/testthat/_snaps/render.md index 700823873..fbb5bef2b 100644 --- a/tests/testthat/_snaps/render.md +++ b/tests/testthat/_snaps/render.md @@ -1,7 +1,7 @@ # check_bootswatch_theme() works Can't find Bootswatch theme "paper" (template.bootswatch) for Bootstrap version "4" (template.bootstrap). - x Edit settings in '_pkgdown.yml' + x Edit settings in 'pkgdown/_pkgdown.yml' # capture data_template() diff --git a/tests/testthat/assets/reference/_pkgdown.yml b/tests/testthat/assets/reference/pkgdown/_pkgdown.yml similarity index 100% rename from tests/testthat/assets/reference/_pkgdown.yml rename to tests/testthat/assets/reference/pkgdown/_pkgdown.yml diff --git a/tests/testthat/test-topics.R b/tests/testthat/test-topics.R index 8b1eeeb4b..f21e5e29d 100644 --- a/tests/testthat/test-topics.R +++ b/tests/testthat/test-topics.R @@ -153,7 +153,7 @@ test_that("full topic selection process works", { pkg <- local_pkgdown_site(test_path("assets/reference")) # can mix local and remote - out <- section_topics(c("a", "base::mean"), pkg$topics, pkg$src_path) + out <- section_topics(c("a", "base::mean"), pkg$topics, pkg$src_path, pkg) expect_equal(unname(out$name), c("a", "base::mean")) # concepts and keywords work @@ -163,6 +163,14 @@ test_that("full topic selection process works", { pkg$src_path ) expect_equal(unname(out$name), c("b", "a")) + # concepts and keywords error with correct path + expect_error(section_topics( + c("foo2"), + pkg$topics, + pkg$src_path, + pkg + ), + regexp = "pkgdown/_pkgdown.yml") }) test_that("an unmatched selection with a matched selection does not select everything", {