forked from rstudio/bookdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite.R
74 lines (62 loc) · 2.24 KB
/
site.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
#' R Markdown site generator for bookdown
#'
#' Implementation of custom R Markdown site generator for bookdown.
#'
#' @inheritParams rmarkdown::render_site
#'
#' @export
bookdown_site = function(input, ...) {
on.exit(opts$restore(), add = TRUE)
# need to switch to the input directory for computing the config
oldwd = setwd(input)
on.exit(setwd(oldwd), add = TRUE)
# load the config for the input directory
config = load_config()
# get the name from the config (default to the directory name
# if there is no name in the config)
name = find_book_name(config, basename(normalizePath(input)))
# get the book dir from the config
book_dir = find_book_dir(config)
# Custom render function. bookdown rendering is more complex than
# for most formats and as a result uses a custom R script (_render.R)
# or Makefile to define what's required to render the book.
render = function(input_file, output_format, envir, quiet, encoding, ...) {
# input_file indicates that caller (likely the IDE) would like to
# build a single file of the website only
if (is.null(input_file)) {
in_dir(input, render_book_script(output_format, envir, quiet))
} else {
render_book(input_file, output_format, envir = envir, preview = TRUE)
}
}
clean = function() {
suppressMessages(clean_book(clean = FALSE))
}
# return site generator
list(
name = name,
output_dir = book_dir,
render = render,
clean = clean
)
}
# render the book via _render.R or Makefile, or fallback to render_book()
render_book_script = function(output_format = NULL, envir = globalenv(), quiet = TRUE) {
result = 0
if (length(script <- existing_r('_render', TRUE))) {
result = Rscript(c(if (quiet) '--quiet', script, shQuote(output_format)))
} else if (file.exists('Makefile')) {
result = system2('make')
} else {
render_book('index.Rmd', output_format = output_format, envir = envir)
}
if (result != 0) stop('Error ', result, ' attempting to render book')
}
find_book_dir = function(config) {
d = output_dirname(NULL, config, create = FALSE)
if (is.null(d)) '.' else d
}
find_book_name = function(config, default) {
name = with_ext(book_filename(config, fallback = FALSE), '')
if (is.null(name)) default else name
}