From c18a327a265dc92f4243ca595cb0e01762f0aff2 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Sat, 23 Nov 2024 08:02:21 -0800 Subject: [PATCH] Catch up on the article side --- R/vignette.R | 51 ++++++++++++++++++++++------------ man/use_vignette.Rd | 2 +- tests/testthat/test-vignette.R | 30 ++++++++++++++------ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/R/vignette.R b/R/vignette.R index 2b7fcaaaf..59b4c32c0 100644 --- a/R/vignette.R +++ b/R/vignette.R @@ -67,15 +67,29 @@ use_vignette <- function(name, title = NULL) { #' @export #' @rdname use_vignette -use_article <- function(name, title = name) { +use_article <- function(name, title = NULL) { check_is_package("use_article()") + check_required(name) + maybe_name(title) - deps <- proj_deps() - if (!"rmarkdown" %in% deps$package) { - proj_desc_field_update("Config/Needs/website", "rmarkdown", append = TRUE) + ext <- get_vignette_extension(name) + if (ext == "qmd") { + check_installed("quarto") + check_installed("pkgdown", version = "2.1.0") } - use_vignette_template("article.Rmd", name, title, subdir = "articles") + name <- path_ext_remove(name) + title <- title %||% name + + if (tolower(ext) == "rmd") { + proj_desc_field_update("Config/Needs/website", "rmarkdown", overwrite = TRUE, append = TRUE) + use_vignette_template("article.Rmd", name, title, subdir = "articles") + } else { + # check this dependency stuff + use_dependency("quarto", "Suggests") + proj_desc_field_update("Config/Needs/website", "quarto", overwrite = TRUE, append = TRUE) + use_vignette_template("article.qmd", name, title, subdir = "articles") + } use_build_ignore("vignettes/articles") invisible() @@ -89,22 +103,23 @@ use_vignette_template <- function(template, name, title, subdir = NULL) { ext <- get_vignette_extension(template) - use_directory("vignettes") - if (!is.null(subdir)) { - use_directory(path("vignettes", subdir)) + if (is.null(subdir)) { + target_dir <- "vignettes" + } else { + target_dir <- path("vignettes", subdir) } - use_git_ignore(c("*.html", "*.R"), directory = "vignettes") + + use_directory(target_dir) + + use_git_ignore(c("*.html", "*.R"), directory = target_dir) if (ext == "qmd") { - use_build_ignore("vignettes/.quarto") - use_build_ignore("vignettes/*_files") - use_git_ignore("*_files", "vignettes") + use_git_ignore("**/.quarto/") + use_git_ignore("*_files", target_dir) + use_build_ignore(path(target_dir, ".quarto")) + use_build_ignore(path(target_dir, "*_files")) } - if (is.null(subdir)) { - path <- path("vignettes", asciify(name), ext = ext) - } else { - path <- path("vignettes", subdir, asciify(name), ext = ext) - } + path <- path(target_dir, asciify(name), ext = ext) data <- list( Package = project_name(), @@ -144,7 +159,7 @@ check_vignette_extension <- function(ext) { if (! ext %in% c("Rmd", "rmd", "qmd")) { valid_exts_cli <- cli::cli_vec( c("Rmd", "qmd"), - style = list("vec-last" = " or ", "vec-sep2" = " or ") + style = list("vec-sep2" = " or ") ) ui_abort(c( "Unsupported file extension: {.val {ext}}", diff --git a/man/use_vignette.Rd b/man/use_vignette.Rd index dfd7dac07..9dcf81700 100644 --- a/man/use_vignette.Rd +++ b/man/use_vignette.Rd @@ -7,7 +7,7 @@ \usage{ use_vignette(name, title = NULL) -use_article(name, title = name) +use_article(name, title = NULL) } \arguments{ \item{name}{File name to use for new vignette. Should consist only of diff --git a/tests/testthat/test-vignette.R b/tests/testthat/test-vignette.R index bb2d67633..c23681fe7 100644 --- a/tests/testthat/test-vignette.R +++ b/tests/testthat/test-vignette.R @@ -70,24 +70,38 @@ test_that("use_vignette() does the promised setup, mix of Rmd and qmd", { }) # use_article ------------------------------------------------------------- - -test_that("use_article goes in article subdirectory", { +test_that("use_article() does the promised setup, Rmd", { create_local_package() + local_interactive(FALSE) + + # Let's have another package already in Config/Needs/website + proj_desc_field_update("Config/Needs/website", "somepackage") + use_article("name", "title") - use_article("test") - expect_proj_file("vignettes/articles/test.Rmd") + expect_proj_file("vignettes/articles/name.Rmd") + + expect_setequal( + proj_desc()$get_list("Config/Needs/website"), + c("rmarkdown", "somepackage") + ) }) -test_that("use_article() adds rmarkdown to Config/Needs/website", { +# Note that qmd articles seem to cause problems for build_site() rn +# https://github.com/r-lib/pkgdown/issues/2821 +test_that("use_article() does the promised setup, qmd", { create_local_package() + local_check_installed() local_interactive(FALSE) - proj_desc_field_update("Config/Needs/website", "somepackage", append = TRUE) - use_article("name", "title") + # Let's have another package already in Config/Needs/website + proj_desc_field_update("Config/Needs/website", "somepackage") + use_article("name.qmd", "title") + + expect_proj_file("vignettes/articles/name.qmd") expect_setequal( proj_desc()$get_list("Config/Needs/website"), - c("rmarkdown", "somepackage") + c("quarto", "somepackage") ) })