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 18 commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Depends:
Imports:
bslib (>= 0.3.1),
callr (>= 3.7.3),
cli,
cli (>= 3.6.1),
desc (>= 1.4.0),
digest,
downlit (>= 0.4.0),
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export(autolink_html)
export(build_article)
export(build_articles)
export(build_articles_index)
export(build_favicon)
export(build_favicons)
export(build_home)
export(build_home_index)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 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).
* Remove redundant entries in the documentation index when multiple explicit `@usage` tags are provided (@klmr, #2302)
Expand Down
15 changes: 4 additions & 11 deletions R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ build_articles <- function(pkg = ".",
return(invisible())
}

rule("Building articles")
cli::cli_rule("Building articles")

build_articles_index(pkg)
purrr::walk(
Expand Down Expand Up @@ -207,7 +207,7 @@ build_article <- function(name,
# allow code sharing with building of the index.
vig <- match(name, pkg$vignettes$name)
if (is.na(vig)) {
stop("Can't find article called ", src_path(name), call. = FALSE)
cli::cli_abort("Can't find article {.file {name}}")
}

input <- pkg$vignettes$file_in[vig]
Expand Down Expand Up @@ -368,13 +368,7 @@ data_articles_index <- function(pkg = ".") {
missing <- setdiff(pkg$vignettes$name, c(listed, pkg$package))

if (length(missing) > 0) {
abort(
paste0(
"Vignettes missing from index: ",
paste(missing, collapse = ", ")
),
call. = FALSE
)
cli::cli_abort("{cli::qty(missing)} Vignette{?s} missing from index: {.file {missing}}")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

print_yaml(list(
Expand All @@ -385,7 +379,7 @@ data_articles_index <- function(pkg = ".") {

data_articles_index_section <- function(section, pkg) {
if (!set_contains(names(section), c("title", "contents"))) {
abort("Section must have components `title`, `contents`")
cli::cli_abort("Section must have components {.field title}, {.field contents}")
}

# Match topics against any aliases
Expand Down Expand Up @@ -424,7 +418,6 @@ default_articles_index <- function(pkg = ".") {
return(NULL)
}


print_yaml(list(
list(
title = tr_("All vignettes"),
Expand Down
42 changes: 19 additions & 23 deletions R/build-favicons.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
rlang::check_installed("openssl")
pkg <- as_pkgdown(pkg)

rule("Building favicons")
cli::cli_rule("Building favicons")

logo_path <- find_logo(pkg$src_path)

if (is.null(logo_path)) {
stop("Can't find package logo PNG or SVG to build favicons.", call. = FALSE)
cli::cli_abort(
"Can't find package logo PNG or SVG to build favicons.",
call = caller_env()
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
)
}

if (has_favicons(pkg) && !overwrite) {
message("Favicons already exist in `pkgdown/`. Set `overwrite = TRUE` to re-create.")
cli::cli_inform(c(
"Favicons already exist in {.path pkgdown}",
"i" = "Set {.var overwrite = TRUE} to re-create."
))
return(invisible())
}

message("Building favicons with realfavicongenerator.net...")
cli::cli_inform("Building favicons with {.url https://realfavicongenerator.net} ...")

logo <- readBin(logo_path, what = "raw", n = fs::file_info(logo_path)$size)

Expand Down Expand Up @@ -66,17 +72,19 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
quiet = TRUE
)
if (httr::http_error(resp)) {
stop("API request failed.", call. = FALSE)
cli::cli_abort("API request failed.", call = caller_env())
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
}

content <- httr::content(resp)
result <- content$favicon_generation_result

if (!identical(result$result$status, "success")) {
stop(
"API request failed. ", "
Please submit bug report to <https://github.com/r-lib/pkgdown/issues>",
call. = FALSE
cli::cli_abort(
c(
"API request failed.",
"i" = "{.href [Please submit a bug report](https://github.com/r-lib/pkgdown/issues)}"
),
call = caller_env()
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -93,27 +101,15 @@ build_favicons <- function(pkg = ".", overwrite = FALSE) {
utils::unzip(tmp, exdir = path(pkg$src_path, "pkgdown", "favicon"))
},
warning = function(e) {
abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
},
error = function(e) {
abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
cli::cli_abort("Your logo file couldn't be processed and may be corrupt.", parent = e)
})

invisible()
}

#' Deprecated as of pkgdown 1.4.0
#' @rdname build_favicons
#' @inheritParams build_favicons
#' @export
build_favicon <- function(pkg, overwrite) {
message(
"`build_favicon()` is deprecated as of pkgdown 1.4.0. ",
"Please use `build_favicons()` instead."
)
build_favicons(pkg, overwrite)
}

copy_favicons <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

Expand Down
4 changes: 2 additions & 2 deletions R/build-github.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build_site_github_pages <- function(pkg = ".",
pkg <- as_pkgdown(pkg, override = list(destination = dest_dir))

if (clean) {
rule("Cleaning files from old site", line = 1)
cli::cli_rule("Cleaning files from old site")
clean_site(pkg)
}

Expand All @@ -36,7 +36,7 @@ build_site_github_pages <- function(pkg = ".",
}

build_github_pages <- function(pkg = ".") {
rule("Extra files for GitHub pages")
cli::cli_rule("Extra files for GitHub pages")
pkg <- as_pkgdown(pkg)

# Add .nojekyll since site is static HTML
Expand Down
4 changes: 2 additions & 2 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ role_lookup <- function(abbr) {

out <- unname(roles[abbr])
if (any(is.na(out))) {
missing <- paste0("'", abbr[is.na(out)], "'", collapse = ", ")
warn(paste0("Unknown MARC role abbreviation ", missing))
missing <- abbr[is.na(out)]
cli::cli_warn("Unknown MARC role abbreviation{?s}: {.field {missing}}")
out[is.na(out)] <- abbr[is.na(out)]
}
out
Expand Down
24 changes: 14 additions & 10 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ data_home_sidebar <- function(pkg = ".") {

if (length(html_path)) {
if (!file.exists(html_path)) {
abort(
sprintf(
"Can't find file '%s' specified by %s.",
pkg$meta$home$sidebar$html,
pkgdown_field(pkg, c("home", "sidebar", "html"))
)
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 = paste0(msg_fld, " is misconfigured.")
jayhesselberth marked this conversation as resolved.
Show resolved Hide resolved
))

}
return(read_file(html_path))
}
Expand Down Expand Up @@ -215,10 +219,10 @@ check_missing_images <- function(pkg, src_path, dst_path) {
exists <- fs::file_exists(path(pkg$dst_path, rel_path))

if (any(!exists)) {
paths <- encodeString(rel_src[!exists], quote = "'")
warn(c(
paste0("Missing images in '", src_path, "': ", paste0(paths, collapse = ", ")),
i = "pkgdown can only use images in 'man/figures' and 'vignettes'"
paths <- rel_src[!exists]
cli::cli_warn(c(
"Missing images in {.file {src_path}}: {.file {paths}}",
i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
))
}
}
2 changes: 1 addition & 1 deletion R/build-home-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ build_home_md <- function(pkg) {
}

render_md <- function(pkg, filename) {
cat_line("Reading ", src_path(path_rel(filename, pkg$src_path)))
cli::cli_inform("Reading {src_path(path_rel(filename, pkg$src_path))}")

body <- markdown_body(filename, strip_header = TRUE)
path <- path_ext_set(basename(filename), "html")
Expand Down
2 changes: 1 addition & 1 deletion R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ build_home <- function(pkg = ".",
quiet = TRUE) {

pkg <- section_init(pkg, depth = 0L, override = override)
rule("Building home")
cli::cli_rule("Building home")
dir_create(pkg$dst_path)

build_citation_authors(pkg)
Expand Down
8 changes: 4 additions & 4 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ build_news <- function(pkg = ".",
if (!has_news(pkg$src_path))
return()

rule("Building news")
cli::cli_rule("Building news")
dir_create(path(pkg$dst_path, "news"))

switch(news_style(pkg$meta),
Expand Down Expand Up @@ -151,7 +151,7 @@ data_news <- function(pkg = list()) {
sections <- xml2::xml_find_all(xml, "./body/div")
footnotes <- has_class(sections, "footnotes")
if (any(footnotes)) {
warn("Footnotes in NEWS.md are not currently supported")
cli::cli_warn("Footnotes in NEWS.md are not currently supported")
}
sections <- sections[!footnotes]

Expand All @@ -160,10 +160,10 @@ data_news <- function(pkg = list()) {
xml2::xml_name()
ulevels <- unique(levels)
if (!identical(ulevels, "h1") && !identical(ulevels, "h2")) {
abort(c(
cli::cli_abort(c(
"Invalid NEWS.md: inconsistent use of section headings.",
i = "Top-level headings must be either all <h1> or all <h2>.",
i = "See ?build_news for more details."
i = "See {.help pkgdown::build_news} for more details."
))
}
if (ulevels == "h1") {
Expand Down
18 changes: 10 additions & 8 deletions R/build-redirects.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ build_redirects <- function(pkg = ".",
return(invisible())
}

rule("Building redirects")
cli::cli_rule("Building redirects")
if (is.null(pkg$meta$url)) {
abort(sprintf("%s required to generate redirects", pkgdown_field(pkg, "url")))
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,12 +26,13 @@ build_redirects <- function(pkg = ".",

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

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

if (any_null) {
abort(
msg_fld <- pkgdown_field(pkg, "reference", cfg = TRUE, fmt = TRUE)
cli::cli_abort(
c(
sprintf(
"Item %s in section %s in %s is empty.",
toString(which(null)),
index,
pkgdown_field(pkg, "reference")
),
i = "Either delete the empty line or add a function name."
)
"Item {.field {which(null)}} in section {index} is empty.",
x = paste0("Delete the empty line or add function name to ", msg_fld, ".")
), call = caller_env()
)
}

Expand All @@ -89,16 +85,12 @@ check_all_characters <- function(contents, index, pkg) {
return(invisible())
}

abort(
msg_fld <- pkgdown_field(pkg, "reference", cfg = TRUE, fmt = TRUE)
cli::cli_abort(
c(
sprintf(
"Item %s in section %s in %s must be a character.",
toString(which(not_char)),
index,
pkgdown_field(pkg, "reference")
),
i = "You might need to add '' around e.g. - 'N' or - 'off'."
)
"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, ".")
), call = caller_env()
)

}
Expand Down
18 changes: 9 additions & 9 deletions R/build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@ build_reference <- function(pkg = ".",
topics = NULL) {
pkg <- section_init(pkg, depth = 1L, override = override)

if (!missing(document)) {
warning("`document` is deprecated. Please use `devel` instead.", call. = FALSE)
if (document != "DEPRECATED") {
lifecycle::deprecate_warn(
"1.4.0",
"build_site(document)",
details = "Please use `build_site(devel)` instead."
)
devel <- document
}

rule("Building function reference")
cli::cli_rule("Building function reference")
build_reference_index(pkg)

copy_figures(pkg)
Expand Down Expand Up @@ -274,7 +278,7 @@ build_reference_topic <- function(topic,
if (lazy && !out_of_date(in_path, out_path))
return(invisible())

cat_line("Reading ", src_path("man", topic$file_in))
cli::cli_inform("Reading {src_path(path('man', topic$file_in))}")

data <- withCallingHandlers(
data_reference_topic(
Expand All @@ -284,11 +288,7 @@ build_reference_topic <- function(topic,
run_dont_run = run_dont_run
),
error = function(err) {
msg <- c(
paste0("Failed to parse Rd in ", topic$file_in),
i = err$message
)
abort(msg, parent = err)
cli::cli_abort("Failed to parse Rd in {.file {topic$file_in}}", parent = err)
}
)

Expand Down
Loading
Loading