Skip to content

Commit

Permalink
Loop correctly on splitted file by file_scope function (#2150)
Browse files Browse the repository at this point in the history
Co-authored-by: Yihui Xie <[email protected]>
  • Loading branch information
cderv and yihui authored May 28, 2021
1 parent 95178ff commit 8be459a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rmarkdown
Type: Package
Title: Dynamic Documents for R
Version: 2.8.4
Version: 2.8.5
Authors@R: c(
person("JJ", "Allaire", role = "aut", email = "[email protected]"),
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
rmarkdown 2.9
================================================================================

- Fixed an issue with `copy_ressource = TRUE` in `html_document_base` where very long HTML documents were truncated during post processing (thanks, @oliviermeslin, #2145).
- Fix an error thrown with output format using a `file_scope` function (like in **bookdown**) (thanks, @rfaelens, #2149).

- Fix an issue with `copy_ressource = TRUE` in `html_document_base` where very long HTML documents were truncated during post processing (thanks, @oliviermeslin, #2145).

- When rendering a `runtime: shiny` document, an extra temp folder will be used in the output path. With the extra temp random folder in the path, predictable output file names may be used. (#2137)

Expand Down
43 changes: 27 additions & 16 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -891,22 +891,15 @@ render <- function(input,
# in case the output format turns on the --file-scope flag, run its
# file_scope function to split the input into multiple files
input_files <- input
if (!is.null(output_format$file_scope) &&
length(inputs <- output_format$file_scope(input)) > 1) {

# add the --file-scope option
pandoc_args <- c(pandoc_args, "--file-scope")

# write the split content into *.split.md files
input_files <- unlist(lapply(inputs, function(input) {
file <- file_with_meta_ext(input$name, "split", "md")
file <- file.path(dirname(input), file)
write_utf8(input$content, file)
file
}))

# cleanup the split files after render
on.exit(unlink(input_files), add = TRUE)
if (is.function(output_format$file_scope)) {
input_files <- file_scope_split(input, output_format$file_scope)
# ignore if input_files has not really been splitted
if (length(input_files) > 1) {
# add the --file-scope option
pandoc_args <- c(pandoc_args, "--file-scope")
# cleanup the split files after render
on.exit(unlink(input_files), add = TRUE)
}
}

# if we don't detect any invalid shell characters in the
Expand Down Expand Up @@ -1192,3 +1185,21 @@ resolve_df_print <- function(df_print) {
#' @keywords NULL
#' @export
output_metadata = knitr:::new_defaults()

file_scope_split <- function(input, fun) {
inputs <- fun(input)

# file_scope_fun should split the input file in several
# do nothing if not and return input file unsplited
if (length(inputs) <= 1) return(input)

# write the split content into *.split.md files
input_files <- lapply(inputs, function(f) {
file <- file_with_meta_ext(f$name, "split", "md")
file <- file.path(dirname(input), file)
write_utf8(f$content, file)
file
})

unlist(input_files)
}
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/render/A.split.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# H1
content1
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/render/B.split.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# H2
content2
20 changes: 20 additions & 0 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("Metadata is available before pre_knit", {
message_pre_knit = 'pre_knit handles metadata'
fmt <- md_document()
Expand All @@ -11,3 +14,20 @@ test_that("Metadata is available before pre_knit", {
writeLines('---\nfoo: bar\n---', input_file)
expect_message(render(input_file, fmt, quiet = TRUE), message_pre_knit)
})

test_that("file_scope split correctly input file", {
rmd <- local_rmd_file(c("# H1", "content1", "# H2", "content2"))
file_scope_fun <- function(file) {
x <- xfun::read_utf8(file)
list(
list(name = "A", content = x[1:2]),
list(name = "B", content = x[3:4])
)
}
splitted <- file_scope_split(rmd, file_scope_fun)
expect_true(all(file.exists(splitted)))
on.exit(unlink(splitted), add = TRUE, after = FALSE)
expect_match(splitted, "[.]split[.]md$")
expect_snapshot_file(splitted[1])
expect_snapshot_file(splitted[2])
})

0 comments on commit 8be459a

Please sign in to comment.