Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to copy resources of Pandoc. #1738

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
S3method(knit_print,grouped_df)
S3method(knit_print,rowwise_df)
S3method(knit_print,tbl_sql)
S3method(pandoc_copy_template,character)
S3method(pandoc_copy_template,rmarkdown_output_format)
S3method(prepare_evaluate_output,default)
S3method(prepare_evaluate_output,htmlwidget)
S3method(prepare_evaluate_output,knit_asis)
Expand Down Expand Up @@ -57,6 +59,8 @@ export(paged_table)
export(pandoc_available)
export(pandoc_citeproc_convert)
export(pandoc_convert)
export(pandoc_copy_data)
export(pandoc_copy_template)
export(pandoc_exec)
export(pandoc_highlight_args)
export(pandoc_include_args)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ rmarkdown 2.1

- Ensure the `tempdir()` exists (via `tempdir(TRUE)`) when writing HTML dependencies to a temporary file, because this directory might be erased by accident (thanks, Kurt Hornik).

- Add `pandoc_copy_data` to copy data files provided by Pandoc
(e.g, `pandoc_copy_data("reference.docx")`) (thanks, @atusy #1738)

- Add `pandoc_copy_template` to copy templates provided by R Markdown or Pandoc.
(e.g., `pandoc_copy_template(html_document()`) (thanks, @atusy #1738)

rmarkdown 2.0
================================================================================
Expand Down
90 changes: 90 additions & 0 deletions R/pandoc_copy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Copy a Pandoc's data file
#'
#' @param data The name of Pandoc's data file (e.g., "reference.docx")
#' @param output The name of the output file. If \code{NULL} (default), the name
#' will be same as the \code{data} argument.
#' @param overwrite Whether or not to overwrite the existing file. Default to
#' \code{FALSE}.
#'
#' @return The name of the output file.
#'
#' @seealso \url{https://pandoc.org/MANUAL.html#option--print-default-data-file}
#'
#' @examples
#' \dontrun{
#' pandoc_copy_data("reference.docx")
#' }
#'
#' @export
pandoc_copy_data <- function(data, output = data, overwrite = FALSE) {
if (file.exists(output) && !overwrite) {
message(output, " already exists.")
return(output)
}

system(paste(
quoted(pandoc()), "-o", output, "--print-default-data-file", data,
collapse = " "
))
output
}

#' Copy a template file for Pandoc
#'
#' @param format To copy R Markdown's template, specify a result of a formatting
#' function (e.g., \code{html_document()}). To copy Pandoc's original template,
#' specify the format as a character (e.g., "html").
#' @param output The name of the output file. If using \code{NULL} then the
#' output filename will be based on the \code{format} argument.
#' @inheritParams pandoc_copy_data
#'
#' @return The name of the output file.
#'
#' @seealso \url{https://pandoc.org/MANUAL.html#templates}
#'
#' @examples
#' \dontrun{
#' # Copy the html_document's template
#' pandoc_copy_template(html_document())
#'
#' # Copy the Pandoc's html template
#' pandoc_copy_template("html")
#' }
#'
#' @export
pandoc_copy_template <- function(format, output = NULL, overwrite = FALSE) {
UseMethod("pandoc_copy_template")
}

#' @export
pandoc_copy_template.character <- function(
format, output = NULL, overwrite = FALSE
) {
if (is.null(output)) output <- paste0("template.", format)
if (file.exists(output) && !overwrite) {
message(output, " already exists.")
return(output)
}

system(paste(
quoted(pandoc()), "-o", output, "--print-default-template", format,
collapse = " "
))
output
}

#' @export
pandoc_copy_template.rmarkdown_output_format <- function(
format, output = NULL, overwrite = FALSE
) {
template <- format$pandoc$args[which(format$pandoc$args == "--template") + 1L]
if (is.null(output)) output <- sub(".*[\\/]", "", template)

if (file.exists(output) && !overwrite) {
message(output, " already exists.")
return(output)
}

file.copy(template, output, overwrite = overwrite)
output
}
32 changes: 32 additions & 0 deletions man/pandoc_copy_data.Rd

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

38 changes: 38 additions & 0 deletions man/pandoc_copy_template.Rd

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