Skip to content

Commit

Permalink
Enable use of pandoc --file-scope for input files originating from mu…
Browse files Browse the repository at this point in the history
…ltiple Rmds (#1837)
  • Loading branch information
jjallaire committed Jun 8, 2020
1 parent b311c18 commit 028f8c2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,7 +1,7 @@
Package: rmarkdown
Type: Package
Title: Dynamic Documents for R
Version: 2.2.1
Version: 2.2.2
Authors@R: c(
person("JJ", "Allaire", role = "aut", email = "jj@rstudio.com"),
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,6 +1,8 @@
rmarkdown 2.3
================================================================================

- Added `file_scope` option to `render()`, a function used to split markdown inputs to pandoc into multiple files. This is for the purpose of invoking pandoc with the `--file-scope` option, which in turn enables graceful handling of duplicate footnote numbers across chapters (#1837).

- Added the customizable `lang` atrribute to `ioslides_presentation` output (thanks, @jooyoungseo, #1841).


Expand Down
40 changes: 36 additions & 4 deletions R/render.R
Expand Up @@ -190,6 +190,14 @@ NULL
#' uses knitr's \code{root.dir} knit option. If \code{NULL} then the behavior
#' will follow the knitr default, which is to use the parent directory of the
#' document.
#' @param file_scope An optional function that will split markdown
#' inputs to pandoc into multiple files. This is for the purpose of invoking
#' pandoc with the \code{--file-scope} option, which allows footnotes in
#' different files with the same identifiers to work as expected. This is
#' useful when the caller has concatenated a set of Rmd files together (as
#' \pkg{bookdown} does), and those files may have conflicting footnote numbers.
#' The function should return a named list of files w/ \code{name} and
#' \code{content} for each file.
#' @param runtime The runtime target for rendering. The \code{static} option
#' produces output intended for static files; \code{shiny} produces output
#' suitable for use in a Shiny document (see \code{\link{run}}). The default,
Expand Down Expand Up @@ -242,6 +250,7 @@ render <- function(input,
output_yaml = NULL,
intermediates_dir = NULL,
knit_root_dir = NULL,
file_scope = NULL,
runtime = c("auto", "static", "shiny", "shiny_prerendered"),
clean = TRUE,
params = NULL,
Expand Down Expand Up @@ -286,6 +295,7 @@ render <- function(input,
output_options = output_options,
intermediates_dir = intermediates_dir,
knit_root_dir = knit_root_dir,
file_scope = file_scope,
runtime = runtime,
clean = clean,
params = params,
Expand Down Expand Up @@ -847,12 +857,34 @@ render <- function(input,
utf8_input <- path.expand(utf8_input)
output <- path.expand(output)

# determine args and input files (if we have an file_scope then we
# we need to split the files use it here)
pandoc_args <- output_format$pandoc$args
input_files <- utf8_input
if (!is.null(file_scope)) {

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

# determine new input files
inputs <- file_scope(utf8_input)
input_files <- unlist(lapply(inputs, function(input) {
file <- file_with_meta_ext(input$name, "split", "md")
file <- file.path(dirname(utf8_input), file)
write_utf8(input$content, file)
file
}))

# cleanup the split files after render
on.exit(unlink(input_files), add = TRUE)
}

# if we don't detect any invalid shell characters in the
# target path, then just call pandoc directly
if (!grepl(.shell_chars_regex, output) && !grepl(.shell_chars_regex, utf8_input)) {
return(pandoc_convert(
utf8_input, pandoc_to, output_format$pandoc$from, output,
citeproc, output_format$pandoc$args, !quiet
input_files, pandoc_to, output_format$pandoc$from, output,
citeproc, pandoc_args, !quiet
))
}

Expand All @@ -874,8 +906,8 @@ render <- function(input,

# call pandoc to render file
status <- pandoc_convert(
utf8_input, pandoc_to, output_format$pandoc$from, pandoc_output_tmp,
citeproc, output_format$pandoc$args, !quiet
input_files, pandoc_to, output_format$pandoc$from, pandoc_output_tmp,
citeproc, pandoc_args, !quiet
)

# construct output path (when passed only a file name to '--output',
Expand Down
10 changes: 10 additions & 0 deletions man/render.Rd

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

0 comments on commit 028f8c2

Please sign in to comment.