diff --git a/R/build-home-index.R b/R/build-home-index.R index cf5cf79d9..d589970b4 100644 --- a/R/build-home-index.R +++ b/R/build-home-index.R @@ -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) +} diff --git a/R/pkgdown.R b/R/pkgdown.R index ef88c1100..d61a3cecf 100644 --- a/R/pkgdown.R +++ b/R/pkgdown.R @@ -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 - 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 -} diff --git a/R/rd-html.R b/R/rd-html.R index 12466497a..2e8a4acd5 100644 --- a/R/rd-html.R +++ b/R/rd-html.R @@ -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("") diff --git a/R/utils-io.R b/R/utils-io.R index 90d078e9c..87ce958ca 100644 --- a/R/utils-io.R +++ b/R/utils-io.R @@ -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 } @@ -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) } - diff --git a/R/utils.R b/R/utils.R index f95dac9d2..bb33a495a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 = "") } @@ -53,6 +43,10 @@ unwrap_purrr_error <- function(code) { ) } +tr_ <- function(...) { + enc2utf8(gettext(..., domain = "R-pkgdown")) +} + # devtools metadata ------------------------------------------------------- system_file <- function(..., package) { @@ -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) @@ -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")) -} diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 43d617c8b..c3f89ac5a 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -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 + 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)) @@ -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 +} + \ No newline at end of file diff --git a/tests/testthat/test-build-home-index.R b/tests/testthat/test-build-home-index.R index 317164ac1..283b39126 100644 --- a/tests/testthat/test-build-home-index.R +++ b/tests/testthat/test-build-home-index.R @@ -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." + ) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 27a5cc460..e1acabed4 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -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))