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