Skip to content

Commit

Permalink
Eliminate magrittr and rvest deps
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 30, 2024
1 parent 3ae60ad commit 9c8db04
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 160 deletions.
2 changes: 0 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Imports:
fs (>= 1.4.0),
httr2 (>= 1.0.0),
jsonlite,
magrittr,
openssl,
purrr (>= 1.0.0),
ragg,
Expand All @@ -57,7 +56,6 @@ Suggests:
rsconnect,
rstudioapi,
rticles,
rvest,
sass,
testthat (>= 3.1.3),
tools
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ export(template_navbar)
export(template_reference)
import(fs)
import(rlang)
importFrom(magrittr,"%>%")
importFrom(utils,installed.packages)
9 changes: 4 additions & 5 deletions R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ data_articles_index <- function(pkg = ".", call = caller_env()) {
))

# Check for unlisted vignettes
listed <- sections %>%
purrr::map("contents") %>%
purrr::map(. %>% purrr::map_chr("name")) %>%
purrr::flatten_chr() %>%
unique()
all_names <- purrr::map(sections, function(section) {
purrr::map_chr(section$contents, "name")
})
listed <- unique(purrr::list_c(all_names))

missing <- setdiff(articles$name, listed)
# Exclude get started vignette or article #2150
Expand Down
7 changes: 3 additions & 4 deletions R/build-footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ footnote_components <- function(pkg = ".", call = caller_env()) {
default = default_roles(),
call = call
)
authors <- data_authors(pkg, roles = roles)$main %>%
purrr::map_chr("name") %>%
paste(collapse = ", ")
authors <- data_authors(pkg, roles = roles)$main
authors_str <- paste(purrr::map_chr(authors, "name"), collapse = ", ")

prefix <- config_pluck_string(
pkg,
"authors.footer.text",
default = tr_("Developed by"),
call = call
)
developed_by <- paste0(trimws(prefix), " ", authors, ".")
developed_by <- paste0(trimws(prefix), " ", authors_str, ".")

