Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cli functions #2378

Merged
merged 20 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* Deprecated `build_favicon()` was removed (`build_favicons()` remains).
* Use [cli](https://github.com/r-lib/cli) to provide interactive feedback.
* Preserve Markdown code blocks with class rmd from roxygen2 docs (@salim-b, #2298).
* Avoid unwanted linebreaks from parsing `DESCRIPTION` (@salim-b, #2247).
Expand Down
6 changes: 3 additions & 3 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ data_home_sidebar_authors <- function(pkg = ".") {
bullets <- c(
markdown_text_inline(
pkg$meta$authors$sidebar$before,
pkgdown_field(c("authors", "sidebar", "before"))
pkgdown_field(pkg, c("authors", "sidebar", "before"))
),
authors,
markdown_text_inline(
pkg$meta$authors$sidebar$after,
pkgdown_field(c("authors", "sidebar", "after"))
pkgdown_field(pkg, c("authors", "sidebar", "after"))
)
)

Expand Down Expand Up @@ -96,7 +96,7 @@ author_name <- function(x, authors, pkg) {
if (!is.null(author$html)) {
name <- markdown_text_inline(
author$html,
pkgdown_field(c("authors", name, "html"))
pkgdown_field(pkg, c("authors", name, "html"))
)
}

Expand Down
8 changes: 7 additions & 1 deletion R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ data_home_sidebar <- function(pkg = ".") {
if (length(html_path)) {
if (!file.exists(html_path)) {
rel_html_path <- fs::path_rel(html_path, pkg$src_path)

msg_fld <- pkgdown_field(
pkg, c('home', 'sidebar', 'html'), fmt = TRUE, cfg = TRUE
)

cli::cli_abort(c(
"Can't locate {.file {rel_html_path}}.",
"x" = "{.field {pkgdown_field(c('home', 'sidebar', 'html'))}} in {.file {pkgdown_config_relpath(pkg)}} is misconfigured."
x = paste0(msg_fld, " is misconfigured.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
))

}
return(read_file(html_path))
}
Expand Down
7 changes: 5 additions & 2 deletions R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ build_redirects <- function(pkg = ".",

cli::cli_rule("Building redirects")
if (is.null(pkg$meta$url)) {
cli::cli_abort("{.field {pkgdown_field('url')}} in {.file {pkgdown_config_relpath(pkg)}} required to generate redirects")
msg_fld <- pkgdown_field(pkg, "url", cfg = TRUE, fmt = TRUE)
cli::cli_abort(paste0(msg_fld, " is required to generate redirects."))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

purrr::iwalk(
Expand All @@ -25,8 +26,10 @@ build_redirects <- function(pkg = ".",

build_redirect <- function(entry, index, pkg) {
if (!is.character(entry) || length(entry) != 2) {
msg_fld <- pkgdown_field(pkg, "url", cfg = TRUE, fmt = TRUE)
cli::cli_abort(
"Entry {.emph {index}} in {.field {pkgdown_field('redirects')}} must be a character vector of length 2.",
"Entry {.emph {index}} must be a character vector of length 2.",
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
x = paste0("Edit ", msg_fld, ".")
)
}

Expand Down
10 changes: 6 additions & 4 deletions R/build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ check_all_characters <- function(contents, index, pkg) {
any_null <- any(null)

if (any_null) {
msg_fld <- pkgdown_field(pkg, "reference", cfg = TRUE, fmt = TRUE)
cli::cli_abort(c(
"Item {.field {which(null)}} in section {index} in {.field {pkgdown_field('reference')}} is empty.",
i = "Either delete the empty line or add a function name in {.file {pkgdown_config_relpath(pkg)}}."
"Item {.field {which(null)}} in section {index} is empty.",
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
x = paste0("Delete the empty line or add function name to ", msg_fld, ".")
))
}

Expand All @@ -82,9 +83,10 @@ check_all_characters <- function(contents, index, pkg) {
return(invisible())
}

msg_fld <- pkgdown_field(pkg, "reference", cfg = TRUE, fmt = TRUE)
cli::cli_abort(c(
"Item {.field {which(not_char)}} in section {index} in {.field {pkgdown_field('reference')}} must be a character.",
i = "You might need to add '' around e.g. - 'N' or - 'off' in {.file {pkgdown_config_relpath(pkg)}}."
"Item {.field {which(not_char)}} in section {index} must be a character.",
x = paste0("You might need to add '' around e.g. - 'N' or - 'off' to ", msg_fld, ".")
))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved

}
Expand Down
6 changes: 3 additions & 3 deletions R/figure.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ match_fun <- function(x) {
f <- eval(e, globalenv())

if (!is.function(f)) {
cli::cli_abort("{.var x} must evaluate to a function")
cli::cli_abort("{.var x} must evaluate to a function", call = caller_env())
}

f
} else {
cli::cli_abort("{.var x} must be a function or string")
cli::cli_abort("{.var x} must be a function or string", call = caller_env())
}
}

Expand All @@ -157,6 +157,6 @@ fun_name <- function(x) {
} else if (is_call(expr, "::")) {
as.character(expr[[3]])
} else {
cli::cli_abort("Unknown input")
cli::cli_abort("Unknown input", call = caller_env())
}
}
22 changes: 14 additions & 8 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ as_pkgdown <- function(pkg = ".", override = list()) {
}

if (!dir_exists(pkg)) {
cli::cli_abort("{.file {pkg}} is not an existing directory")
cli::cli_abort(
"{.file {pkg}} is not an existing directory",
call = caller_env()
)
}

desc <- read_desc(pkg)
Expand Down Expand Up @@ -89,12 +92,12 @@ is_pkgdown <- function(x) inherits(x, "pkgdown")
read_desc <- function(path = ".") {
path <- path(path, "DESCRIPTION")
if (!file_exists(path)) {
cli::cli_abort("Can't find {.file DESCRIPTION}")
cli::cli_abort("Can't find {.file DESCRIPTION}", call = caller_env())
}
desc::description$new(path)
}

check_bootstrap_version <- function(version, pkg = list()) {
check_bootstrap_version <- function(version, pkg) {
if (is.null(version)) {
3
} else if (version %in% c(3, 5)) {
Expand All @@ -103,11 +106,14 @@ check_bootstrap_version <- function(version, pkg = list()) {
cli::cli_warn("{.var bootstrap: 4} no longer supported, using {.var bootstrap: 5} instead")
5
} else {
field <- pkgdown_field(c("template", "bootstrap"))
cli::cli_abort(c(
"Boostrap version must be 3 or 5.",
"x" = "You specified a value of {.val {version}} in {.field {field}}."
))
msg_fld <- pkgdown_field(pkg, c("template", "bootstrap"), cfg = TRUE, fmt = TRUE)
cli::cli_abort(
c(
"Boostrap version must be 3 or 5.",
x = paste0("You set a value of {.val {version}} to ", msg_fld, ".")
),
call = caller_env()
)
}
}

Expand Down
6 changes: 2 additions & 4 deletions R/rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ as_html.tag <- function(x, ...) {
flatten_text(x, ...)
} else {
cli::cli_inform("Unknown tag: ", paste(class(x), collapse = "/"))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
""
}
}

Expand Down Expand Up @@ -567,10 +568,7 @@ parse_opts <- function(string) {
stop_bad_tag <- function(tag, msg = NULL) {
bad_tag <- paste0("\\", tag, "{}")
msg_abort <- 'Failed to parse tag {.val {bad_tag}}.'
if (!is.null(msg)) {
msg_abort <- c(msg_abort, "x" = msg)
}
cli::cli_abort(msg_abort)
cli::cli_abort(c(msg_abort, i = msg), call = caller_env())
}

is_newline <- function(x, trim = FALSE) {
Expand Down
12 changes: 8 additions & 4 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,31 @@ check_open_graph <- function(og) {
if ("twitter" %in% names(og)) {
if (is.character(og$twitter) && length(og$twitter) == 1 && grepl("^@", og$twitter)) {
cli::cli_abort(
"The {.var opengraph: twitter} option must be a list."
"The {.var opengraph: twitter} option must be a list.",
call = caller_env()
)
}
if (!is.list(og$twitter)) {
cli::cli_abort("The {.var opengraph: twitter} option must be a list.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}
if (is.null(og$twitter$creator) && is.null(og$twitter$site)) {
cli::cli_abort(
"{.var opengraph: twitter} must include either {.val creator} or {.val site}."
"{.var opengraph: twitter} must include either {.val creator} or {.val site}.",
call = caller_env()
)
}
}
if ("image" %in% names(og)) {
if (is.character(og$image) && length(og$image) == 1) {
cli::cli_abort(
"The {.var opengraph: image} option must be a list."
"The {.var opengraph: image} option must be a list.",
call = caller_env()
)
}
if (!is.list(og$image)) {
cli::cli_abort(
"The {.var opengraph: image} option must be a list."
"The {.var opengraph: image} option must be a list.",
call = caller_env()
)
}
}
Expand Down
7 changes: 4 additions & 3 deletions R/sitrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ pkgdown_sitrep <- function(pkg = ".") {
warns <- c()

if (is.null(pkg$meta[["url"]])) {
warns <- c(warns, "x" = "{.field {pkgdown_field('url')}} is not configured in {.file {pkgdown_config_relpath(pkg)}}. See {.vignette pkgdown::metatdata}.")
msg_fld <- pkgdown_field(pkg, "url", cfg = TRUE, fmt = TRUE)
warns <- c(warns, x = paste0(msg_fld, "is misconfigured. See {.vignette pkgdown::metatdata}."))
}

desc_urls <- pkg$desc$get_urls()
desc_urls <- sub("/$", "", desc_urls)
if (length(desc_urls) == 0 || !pkg$meta[["url"]] %in% desc_urls) {
warns <- c(warns, "x" = "{.file DESCRIPTION} {.field URL} is empty.")
warns <- c(warns, x = "{.file DESCRIPTION} {.field URL} is empty.")
}

if (length(warns) == 0) {
cli::cli_alert_success("pkgdown situation report: {.emph {cli::col_green('all clear')}}")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
cli::cli_alert("{.emph Double-check the following URLs:}")
cli::cli_inform("{.emph Double-check the following URLs:}")
cli::cli_inform("{.file {pkgdown_config_relpath(pkg)}} contains URL {.url {pkg$meta['url']}}")
cli::cli_inform("{.file DESCRIPTION} contains URL{?s} {.url {desc_urls}}")
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ check_bootswatch_theme <- function(bootswatch_theme, bs_version, pkg) {
sprintf(
"Can't find Bootswatch theme {.val %s} ({.field %s}) for Bootstrap version {.val %s} ({.field %s}).",
bootswatch_theme,
pkgdown_field(c("template", "bootswatch")),
pkgdown_field(pkg, c("template", "bootswatch")),
bs_version,
pkgdown_field(c("template", "bootstrap"))
pkgdown_field(pkg, c("template", "bootstrap"))
),
x = "Edit settings in {.file {pkgdown_config_relpath(pkg)}}"
))
Expand Down
10 changes: 7 additions & 3 deletions R/topics.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ all_sign <- function(x, text) {
}
}

cli::cli_abort("Must be all negative or all positive: {.val {text}}")
cli::cli_abort(
"Must be all negative or all positive: {.val {text}}",
call = caller_env()
)
}

match_env <- function(topics) {
Expand Down Expand Up @@ -172,10 +175,11 @@ match_eval <- function(string, env) {
}
}

topic_must <- function(message, topic, parent = NULL) {
topic_must <- function(message, topic, ..., call = NULL) {
cli::cli_abort(
"In {.file _pkgdown.yml}, topic must {message}, not {.val {topic}}",
parent = parent
...,
call = call
)
}

Expand Down
1 change: 1 addition & 0 deletions R/utils-fs.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ path_pkgdown <- function(...) {
pkgdown_config_relpath <- function(pkg) {
pkg <- as_pkgdown(pkg)
config_path <- pkgdown_config_path(pkg$src_path)

fs::path_rel(config_path, pkg$src_path)
}
30 changes: 24 additions & 6 deletions R/utils-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ check_yaml_has <- function(missing, where, pkg) {
}

missing_components <- lapply(missing, function(x) c(where, x))
missing_fields <- purrr::map_chr(missing_components, pkgdown_field)
msg_flds <- pkgdown_field(pkg, missing_components, fmt = TRUE, cfg = TRUE)

cli::cli_abort(
"Can't find {cli::qty(missing_fields)} component{?s} {.field {missing_fields}} in {.file {pkgdown_config_relpath(pkg)}}."
paste0("Can't find {cli::qty(missing)} component{?s} ", msg_flds, ".")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -19,13 +19,31 @@ yaml_character <- function(pkg, where) {
} else if (is.character(x)) {
x
} else {
fld <- pkgdown_field(where)
cli::cli_abort("{fld} must be a character vector")
fld <- pkgdown_field(pkg, where, fmt = TRUE)
cli::cli_abort(paste0(fld, " must be a character vector."))
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}
}

pkgdown_field <- function(fields) {
purrr::map_chr(list(fields), ~ paste0(.x, collapse = "."))
pkgdown_field <- function(pkg, fields, cfg = FALSE, fmt = FALSE) {

if (!is.list(fields)) fields <- list(fields)

flds <- purrr::map_chr(fields, ~ paste0(.x, collapse = "."))
if (fmt) {
flds <- paste0("{.field ", flds, "}")
}

if (cfg) {
config_path <- pkgdown_config_relpath(pkg)
if (fmt) {
config_path <- paste0("{.file ", config_path, "}")
}

paste0(flds, " in ", config_path)
} else {

flds
}
}

# print helper ------------------------------------------------------------
Expand Down
38 changes: 28 additions & 10 deletions tests/testthat/_snaps/build-home-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,41 @@

# data_home_sidebar() outputs informative error messages

Can't find component home.sidebar.components.fancy in '_pkgdown.yml'.
Code
data_home_sidebar(pkg)
Condition
Error in `check_yaml_has()`:
! Can't find component home.sidebar.components.fancy in '_pkgdown.yml'.

---

Can't find components home.sidebar.components.fancy and home.sidebar.components.cool in '_pkgdown.yml'.
Code
data_home_sidebar(pkg)
Condition
Error in `check_yaml_has()`:
! Can't find components home.sidebar.components.fancy in '_pkgdown.yml'.
Can't find components home.sidebar.components.cool in '_pkgdown.yml'.

---

i In index: 1.
i With name: fancy.
Caused by error in `check_yaml_has()`:
! Can't find component home.sidebar.components.fancy.title in '_pkgdown.yml'.
Code
data_home_sidebar(pkg)
Condition
Error in `purrr::map2()`:
i In index: 1.
i With name: fancy.
Caused by error in `check_yaml_has()`:
! Can't find component home.sidebar.components.fancy.title in '_pkgdown.yml'.

---

i In index: 1.
i With name: fancy.
Caused by error in `check_yaml_has()`:
! Can't find components home.sidebar.components.fancy.title and home.sidebar.components.fancy.text in '_pkgdown.yml'.
Code
data_home_sidebar(pkg)
Condition
Error in `purrr::map2()`:
i In index: 1.
i With name: fancy.
Caused by error in `check_yaml_has()`:
! Can't find components home.sidebar.components.fancy.title in '_pkgdown.yml'.
Can't find components home.sidebar.components.fancy.text in '_pkgdown.yml'.

Loading
Loading