Skip to content

Commit 69a4c02

Browse files
authored
output_file now map to output-file Quarto meta and not the cli flag (#257)
* `output_file` now map to `output-file` Quarto meta and not the cli flag ```` quarto_render("test.qmd", quarto_args = c("--output", "test.html")) ```` Can restore the previous behavior by setting the command line flag * Adapt tests to new behavior of output_file
1 parent d3c732c commit 69a4c02

File tree

6 files changed

+57
-14
lines changed

6 files changed

+57
-14
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9017
3+
Version: 1.4.4.9018
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# quarto (development version)
22

3+
- `quarto_render(output_file = )` now sets the `output-file` Quarto metadata instead of the `--output` CLI flag. This allows the output file information to correctly be processed by Quarto, as if passed in a YAML header. e.g. it allows to support multiple output formats in the same render call. `quarto_render(quarto_args = c('--output', 'dummy.html'))` can still be used to set the `--output` CLI flag to enforce using the CLI flag and not the metadata processed by Quarto (#251, #43).
4+
35
- Added `check_newer_version()` function to check if a newer version of Quarto is available. The function compares the current Quarto version against the latest stable and prerelease versions. It is aimed for verbosity by default (`verbose = TRUE`), but `verbose = FALSE` can also be set for just checking update availability with TRUE or FALSE return values. Version information is cached per session for up to 24 hours to minimize network requests.
46

57
- Added `write_yaml_metadata_block()` function to dynamically set YAML metadata in Quarto documents from R code chunks. This addresses the limitation where Quarto metadata must be static and defined in the document header. The function enables conditional content and metadata-driven document behavior based on R computations (thanks, @kmasiello, #137, #160).

R/render.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ quarto_render <- function(
154154
if (!missing(output_format)) {
155155
args <- c(args, "--to", paste(output_format, collapse = ","))
156156
}
157-
if (!missing(output_file)) {
158-
args <- c(args, "--output", output_file)
157+
if (!is.null(output_file)) {
158+
# handle problem with cli flag
159+
# https://github.com/quarto-dev/quarto-cli/issues/8399
160+
# args <- c(args, "--output", output_file)
161+
metadata[['output-file']] <- output_file
159162
}
160163
if (!missing(execute)) {
161164
args <- c(args, ifelse(isTRUE(execute), "--execute", "--no-execute"))
@@ -187,13 +190,11 @@ quarto_render <- function(
187190
args <- c(args, "--cache-refresh")
188191
}
189192
# metadata to pass to quarto render
190-
if (!missing(metadata)) {
193+
if (!is.null(metadata)) {
191194
# We merge meta if there is metadata_file passed
192195
if (!missing(metadata_file)) {
193-
metadata <- merge_list(
194-
yaml::read_yaml(metadata_file, eval.expr = FALSE),
195-
metadata
196-
)
196+
file_content <- yaml::read_yaml(metadata_file, eval.expr = FALSE)
197+
metadata <- merge_list(file_content, metadata)
197198
}
198199
meta_file <- tempfile(pattern = "quarto-meta", fileext = ".yml")
199200
on.exit(unlink(meta_file), add = TRUE)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Pandoc
22
Meta
33
{ unMeta =
4-
fromList [ ( "title" , MetaInlines [ Str "test2" ] ) ]
4+
fromList
5+
[ ( "any" , MetaInlines [ Str "one" ] )
6+
, ( "other" , MetaInlines [ Str "thing" ] )
7+
, ( "title" , MetaInlines [ Str "test2" ] )
8+
]
59
}
610
[ Para [ Str "content" ] ]

tests/testthat/helper.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ local_quarto_project <- function(
9494
.render <- function(
9595
input,
9696
output_file = NULL,
97+
quarto_args = NULL,
9798
...,
9899
.quiet = TRUE,
99100
.env = parent.frame()
@@ -102,20 +103,31 @@ local_quarto_project <- function(
102103
skip_if_not_installed("withr")
103104
# work inside input directory
104105
withr::local_dir(dirname(input))
106+
output_file_forced <- NULL
105107
if (is.null(output_file)) {
106-
output_file <- basename(withr::local_file(
108+
output_file_forced <- basename(withr::local_file(
107109
xfun::with_ext(input, "test.out"),
108110
.local_envir = .env
109111
))
112+
# we enforce output file using CLI arg
113+
quarto_args <- c(
114+
quarto_args,
115+
"--output",
116+
output_file_forced
117+
)
118+
} else {
119+
NULL
110120
}
111121
expect_no_error(quarto_render(
112122
basename(input),
113123
output_file = output_file,
114124
quiet = .quiet,
125+
quarto_args = quarto_args,
115126
...
116127
))
117-
expect_true(file.exists(output_file))
118-
normalizePath(output_file)
128+
out <- output_file %||% output_file_forced
129+
expect_true(file.exists(out))
130+
normalizePath(out)
119131
}
120132

121133
.render_and_read <- function(input, ...) {

tests/testthat/test-render.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ test_that("metadata-file and metadata are merged in quarto_render", {
4242
skip_if_not_installed("withr")
4343
qmd <- local_qmd_file(c("content"))
4444
yaml <- withr::local_tempfile(fileext = ".yml")
45-
write_yaml(list(title = "test"), yaml)
45+
write_yaml(list(title = "test", other = "thing"), yaml)
4646
expect_snapshot_qmd_output(
4747
name = "metadata-merged",
4848
input = qmd,
4949
output_format = "native",
5050
metadata_file = yaml,
51-
metadata = list(title = "test2")
51+
metadata = list(title = "test2", any = "one")
5252
)
5353
})
5454

@@ -99,3 +99,27 @@ test_that("`quarto_render(as_job = TRUE)` is wrapable", {
9999
Sys.sleep(10)
100100
expect_true(file.exists(output))
101101
})
102+
103+
test_that("quarto_render allows to pass output-file meta", {
104+
skip_if_no_quarto()
105+
qmd <- local_qmd_file(c(
106+
"---",
107+
"title: Example title",
108+
"format:",
109+
" html:",
110+
" toc: true",
111+
" docx:",
112+
" toc: true",
113+
"---",
114+
""
115+
))
116+
quarto_render(
117+
qmd,
118+
output_file = "final_report",
119+
output_format = "all",
120+
quiet = TRUE
121+
)
122+
withr::local_dir(dirname(qmd))
123+
expect_true(file.exists("final_report.html"))
124+
expect_true(file.exists("final_report.docx"))
125+
})

0 commit comments

Comments
 (0)