Skip to content

Commit

Permalink
Add support for child=c("child.Rmd") in find_external_resources() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv committed Sep 13, 2024
1 parent da85f8c commit 9bf0910
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rmarkdown 2.29
================================================================================

- `find_external_resources()` now correctly detects knitr child document provided with option like `child = c("child.Rmd")` (thanks, @rempsyc, #2574).

rmarkdown 2.28
================================================================================
Expand Down
2 changes: 1 addition & 1 deletion R/html_resources.R
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ discover_rmd_resources <- function(rmd_file, discover_single_resource) {
rmd_content[idx], chunk_start,
chunk_start + attr(chunk_line, "capture.length", exact = TRUE) - 2
)
for (child_expr in c("\\bchild\\s*=\\s*'([^']+)'", "\\bchild\\s*=\\s*\"([^\"]+)\"")) {
for (child_expr in c("\\bchild\\s*=\\s*(?:c\\()?'([^']+)'\\)?", "\\bchild\\s*=\\s*(?:c\\()?\"([^\"]+)\"\\)?")) {
child_match <- gregexpr(child_expr, chunk_text, perl = TRUE)[[1]]
if (child_match > 0) {
child_start <- attr(child_match, "capture.start", exact = TRUE)
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-resources.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,24 @@ test_that("multiple resources in the includes option can be discovered", {

expect_equal(resources, expected)
})

test_that("knitr child are correctly discovered as resources from chunk options", {
expect_child_resource_found <- function(child_opts, child) {
dir <- withr::local_tempdir("find-child")
withr::local_dir(dir)
rmd <- "test.Rmd"
xfun::write_utf8(
text = knitr::knit_expand(text = c("```{r,<<child_opts>>}", "```"), delim = c("<<", ">>")),
rmd
)
xfun::write_utf8(c("Content"), child)
expect_contains(find_external_resources(!!rmd)$path, !!child)
}
child <- "child.Rmd"
expect_child_resource_found('child="child.Rmd"', child)
expect_child_resource_found(' child = "child.Rmd"', child)
expect_child_resource_found('child=c("child.Rmd")', child)
expect_child_resource_found("child='child.Rmd'", child)
expect_child_resource_found(" child = 'child.Rmd'", child)
expect_child_resource_found("child=c('child.Rmd')", child)
})

0 comments on commit 9bf0910

Please sign in to comment.