forked from rstudio/rmarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.R
443 lines (370 loc) · 15.5 KB
/
render.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#' The YAML metadata of the current R Markdown document
#'
#' The object \code{metadata} stores the YAML metadata of the current R Markdown
#' document as a list, which you may use in the R code chunks, e.g.
#' \code{metadata$title} (the title of the document), \code{metadata$author},
#' and \code{metadata$foo} (if you have a YAML field named \code{foo}), etc.
#' @export
metadata <- list()
#' @export
render <- function(input,
output_format = NULL,
output_file = NULL,
output_dir = NULL,
output_options = NULL,
intermediates_dir = NULL,
runtime = c("auto", "static", "shiny"),
clean = TRUE,
envir = parent.frame(),
quiet = FALSE,
encoding = getOption("encoding")) {
perf_timer_start("render")
# check for "all" output formats
if (identical(output_format, "all")) {
output_format <- enumerate_output_formats(input, envir, encoding)
if (is.null(output_format))
output_format <- "html_document"
}
# check for a list of output formats -- if there is more than one
# then recursively call this function with each format by name
if (is.character(output_format) && length(output_format) > 1) {
outputs <- character()
for (format in output_format) {
# the output_file argument is intentionally ignored (we can't give
# the same name to each rendered output); copy the rest by name
output <- render(input = input,
output_format = format,
output_file = NULL,
output_dir = output_dir,
output_options = output_options,
intermediates_dir = intermediates_dir,
runtime = runtime,
clean = clean,
envir = envir,
quiet = quiet,
encoding = encoding)
outputs <- c(outputs, output)
}
return(invisible(outputs))
}
# check for required version of pandoc
required_pandoc <- "1.12.3"
if (!pandoc_available(required_pandoc)) {
stop("pandoc version ", required_pandoc, " or higher ",
"is required and was not found.", call. = FALSE)
}
# setup a cleanup function for intermediate files
intermediates <- c()
on.exit(lapply(intermediates,
function(f) {
if (clean && file.exists(f))
unlink(f, recursive = TRUE)
}),
add = TRUE)
# ensure we have a directory to store intermediates
if (!is.null(intermediates_dir) && !file.exists(intermediates_dir))
dir.create(intermediates_dir)
intermediates_loc <- function(file) {
if (is.null(intermediates_dir))
file
else
file.path(intermediates_dir, file)
}
# if the input file has shell characters in its name then make a copy that
# doesn't have shell characters
if (grepl(.shell_chars_regex, basename(input))) {
input_no_shell_chars <- intermediates_loc(
file_name_without_shell_chars(basename(input)))
if (file.exists(input_no_shell_chars)) {
stop("The name of the input file cannot contain the special shell ",
"characters: ", .shell_chars_regex, " (attempted to copy to a ",
"version without those characters '", input_no_shell_chars, "' ",
"however that file already exists)", call. = FALSE)
}
file.copy(input, input_no_shell_chars, overwrite = TRUE)
intermediates <- c(intermediates, input_no_shell_chars)
input <- input_no_shell_chars
}
# execute within the input file's directory
oldwd <- setwd(dirname(tools::file_path_as_absolute(input)))
on.exit(setwd(oldwd), add = TRUE)
# reset the name of the input file to be relative and calculate variations
# on the filename for our various intermediate targets
input <- basename(input)
knit_input <- input
knit_output <- intermediates_loc(file_with_meta_ext(input, "knit", "md"))
intermediates <- c(intermediates, knit_output)
utf8_input <- intermediates_loc(file_with_meta_ext(input, "utf8", "md"))
intermediates <- c(intermediates, utf8_input)
# track whether this was straight markdown input (to prevent keep_md later)
md_input <- identical(tolower(tools::file_ext(input)), "md")
# if this is an R script then spin it first
if (identical(tolower(tools::file_ext(input)), "r")) {
# make a copy of the file to spin
spin_input <- intermediates_loc(file_with_meta_ext(input, "spin", "R"))
file.copy(input, spin_input, overwrite = TRUE)
intermediates <- c(intermediates, spin_input)
# spin it
spin_rmd <- knitr::spin(spin_input,
knit = FALSE,
envir = envir,
format = "Rmd")
intermediates <- c(intermediates, spin_rmd)
knit_input <- spin_rmd
# append default metadata (this will be ignored if there is user
# metadata elsewhere in the file)
metadata <- paste('\n',
'---\n',
'title: "', input, '"\n',
'author: "', Sys.info()[["user"]], '"\n',
'date: "', date(), '"\n',
'---\n'
, sep = "")
if (!identical(encoding, "native.enc"))
metadata <- iconv(metadata, to = encoding)
cat(metadata, file = knit_input, append = TRUE)
}
# read the input file
input_lines <- read_lines_utf8(knit_input, encoding)
# read the yaml front matter
yaml_front_matter <- parse_yaml_front_matter(input_lines)
# if we haven't been passed a fully formed output format then
# resolve it by looking at the yaml
if (!is_output_format(output_format)) {
output_format <- output_format_from_yaml_front_matter(input_lines,
output_options,
output_format)
output_format <- create_output_format(output_format$name,
output_format$options)
}
pandoc_to <- output_format$pandoc$to
# determine whether we need to run citeproc (based on whether we
# have references in the input)
run_citeproc <- citeproc_required(yaml_front_matter, input_lines)
# generate outpout file based on input filename
if (is.null(output_file))
output_file <- pandoc_output_file(input, output_format$pandoc)
# if an output_dir was specified then concatenate it with the output file
if (!is.null(output_dir)) {
if (!file.exists(output_dir))
dir.create(output_dir)
output_file <- file.path(output_dir, basename(output_file))
}
output_dir <- dirname(output_file)
# use output filename based files dir
files_dir <-file.path(output_dir, knitr_files_dir(basename(output_file)))
# default to no cache_dir knit_meta (some may be generated by the knit)
cache_dir <- NULL
knit_meta <- NULL
# knit if necessary
if (tolower(tools::file_ext(input)) %in% c("r", "rmd", "rmarkdown")) {
# restore options and hooks after knit
optk <- knitr::opts_knit$get()
on.exit(knitr::opts_knit$restore(optk), add = TRUE)
optc <- knitr::opts_chunk$get()
on.exit(knitr::opts_chunk$restore(optc), add = TRUE)
hooks <- knitr::knit_hooks$get()
on.exit(knitr::knit_hooks$restore(hooks), add = TRUE)
# reset knit_meta (and ensure it's always reset before exiting render)
knit_meta_reset()
on.exit(knit_meta_reset(), add = TRUE)
# default rendering and chunk options
knitr::render_markdown()
knitr::opts_chunk$set(tidy = FALSE, error = FALSE)
# enable knitr hooks to have knowledge of the final output format
knitr::opts_knit$set(rmarkdown.pandoc.to = pandoc_to)
knitr::opts_knit$set(rmarkdown.keep_md = output_format$keep_md)
knitr::opts_knit$set(rmarkdown.version = 2)
# trim whitespace from around source code
if (packageVersion("knitr") < "1.5.23") {
local({
hook_source = knitr::knit_hooks$get('source')
knitr::knit_hooks$set(source = function(x, options) {
hook_source(strip_white(x), options)
})
})
}
# use filename based figure and cache directories
figures_dir <- paste(files_dir, "/figure-", pandoc_to, "/", sep = "")
knitr::opts_chunk$set(fig.path=figures_dir)
cache_dir <-knitr_cache_dir(input, pandoc_to)
knitr::opts_chunk$set(cache.path=cache_dir)
# merge user options and hooks
if (!is.null(output_format$knitr)) {
knitr::opts_knit$set(as.list(output_format$knitr$opts_knit))
knitr::opts_chunk$set(as.list(output_format$knitr$opts_chunk))
knitr::knit_hooks$set(as.list(output_format$knitr$knit_hooks))
}
# presume that we're rendering as a static document unless specified
# otherwise in the parameters
runtime <- match.arg(runtime)
if (identical(runtime, "auto")) {
if (!is.null(yaml_front_matter$runtime))
runtime <- yaml_front_matter$runtime
else
runtime <- "static"
}
knitr::opts_knit$set(rmarkdown.runtime = runtime)
# make the yaml_front_matter available as 'metadata' within the
# knit environment (unless it is already defined there in which case
# we emit a warning)
env <- environment(render)
unlockBinding("metadata", env)
on.exit({
env$metadata <- list()
lockBinding("metadata", env)
}, add = TRUE)
env$metadata <- yaml_front_matter
perf_timer_start("knitr")
# perform the knit
input <- knitr::knit(knit_input,
knit_output,
envir = envir,
quiet = quiet,
encoding = encoding)
perf_timer_stop("knitr")
# pull any R Markdown warnings from knit_meta and emit
rmd_warnings <- knit_meta_reset(class = "rmd_warning")
for (rmd_warning in rmd_warnings) {
message("Warning: ", rmd_warning)
}
# collect remaining knit_meta
knit_meta <- knit_meta_reset()
# if this isn't html and there are html dependencies then flag an error
if (!(is_pandoc_to_html(output_format$pandoc) ||
identical(tolower(tools::file_ext(output_file)), "html"))) {
if (has_html_dependencies(knit_meta)) {
stop("Functions that produce HTML output found in document targeting ",
pandoc_to, " output.\nPlease change the output type ",
"of this document to HTML.", call. = FALSE)
}
if (!identical(runtime, "static")) {
stop("Runtime '", runtime, "' is not supported for ",
pandoc_to, " output.\nPlease change the output type ",
"of this document to HTML.", call. = FALSE)
}
}
}
# clean the files_dir if we've either been asking to clean supporting files or
# the knitr cache is active
if (output_format$clean_supporting && (is.null(cache_dir) || !file.exists(cache_dir)))
intermediates <- c(intermediates, files_dir)
# read the input text as UTF-8 then write it back out
input_text <- read_lines_utf8(input, encoding)
writeLines(input_text, utf8_input, useBytes = TRUE)
perf_timer_start("pre-processor")
# call any pre_processor
if (!is.null(output_format$pre_processor)) {
extra_args <- output_format$pre_processor(yaml_front_matter,
utf8_input,
runtime,
knit_meta,
files_dir,
output_dir)
output_format$pandoc$args <- c(output_format$pandoc$args, extra_args)
}
perf_timer_stop("pre-processor")
# if we are running citeproc then explicitly forward the bibliography
# on the command line (works around pandoc-citeproc issue whereby yaml
# strings that begin with numbers are interpreted as numbers)
if (!is.null(yaml_front_matter$bibliography)) {
output_format$pandoc$args <- c(output_format$pandoc$args,
rbind("--bibliography", pandoc_path_arg(yaml_front_matter$bibliography)))
}
perf_timer_start("pandoc")
# run intermediate conversion if it's been specified
if (output_format$pandoc$keep_tex) {
pandoc_convert(utf8_input,
pandoc_to,
output_format$pandoc$from,
file_with_ext(output_file, "tex"),
run_citeproc,
output_format$pandoc$args,
!quiet)
}
# run the main conversion
pandoc_convert(utf8_input,
pandoc_to,
output_format$pandoc$from,
output_file,
run_citeproc,
output_format$pandoc$args,
!quiet)
perf_timer_stop("pandoc")
perf_timer_start("post-processor")
# if there is a post-processor then call it
if (!is.null(output_format$post_processor))
output_file <- output_format$post_processor(yaml_front_matter,
utf8_input,
output_file,
clean,
!quiet)
if (!quiet)
message("\nOutput created: ", output_file)
perf_timer_stop("post-processor")
perf_timer_stop("render")
# write markdown output if requested
if (output_format$keep_md && !md_input) {
md <- c(md_header_from_front_matter(yaml_front_matter),
partition_yaml_front_matter(input_text)$body)
writeLines(md, file_with_ext(output_file, "md"), useBytes = TRUE)
}
# return the full path to the output file
invisible(tools::file_path_as_absolute(output_file))
}
#' Render supporting files for an input document
#'
#' Render (copy) required supporting files for an input document to the _files
#' directory associated with the document.
#'
#' @param from Directory to copy from
#' @param files_dir Directory to copy files into
#' @param rename_to Optional rename of source directory after it is copied
#'
#' @return The relative path to the supporting files. This path is suitable
#' for inclusion in HTML\code{href} and \code{src} attributes.
#'
#' @export
render_supporting_files <- function(from, files_dir, rename_to = NULL) {
# auto-create directory for supporting files
if (!file.exists(files_dir))
dir.create(files_dir)
# target directory is based on the dirname of the path or the rename_to
# value if it was provided
target_stage_dir <- file.path(files_dir, basename(from))
target_dir <- file.path(files_dir, ifelse(is.null(rename_to),
basename(from),
rename_to))
# copy the directory if it hasn't already been copied
if (!file.exists(target_dir) && !file.exists(target_stage_dir)) {
file.copy(from = from,
to = files_dir,
recursive = TRUE)
if (!is.null(rename_to)) {
file.rename(from = target_stage_dir,
to = target_dir)
}
}
# return the target dir (used to form links in the HTML)
target_dir
}
# reset knitr meta output (returns any meta output generated since the last
# call to knit_meta_reset), optionally scoped to a specific output class
knit_meta_reset <- function(class = NULL) {
if (packageVersion("knitr") >= "1.5.26")
knitr::knit_meta(class, clean = TRUE)
else
NULL
}
md_header_from_front_matter <- function(front_matter) {
md <- c()
if (!is.null(front_matter$title))
md <- c(md, paste("# ", front_matter$title, sep = ""))
if (is.character(front_matter$author)) {
authors <- paste(front_matter$author, " ", sep = "")
md <- c(md, authors)
}
if (!is.null(front_matter$date))
md <- c(md, paste(front_matter$date, " ", sep = ""))
md
}