| 1 |
#' Define an R Markdown output format |
| 2 |
#' |
| 3 |
#' Define an R Markdown output format based on a combination of knitr and pandoc |
| 4 |
#' options. |
| 5 |
#' @param knitr Knitr options for an output format (see |
| 6 |
#' \code{\link{knitr_options}}) |
| 7 |
#' @param pandoc Pandoc options for an output format (see |
| 8 |
#' \code{\link{pandoc_options}}) |
| 9 |
#' @param keep_md Keep the markdown file generated by knitting. Note that if |
| 10 |
#' this is \code{TRUE} then \code{clean_supporting} will always be |
| 11 |
#' \code{FALSE}. |
| 12 |
#' @param clean_supporting Cleanup any supporting files after conversion see |
| 13 |
#' \code{\link{render_supporting_files}} |
| 14 |
#' @param df_print Method to be used for printing data frames. Valid values |
| 15 |
#' include "default", "kable", "tibble", and "paged". The "default" method |
| 16 |
#' uses a corresponding S3 method of \code{print}, typically |
| 17 |
#' \code{print.data.frame}. The "kable" method uses the |
| 18 |
#' \code{\link[knitr:kable]{knitr::kable}} function. The "tibble" method uses |
| 19 |
#' the \pkg{tibble} package to print a summary of the data frame. The "paged" |
| 20 |
#' method creates a paginated HTML table (note that this method is only valid |
| 21 |
#' for formats that produce HTML). In addition to the named methods you can |
| 22 |
#' also pass an arbitrary function to be used for printing data frames. You |
| 23 |
#' can disable the \code{df_print} behavior entirely by setting the option |
| 24 |
#' \code{rmarkdown.df_print} to \code{FALSE}. See |
| 25 |
#' \href{https://yihui.org/rmarkdown/html-document.html#data-frame-printing}{Data |
| 26 |
#' frame printing section} in bookdown book for examples. |
| 27 |
#' @param pre_knit An optional function that runs before knitting which receives |
| 28 |
#' the \code{input} (input filename passed to \code{render}), \code{metadata} |
| 29 |
#' (the parsed front matter of the Rmd file) and \code{...} (for future |
| 30 |
#' expansion) arguments. This function can be used to add side effects before |
| 31 |
#' knitting step. |
| 32 |
#' @param post_knit An optional function that runs after knitting which receives |
| 33 |
#' the \code{metadata}, \code{input_file}, \code{runtime}, and \code{...} (for |
| 34 |
#' future expansion) arguments. This function can return additional arguments |
| 35 |
#' to pass to pandoc and can call \code{knitr::knit_meta_add} to add |
| 36 |
#' additional dependencies based on the contents of the input_file or on other |
| 37 |
#' assets side by side with it that may be used to produce html with |
| 38 |
#' dependencies during subsequent processing. |
| 39 |
#' @param pre_processor An optional pre-processor function that receives the |
| 40 |
#' \code{metadata}, \code{input_file}, \code{runtime}, \code{knit_meta}, |
| 41 |
#' \code{files_dir}, and \code{output_dir} and can return additional arguments |
| 42 |
#' to pass to pandoc. |
| 43 |
#' @param intermediates_generator An optional function that receives the |
| 44 |
#' original \code{input_file}, and the intermediates directory (i.e. the |
| 45 |
#' \code{intermediates_dir} argument to \code{\link{render}}). The function |
| 46 |
#' should generate and return the names of any intermediate files required to |
| 47 |
#' render the \code{input_file}. |
| 48 |
#' @param post_processor An optional post-processor function that receives the |
| 49 |
#' \code{metadata}, \code{input_file}, \code{output_file}, \code{clean}, and |
| 50 |
#' \code{verbose} parameters, and can return an alternative |
| 51 |
#' \code{output_file}. |
| 52 |
#' @param on_exit A function to call when \code{rmarkdown::render()} finishes |
| 53 |
#' execution (as registered with a \code{\link{on.exit}} handler). |
| 54 |
#' @param file_scope A function that will split markdown input to pandoc into |
| 55 |
#' multiple named files. This is useful when the caller has concatenated a set |
| 56 |
#' of Rmd files together (as \pkg{bookdown} does), and those files may need to |
| 57 |
#' processed by pandoc using the \code{--file-scope} option. The first |
| 58 |
#' argument is input file paths and the second is \code{NULL} or current file |
| 59 |
#' scope which is a named list of files w/ \code{name} and \code{content} for |
| 60 |
#' each file. The return is the new file scope. Also, the arguments should |
| 61 |
#' include \code{...} for the future extensions. |
| 62 |
#' @param base_format An optional format to extend. |
| 63 |
#' @return An R Markdown output format definition that can be passed to |
| 64 |
#' \code{\link{render}}. |
| 65 |
#' @seealso \link{render}, \link{knitr_options}, \link{pandoc_options} |
| 66 |
#' @examples |
| 67 |
#' \dontrun{ |
| 68 |
#' output_format(knitr = knitr_options(opts_chunk = list(dev = 'png')), |
| 69 |
#' pandoc = pandoc_options(to = "html")) |
| 70 |
#' } |
| 71 |
#' @export |
| 72 |
output_format <- function(knitr, |
| 73 |
pandoc, |
| 74 |
keep_md = FALSE, |
| 75 |
clean_supporting = TRUE, |
| 76 |
df_print = NULL, |
| 77 |
pre_knit = NULL, |
| 78 |
post_knit = NULL, |
| 79 |
pre_processor = NULL, |
| 80 |
intermediates_generator = NULL, |
| 81 |
post_processor = NULL, |
| 82 |
on_exit = NULL, |
| 83 |
file_scope = NULL, |
| 84 |
base_format = NULL) { |
| 85 |
|
| 86 |
format <- list( |
| 87 |
knitr = knitr, |
| 88 |
pandoc = pandoc, |
| 89 |
keep_md = keep_md, |
| 90 |
clean_supporting = if (isTRUE(keep_md)) FALSE else clean_supporting, |
| 91 |
df_print = df_print, |
| 92 |
pre_knit = pre_knit, |
| 93 |
post_knit = post_knit, |
| 94 |
pre_processor = pre_processor, |
| 95 |
intermediates_generator = intermediates_generator, |
| 96 |
post_processor = post_processor, |
| 97 |
file_scope = file_scope, |
| 98 |
on_exit = on_exit |
| 99 |
) |
| 100 |
|
| 101 |
class(format) <- "rmarkdown_output_format" |
| 102 |
|
| 103 |
# if a base format was supplied, merge it with the format we just created |
| 104 |
if (!is.null(base_format)) |
| 105 |
merge_output_formats(base_format, format) |
| 106 |
else |
| 107 |
format |
| 108 |
} |
| 109 |
|
| 110 |
# merges two scalar values; picks the overlay if non-NULL and then the base |
| 111 |
merge_scalar <- function(base, |
| 112 |
overlay) { |
| 113 |
|
| 114 |
if (is.null(base) && is.null(overlay)) |
| 115 |
NULL |
| 116 |
else if (is.null(overlay)) |
| 117 |
base |
| 118 |
else |
| 119 |
overlay |
| 120 |
} |
| 121 |
|
| 122 |
# merges two functions: if both are non-NULL, produces a new function that |
| 123 |
# invokes each and then uses the supplied operation to combine their outputs |
| 124 |
merge_function_outputs <- function(base, |
| 125 |
overlay, |
| 126 |
op) { |
| 127 |
|
| 128 |
if (!is.null(base) && !is.null(overlay)) { |
| 129 |
function(...) { |
| 130 |
op(base(...), overlay(...)) |
| 131 |
} |
| 132 |
} else { |
| 133 |
merge_scalar(base, overlay) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
# merges two post-processors; if both are non-NULL, produces a new function that |
| 138 |
# calls the overlay post-processor and then the base post-processor. |
| 139 |
merge_post_processors <- function(base, |
| 140 |
overlay) { |
| 141 |
|
| 142 |
if (!is.null(base) && !is.null(overlay)) { |
| 143 |
function(metadata, input_file, output_file, ...) { |
| 144 |
|
| 145 |
# record original output file |
| 146 |
original_output_file <- output_file |
| 147 |
|
| 148 |
# call overlay post processor |
| 149 |
output_file <- overlay(metadata, input_file, output_file, ...) |
| 150 |
|
| 151 |
# also call base post processor on original file if requested |
| 152 |
if (!is.null(attr(output_file, "post_process_original"))) |
| 153 |
base(metadata, input_file, original_output_file, ...) |
| 154 |
|
| 155 |
# call base post processor |
| 156 |
base(metadata, input_file, output_file, ...) |
| 157 |
} |
| 158 |
} |
| 159 |
else { |
| 160 |
merge_scalar(base, overlay) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
merge_file_scope <- function(base, |
| 165 |
overlay) { |
| 166 |
if (is.null(overlay)) { |
| 167 |
return(base) |
| 168 |
} |
| 169 |
has_ellipsis <- "..." %in% names(formals(overlay)) |
| 170 |
if (!has_ellipsis) { |
| 171 |
warning("file_scope lacks ... as an argument. ", |
| 172 |
"Otherwise, file_scope replaces file_scope without merging.") |
| 173 |
} |
| 174 |
if (is.null(base) || !has_ellipsis) { |
| 175 |
return(overlay) |
| 176 |
} |
| 177 |
if (has_ellipsis) { |
| 178 |
return(function(x, current_scope = NULL, ...) { |
| 179 |
scope <- base(x, current_scope, ...) |
| 180 |
overlay(x, scope, ...) |
| 181 |
}) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
# merges two output formats |
| 186 |
merge_output_formats <- function(base, |
| 187 |
overlay) { |
| 188 |
|
| 189 |
structure(list( |
| 190 |
knitr = merge_lists(base$knitr, overlay$knitr), |
| 191 |
pandoc = merge_pandoc_options(base$pandoc, overlay$pandoc), |
| 192 |
keep_md = |
| 193 |
merge_scalar(base$keep_md, overlay$keep_md), |
| 194 |
clean_supporting = |
| 195 |
merge_scalar(base$clean_supporting, overlay$clean_supporting), |
| 196 |
df_print = |
| 197 |
merge_scalar(base$df_print, overlay$df_print), |
| 198 |
pre_knit = |
| 199 |
merge_function_outputs(base$pre_knit, overlay$pre_knit, c), |
| 200 |
post_knit = |
| 201 |
merge_function_outputs(base$post_knit, overlay$post_knit, c), |
| 202 |
pre_processor = |
| 203 |
merge_function_outputs(base$pre_processor, overlay$pre_processor, c), |
| 204 |
intermediates_generator = |
| 205 |
merge_function_outputs(base$intermediates_generator, |
| 206 |
overlay$intermediates_generator, c), |
| 207 |
post_processor = |
| 208 |
merge_post_processors(base$post_processor, overlay$post_processor), |
| 209 |
file_scope = merge_file_scope(base$file_scope, overlay$file_scope), |
| 210 |
on_exit = |
| 211 |
merge_on_exit(base$on_exit, overlay$on_exit) |
| 212 |
), class = "rmarkdown_output_format") |
| 213 |
} |
| 214 |
|
| 215 |
merge_on_exit <- function(base, |
| 216 |
overlay) { |
| 217 |
|
| 218 |
function() { |
| 219 |
if (is.function(base)) base() |
| 220 |
if (is.function(overlay)) overlay() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
merge_pandoc_options <- function(base, |
| 225 |
overlay) { |
| 226 |
|
| 227 |
res <- merge_lists(base, overlay, recursive = FALSE) |
| 228 |
res$args <- c(base$args, overlay$args) |
| 229 |
res$lua_filters <- c(base$lua_filters, overlay$lua_filters) |
| 230 |
res |
| 231 |
} |
| 232 |
|
| 233 |
#' Knitr options for an output format |
| 234 |
#' |
| 235 |
#' Define the knitr options for an R Markdown output format. |
| 236 |
#' @param opts_knit List of package level knitr options (see |
| 237 |
#' \code{\link[knitr:opts_knit]{opts_knit}}) |
| 238 |
#' @param opts_chunk List of chunk level knitr options (see |
| 239 |
#' \code{\link[knitr:opts_chunk]{opts_chunk}}) |
| 240 |
#' @param knit_hooks List of hooks for R code chunks, inline R code, and output |
| 241 |
#' (see \code{\link[knitr:knit_hooks]{knit_hooks}}) |
| 242 |
#' @param opts_hooks List of hooks for code chunk options |
| 243 |
#' (see \code{\link[knitr:opts_hooks]{opts_hooks}}) |
| 244 |
#' @param opts_template List of templates for chunk level knitr options (see |
| 245 |
#' \code{\link[knitr:opts_template]{opts_template}}) |
| 246 |
#' @return An list that can be passed as the \code{knitr} argument |
| 247 |
#' of the \code{\link{output_format}} function. |
| 248 |
#' @seealso \link{output_format} |
| 249 |
#' @export |
| 250 |
knitr_options <- function(opts_knit = NULL, |
| 251 |
opts_chunk = NULL, |
| 252 |
knit_hooks = NULL, |
| 253 |
opts_hooks = NULL, |
| 254 |
opts_template = NULL) { |
| 255 |
|
| 256 |
list(opts_knit = opts_knit, |
| 257 |
opts_chunk = opts_chunk, |
| 258 |
knit_hooks = knit_hooks, |
| 259 |
opts_hooks = opts_hooks, |
| 260 |
opts_template = opts_template) |
| 261 |
} |
| 262 |
|
| 263 |
#' Knitr options for a PDF output format |
| 264 |
#' |
| 265 |
#' Define knitr options for an R Markdown output format |
| 266 |
#' that creates PDF output. |
| 267 |
#' @inheritParams html_document |
| 268 |
#' @inheritParams pdf_document |
| 269 |
#' @return An list that can be passed as the \code{knitr} argument of the |
| 270 |
#' \code{\link{output_format}} function. |
| 271 |
#' @seealso \link{knitr_options}, \link{output_format} |
| 272 |
#' @export |
| 273 |
knitr_options_pdf <- function(fig_width, |
| 274 |
fig_height, |
| 275 |
fig_crop, |
| 276 |
dev = 'pdf') { |
| 277 |
|
| 278 |
# default options |
| 279 |
opts_knit <- NULL |
| 280 |
opts_chunk <- list(dev = dev, |
| 281 |
fig.width = fig_width, |
| 282 |
fig.height = fig_height) |
| 283 |
|
| 284 |
# set the dingbats option for the pdf device if required |
| 285 |
if (dev == 'pdf') |
| 286 |
opts_chunk$dev.args <- list(pdf = list(useDingbats = FALSE)) |
| 287 |
|
| 288 |
knit_hooks <- NULL |
| 289 |
|
| 290 |
# apply cropping if requested and we have pdfcrop and ghostscript |
| 291 |
if (identical(fig_crop, 'auto')) fig_crop <- has_crop_tools(FALSE) else { |
| 292 |
if (fig_crop && !has_crop_tools()) fig_crop <- FALSE |
| 293 |
} |
| 294 |
if (fig_crop) { |
| 295 |
knit_hooks <- list(crop = knitr::hook_pdfcrop) |
| 296 |
opts_chunk$crop <- TRUE |
| 297 |
} |
| 298 |
|
| 299 |
# return options |
| 300 |
knitr_options(opts_knit = opts_knit, |
| 301 |
opts_chunk = opts_chunk, |
| 302 |
knit_hooks = knit_hooks) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
#' Pandoc options for an output format |
| 307 |
#' |
| 308 |
#' Define the pandoc options for an R Markdown output format. |
| 309 |
#' |
| 310 |
#' The \code{from} argument should be used very cautiously as it's important for |
| 311 |
#' users to be able to rely on a stable definition of supported markdown |
| 312 |
#' extensions. |
| 313 |
#' @param to Pandoc format to convert to |
| 314 |
#' @param from Pandoc format to convert from |
| 315 |
#' @param args Character vector of command line arguments to pass to pandoc |
| 316 |
#' @param keep_tex Keep the intermediate tex file used in the conversion to PDF |
| 317 |
#' (applies only to 'latex' and 'beamer' target formats) |
| 318 |
#' @param latex_engine LaTeX engine to producing PDF output (applies only to |
| 319 |
#' 'latex' and 'beamer' target formats) |
| 320 |
#' @param ext File extension (e.g. ".tex") for output file (if \code{NULL} |
| 321 |
#' chooses default based on \code{to}). This is typically used to force the |
| 322 |
#' final output of a latex or beamer conversion to be \code{.tex} rather than |
| 323 |
#' \code{.pdf}. |
| 324 |
#' @param lua_filters Character vector of file paths to Lua filters to use with |
| 325 |
#' this format. They will be added to pandoc command line call using |
| 326 |
#' \code{--lua-filter} argument. See \code{vignette("lua-filters", package = |
| 327 |
#' "rmarkdown")} to know more about Lua filters. |
| 328 |
#' @param convert_fun A function to convert the input file to the desired output |
| 329 |
#' format in \code{\link{render}()}. If not provided, |
| 330 |
#' \code{\link{pandoc_convert}()} will be used. If a custom function is |
| 331 |
#' provided, its arguments and returned value should match the |
| 332 |
#' \code{pandoc_convert()} function. Note that this function does not have to |
| 333 |
#' use Pandoc but can also use other tools such as \pkg{commonmark}. |
| 334 |
#' @return An list that can be passed as the \code{pandoc} argument of the |
| 335 |
#' \code{\link{output_format}} function. |
| 336 |
#' @seealso \link{output_format}, \link{rmarkdown_format} |
| 337 |
#' @export |
| 338 |
pandoc_options <- function(to, |
| 339 |
from = rmarkdown_format(), |
| 340 |
args = NULL, |
| 341 |
keep_tex = FALSE, |
| 342 |
latex_engine = c("pdflatex", "lualatex", "xelatex", "tectonic"), |
| 343 |
ext = NULL, |
| 344 |
lua_filters = NULL, |
| 345 |
convert_fun = NULL) { |
| 346 |
list(to = to, |
| 347 |
from = from, |
| 348 |
args = args, |
| 349 |
keep_tex = keep_tex, |
| 350 |
latex_engine = match.arg(latex_engine), |
| 351 |
ext = ext, |
| 352 |
convert_fun = convert_fun, |
| 353 |
lua_filters = lua_filters) |
| 354 |
} |
| 355 |
|
| 356 |
#' R Markdown input format definition |
| 357 |
#' |
| 358 |
#' Compose a pandoc markdown input definition for R Markdown |
| 359 |
#' that can be passed as the \code{from} argument of \link{pandoc_options}. |
| 360 |
#' |
| 361 |
#' By default R Markdown is defined as all pandoc markdown extensions with |
| 362 |
#' the following tweaks for backward compatibility with the markdown package |
| 363 |
#' (+ features are added, - features are removed): |
| 364 |
#' |
| 365 |
#' \tabular{l}{ |
| 366 |
#' \code{+autolink_bare_uris} \cr |
| 367 |
#' \code{+tex_math_single_backslash} \cr |
| 368 |
#' } |
| 369 |
#' |
| 370 |
#' For more on pandoc markdown see the |
| 371 |
#' \href{https://pandoc.org/MANUAL.html}{pandoc online documentation}. |
| 372 |
#' @param implicit_figures Automatically make figures from images (defaults to \code{TRUE}). |
| 373 |
#' @param extensions Markdown extensions to be added or removed from the |
| 374 |
#' default definition of R Markdown. |
| 375 |
#' @return Pandoc markdown format specification |
| 376 |
#' @examples |
| 377 |
#' \dontrun{ |
| 378 |
#' rmarkdown_format("-implicit_figures") |
| 379 |
#' } |
| 380 |
#' @seealso \link{output_format}, \link{pandoc_options} |
| 381 |
#' @export |
| 382 |
rmarkdown_format <- function(extensions = NULL) { |
| 383 |
|
| 384 |
format <- c("markdown") |
| 385 |
|
| 386 |
# only add extensions if the user hasn't already specified |
| 387 |
# a manual override for them |
| 388 |
addExtension <- function(extension) { |
| 389 |
if (length(grep(extension, extensions)) == 0) |
| 390 |
format <<- c(format, paste0("+", extension)) |
| 391 |
} |
| 392 |
|
| 393 |
addExtension("autolink_bare_uris") |
| 394 |
addExtension("tex_math_single_backslash") |
| 395 |
|
| 396 |
format <- c(format, extensions, recursive = TRUE) |
| 397 |
|
| 398 |
paste(format, collapse = "") |
| 399 |
} |
| 400 |
|
| 401 |
#' Determine the default output format for an R Markdown document |
| 402 |
#' |
| 403 |
#' Read the YAML metadata (and any common output YAML file) for the |
| 404 |
#' document and return the output format that will be generated by |
| 405 |
#' a call to \code{\link{render}}. |
| 406 |
#' |
| 407 |
#' This function is useful for front-end tools that require additional |
| 408 |
#' knowledge of the output to be produced by \code{\link{render}} (e.g. to |
| 409 |
#' customize the preview experience). |
| 410 |
#' @param input Input file (Rmd or plain markdown) |
| 411 |
#' @param output_yaml Paths to YAML files specifying output formats and their |
| 412 |
#' configurations. The first existing one is used. If none are found, then |
| 413 |
#' the function searches YAML files specified to the \code{output_yaml} top-level |
| 414 |
#' parameter in the YAML front matter, _output.yml or _output.yaml, and then uses |
| 415 |
#' the first existing one. |
| 416 |
#' @return A named list with a \code{name} value containing the format |
| 417 |
#' name and an \code{options} value that is a list containing all the options |
| 418 |
#' for the format and their values. An option's default value will be returned |
| 419 |
#' if the option isn't set explicitly in the document. |
| 420 |
#' @export |
| 421 |
default_output_format <- function(input, output_yaml = NULL) { |
| 422 |
|
| 423 |
# execute within the input file's directory (this emulates the way |
| 424 |
# yaml front matter discovery is done within render) |
| 425 |
oldwd <- setwd(dirname(abs_path(input))) |
| 426 |
on.exit(setwd(oldwd), add = TRUE) |
| 427 |
|
| 428 |
# because we're now within the same directory as the input file, |
| 429 |
# we just need its basename |
| 430 |
input <- basename(input) |
| 431 |
|
| 432 |
# parse the YAML and front matter and get the explicitly set options |
| 433 |
input_lines <- read_utf8(input) |
| 434 |
format <- output_format_from_yaml_front_matter( |
| 435 |
input_lines, output_yaml = output_yaml) |
| 436 |
|
| 437 |
# look up the formals of the output function to get the full option list and |
| 438 |
# merge against the explicitly set list |
| 439 |
format_function <- tryCatch( |
| 440 |
eval(xfun::parse_only(format$name)), error = function(e) NULL |
| 441 |
) |
| 442 |
# if we can't find the function, default to html_document with no options |
| 443 |
if (!is.function(format_function)) |
| 444 |
return(list(name = "html_document", options = list())) |
| 445 |
format$options <- merge_lists(as.list(formals(format_function)), |
| 446 |
format$options, |
| 447 |
recursive = FALSE) |
| 448 |
format |
| 449 |
} |
| 450 |
|
| 451 |
#' Resolve the output format for an R Markdown document |
| 452 |
#' |
| 453 |
#' Read the YAML metadata (and any common output YAML file) for the |
| 454 |
#' document and return an output format object that can be |
| 455 |
#' passed to the \code{\link{render}} function. |
| 456 |
#' |
| 457 |
#' This function is useful for front-end tools that need to modify |
| 458 |
#' the default behavior of an output format. |
| 459 |
#' @inheritParams default_output_format |
| 460 |
#' @param input Input file (Rmd or plain markdown) |
| 461 |
#' @param output_format Name of output format (or \code{NULL} to use |
| 462 |
#' the default format for the input file). |
| 463 |
#' @param output_options List of output options that should override the |
| 464 |
#' options specified in metadata. |
| 465 |
#' @return An R Markdown output format definition that can be passed to |
| 466 |
#' \code{\link{render}}. |
| 467 |
#' @export |
| 468 |
resolve_output_format <- function(input, |
| 469 |
output_format = NULL, |
| 470 |
output_options = NULL, |
| 471 |
output_yaml = NULL) { |
| 472 |
|
| 473 |
# read the input file |
| 474 |
input_lines <- read_utf8(input) |
| 475 |
|
| 476 |
# validate that the output format is either NULL or a character vector |
| 477 |
if (!is.null(output_format) && !is.character(output_format)) |
| 478 |
stop("output_format must be a character vector") |
| 479 |
|
| 480 |
# resolve the output format by looking at the yaml |
| 481 |
output_format <- |
| 482 |
output_format_from_yaml_front_matter( |
| 483 |
input_lines, |
| 484 |
output_options, |
| 485 |
output_format, |
| 486 |
output_yaml) |
| 487 |
|
| 488 |
# return it |
| 489 |
create_output_format(output_format$name, output_format$options) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
#' Determine all output formats for an R Markdown document |
| 494 |
#' |
| 495 |
#' Read the YAML metadata (and any common output YAML file) for the |
| 496 |
#' document and return the output formats that will be generated by |
| 497 |
#' a call to \code{\link{render}}. |
| 498 |
#' |
| 499 |
#' This function is useful for front-end tools that require additional |
| 500 |
#' knowledge of the output to be produced by \code{\link{render}} (e.g. to |
| 501 |
#' customize the preview experience). |
| 502 |
#' @inheritParams default_output_format |
| 503 |
#' @param input Input file (Rmd or plain markdown) |
| 504 |
#' @return A character vector with the names of all output formats. |
| 505 |
#' @export |
| 506 |
all_output_formats <- function(input, output_yaml = NULL) { |
| 507 |
enumerate_output_formats(input, output_yaml = output_yaml) |
| 508 |
} |
| 509 |
|
| 510 |
# Synthesize the output format for a document from it's YAML. If we can't |
| 511 |
# find an output format then we use a default one based on the output_file extension |
| 512 |
# or just return html_document |
| 513 |
output_format_from_yaml_front_matter <- function(input_lines, |
| 514 |
output_options = NULL, |
| 515 |
output_format_name = NULL, |
| 516 |
output_yaml = NULL, |
| 517 |
output_file = NULL) { |
| 518 |
|
| 519 |
format_name <- output_format_name |
| 520 |
|
| 521 |
# ensure input is the correct data type |
| 522 |
if (!is_null_or_string(format_name)) { |
| 523 |
stop2("Unrecognized output format specified") |
| 524 |
} |
| 525 |
|
| 526 |
# parse the yaml |
| 527 |
yaml_input <- parse_yaml_front_matter(input_lines) |
| 528 |
|
| 529 |
# default to no options |
| 530 |
format_options <- list() |
| 531 |
|
| 532 |
# parse _site.yml output format if we have it |
| 533 |
config <- site_config(".") |
| 534 |
yaml_site <- config[["output"]] |
| 535 |
|
| 536 |
# parse common output yaml file if we have it |
| 537 |
output_yaml <- c(output_yaml, yaml_input$output_yaml, "_output.yml", "_output.yaml") |
| 538 |
output_yaml <- output_yaml[file.exists(output_yaml)][1L] |
| 539 |
yaml_common <- if (!is.na(output_yaml)) yaml_load_file(output_yaml) |
| 540 |
|
| 541 |
# merge _site.yml and output_yaml |
| 542 |
yaml_common <- merge_output_options(yaml_site, yaml_common) |
| 543 |
|
| 544 |
# parse output format from front-matter if we have it |
| 545 |
if (length(yaml_common) || length(yaml_input[["output"]])) { |
| 546 |
|
| 547 |
# alias the output format yaml |
| 548 |
yaml_output <- yaml_input[["output"]] |
| 549 |
|
| 550 |
# merge against common output yaml |
| 551 |
yaml_output <- merge_output_options(yaml_common, yaml_output) |
| 552 |
|
| 553 |
# if a named format was provided then try to find it |
| 554 |
if (!is.null(format_name)) { |
| 555 |
|
| 556 |
# if this is a named element of the list then use that |
| 557 |
if (format_name %in% names(yaml_output)) { |
| 558 |
|
| 559 |
format_options <- yaml_output[[format_name]] |
| 560 |
|
| 561 |
# otherwise this could be a heterogeneous list of characters and |
| 562 |
# lists so scan for an embedded list |
| 563 |
} else { |
| 564 |
for (format in yaml_output) { |
| 565 |
if (is.list(format) && !is.null(format[[format_name]])) |
| 566 |
format_options <- format[[format_name]] |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
# if the options are just "default" then that's the same as empty list |
| 571 |
if (identical(format_options, "default")) format_options <- list() |
| 572 |
|
| 573 |
# no named format passed so take the first element |
| 574 |
} else { |
| 575 |
if (is.list(yaml_output[[1]])) { |
| 576 |
# check for named list |
| 577 |
if (nzchar(format_name <- names(yaml_output)[[1]])) { |
| 578 |
format_options <- yaml_output[[1]] |
| 579 |
# nested named list |
| 580 |
} else { |
| 581 |
format_name <- names(yaml_output[[1]])[[1]] |
| 582 |
format_options <- yaml_output[[1]][[format_name]] |
| 583 |
} |
| 584 |
} else if (is.list(yaml_output) && |
| 585 |
(is.null(yaml_output[[1]]) || |
| 586 |
identical(yaml_output[[1]], "default"))) { |
| 587 |
format_name <- names(yaml_output)[[1]] |
| 588 |
|
| 589 |
} else { |
| 590 |
format_name <- yaml_output[[1]] |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
# no output formats defined in the file, just take the passed format by name, |
| 595 |
# or default to a format based on the output_file extension if any, |
| 596 |
# or html_document |
| 597 |
} else { |
| 598 |
if (is.null(format_name)) { |
| 599 |
format_name <- output_format_string_from_ext(output_file) |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
# merge any output_options passed in the call to render |
| 604 |
if (!is.null(output_options)) { |
| 605 |
|
| 606 |
# create the output format function so we can check it's formals. strip options |
| 607 |
# not within the formals |
| 608 |
output_func <- create_output_format_function(format_name) |
| 609 |
output_func_params <- names(formals(output_func)) |
| 610 |
if (!"..." %in% output_func_params) { |
| 611 |
unsupported_options <- setdiff(names(output_options), output_func_params) |
| 612 |
output_options[unsupported_options] <- NULL |
| 613 |
} |
| 614 |
|
| 615 |
# merge the output_options |
| 616 |
format_options <- merge_output_options(format_options, output_options) |
| 617 |
} |
| 618 |
|
| 619 |
# return the format name and options |
| 620 |
list(name = format_name, options = format_options) |
| 621 |
} |
| 622 |
|
| 623 |
create_output_format <- function(name, |
| 624 |
options) { |
| 625 |
|
| 626 |
# validate the name |
| 627 |
if (is.null(name)) |
| 628 |
stop2("The output format name must not be NULL") |
| 629 |
if (name == "revealjs_presentation") |
| 630 |
stop("reveal.js presentations are now located in a separate package: ", |
| 631 |
"https://github.com/jjallaire/revealjs") |
| 632 |
|
| 633 |
# lookup the function |
| 634 |
output_format_func <- create_output_format_function(name) |
| 635 |
|
| 636 |
# call the function |
| 637 |
output_format <- do.call(output_format_func, options) |
| 638 |
if (!is_output_format(output_format)) |
| 639 |
stop2("Format is not of class rmarkdown_output_format") |
| 640 |
|
| 641 |
# return the format |
| 642 |
output_format |
| 643 |
} |
| 644 |
|
| 645 |
output_format_string_from_ext <- function(output_file) { |
| 646 |
default_format <- "html_document" |
| 647 |
if (is.null(output_file)) return(default_format) |
| 648 |
switch(xfun::file_ext(output_file), |
| 649 |
html = "html_document", |
| 650 |
pdf = "pdf_document", |
| 651 |
docx = "word_document", |
| 652 |
default_format # always been the default format in R Markdown |
| 653 |
) |
| 654 |
} |
| 655 |
|
| 656 |
create_output_format_function <- function(name) { |
| 657 |
output_format_func <- eval(xfun::parse_only(name)) |
| 658 |
if (!is.function(output_format_func)) |
| 659 |
stop2("YAML output format must evaluate to a function") |
| 660 |
output_format_func |
| 661 |
} |
| 662 |
|
| 663 |
is_output_format <- function(x) { |
| 664 |
inherits(x, "rmarkdown_output_format") |
| 665 |
} |
| 666 |
|
| 667 |
enumerate_output_formats <- function(input, envir, encoding, output_yaml = NULL) { |
| 668 |
|
| 669 |
# read the input |
| 670 |
input_lines <- read_utf8(input) |
| 671 |
|
| 672 |
# if this is an R file then spin it |
| 673 |
if (identical(tolower(xfun::file_ext(input)), "r")) |
| 674 |
input_lines <- knitr::spin(text = input_lines, knit = FALSE) |
| 675 |
|
| 676 |
# parse _site.yml output format if we have it |
| 677 |
config <- site_config(input) |
| 678 |
if (!is.null(config) && !is.null(config[["output"]])) { |
| 679 |
site_output_format_yaml <- config[["output"]] |
| 680 |
} else { |
| 681 |
site_output_format_yaml <- list() |
| 682 |
} |
| 683 |
|
| 684 |
# read the ymal front matter |
| 685 |
yaml_front_matter <- parse_yaml_front_matter(input_lines) |
| 686 |
|
| 687 |
# read any output yaml |
| 688 |
output_yaml <- file.path( |
| 689 |
dirname(input), |
| 690 |
c(output_yaml, yaml_front_matter$output_yaml, "_output.yml", "_output.yaml") |
| 691 |
) |
| 692 |
output_yaml <- output_yaml[file.exists(output_yaml)][1L] |
| 693 |
common_output_format_yaml <- if (is.na(output_yaml)) { |
| 694 |
list() |
| 695 |
} else { |
| 696 |
yaml_load_file(output_yaml) |
| 697 |
} |
| 698 |
|
| 699 |
# merge site and common |
| 700 |
common_output_format_yaml <- merge_output_options(site_output_format_yaml, |
| 701 |
common_output_format_yaml) |
| 702 |
|
| 703 |
# parse output formats from front-matter if we have it |
| 704 |
if (length(common_output_format_yaml) > 0 || |
| 705 |
length(yaml_front_matter[["output"]]) > 0) { |
| 706 |
|
| 707 |
# alias the output format yaml |
| 708 |
output_format_yaml <- yaml_front_matter[["output"]] |
| 709 |
|
| 710 |
# merge against common output yaml |
| 711 |
output_format_yaml <- merge_output_options(common_output_format_yaml, |
| 712 |
output_format_yaml) |
| 713 |
} else { |
| 714 |
output_format_yaml <- NULL |
| 715 |
} |
| 716 |
|
| 717 |
# return them by name |
| 718 |
if (is.character(output_format_yaml)) { |
| 719 |
output_format_yaml |
| 720 |
} else if (is.list(output_format_yaml)) { |
| 721 |
names(output_format_yaml) |
| 722 |
} else { |
| 723 |
NULL |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
#' Parse the YAML front matter from a file |
| 728 |
#' @inheritParams default_output_format |
| 729 |
#' @inheritParams render |
| 730 |
#' @keywords internal |
| 731 |
#' @export |
| 732 |
yaml_front_matter <- function(input, encoding = 'UTF-8') { |
| 733 |
parse_yaml_front_matter(read_utf8(input)) |
| 734 |
} |
| 735 |
|
| 736 |
parse_yaml_front_matter <- function(input_lines) { |
| 737 |
|
| 738 |
partitions <- partition_yaml_front_matter(input_lines) |
| 739 |
if (!is.null(partitions$front_matter)) { |
| 740 |
front_matter <- partitions$front_matter |
| 741 |
if (length(front_matter) > 2) { |
| 742 |
front_matter <- front_matter[2:(length(front_matter) - 1)] |
| 743 |
front_matter <- one_string(front_matter) |
| 744 |
parsed_yaml <- yaml_load(front_matter) |
| 745 |
if (is.list(parsed_yaml)) |
| 746 |
parsed_yaml |
| 747 |
else |
| 748 |
list() |
| 749 |
} |
| 750 |
else |
| 751 |
list() |
| 752 |
} |
| 753 |
else |
| 754 |
list() |
| 755 |
} |
| 756 |
|
| 757 |
partition_yaml_front_matter <- function(input_lines) { |
| 758 |
|
| 759 |
validate_front_matter <- function(delimiters) { |
| 760 |
if (length(delimiters) >= 2 && |
| 761 |
(delimiters[2] - delimiters[1] > 1) && |
| 762 |
grepl("^---\\s*$", input_lines[delimiters[1]])) { |
| 763 |
# verify that it's truly front matter, not preceded by |
| 764 |
# other content except blank lines or special comments |
| 765 |
# in html_notebook's intermediate .knit.md |
| 766 |
if (delimiters[1] == 1) { |
| 767 |
TRUE |
| 768 |
} else all(grepl( |
| 769 |
"^\\s*(<!-- rnb-\\w*-(begin|end) -->)?\\s*$", |
| 770 |
input_lines[1:delimiters[1] - 1] |
| 771 |
)) |
| 772 |
} else { |
| 773 |
FALSE |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
# is there yaml front matter? |
| 778 |
delimiters <- grep("^(---|\\.\\.\\.)\\s*$", input_lines) |
| 779 |
if (validate_front_matter(delimiters)) { |
| 780 |
|
| 781 |
front_matter <- input_lines[(delimiters[1]):(delimiters[2])] |
| 782 |
|
| 783 |
input_body <- c() |
| 784 |
|
| 785 |
if (delimiters[1] > 1) |
| 786 |
input_body <- c(input_body, |
| 787 |
input_lines[1:delimiters[1] - 1]) |
| 788 |
|
| 789 |
if (delimiters[2] < length(input_lines)) |
| 790 |
input_body <- c(input_body, |
| 791 |
input_lines[-(1:delimiters[2])]) |
| 792 |
|
| 793 |
list(front_matter = front_matter, |
| 794 |
body = input_body) |
| 795 |
} |
| 796 |
else { |
| 797 |
list(front_matter = NULL, |
| 798 |
body = input_lines) |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
merge_output_options <- function(base_options, |
| 803 |
overlay_options) { |
| 804 |
|
| 805 |
# if either one of these is a character vector then normalize to a named list |
| 806 |
normalize_list <- function(target) { |
| 807 |
if (is.null(target)) { |
| 808 |
list() |
| 809 |
} else if (is.character(target)) { |
| 810 |
setNames(lapply(target, function(x) list()), target) |
| 811 |
} else { |
| 812 |
target[names(target) != "..."] # remove symbols (...) from list |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
merge_lists(normalize_list(base_options), normalize_list(overlay_options)) |
| 817 |
} |
| 818 |
|
| 819 |
is_pandoc_to_html <- function(options) { |
| 820 |
options$to %in% c("html", "html4", "html5") |
| 821 |
} |
| 822 |
|
| 823 |
citeproc_required <- function(yaml_front_matter, |
| 824 |
input_lines = NULL) { |
| 825 |
# TODO: remove the hack below after BETS is updated on CRAN https://github.com/nmecsys/BETS/pull/18 |
| 826 |
if (tryCatch(xfun::check_old_package('BETS', '0.4.9'), error = function(e) FALSE)) return(FALSE) |
| 827 |
( |
| 828 |
is.null(yaml_front_matter$citeproc) || |
| 829 |
yaml_front_matter$citeproc |
| 830 |
) && ( |
| 831 |
!is.null(yaml_front_matter$bibliography) || |
| 832 |
!is.null(yaml_front_matter$references) || |
| 833 |
# detect references: and bibliography: outside of yaml header |
| 834 |
# as Pandoc is supporting |
| 835 |
# TODO: remove when supporting multiple yaml block |
| 836 |
# https://github.com/rstudio/rmarkdown/issues/1891 |
| 837 |
length(grep("^references:\\s*$", input_lines)) > 0 || |
| 838 |
length(grep("^bibliography:\\s*$", input_lines)) > 0 |
| 839 |
) |
| 840 |
} |
| 841 |
|
| 842 |
#' Define and merge an R Markdown's output format dependency |
| 843 |
#' |
| 844 |
#' Define and merge a dependency such as pre/post-processors from within |
| 845 |
#' chunks. The merge happens explicitly when a list of dependencies are |
| 846 |
#' passed to \code{knitr::knit_meta_add()} or implicitly when a dependency |
| 847 |
#' is \code{knitr::knit_print}ed. Defining a function that does the former is |
| 848 |
#' the best way for package developers to share the dependency. On the |
| 849 |
#' contrary, the latter is useful to declare a document-specific dependency. |
| 850 |
#' This function shares some arguments with \code{\link{output_format}}, |
| 851 |
#' but lacks the others because dependency is resolved after \code{post_knit} |
| 852 |
#' and before \code{pre_processor}. |
| 853 |
#' |
| 854 |
#' @param name A dependency name. If some dependencies share the same name, |
| 855 |
#' then only the first one will be merged to the output format. |
| 856 |
#' @inheritParams output_format |
| 857 |
#' |
| 858 |
#' @return An list of arguments with the "rmd_dependency" class. |
| 859 |
#' |
| 860 |
#' @examples |
| 861 |
#' # Implicitly add lua filters from within a chunk |
| 862 |
#' # This relies on (implicit) printing of the dependency in a chunk via |
| 863 |
#' # knitr::knit_print()` |
| 864 |
#' output_format_dependency( |
| 865 |
#' "lua_filter1", |
| 866 |
#' pandoc = list(lua_filters = "example1.lua") |
| 867 |
#' ) |
| 868 |
#' |
| 869 |
#' # Explicitly add lua filters from within a chunk |
| 870 |
#' knitr::knit_meta_add(list(output_format_dependency( |
| 871 |
#' "lua_filter2", |
| 872 |
#' pandoc = list(lua_filters = "example2.lua") |
| 873 |
#' ))) |
| 874 |
#' |
| 875 |
#' # List the available dependencies |
| 876 |
#' # Note that the list may include dependencies with duplicated names. In that |
| 877 |
#' # case, the first one is merged to the output format and the others are |
| 878 |
#' # discarded. |
| 879 |
#' str(knitr::knit_meta("output_format_dependency", clean = FALSE)) |
| 880 |
#' |
| 881 |
#' @export |
| 882 |
output_format_dependency <- function(name, |
| 883 |
pandoc = list(), |
| 884 |
pre_processor = NULL, |
| 885 |
post_processor = NULL, |
| 886 |
file_scope = NULL, |
| 887 |
on_exit = NULL) { |
| 888 |
# Some arguments are NULL |
| 889 |
# to ensure inheriting the values from the base output format |
| 890 |
structure(list(name = name, |
| 891 |
knitr = NULL, # must be NULL because merge happens after knit |
| 892 |
pandoc = pandoc, |
| 893 |
pre_processor = pre_processor, |
| 894 |
keep_md = NULL, |
| 895 |
clean_supporting = NULL, |
| 896 |
post_processor = post_processor, |
| 897 |
file_scope = file_scope, |
| 898 |
on_exit = on_exit), |
| 899 |
class = "output_format_dependency") |
| 900 |
} |
| 901 |
|
| 902 |
#' @export |
| 903 |
knit_print.output_format_dependency <- function(x, ...) { |
| 904 |
knitr::asis_output(list(), meta = list(x)) |
| 905 |
} |
| 906 |
|
| 907 |
merge_output_format_dependency <- function(fmt, dep) { |
| 908 |
dep$name <- NULL # remove to be consistent with arguments of output_format |
| 909 |
dep$base_format <- fmt |
| 910 |
do.call(output_format, dep) |
| 911 |
} |
| 912 |
|
| 913 |
merge_output_format_dependencies <- function(fmt, deps) { |
| 914 |
skip <- c() |
| 915 |
for (d in deps) { |
| 916 |
if (inherits(d, "output_format_dependency") && !isTRUE(skip[d$name])) { |
| 917 |
skip[d$name] <- TRUE |
| 918 |
fmt <- merge_output_format_dependency(fmt, d) |
| 919 |
} |
| 920 |
} |
| 921 |
fmt |
| 922 |
} |