Skip to content

Commit

Permalink
Catch up on the article side
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Nov 23, 2024
1 parent 742c2d8 commit c18a327
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
51 changes: 33 additions & 18 deletions R/vignette.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(),
Expand Down Expand Up @@ -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}}",
Expand Down
2 changes: 1 addition & 1 deletion man/use_vignette.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions tests/testthat/test-vignette.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})

Expand Down

0 comments on commit c18a327

Please sign in to comment.