| 1 |
#' Convert to a ConTeXt document |
| 2 |
#' |
| 3 |
#' Format for converting from R Markdown to PDF using ConTeXt. |
| 4 |
#' |
| 5 |
#' ConTeXt needs to be installed, e.g., you can install it with |
| 6 |
#' \code{tinytex::tlmgr_install("context")}. |
| 7 |
#' |
| 8 |
#' R Markdown documents can have optional metadata that is used to generate a |
| 9 |
#' document header that includes the title, author, and date. For more details |
| 10 |
#' see the documentation on R Markdown \link[=rmd_metadata]{metadata}. |
| 11 |
#' |
| 12 |
#' R Markdown documents also support citations. You can find more information on |
| 13 |
#' the markdown syntax for citations in the |
| 14 |
#' \href{https://pandoc.org/MANUAL.html#citations}{Bibliographies |
| 15 |
#' and Citations} article in the online documentation. |
| 16 |
#' @inheritParams pdf_document |
| 17 |
#' @param context_path Path of the ConTeXt executable. If not provided, ConTeXt |
| 18 |
#' has to be available from the \code{PATH} environment variable. |
| 19 |
#' @param context_args Command line arguments passed to ConTeXt. |
| 20 |
#' @param ext Format of the output document (defaults to ".pdf"). |
| 21 |
#' @return R Markdown output format to pass to \code{\link{render}}. |
| 22 |
#' @examples |
| 23 |
#' \dontrun{ |
| 24 |
#' library(rmarkdown) |
| 25 |
#' |
| 26 |
#' # simple invocation |
| 27 |
#' render("input.Rmd", context_document()) |
| 28 |
#' } |
| 29 |
#' @export |
| 30 |
context_document <- function(toc = FALSE, |
| 31 |
toc_depth = 2, |
| 32 |
number_sections = FALSE, |
| 33 |
fig_width = 6.5, |
| 34 |
fig_height = 4.5, |
| 35 |
fig_crop = 'auto', |
| 36 |
fig_caption = TRUE, |
| 37 |
dev = "pdf", |
| 38 |
df_print = "default", |
| 39 |
template = NULL, |
| 40 |
keep_tex = FALSE, |
| 41 |
keep_md = FALSE, |
| 42 |
citation_package = c("default", "natbib", "biblatex"), |
| 43 |
includes = NULL, |
| 44 |
md_extensions = NULL, |
| 45 |
output_extensions = NULL, |
| 46 |
pandoc_args = NULL, |
| 47 |
context_path = NULL, |
| 48 |
context_args = NULL, |
| 49 |
ext = c(".pdf", ".tex")) { |
| 50 |
context_path <- normalize_path(context_path, must_work = TRUE) |
| 51 |
sys_context <- if (is.null(context_path)) find_program("context") else context_path |
| 52 |
ext <- match.arg(ext) |
| 53 |
if (identical(ext, ".pdf") && !nzchar(sys_context)) |
| 54 |
stop2("Cannot find ConTeXt.\n", |
| 55 |
"Please, check that ConTeXt is installed.\n", |
| 56 |
"For more information, see the help page '?context_document'." |
| 57 |
) |
| 58 |
|
| 59 |
# base pandoc options for all ConTeXt output |
| 60 |
args <- c("--standalone") |
| 61 |
|
| 62 |
# Pass the path of ConTeXt |
| 63 |
if (!is.null(context_path)) { |
| 64 |
args <- c(args, pandoc_latex_engine_args(context_path)) |
| 65 |
} |
| 66 |
|
| 67 |
# context command line arguments |
| 68 |
if (length(context_args)) |
| 69 |
args <- c(args, paste("--pdf-engine-opt", context_args, sep = "=")) |
| 70 |
|
| 71 |
# table of contents |
| 72 |
args <- c(args, pandoc_toc_args(toc, toc_depth)) |
| 73 |
|
| 74 |
# numbered sections |
| 75 |
if (number_sections) |
| 76 |
args <- c(args, "--number-sections") |
| 77 |
|
| 78 |
# template |
| 79 |
if (!is.null(template)) |
| 80 |
args <- c(args, "--template", pandoc_path_arg(template)) |
| 81 |
|
| 82 |
# citation package |
| 83 |
args <- c(args, citation_package_arg(citation_package)) |
| 84 |
|
| 85 |
# content includes |
| 86 |
args <- c(args, includes_to_pandoc_args(includes)) |
| 87 |
|
| 88 |
# args args |
| 89 |
args <- c(args, pandoc_args) |
| 90 |
|
| 91 |
clean_supporting <- identical(ext, ".pdf") && !isTRUE(keep_tex) |
| 92 |
|
| 93 |
# post processor |
| 94 |
post_processor <- NULL |
| 95 |
# if keep_tex=TRUE, generate the ConTeXt file with Pandoc |
| 96 |
# and call ConTeXt using a post processor to generate the PDF file |
| 97 |
if (identical(ext, ".pdf") && isTRUE(keep_tex)) { |
| 98 |
ext <- ".tex" # Use Pandoc to generate the ConTeXt file |
| 99 |
post_processor <- function(metadata, input_file, output_file, clean, verbose) { |
| 100 |
context_args <- unique(c( |
| 101 |
context_args, |
| 102 |
# ConTeXt produces some auxiliary files: |
| 103 |
# direct PDF generation by Pandoc never produces these auxiliary |
| 104 |
# files because Pandoc runs ConTeXt in a temporary directory. |
| 105 |
# Replicate Pandoc's behavior using "--purgeall" option |
| 106 |
"--purgeall", |
| 107 |
# Pandoc runs ConteXt with "--batchmode" option. Do the same. |
| 108 |
"--batchmode" |
| 109 |
)) |
| 110 |
|
| 111 |
# ConTeXt is extremely verbose |
| 112 |
# Pandoc output these informations when run in its verbose mode |
| 113 |
# Replicate Pandoc's behavior |
| 114 |
is_pandoc_verbose <- !is.na(match("--verbose", pandoc_args)) |
| 115 |
stdout <- if (is_pandoc_verbose) "" else FALSE |
| 116 |
system2(sys_context, c(output_file, context_args), stdout = stdout) |
| 117 |
xfun::with_ext(output_file, "pdf") |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
# return format |
| 122 |
output_format( |
| 123 |
knitr = knitr_options_pdf(fig_width, fig_height, fig_crop, dev), |
| 124 |
pandoc = pandoc_options( |
| 125 |
to = paste(c("context", output_extensions), collapse = ""), |
| 126 |
from = from_rmarkdown(fig_caption, md_extensions), |
| 127 |
args = args, |
| 128 |
keep_tex = FALSE, |
| 129 |
ext = ext, |
| 130 |
lua_filters = pkg_file_lua("pagebreak.lua") |
| 131 |
), |
| 132 |
clean_supporting = !isTRUE(keep_tex), |
| 133 |
keep_md = keep_md, |
| 134 |
df_print = df_print, |
| 135 |
intermediates_generator = general_intermediates_generator, |
| 136 |
post_processor = post_processor |
| 137 |
) |
| 138 |
} |