Skip to content

Commit

Permalink
Add is_using_quarto()
Browse files Browse the repository at this point in the history
This function detect `_quarto.yml` or `*.qmd` in a directory

closes #103
  • Loading branch information
cderv committed Jan 25, 2024
1 parent 0884290 commit 7e9796d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: quarto
Title: R Interface to 'Quarto' Markdown Publishing System
Version: 1.3.7
Version: 1.3.8
Authors@R: c(
person("JJ", "Allaire", , "jj@rstudio.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0174-9868")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(is_using_quarto)
export(quarto_add_extension)
export(quarto_inspect)
export(quarto_path)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# quarto (development version)

- Add `is_using_quarto()` to check if a directory requires using Quarto (i.e. it has a `_quarto.yml` or at least one `*.qmd` file) (thanks, @hadley, #103).

- Add `quarto_args` to `quarto_render()` and other commands to pass additional arguments to `quarto` CLI commands. This is for advanced usage e.g. when new options are added to Quarto CLI that would not be user-facing in this package's functions (thanks, @gadenbuie, #125).

- Add `quiet` argument to pass `--quiet` to `quarto` CLI commands
Expand Down
30 changes: 30 additions & 0 deletions R/quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,33 @@ quarto_run_what <- function(what = character(), args = character(), quarto_bin =
res <- quarto_run(quarto_bin, args = c(what, args), echo = echo, ..., .call = .call)
invisible(res)
}

#' Check is a directory is using quarto
#'
#' This function will check if a directory is using quarto by looking for
#' - `_quarto.yml` at its root
#' - at least one `.qmd` file in the directory
#'
#' @param dir The directory to check
#' @examples
#' dir.create(tmpdir <- tempfile())
#' is_using_quarto(tmpdir)
#' file.create(file.path(tmpdir, "_quarto.yml"))
#' is_using_quarto(tmpdir)
#' @export
is_using_quarto <- function(dir = ".", verbose = FALSE) {
has_quarto_yml <- length(list.files(dir, pattern = "_quarto\\.yml$", full.names = TRUE)) > 0
has_qmd <- length(list.files(dir, pattern = "\\.qmd$", full.names = TRUE)) > 0
if (has_quarto_yml) {
if (verbose) cli::cli_inform("A {.file _quarto.yml} has been found.")
return(TRUE)
} else if (has_qmd) {
if (verbose) cli::cli_inform("At least one file {.code *.qmd} has been found.")
return(TRUE)
}
# not a directory using Quarto
if (verbose) cli::cli_inform("No {.file _quarto.yml} or {.code *.qmd} has been found.")
return(FALSE)
}


1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ reference:
- quarto_inspect
- quarto_path
- quarto_version
- is_using_quarto

24 changes: 24 additions & 0 deletions man/is_using_quarto.Rd

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

36 changes: 36 additions & 0 deletions tests/testthat/_snaps/quarto.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,39 @@
Caused by error:
! System command 'quarto' failed

# is_using_quarto correctly check directory

Code
is_using_quarto(dirname(qmd), verbose = TRUE)
Message
At least one file `*.qmd` has been found.
Output
[1] TRUE

---

Code
is_using_quarto(dirname(qmd), verbose = TRUE)
Message
A '_quarto.yml' has been found.
Output
[1] TRUE

---

Code
is_using_quarto(dirname(qmd), verbose = TRUE)
Message
A '_quarto.yml' has been found.
Output
[1] TRUE

---

Code
is_using_quarto(dirname(qmd), verbose = TRUE)
Message
No '_quarto.yml' or `*.qmd` has been found.
Output
[1] FALSE

22 changes: 22 additions & 0 deletions tests/testthat/test-quarto.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,25 @@ test_that("quarto_run gives guidance in error", {
transform = transform_quarto_cli_in_output()
)
})

test_that("is_using_quarto correctly check directory", {
qmd <- local_qmd_file(c("content"))
# Only qmd
expect_true(is_using_quarto(dirname(qmd)))
expect_snapshot(is_using_quarto(dirname(qmd), verbose = TRUE))
# qmd and _quarto.yml
write_yaml(list(project = list(type = "default")), file = file.path(dirname(qmd), "_quarto.yml"))
expect_true(is_using_quarto(dirname(qmd)))
expect_snapshot(is_using_quarto(dirname(qmd), verbose = TRUE))
# Only _quarto.yml
unlink(qmd)
expect_true(is_using_quarto(dirname(qmd)))
expect_snapshot(is_using_quarto(dirname(qmd), verbose = TRUE))
# Empty dir
unlink(file.path(dirname(qmd), "_quarto.yml"))
expect_false(is_using_quarto(dirname(qmd)))
expect_snapshot(is_using_quarto(dirname(qmd), verbose = TRUE))
# Non empty dir
withr::local_dir(dirname(qmd))
withr::local_file("test.txt")
})

0 comments on commit 7e9796d

Please sign in to comment.