| 1 |
#' A Pandoc wrapper to convert documents to other formats |
| 2 |
#' |
| 3 |
#' This function calls Pandoc to convert documents to other formats such as |
| 4 |
#' HTML, LaTeX/PDF and Word, etc, (optionally) based on a configuration file or |
| 5 |
#' in-file configurations which specify the options to use for Pandoc. |
| 6 |
#' |
| 7 |
#' There are two ways to input the Pandoc configurations -- through a config |
| 8 |
#' file, or embed the configurations in the input file as special comments |
| 9 |
#' between \verb{<!--pandoc} and \verb{-->}. |
| 10 |
#' |
| 11 |
#' The configuration file is a DCF file (see \code{\link{read.dcf}}). This file |
| 12 |
#' must contain a field named \code{t} which means the output format. The |
| 13 |
#' configurations are written in the form of \code{tag:value} and passed to |
| 14 |
#' Pandoc (if no value is needed, just leave it empty, e.g. the option |
| 15 |
#' \code{standalone} or \code{s} for short). If there are multiple output |
| 16 |
#' formats, write each format and relevant configurations in a block, and |
| 17 |
#' separate blocks with blank lines. |
| 18 |
#' |
| 19 |
#' If there are multiple records of the \code{t} field in the configuration, the |
| 20 |
#' input markdown file will be converted to all these formats by default, unless |
| 21 |
#' the \code{format} argument is specified as one single format. |
| 22 |
#' @param input A character vector of Markdown filenames (must be encoded in |
| 23 |
#' UTF-8). |
| 24 |
#' @param format Name of the output format (see References). This can be a |
| 25 |
#' character vector of multiple formats; by default, it is obtained from the |
| 26 |
#' \code{t} field in the configuration. If the configuration is empty or the |
| 27 |
#' \code{t} field is not found, the default output format will be |
| 28 |
#' \code{'html'}. |
| 29 |
#' @param config Path to the Pandoc configuration file. If missing, it is |
| 30 |
#' assumed to be a file with the same base name as the \code{input} file and |
| 31 |
#' an extension \code{.pandoc} (e.g. for \file{foo.md} it looks for |
| 32 |
#' \file{foo.pandoc}) |
| 33 |
#' @param ext Filename extensions. By default, the extension is inferred from |
| 34 |
#' the \code{format}, e.g. \code{latex} creates \code{pdf}, \code{dzslides} |
| 35 |
#' creates \code{html}, and so on |
| 36 |
#' @return The output filename(s) (or an error if the conversion failed). |
| 37 |
#' @references Pandoc: \url{https://pandoc.org}; Examples and rules of the |
| 38 |
#' configurations: \url{https://yihui.org/knitr/demo/pandoc/} |
| 39 |
#' |
| 40 |
#' Also see R Markdown (v2) at \url{https://rmarkdown.rstudio.com}. The |
| 41 |
#' \pkg{rmarkdown} package has several convenience functions and templates |
| 42 |
#' that make it very easy to use Pandoc. The RStudio IDE also has |
| 43 |
#' comprehensive support for it, so I'd recommend users who are not familiar |
| 44 |
#' with command-line tools to use the \pkg{rmarkdown} package instead. |
| 45 |
#' @seealso \code{\link{read.dcf}} |
| 46 |
#' @export |
| 47 |
#' @examples system('pandoc -h') # see possible output formats |
| 48 |
pandoc = function(input, format, config = getOption('config.pandoc'), ext = NA) { |
| 49 |
exec = tryCatch(rmarkdown::pandoc_exec(), error = function(e) Sys.which('pandoc')) |
| 50 |
if (length(exec) != 1 || exec == '') |
| 51 |
stop('Please install either RStudio or Pandoc (https://pandoc.org)') |
| 52 |
cfg = if (is.null(config)) with_ext(input[1L], 'pandoc') else config |
| 53 |
txt = pandoc_cfg(read_utf8(input[1])) |
| 54 |
if (file.exists(cfg)) txt = c(txt, '', read_utf8(cfg)) |
| 55 |
con = textConnection(txt, encoding = 'UTF-8'); on.exit(close(con), add = TRUE) |
| 56 |
cfg = read.dcf(con) |
| 57 |
nms = colnames(cfg) |
| 58 |
if (length(nms) && 'format' %in% nms) { |
| 59 |
warning('the "format" field in the configuration must be renamed to "t"') |
| 60 |
colnames(cfg)[nms == 'format'] = 't' # for backward compatibility |
| 61 |
} |
| 62 |
if (missing(format)) format = pandoc_fmt(cfg) |
| 63 |
mapply( |
| 64 |
pandoc_one, input, format, ext, exec, MoreArgs = list(cfg = cfg), |
| 65 |
USE.NAMES = FALSE |
| 66 |
) |
| 67 |
} |
| 68 |
# format is a scalar |
| 69 |
pandoc_one = function(input, format, ext, exec, cfg) { |
| 70 |
cmn = NULL # common arguments |
| 71 |
if (nrow(cfg) == 0L) cfg = character(0) else if (nrow(cfg) == 1L) { |
| 72 |
if ('t' %in% colnames(cfg)) { |
| 73 |
cfg = if (cfg[1L, 't'] == format) drop(cfg) else NA |
| 74 |
} else {cmn = drop(cfg); cfg = NA} |
| 75 |
} else { |
| 76 |
if (!('t' %in% colnames(cfg))) |
| 77 |
stop('for a config file with multiple output formats, there must be a field named "t"') |
| 78 |
if (sum(idx <- is.na(cfg[, 't'])) > 1L) |
| 79 |
stop('at most one "t" field can be NA') |
| 80 |
if (sum(idx) == 1L) cmn = cfg[idx, ] |
| 81 |
cfg = cfg[!idx, , drop = FALSE] |
| 82 |
cfg = cfg[cfg[, 't'] == format, ] |
| 83 |
if (!is.null(dim(cfg))) { |
| 84 |
if (nrow(cfg) > 1) stop('the output format is not unique in config') |
| 85 |
cfg = character(0) # nrow(cfg) == 0; format not found in cfg |
| 86 |
} |
| 87 |
} |
| 88 |
out = unname(if (!is.na(cfg['o'])) cfg['o'] else { |
| 89 |
if (!is.na(cfg['output'])) cfg['output'] else { |
| 90 |
with_ext(input, if (is.na(ext)) pandoc_ext(format) else ext) |
| 91 |
} |
| 92 |
}) |
| 93 |
cfg = cfg[setdiff(names(cfg), c('o', 'output', 't'))] |
| 94 |
cmd = c( |
| 95 |
pandoc_arg(cfg), pandoc_arg(cmn), '-t', format, '-o', shQuote(out), shQuote(input) |
| 96 |
) |
| 97 |
message('Executing: pandoc ', paste(cmd, collapse = ' ')) |
| 98 |
if (system2(exec, cmd) == 0L) out else stop('conversion failed') |
| 99 |
} |
| 100 |
|
| 101 |
# detect output format from config |
| 102 |
pandoc_fmt = function(config) { |
| 103 |
fields = colnames(config) |
| 104 |
if (prod(dim(config)) == 0 || !('t' %in% fields)) return('html') |
| 105 |
na.omit(config[, 't']) |
| 106 |
} |
| 107 |
|
| 108 |
# infer output extension from format |
| 109 |
pandoc_ext = function(format) { |
| 110 |
if (grepl('^html', format)) return('html') |
| 111 |
if (grepl('^latex|beamer|context|texinfo', format)) return('pdf') |
| 112 |
if (format %in% c('s5', 'slidy', 'slideous', 'dzslides')) return('html') |
| 113 |
if (grepl('^rst', format)) return('rst') |
| 114 |
if (format == 'opendocument') return('xml') |
| 115 |
format |
| 116 |
} |
| 117 |
|
| 118 |
# give me a vector of arguments, I turn them into command-line |
| 119 |
pandoc_arg = function(x) { |
| 120 |
if (length(x) == 0L || all(is.na(x))) return() |
| 121 |
x = x[!is.na(x)] # options not provided |
| 122 |
nms = names(x) |
| 123 |
if (any(grepl('\n', x))) { |
| 124 |
# one argument used multiple times, e.g. --bibliography |
| 125 |
x = str_split(x, '\n') |
| 126 |
nms = rep(nms, sapply(x, length)) |
| 127 |
x = unlist(x) |
| 128 |
} |
| 129 |
a1 = nchar(nms) == 1L |
| 130 |
paste0(ifelse(a1, '-', '--'), nms, ifelse(x == '', '', ifelse(a1, ' ', '=')), x) |
| 131 |
} |
| 132 |
|
| 133 |
# identify pandoc config in markdown comments |
| 134 |
pandoc_cfg = function(x) { |
| 135 |
if (length(i1 <- grep('^<!--pandoc', x)) == 0L || |
| 136 |
length(i2 <- grep('-->\\s*$', x)) == 0L) return(character(0)) |
| 137 |
i1 = i1[1L]; if (all(i2 < i1)) return(character(0)) |
| 138 |
i2 = i2[i2 >= i1][1L] |
| 139 |
cfg = x[i1:i2] |
| 140 |
cfg[1L] = gsub('^<!--pandoc\\s*', '', cfg[1L]) |
| 141 |
cfg[length(cfg)] = gsub('-->\\s*$', '', cfg[length(cfg)]) |
| 142 |
cfg |
| 143 |
} |