Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Suggests:
MASS,
mockery (>= 0.4.3),
pingr (>= 2.0.1),
quarto (>= 1.5.1),
rhub (>= 1.1.1),
rmarkdown (>= 2.14),
rstudioapi (>= 0.13),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(as.package)
export(bash)
export(build)
export(build_manual)
export(build_md)
export(build_readme)
export(build_rmd)
export(build_site)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# devtools (development version)
* `build_rmd()` is deprecated in favor of `build_md()` which also handles Quarto.

# devtools 2.4.6

Expand Down
120 changes: 95 additions & 25 deletions R/build-readme.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#' Build a Rmarkdown files package
#' Build a qmd or Rmarkdown files package
#'
#' `build_rmd()` is a wrapper around [rmarkdown::render()] that first installs
#' a temporary copy of the package, and then renders each `.Rmd` in a clean R
#' session. `build_readme()` locates your `README.Rmd` and builds it into a
#' `README.md`
#' `build_md()` is a wrapper around [rmarkdown::render()] and
#' [quarto::quarto_render()] that first installs a temporary copy of the
#' package, and then renders each `.Rmd` or `.qmd` in a clean R session.
#' `build_readme()` locates your `README.Rmd` or `README.qmd` and builds it into
#' a `README.md`.
#'
#' @param files The Rmarkdown files to be rendered.
#' @param files The R Markdown or Quarto files to be rendered.
#' @param path path to the package to build the readme.
#' @param ... additional arguments passed to [rmarkdown::render()]
#' @param output_options A list of options passed to [rmarkdown::render()] for
#' `.Rmd` inputs. Ignored for Quarto `.qmd` inputs.
#' @param ... additional arguments passed to [rmarkdown::render()] for `.Rmd`
#' inputs or to [quarto::quarto_render()] for `.qmd` inputs. Arguments are shared.
#' @inheritParams install
#' @inheritParams rmarkdown::render
#' @export
build_rmd <- function(files, path = ".", output_options = list(), ..., quiet = TRUE) {
build_md <- function(
files,
path = ".",
output_options = list(),
...,
quiet = TRUE
) {
check_dots_used(action = getOption("devtools.ellipsis_action", rlang::warn))

pkg <- as.package(path)

rlang::check_installed("rmarkdown")
save_all()

paths <- files
Expand All @@ -28,40 +37,101 @@ build_rmd <- function(files, path = ".", output_options = list(), ..., quiet = T
cli::cli_abort("Can't find file{?s}: {.path {files[!ok]}}.")
}

local_install(pkg, quiet = TRUE)
is_rmd <- grepl("\\.[Rr]md$", paths)
is_qmd <- grepl("\\.[Qq]md$", paths)

if (!all(is_rmd | is_qmd)) {
cli::cli_abort("Can only build {.file .Rmd} or {.file .qmd} files.")
}

# Ensure rendering github_document() doesn't generate HTML file
output_options$html_preview <- FALSE
if (any(is_rmd)) {
rlang::check_installed("rmarkdown")
# Ensure rendering github_document() doesn't generate HTML file
output_options$html_preview <- FALSE
}

if (any(is_qmd)) {
rlang::check_installed("quarto")
}

local_install(pkg, quiet = TRUE)
env <- r_env_vars()

for (path in paths) {
cli::cli_inform(c(i = "Building {.path {path}}"))
callr::r_safe(
function(...) rmarkdown::render(...),
args = list(input = path, ..., output_options = output_options, quiet = quiet),
show = TRUE,
spinner = FALSE,
stderr = "2>&1"
)
if (grepl("\\.[Qq]md$", path)) {
callr::r_safe(
function(...) quarto::quarto_render(...),
args = list(
input = path,
...,
quiet = quiet
),
show = TRUE,
spinner = FALSE,
stderr = "2>&1",
env = env
)
} else {
callr::r_safe(
function(...) rmarkdown::render(...),
args = list(
input = path,
...,
output_options = output_options,
quiet = quiet
),
show = TRUE,
spinner = FALSE,
stderr = "2>&1",
env = env
)
}
}

invisible(TRUE)
}

#' @rdname build_rmd
#' @rdname build_md
#' @export
build_rmd <- function(...) {
cli::cli_warn(
c(
"!" = "{.fn build_rmd} is deprecated.",
"i" = "Please use {.fn build_md} instead."
),
.frequency = "regularly",
.frequency_id = "build_rmd"
)
build_md(...)
}

#' @rdname build_md
#' @export
build_readme <- function(path = ".", quiet = TRUE, ...) {
pkg <- as.package(path)

regexp <- paste0(path_file(pkg$path), "/(inst/)?readme[.]rmd")
readme_path <- path_abs(dir_ls(pkg$path, ignore.case = TRUE, regexp = regexp, recurse = 1, type = "file"))
regexp <- paste0(path_file(pkg$path), "/(inst/)?readme[.](r|q)md$")
readme_path <- path_abs(
dir_ls(
pkg$path,
ignore.case = TRUE,
regexp = regexp,
recurse = 1,
type = "file"
)
)

if (length(readme_path) == 0) {
cli::cli_abort("Can't find {.file README.Rmd} or {.file inst/README.Rmd}.")
cli::cli_abort(
"Can't find {.file README.Rmd}, {.file inst/README.Rmd}, {.file README.qmd}, or {.file inst/README.qmd}."
)
}
if (length(readme_path) > 1) {
cli::cli_abort("Can't have both {.file README.Rmd} and {.file inst/README.Rmd}.")
cli::cli_abort(
"Can't have multiple README sources: {.file README.Rmd}, {.file inst/README.Rmd}, {.file README.qmd}, or {.file inst/README.qmd}."
)
}

build_rmd(readme_path, path = path, quiet = quiet, ...)
build_md(readme_path, path = path, quiet = quiet, ...)
}
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pre
processx
profvis
pryr
qmd
rOpenSci
randomises
rcmdcheck
Expand Down
34 changes: 34 additions & 0 deletions man/build_md.Rd

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

32 changes: 0 additions & 32 deletions man/build_rmd.Rd

This file was deleted.

10 changes: 7 additions & 3 deletions man/check.Rd

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

30 changes: 27 additions & 3 deletions tests/testthat/_snaps/build-readme.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
# useful errors if too few or too many
# useful errors if too few or too many (Rmd)

Code
build_readme(pkg)
Condition
Error in `build_readme()`:
! Can't find 'README.Rmd' or 'inst/README.Rmd'.
! Can't find 'README.Rmd', 'inst/README.Rmd', 'README.qmd', or 'inst/README.qmd'.

---

Code
build_readme(pkg)
Condition
Error in `build_readme()`:
! Can't have both 'README.Rmd' and 'inst/README.Rmd'.
! Can't have multiple README sources: 'README.Rmd', 'inst/README.Rmd', 'README.qmd', or 'inst/README.qmd'.

# useful errors if too few or too many (qmd)

Code
build_readme(pkg)
Condition
Error in `build_readme()`:
! Can't find 'README.Rmd', 'inst/README.Rmd', 'README.qmd', or 'inst/README.qmd'.

---

Code
build_readme(pkg)
Condition
Error in `build_readme()`:
! Can't have multiple README sources: 'README.Rmd', 'inst/README.Rmd', 'README.qmd', or 'inst/README.qmd'.

# useful errors if too many--mixed Quarto and Rmd

Code
build_readme(pkg)
Condition
Error in `build_readme()`:
! Can't have multiple README sources: 'README.Rmd', 'inst/README.Rmd', 'README.qmd', or 'inst/README.qmd'.

Loading
Loading