Skip to content

Commit

Permalink
Tidying up utils and helpers (#2651)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Jun 10, 2024
1 parent 7180f25 commit fd8ed24
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 99 deletions.
5 changes: 5 additions & 0 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,8 @@ req_pkgdown_cache <- function(req) {
max_age = 86400 # 1 day
)
}

# authors forced to wrap words in '' to prevent spelling errors
cran_unquote <- function(string) {
gsub("\\'(.*?)\\'", "\\1", string)
}
59 changes: 0 additions & 59 deletions R/pkgdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,3 @@ local_envvar_pkgdown <- function(pkg, scope = parent.frame()) {
.local_envir = scope
)
}

local_pkgdown_site <- function(path = NULL,
meta = list(),
desc = list(),
env = caller_env()) {
check_string(path, allow_null = TRUE)

dst_path <- withr::local_tempdir(.local_envir = env)
# Simulate init_site() so we only have to run it if we care about <head>
file_create(path(dst_path, "pkgdown.yml"))
dir_create(path(dst_path, "deps"))
file_create(path(dst_path, "deps", "data-deps.txt"))

meta <- modify_list(meta, list(destination = dst_path))

if (is.null(path)) {
path <- withr::local_tempdir(.local_envir = env)

description <- desc::desc("!new")
description$set("Package", "testpackage")
description$set("Title", "A test package")
if (length(desc) > 0)
inject(description$set(!!!desc))
description$write(file = path(path, "DESCRIPTION"))

# Default to BS5 only if template not specified
meta$template <- meta$template %||% list(bootstrap = 5)

# Record meta in case we re-run as_pkgdown()
yaml::write_yaml(meta, path(path, "_pkgdown.yml"))

# Make it a bit easier to create other files
dir_create(path(path, "R"))
dir_create(path(path, "vignettes"))
}

as_pkgdown(path, meta)
}

local_pkgdown_template_pkg <- function(path = NULL, meta = NULL, env = parent.frame()) {
if (is.null(path)) {
path <- withr::local_tempdir(.local_envir = env)
desc <- desc::desc("!new")
desc$set("Package", "templatepackage")
desc$set("Title", "A test template package")
desc$write(file = path(path, "DESCRIPTION"))
}

if (!is.null(meta)) {
path_pkgdown_yml <- path(path, "inst", "pkgdown", "_pkgdown.yml")
dir_create(path_dir(path_pkgdown_yml))
yaml::write_yaml(meta, path_pkgdown_yml)
}

pkgload::load_all(path, quiet = TRUE)
withr::defer(pkgload::unload("templatepackage"), envir = env)

path
}
7 changes: 7 additions & 0 deletions R/rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ flatten_para <- function(x, ...) {
paste0(blocks, collapse = "")
}

split_at_linebreaks <- function(text) {
if (length(text) == 0) {
character()
} else {
strsplit(text, "\\n\\s*\\n")[[1]]
}
}

flatten_text <- function(x, ...) {
if (length(x) == 0) return("")
Expand Down
5 changes: 2 additions & 3 deletions R/utils-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ read_file <- function(path) {
paste0(lines, "\n", collapse = "")
}

# Inspired by roxygen2 utils-io.R (https://github.com/klutometis/roxygen/) --------

# Inspired by roxygen2 utils-io.R (https://github.com/klutometis/roxygen/) --------
read_lines <- function(path, n = -1L) {
base::readLines(path, n = n, encoding = "UTF-8", warn = FALSE) # nolint
}
Expand All @@ -22,8 +22,7 @@ file_equal <- function(src, dst) {
return(FALSE)

src_hash <- digest::digest(file = src, algo = "xxhash64")
dst_hash <- digest::digest(file = dst, algo = "xxhash64")
dst_hash <- digest::digest(file = dst, algo = "xxhash64")

identical(src_hash, dst_hash)
}

32 changes: 4 additions & 28 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
set_contains <- function(haystack, needles) {
all(needles %in% haystack)
}

split_at_linebreaks <- function(text) {
if (length(text) < 1)
return(character())
strsplit(text, "\\n\\s*\\n")[[1]]
}

up_path <- function(depth) {
paste(rep.int("../", depth), collapse = "")
}
Expand Down Expand Up @@ -53,6 +43,10 @@ unwrap_purrr_error <- function(code) {
)
}

tr_ <- function(...) {
enc2utf8(gettext(..., domain = "R-pkgdown"))
}

# devtools metadata -------------------------------------------------------