# pkgdown
built_with <- sprintf(
Expand Down
20 changes: 6 additions & 14 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,20 @@ data_authors <- function(pkg = ".", roles = default_roles(), call = caller_env()
inst <- NULL
}

all <- pkg %>%
pkg_authors() %>%
purrr::map(author_list, author_info, pkg = pkg)

main <- pkg %>%
pkg_authors(roles) %>%
purrr::map(author_list, author_info, pkg = pkg)
authors_all <- pkg_authors(pkg)
authors_main <- pkg_authors(pkg, roles)

all <- purrr::map(authors_all, author_list, author_info, pkg = pkg)
main <- purrr::map(authors_main, author_list, author_info, pkg = pkg)
more_authors <- length(main) != length(all)

comments <- pkg %>%
pkg_authors() %>%
purrr::map(author_list, author_info, pkg = pkg) %>%
purrr::map("comment") %>%
purrr::compact() %>%
length() > 0
comments <- purrr::compact(purrr::map(all, "comment"))

print_yaml(list(
all = all,
main = main,
inst = inst,
needs_page = more_authors || comments || !is.null(inst),
needs_page = more_authors || length(comments) > 0 || !is.null(inst),
before = config_pluck_markdown_block(pkg, "template.authors.before", call = call),
after = config_pluck_markdown_block(pkg, "template.authors.after", call = call)
))
Expand Down
31 changes: 15 additions & 16 deletions R/build-news.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ build_news_multi <- function(pkg = ".") {
news_paged <- tibble::tibble(
version = levels(page),
file_out = paste0("news-", version, ".html"),
contents = news[c("html", "version", "anchor")] %>% split(page)
contents = split(news[c("html", "version", "anchor")], page)
)

render_news <- function(version, file_out, contents) {
Expand All @@ -131,13 +131,13 @@ build_news_multi <- function(pkg = ".") {
path("news", file_out),
)
}
news_paged %>% purrr::pmap(render_news)
purrr::pwalk(news_paged, render_news)

render_page(
pkg,
"news-index",
list(
versions = news_paged %>% purrr::transpose(),
versions = purrr::transpose(news_paged),
pagetitle = tr_("News")
),
path("news", "index.html")
Expand All @@ -158,9 +158,8 @@ data_news <- function(pkg = list()) {
}
sections <- sections[!footnotes]

levels <- sections %>%
xml2::xml_find_first(".//h1|.//h2|.//h3|.//h4|.//h5") %>%
xml2::xml_name()
headings <- xml2::xml_find_first(sections, ".//h1|.//h2|.//h3|.//h4|.//h5")
levels <- xml2::xml_name(headings)
ulevels <- unique(levels)
if (!identical(ulevels, "h1") && !identical(ulevels, "h2")) {
cli::cli_abort(
Expand Down Expand Up @@ -197,16 +196,16 @@ data_news <- function(pkg = list()) {
} else {
timeline <- NULL
}

html <- sections %>%
purrr::walk2(
versions,
tweak_news_heading,
timeline = timeline,
bs_version = pkg$bs_version
) %>%
purrr::map_chr(as.character, options = character()) %>%
purrr::map_chr(repo_auto_link, pkg = pkg)
purrr::walk2(
sections,
versions,
tweak_news_heading,
timeline = timeline,
bs_version = pkg$bs_version
)
html <- purrr::map_chr(sections, as.character, options = character())
html <- purrr::map_chr(html, repo_auto_link, pkg = pkg)

anchors <- xml2::xml_attr(sections, "id")
news <- tibble::tibble(
Expand Down
10 changes: 3 additions & 7 deletions R/build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ data_reference_index <- function(pkg = ".", error_call = caller_env()) {
return(list())
}

unwrap_purrr_error(
rows <- meta %>%
purrr::imap(data_reference_index_rows, pkg = pkg, call = error_call) %>%
purrr::compact() %>%
unlist(recursive = FALSE)
)
rows <- unwrap_purrr_error(purrr::imap(meta, data_reference_index_rows, pkg = pkg, call = error_call))
rows <- purrr::list_c(rows)

has_icons <- purrr::some(rows, ~ .x$row_has_icons %||% FALSE)

Expand Down Expand Up @@ -163,7 +159,7 @@ default_reference_index <- function(pkg = ".") {

check_missing_topics <- function(rows, pkg, error_call = caller_env()) {
# Cross-reference complete list of topics vs. topics found in index page
all_topics <- rows %>% purrr::map("names") %>% unlist(use.names = FALSE)
all_topics <- purrr::list_c(purrr::map(rows, "names"))
in_index <- pkg$topics$name %in% all_topics

missing <- !in_index & !pkg$topics$internal
Expand Down
14 changes: 5 additions & 9 deletions R/build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -394,18 +394,14 @@ data_reference_topic <- function(topic,
"seealso", "section", "author"
))
sections <- topic$rd[tag_names %in% section_tags]
out$sections <- sections %>%
purrr::map(as_data) %>%
purrr::map(add_slug)

out$sections <- purrr::map(sections, function(section) {
data <- as_data(section)
data$slug <- make_slug(data$title)
data
})
out
}

add_slug <- function(x) {
x$slug <- make_slug(x$title)
x
}

make_slug <- function(x) {
x <- strip_html_tags(x)
x <- tolower(x)
Expand Down
4 changes: 2 additions & 2 deletions R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ news_search_index <- function(path, pkg) {
file_search_index <- function(path, pkg) {
html <- xml2::read_html(path(pkg$dst_path, path), encoding = "UTF-8")
# Get page title
title <- xml2::xml_find_first(html, ".//meta[@property='og:title']") %>%
xml2::xml_attr("content")
title_element <- xml2::xml_find_first(html, ".//meta[@property='og:title']")
title <- xml2::xml_attr(title_element, "content")

# Get contents minus logo
node <- xml2::xml_find_all(html, ".//main")
Expand Down
11 changes: 7 additions & 4 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ markdown_body <- function(pkg, path, strip_header = FALSE) {
# Extract body of html - as.character renders as xml which adds
# significant whitespace in tags like pre
transformed_path <- withr::local_tempfile()
xml %>%
xml2::xml_find_first(".//body") %>%
xml2::write_html(transformed_path, format = FALSE)
body <- xml2::xml_find_first(xml, ".//body")
xml2::write_html(body, transformed_path, format = FALSE)

lines <- read_lines(transformed_path)
lines <- sub("<body>", "", lines, fixed = TRUE)
Expand Down Expand Up @@ -80,7 +79,7 @@ markdown_path_html <- function(pkg, path, strip_header = FALSE) {

markdown_to_html <- function(pkg, text, dedent = 4, bs_version = 3) {
if (dedent) {
text <- gsub(paste0("($|\n)", strrep(" ", dedent)), "\\1", text, perl = TRUE)
text <- dedent(text, dedent)
}

md_path <- withr::local_tempfile()
Expand All @@ -94,6 +93,10 @@ markdown_to_html <- function(pkg, text, dedent = 4, bs_version = 3) {
html
}

dedent <- function(x, n = 4) {
gsub(paste0("($|\n)", strrep(" ", n)), "\\1", x, perl = TRUE)
}

convert_markdown_to_html <- function(pkg, in_path, out_path, ...) {
if (rmarkdown::pandoc_available("2.0")) {
from <- "markdown+gfm_auto_identifiers-citations+emoji+autolink_bare_uris"
Expand Down
10 changes: 3 additions & 7 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,12 @@ package_rd <- function(path = ".") {
}

extract_tag <- function(x, tag) {
x %>%
purrr::keep(inherits, tag) %>%
purrr::map_chr(c(1, 1))
purrr::map_chr(purrr::keep(x, inherits, tag), c(1, 1))
}

extract_title <- function(x) {
x %>%
purrr::detect(inherits, "tag_title") %>%
flatten_text(auto_link = FALSE) %>%
str_squish()
title <- purrr::detect(x, inherits, "tag_title")
str_squish(flatten_text(title, auto_link = FALSE))
}

extract_source <- function(x) {
Expand Down
1 change: 0 additions & 1 deletion R/pkgdown-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom magrittr %>%
#' @importFrom utils installed.packages
#' @import rlang
#' @import fs
Expand Down
21 changes: 6 additions & 15 deletions R/rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@ flatten_para <- function(x, ...) {
needs_split <- !is_block & !empty
html[needs_split] <- purrr::map(html[needs_split], split_at_linebreaks)

blocks <- html %>%
split(groups) %>%
purrr::map(unlist) %>%
purrr::map_chr(paste, collapse = "")
blocks <- purrr::map_chr(split(html, groups), function(x) {
paste(unlist(x), collapse = "")
})

# There are three types of blocks:
# 1. Combined text and inline tags
# 2. Paragraph breaks (text containing only "\n")
# 3. Block-level tags
#
# Need to wrap 1 in <p>
needs_p <- (!(is_nl | is_block)) %>%
split(groups) %>%
purrr::map_lgl(any)

needs_p <- purrr::map_lgl(split(!(is_nl | is_block), groups), any)
blocks[needs_p] <- paste0("<p>", str_trim(blocks[needs_p]), "</p>")

paste0(blocks, collapse = "")
Expand Down Expand Up @@ -384,10 +380,7 @@ parse_items <- function(rd, ...) {
paste0("<li>", flatten_para(x, ...), "</li>\n")
}

rd %>%
split(group) %>%
purrr::map_chr(parse_item) %>%
paste(collapse = "")
paste(purrr::map_chr(split(rd, group), parse_item), collapse = "")
}

parse_descriptions <- function(rd, ..., id_prefix = NULL) {
Expand Down Expand Up @@ -417,9 +410,7 @@ parse_descriptions <- function(rd, ..., id_prefix = NULL) {
}
}

rd %>%
purrr::map_chr(parse_item) %>%
paste(collapse = "")
paste(purrr::map_chr(rd, parse_item), collapse = "")
}

# Marking text ------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ NULL
#' Test case: links
#'
#' ```{r}
#' magrittr::subtract(10, 1)
#' jsonlite::minify("{}")
#' ```
#'
#' @name test-links
#' @keywords internal
#' @family tests
#' @examples
#' magrittr::subtract(10, 1)
#' jsonlite::minify("{}")
#'
#' library(magrittr, warn.conflicts = FALSE)
#' subtract(10, 1)
#' library(jsonlite, warn.conflicts = FALSE)
#' minify("{}")
NULL

#' Test case: figures
Expand Down
27 changes: 9 additions & 18 deletions R/topics.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ match_env <- function(topics) {
# dplyr-like matching functions

any_alias <- function(f, ..., .internal = FALSE) {
alias_match <- topics$alias %>%
unname() %>%
purrr::map(f, ...) %>%
purrr::map_lgl(any)

name_match <- topics$name %>%
purrr::map_lgl(f, ...)
f <- as_function(f)
alias_match <- purrr::map_lgl(unname(topics$alias), function(x) {
any(f(x, ...))
})
name_match <- purrr::map_lgl(topics$name, f, ...)

which((alias_match | name_match) & is_public(.internal))
}
Expand Down Expand Up @@ -140,27 +138,20 @@ match_env <- function(topics) {
check_string(x)
check_bool(internal)

match <- topics$concepts %>%
purrr::map(~ str_trim(.) == x) %>%
purrr::map_lgl(any)

match <- purrr::map_lgl(topics$concepts, ~ any(str_trim(.) == x))
which(match & is_public(internal))
}
fns$lacks_concepts <- function(x, internal = FALSE) {
check_character(x)
check_bool(internal)

nomatch <- topics$concepts %>%
purrr::map(~ match(str_trim(.), x, nomatch = FALSE)) %>%
purrr::map_lgl(~ length(.) == 0L | all(. == 0L))

which(nomatch & is_public(internal))

match <- purrr::map_lgl(topics$concepts, ~ any(str_trim(.) == x))
which(!match & is_public(internal))
}
fns$lacks_concept <- fns$lacks_concepts
out
}


match_eval <- function(string, env) {
# Early return in case string already matches symbol
if (env_has(env, string)) {
Expand Down
Loading

0 comments on commit 9c8db04

Please sign in to comment.