diff --git a/NAMESPACE b/NAMESPACE index f046fbefe0..2f23e4b4d1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS.md b/NEWS.md index a1e87e6cd7..85a33d8bb8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ================================================================================ diff --git a/R/pandoc_copy.R b/R/pandoc_copy.R new file mode 100644 index 0000000000..996815ca75 --- /dev/null +++ b/R/pandoc_copy.R @@ -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 +} diff --git a/man/pandoc_copy_data.Rd b/man/pandoc_copy_data.Rd new file mode 100644 index 0000000000..342fbaceed --- /dev/null +++ b/man/pandoc_copy_data.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pandoc_copy.R +\name{pandoc_copy_data} +\alias{pandoc_copy_data} +\title{Copy a Pandoc's data file} +\usage{ +pandoc_copy_data(data, output = data, overwrite = FALSE) +} +\arguments{ +\item{data}{The name of Pandoc's data file (e.g., "reference.docx")} + +\item{output}{The name of the output file. If \code{NULL} (default), the name +will be same as the \code{data} argument.} + +\item{overwrite}{Whether or not to overwrite the existing file. Default to +\code{FALSE}.} +} +\value{ +The name of the output file. +} +\description{ +Copy a Pandoc's data file +} +\examples{ +\dontrun{ +pandoc_copy_data("reference.docx") +} + +} +\seealso{ +\url{https://pandoc.org/MANUAL.html#option--print-default-data-file} +} diff --git a/man/pandoc_copy_template.Rd b/man/pandoc_copy_template.Rd new file mode 100644 index 0000000000..64b2051255 --- /dev/null +++ b/man/pandoc_copy_template.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pandoc_copy.R +\name{pandoc_copy_template} +\alias{pandoc_copy_template} +\title{Copy a template file for Pandoc} +\usage{ +pandoc_copy_template(format, output = NULL, overwrite = FALSE) +} +\arguments{ +\item{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").} + +\item{output}{The name of the output file. If using \code{NULL} then the +output filename will be based on the \code{format} argument.} + +\item{overwrite}{Whether or not to overwrite the existing file. Default to +\code{FALSE}.} +} +\value{ +The name of the output file. +} +\description{ +Copy a template file for Pandoc +} +\examples{ +\dontrun{ +# Copy the html_document's template +pandoc_copy_template(html_document()) + +# Copy the Pandoc's html template +pandoc_copy_template("html") +} + +} +\seealso{ +\url{https://pandoc.org/MANUAL.html#templates} +}