From c718b6db648be3ba10a3b1ec35ca6b56ed32d7dd Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 14 Dec 2019 11:58:58 +0900 Subject: [PATCH 1/6] add functions to copy pandoc resources --- NAMESPACE | 4 +++ R/pandoc_copy.R | 53 +++++++++++++++++++++++++++++++++++++ man/pandoc_copy_data.Rd | 20 ++++++++++++++ man/pandoc_copy_template.Rd | 24 +++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 R/pandoc_copy.R create mode 100644 man/pandoc_copy_data.Rd create mode 100644 man/pandoc_copy_template.Rd 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/R/pandoc_copy.R b/R/pandoc_copy.R new file mode 100644 index 0000000000..bd197131ac --- /dev/null +++ b/R/pandoc_copy.R @@ -0,0 +1,53 @@ +#' 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. +#' +#' @return The name of the output file. +#' +#' @export +pandoc_copy_data <- function(data, output = data) { + system(paste( + quoted(pandoc()), "-o", output, "--print-default-data-file", data, + collapse = " " + )) + output +} + +#' Copy a template file for Pandoc +#' +#' +#' +#' @param format A format of the template file as a character (e.g., +#' \code{html}) or as a result of a formatting function (e.g., \code{html_document()}). +#' @param output The name of the output file. If using \code{NULL} then the +#' output filename will be based on the \code{format} argument. +#' @param ... Arguments passed to \code{file.copy()} when \format is a result of +#' a formatting function. Otherwise, they are ignored. +#' +#' @return The name of the output file. +#' +#' @export +pandoc_copy_template <- function(format, output = NULL, ...) { + UseMethod("pandoc_copy_template") +} + +#' @export +pandoc_copy_template.character <- function(format, output = NULL, ...) { + if (is.null(output)) output <- paste0("template.", format) + system(paste( + quoted(pandoc()), "-o", output, "--print-default-template", format, + collapse = " " + )) + output +} + +#' @export +pandoc_copy_template.rmarkdown_output_format <- function(format, output = NULL, ...) { + args <- format$pandoc$args + template <- args[which(args == "--template") + 1L] + if (is.null(output)) output <- sub(".*[\\/]", "", template) + file.copy(template, output, ...) + output +} diff --git a/man/pandoc_copy_data.Rd b/man/pandoc_copy_data.Rd new file mode 100644 index 0000000000..2b1f392cf3 --- /dev/null +++ b/man/pandoc_copy_data.Rd @@ -0,0 +1,20 @@ +% 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) +} +\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.} +} +\value{ +The name of the output file. +} +\description{ +Copy a Pandoc's data file +} diff --git a/man/pandoc_copy_template.Rd b/man/pandoc_copy_template.Rd new file mode 100644 index 0000000000..75c87b2871 --- /dev/null +++ b/man/pandoc_copy_template.Rd @@ -0,0 +1,24 @@ +% 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, ...) +} +\arguments{ +\item{format}{A format of the template file as a character (e.g., +\code{html}) or as a result of a formatting function (e.g., \code{html_document()}).} + +\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{...}{Arguments passed to \code{file.copy()} when \format is a result of +a formatting function. Otherwise, they are ignored.} +} +\value{ +The name of the output file. +} +\description{ +Copy a template file for Pandoc +} From adcfcac71f4d8ecac7e7c9394737485a6b99fa83 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 14 Dec 2019 12:46:45 +0900 Subject: [PATCH 2/6] update doc --- R/pandoc_copy.R | 25 +++++++++++++++++++++---- man/pandoc_copy_data.Rd | 9 +++++++++ man/pandoc_copy_template.Rd | 18 ++++++++++++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/R/pandoc_copy.R b/R/pandoc_copy.R index bd197131ac..2af3dbe359 100644 --- a/R/pandoc_copy.R +++ b/R/pandoc_copy.R @@ -6,6 +6,13 @@ #' #' @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) { system(paste( @@ -17,10 +24,9 @@ pandoc_copy_data <- function(data, output = data) { #' Copy a template file for Pandoc #' -#' -#' -#' @param format A format of the template file as a character (e.g., -#' \code{html}) or as a result of a formatting function (e.g., \code{html_document()}). +#' @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. #' @param ... Arguments passed to \code{file.copy()} when \format is a result of @@ -28,6 +34,17 @@ pandoc_copy_data <- function(data, output = 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, ...) { UseMethod("pandoc_copy_template") diff --git a/man/pandoc_copy_data.Rd b/man/pandoc_copy_data.Rd index 2b1f392cf3..cdb8424435 100644 --- a/man/pandoc_copy_data.Rd +++ b/man/pandoc_copy_data.Rd @@ -18,3 +18,12 @@ 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 index 75c87b2871..173bf85a4b 100644 --- a/man/pandoc_copy_template.Rd +++ b/man/pandoc_copy_template.Rd @@ -7,8 +7,9 @@ pandoc_copy_template(format, output = NULL, ...) } \arguments{ -\item{format}{A format of the template file as a character (e.g., -\code{html}) or as a result of a formatting function (e.g., \code{html_document()}).} +\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.} @@ -22,3 +23,16 @@ 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} +} From 37b3a3c998f212af7810185f3050d91bd9d78d0f Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 14 Dec 2019 12:53:35 +0900 Subject: [PATCH 3/6] update NEWS --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) 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 ================================================================================ From 26e4ae30ec18994b20ab751576ad1b86dad606c0 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 14 Dec 2019 19:22:59 +0900 Subject: [PATCH 4/6] add the overwrite option whose default is FALSE --- R/pandoc_copy.R | 38 ++++++++++++++++++++++++++++--------- man/pandoc_copy_data.Rd | 5 ++++- man/pandoc_copy_template.Rd | 6 +++--- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/R/pandoc_copy.R b/R/pandoc_copy.R index 2af3dbe359..552cd10fd9 100644 --- a/R/pandoc_copy.R +++ b/R/pandoc_copy.R @@ -3,6 +3,8 @@ #' @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. #' @@ -14,7 +16,12 @@ #' } #' #' @export -pandoc_copy_data <- function(data, output = data) { +pandoc_copy_data <- function(data, output = data, overwrite = FALSE) { + if (file.exists(output)) { + message(output, "already exists.") + return(output) + } + system(paste( quoted(pandoc()), "-o", output, "--print-default-data-file", data, collapse = " " @@ -29,8 +36,7 @@ pandoc_copy_data <- function(data, output = data) { #' 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. -#' @param ... Arguments passed to \code{file.copy()} when \format is a result of -#' a formatting function. Otherwise, they are ignored. +#' @inheritParams pandoc_copy_data #' #' @return The name of the output file. #' @@ -46,13 +52,20 @@ pandoc_copy_data <- function(data, output = data) { #' } #' #' @export -pandoc_copy_template <- function(format, output = NULL, ...) { +pandoc_copy_template <- function(format, output = NULL, overwrite = FALSE) { UseMethod("pandoc_copy_template") } #' @export -pandoc_copy_template.character <- function(format, output = NULL, ...) { +pandoc_copy_template.character <- function( + format, output = NULL, overwrite = FALSE +) { if (is.null(output)) output <- paste0("template.", format) + if (file.exists(output)) { + message(output, "already exists.") + return(output) + } + system(paste( quoted(pandoc()), "-o", output, "--print-default-template", format, collapse = " " @@ -61,10 +74,17 @@ pandoc_copy_template.character <- function(format, output = NULL, ...) { } #' @export -pandoc_copy_template.rmarkdown_output_format <- function(format, output = NULL, ...) { - args <- format$pandoc$args - template <- args[which(args == "--template") + 1L] +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) - file.copy(template, output, ...) + + if (file.exists(output)) { + 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 index cdb8424435..342fbaceed 100644 --- a/man/pandoc_copy_data.Rd +++ b/man/pandoc_copy_data.Rd @@ -4,13 +4,16 @@ \alias{pandoc_copy_data} \title{Copy a Pandoc's data file} \usage{ -pandoc_copy_data(data, output = data) +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. diff --git a/man/pandoc_copy_template.Rd b/man/pandoc_copy_template.Rd index 173bf85a4b..64b2051255 100644 --- a/man/pandoc_copy_template.Rd +++ b/man/pandoc_copy_template.Rd @@ -4,7 +4,7 @@ \alias{pandoc_copy_template} \title{Copy a template file for Pandoc} \usage{ -pandoc_copy_template(format, output = NULL, ...) +pandoc_copy_template(format, output = NULL, overwrite = FALSE) } \arguments{ \item{format}{To copy R Markdown's template, specify a result of a formatting @@ -14,8 +14,8 @@ 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{...}{Arguments passed to \code{file.copy()} when \format is a result of -a formatting function. Otherwise, they are ignored.} +\item{overwrite}{Whether or not to overwrite the existing file. Default to +\code{FALSE}.} } \value{ The name of the output file. From 00615cdb8e11e48f94643911527572f86fe1fe66 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sat, 14 Dec 2019 19:39:44 +0900 Subject: [PATCH 5/6] add missing space in message --- R/pandoc_copy.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/pandoc_copy.R b/R/pandoc_copy.R index 552cd10fd9..1ee10da90c 100644 --- a/R/pandoc_copy.R +++ b/R/pandoc_copy.R @@ -18,7 +18,7 @@ #' @export pandoc_copy_data <- function(data, output = data, overwrite = FALSE) { if (file.exists(output)) { - message(output, "already exists.") + message(output, " already exists.") return(output) } @@ -62,7 +62,7 @@ pandoc_copy_template.character <- function( ) { if (is.null(output)) output <- paste0("template.", format) if (file.exists(output)) { - message(output, "already exists.") + message(output, " already exists.") return(output) } @@ -81,7 +81,7 @@ pandoc_copy_template.rmarkdown_output_format <- function( if (is.null(output)) output <- sub(".*[\\/]", "", template) if (file.exists(output)) { - message(output, "already exists.") + message(output, " already exists.") return(output) } From 10926837bb109a844c9428040db53448bcdc0a52 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Sun, 15 Dec 2019 09:31:27 +0900 Subject: [PATCH 6/6] fix overwrite argument is ignored --- R/pandoc_copy.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/pandoc_copy.R b/R/pandoc_copy.R index 1ee10da90c..996815ca75 100644 --- a/R/pandoc_copy.R +++ b/R/pandoc_copy.R @@ -17,7 +17,7 @@ #' #' @export pandoc_copy_data <- function(data, output = data, overwrite = FALSE) { - if (file.exists(output)) { + if (file.exists(output) && !overwrite) { message(output, " already exists.") return(output) } @@ -61,7 +61,7 @@ pandoc_copy_template.character <- function( format, output = NULL, overwrite = FALSE ) { if (is.null(output)) output <- paste0("template.", format) - if (file.exists(output)) { + if (file.exists(output) && !overwrite) { message(output, " already exists.") return(output) } @@ -80,7 +80,7 @@ pandoc_copy_template.rmarkdown_output_format <- function( template <- format$pandoc$args[which(format$pandoc$args == "--template") + 1L] if (is.null(output)) output <- sub(".*[\\/]", "", template) - if (file.exists(output)) { + if (file.exists(output) && !overwrite) { message(output, " already exists.") return(output) }