system_file <- function(..., package) {
Expand Down Expand Up @@ -84,24 +78,10 @@ writing_file <- function(path, show) {
cli::cli_inform("Writing {.run [{text}](pkgdown::preview_page('{path}'))}")
}

skip_if_no_pandoc <- function(version = "1.12.3") {
testthat::skip_if_not(rmarkdown::pandoc_available(version))
}

has_internet <- function() {
getOption("pkgdown.internet", default = TRUE)
}

# remove '' quoting
# e.g. 'title' becomes title.s
cran_unquote <- function(string) {
gsub("\\'(.*?)\\'", "\\1", string)
}

isFALSE <- function(x) {
is.logical(x) && length(x) == 1L && !is.na(x) && !x
}

modify_list <- function(x, y) {
if (is.null(x)) {
return(y)
Expand Down Expand Up @@ -221,7 +201,3 @@ print.pkgdown_xml <- function(x, ...) {
cat(as.character(x, options = c("format", "no_declaration")), sep = "\n")
invisible(x)
}

tr_ <- function(...) {
enc2utf8(gettext(..., domain = "R-pkgdown"))
}
68 changes: 68 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
skip_if_no_pandoc <- function(version = "1.12.3") {
testthat::skip_if_not(rmarkdown::pandoc_available(version))
}

# Simulate a package --------------------------------------------------------

local_pkgdown_site <- function(path = NULL,
meta = list(),
desc = list(),
env = caller_env()) {
check_string(path, allow_null = TRUE)

dst_path <- withr::local_tempdir(.local_envir = env)
# Simulate init_site() so we only have to run it if we care about <head>
file_create(path(dst_path, "pkgdown.yml"))
dir_create(path(dst_path, "deps"))
file_create(path(dst_path, "deps", "data-deps.txt"))

meta <- modify_list(meta, list(destination = dst_path))

if (is.null(path)) {
path <- withr::local_tempdir(.local_envir = env)

description <- desc::desc("!new")
description$set("Package", "testpackage")
description$set("Title", "A test package")
if (length(desc) > 0)
inject(description$set(!!!desc))
description$write(file = path(path, "DESCRIPTION"))

# Default to BS5 only if template not specified
meta$template <- meta$template %||% list(bootstrap = 5)

# Record meta in case we re-run as_pkgdown()
yaml::write_yaml(meta, path(path, "_pkgdown.yml"))

# Make it a bit easier to create other files
dir_create(path(path, "R"))
dir_create(path(path, "vignettes"))
}

as_pkgdown(path, meta)
}

pkg_add_file <- function(pkg, path, lines = NULL) {
full_path <- path(pkg$src_path, path)
dir_create(path_dir(full_path))
Expand Down Expand Up @@ -36,3 +80,27 @@ pkg_vignette <- function(..., title = "title") {
c("---", yaml, "---", contents)
}
r_code_block <- function(...) c("```{r}", ..., "```")

# Simulate a template package ------------------------------------------------

local_pkgdown_template_pkg <- function(path = NULL, meta = NULL, env = parent.frame()) {
if (is.null(path)) {
path <- withr::local_tempdir(.local_envir = env)
desc <- desc::desc("!new")
desc$set("Package", "templatepackage")
desc$set("Title", "A test template package")
desc$write(file = path(path, "DESCRIPTION"))
}

if (!is.null(meta)) {
path_pkgdown_yml <- path(path, "inst", "pkgdown", "_pkgdown.yml")
dir_create(path_dir(path_pkgdown_yml))
yaml::write_yaml(meta, path_pkgdown_yml)
}

pkgload::load_all(path, quiet = TRUE)
withr::defer(pkgload::unload("templatepackage"), envir = env)

path
}

8 changes: 8 additions & 0 deletions tests/testthat/test-build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,11 @@ test_that("package repo verification", {
list(repo = "Bioconductor", url = "https://www.bioconductor.org/packages/Biobase")
)
})


test_that("cran_unquote works", {
expect_equal(
cran_unquote("'Quoting' is CRAN's thing."),
"Quoting is CRAN's thing."
)
})
9 changes: 0 additions & 9 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ test_that("pkgdown.internet can be set and read", {
expect_false(has_internet())
})

test_that("cran_unquote works", {
expect_equal(cran_unquote("Quoting is CRAN's thing."),
"Quoting is CRAN's thing.")
expect_equal(cran_unquote("'R-hub' is great!"),
"R-hub is great!")
expect_equal(cran_unquote("From 'README' to 'html' with 'pkgdown'"),
"From README to html with pkgdown")
})

test_that("is_internal_link() works", {
pkg=list(meta=list(url="https://pkgdown.r-lib.org"))
expect_false(is_internal_link("https://github.com", pkg = pkg))
Expand Down

0 comments on commit fd8ed24

Please sign in to comment.