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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
#' Render multiple documents as a website
#'
#' Render all of the R Markdown documents within a directory as a website.
#'
#' The \code{render_site} function enables you to render a collection of
#' markdown documents within a directory as a website. There are two
#' requirements for a directory to be rendered as a website:
#' \enumerate{
#' \item{It must contain either an "index.Rmd" or "index.md" file.}
#' \item{It must contain a site configuration file ("_site.yml").}
#' }
#'
#' The most minimal valid website is an empty "index.Rmd" and an empty
#' "_site.yml". With this configuration a single empty webpage would be
#' generated via a call to \code{render_site}. If you add additional markdown
#' documents to the directory they will also be rendered. By default a site is
#' rendered in the following fashion:
#'
#' \enumerate{
#' \item{R Markdown (.Rmd) and plain markdown (.md) files in the root
#' directory are rendered. Note however that markdown files beginning with "_"
#' are not rendered (this is a convention to designate files that are included
#' by top level documents).}
#' \item{All output and supporting files are copied to a "_site" subdirectory
#' of the website directory (this is configurable, see discussion below).}
#' \item{The following files are \bold{not} copied to the "_site"
#' sub-directory:
#' \itemize{
#' \item{Files beginning with "." (hidden files).}
#' \item{Files beginning with "_"}
#' \item{Files known to contain R source code (e.g. ".R", ".s", ".Rmd"), R
#' data (e.g. ".RData", ".rds"), or configuration data (e.g. ".Rproj",
#' "rsconnect")).}
#' }
#' Note that you can override which files are included or excluded via
#' settings in "_site.yml" (described below).}
#' \item{Normally R Markdown renders documents as self-contained HTML.
#' However, \code{render_site} ensures that dependencies (e.g. CSS,
#' JavaScript, images, etc.) remain in external files. CSS/JavaScript
#' libraries are copied to a "site_libs" sub-directory and plots/images are
#' copied to "_files" sub-directories.}
#' }
#'
#' You can remove the files generated by \code{render_site} using the
#' \code{clean_site} function.
#'
#' @section Configuration:
#' A "_site.yml" file can be used to configure the behavior of site generation.
#' Here is an example configuration file:
#'
#' \preformatted{
#' name: my-website
#' output_dir: _site
#' include: ["demo.R"]
#' exclude: ["docs.txt", "*.csv"]
#' navbar:
#' title: "My Website"
#' left:
#' - text: "Home"
#' href: index.html
#' - text: "About"
#' href: about.html
#' output:
#' html_document:
#' toc: true
#' highlight: textmate
#' }
#'
#' The \code{name} field provides a suggested URL path for your website when it
#' is published (by default this is just the name of the directory containing
#' the site). The \code{output_dir} indicates which directory to copy site
#' content into ("_site" is the default if none is specified). Note that this
#' can be "." to keep all content within the root website directory alongside
#' the source code.
#'
#' The \code{include} and \code{exclude} fields enable you to override the
#' default behavior vis-a-vis what files are copied into the "_site" directory
#' (wildcards can be used as in the above example).
#'
#' The \code{navbar} field can be used to define a navigation bar for websites
#' based on the \code{\link{html_document}} format.
#'
#' Finally, the \code{output} field enables you to specify output options that
#' are common to all documents within the website (you can also still provide
#' local options within each document that override any common options).
#'
#' \code{new_session: true} causes each file to be rendered in a new R session.
#' This prevents the masking problem that arises when different files use
#' functions from different packages (namespaces) that share a common name, such
#' as \code{here::here} and \code{lubridate::here} or \code{dplyr::filter} and
#' \code{MASS::filter}. The default behaviour of \code{render_site} is to use a
#' common R session.
#'
#' @section Custom Site Generation:
#' The behavior of the default site generation function
#' (\code{rmarkdown::default_site}) is described above. It is also possible to
#' define a custom site generator that has alternate behavior. A site generator
#' is an R function that is bound to by including it in the "site:" field of the
#' "index.Rmd" or "index.md" file. For example:
#'
#' \preformatted{
#' title: "My Book"
#' output: bookdown::gitbook
#' site: bookdown::bookdown_site
#' }
#'
#' A site generation function should return a list with the following elements:
#' \itemize{
#' \item{\code{name}} {The name for the website (e.g. the parent directory
#' name).}
#' \item{\code{output_dir} {The directory where the website output is written
#' to. This path should be relative to the site directory (e.g. "." or
#' "_site")}}
#' \item{\code{render}} {An R function that can be called to generate the
#' site. The function should accept the \code{input_file},
#' \code{output_format}, \code{envir}, \code{quiet}, and \code{encoding}
#' arguments.}
#' \item{\code{clean}} {An R function that returns relative paths to the files
#' generated by \code{render_site} (these files are the ones which will be
#' removed by the \code{clean_site} function.}
#' }
#'
#' Note that the \code{input_file} argument will be \code{NULL} when the entire
#' site is being generated. It will be set to a specific file name if a
#' front-end tool is attempting to preview it (e.g. RStudio IDE via the Knit
#' button).
#'
#' When \code{quiet = FALSE} the \code{render} function should also print a line
#' of output using the \code{\link{message}} function indicating which output
#' file should be previewed, for example:
#'
#' \preformatted{if (!quiet)
#' message("\nOutput created: ", output)
#' }
#'
#' Emitting this line enables front-ends like RStudio to determine which file
#' they should open to preview the website.
#'
#' See the source code of the \code{rmarkdown::default_site} function for a
#' example of a site generation function.
#'
#' @param input Website directory (or the name of a file within the directory).
#' @param output_format R Markdown format to convert to (defaults to "all").
#' @param envir The environment in which the code chunks are to be evaluated
#' during knitting (can use \code{\link{new.env}} to guarantee an empty new
#' environment).
#' @param quiet \code{TRUE} to suppress messages and other output.
#' @param encoding The encoding of the input file; see \code{\link{file}}.
#'
#' @return \code{render_site} returns the name of the site output file (relative
#' to the input directory). \code{clean_site} returns the names of the
#' generated files removed during cleaning. \code{site_config} returns the
#' contents of _site.yml as an R list. \code{default_site_generator} returns
#' the default site generator for R Markdown websites.
#' @export
render_site <- function(input = ".",
output_format = "all",
envir = parent.frame(),
quiet = FALSE,
encoding = getOption("encoding")) {
# capture original input
original_input <- input
# normalize to a directory
input <- input_as_dir(input)
# if it's a file then capture that and force output_format to be NULL
# (to only render a single format for incremental/previewing)
input_file <- NULL
if (!dir_exists(original_input)) {
input_file <- original_input
if (output_format == "all")
output_format <- NULL
}
# find the site generator
generator <- site_generator(input, output_format, encoding)
if (is.null(generator))
stop("No site generator found.")
# execute it
generator$render(input_file = input_file,
output_format = output_format,
envir = envir,
quiet = quiet,
encoding = encoding)
# compute the name of the output file. if the input was a filename
# use that as a base (otherwise use index.html)
if (!dir_exists(original_input))
output <- file_with_ext(basename(original_input), "html")
else
output <- "index.html"
output <- file.path(input, generator$output_dir, output)
output <- normalized_relative_to(input, output)
# return it invisibly
invisible(output)
}
#' @rdname render_site
#' @param preview Whether to list the files to be removed rather than actually
#' removing them.
#' @export
clean_site <- function(input = ".", preview = FALSE, quiet = FALSE,
encoding = getOption("encoding")) {
# normalize to a directory
input <- input_as_dir(input)
# find the site generator
generator <- site_generator(input = input,
output_format = NULL,
encoding = encoding)
if (is.null(generator))
stop("No site generator found.")
# get the files to be cleaned
files <- generator$clean()
# if it's just a preview then return the files, otherwise
# actually remove the files
if (preview)
files
else {
if (!quiet) {
cat("Removing files: \n")
cat(paste0(" ", files), sep = "\n")
}
unlink(file.path(input, files), recursive = TRUE)
}
}
#' @rdname render_site
#' @export
site_generator <- function(input = ".",
output_format = NULL,
encoding = getOption("encoding")) {
# normalize input
input <- input_as_dir(input)
# if we have an index.Rmd (or .md) then check it's yaml for "site:"
index <- file.path(input, "index.Rmd")
if (!file.exists(index))
index <- file.path(input, "index.md")
if (file.exists(index)) {
# read index.Rmd and extract the front matter
index_lines <- read_utf8(index, encoding)
front_matter <- parse_yaml_front_matter(index_lines)
# is there a custom site generator function?
if (!is.null(front_matter$site)) {
create_site_generator <- eval(parse(text = front_matter$site))
create_site_generator(input, encoding)
# is there a "_site.yml"?
} else if (file.exists(site_config_file(input))) {
default_site(input, encoding)
# no custom site generator or "_site.yml"
} else {
NULL
}
# no index.Rmd or index.md
} else {
NULL
}
}
#' @rdname render_site
#' @export
site_config <- function(input = ".", encoding = getOption("encoding")) {
# normalize input
input <- input_as_dir(input)
# check for config file
config_file <- site_config_file(input)
if (file.exists(config_file)) {
# parse the yaml
config_lines <- read_utf8(config_file, encoding)
config <- yaml_load(config_lines)
if (!is.list(config))
config <- list()
# provide defaults if necessary
if (is.null(config$name))
config$name <- basename(normalize_path(input))
if (is.null(config$output_dir))
config$output_dir <- "_site"
if (is.null(config$new_session))
config$new_session <- FALSE
# return config
config
# no _site.yml
} else {
NULL
}
}
#' @rdname render_site
#' @param ... Currently unused.
#' @export
default_site_generator <- function(input, encoding = getOption("encoding"), ...) {
default_site(input, encoding, ...)
}
# default site implementation (can be overridden by custom site generators)
default_site <- function(input, encoding = getOption("encoding"), ...) {
# get the site config
config <- site_config(input, encoding)
if (is.null(config))
stop("No site configuration (_site.yml) file found.")
# helper function to get all input files. includes all .Rmd and
# .md files that don't start with "_" (note that we don't do this
# recursively because rmarkdown in general handles applying common
# options/elements across subdirectories poorly)
input_files <- function() {
list.files(input, pattern = "^[^_].*\\.[Rr]?md$")
}
# define render function (use ... to gracefully handle future args)
render <- function(input_file,
output_format,
envir,
quiet,
encoding, ...) {
# track outputs
outputs <- c()
# see if this is an incremental render
incremental <- !is.null(input_file)
# files list is either a single file (for incremental) or all
# file within the input directory
if (incremental)
files <- input_file
else {
files <- file.path(input, input_files())
}
sapply(files, function(x) {
render_one <- if (isTRUE(config$new_session)) {
render_new_session
} else {
render_current_session
}
output <- render_one(input = x,
output_format = output_format,
output_options = list(lib_dir = "site_libs",
self_contained = FALSE),
envir = envir,
quiet = quiet,
encoding = encoding)
# add to global list of outputs
outputs <<- c(outputs, output)
# check for files dir and add that as well
sidecar_files_dir <- knitr_files_dir(output)
files_dir_info <- file.info(sidecar_files_dir)
if (isTRUE(files_dir_info$isdir))
outputs <<- c(outputs, sidecar_files_dir)
})
# do we have a relative output directory? if so then remove,
# recreate, and copy outputs to it (we don't however remove
# it for incremental builds)
if (config$output_dir != '.') {
# remove and recreate output dir if necessary
output_dir <- file.path(input, config$output_dir)
if (file.exists(output_dir)) {
if (!incremental) {
unlink(output_dir, recursive = TRUE)
dir.create(output_dir)
}
} else {
dir.create(output_dir)
}
# move outputs
for (output in outputs) {
# don't move it if it's a _files dir that has a _cache dir
if (grepl("^.*_files$", output)) {
cache_dir <- gsub("_files$", "_cache", output)
if (dir_exists(cache_dir))
next;
}
output_dest <- file.path(output_dir, basename(output))
if (dir_exists(output_dest))
unlink(output_dest, recursive = TRUE)
file.rename(output, output_dest)
}
# copy lib dir a directory at a time (allows it to work with incremental)
lib_dir <- file.path(input, "site_libs")
output_lib_dir <- file.path(output_dir, "site_libs")
if (!file.exists(output_lib_dir))
dir.create(output_lib_dir)
libs <- list.files(lib_dir)
for (lib in libs)
file.copy(file.path(lib_dir, lib), output_lib_dir, recursive = TRUE)
unlink(lib_dir, recursive = TRUE)
# copy other files
copy_site_resources(input, encoding)
}
# Print output created for rstudio preview
if (!quiet) {
# determine output file
output_file <- ifelse(is.null(input_file),
"index.html",
file_with_ext(basename(input_file), "html"))
if (config$output_dir != ".")
output_file <- file.path(config$output_dir, output_file)
message("\nOutput created: ", output_file)
}
}
# define clean function
clean <- function() {
# build list of generated files
generated <- c()
# enumerate rendered markdown files
files <- input_files()
# get html files
html_files <- file_with_ext(files, "html")
# _files peers are always removed (they could be here due to
# output_dir == "." or due to a _cache existing for the page)
html_supporting <- paste0(knitr_files_dir(html_files), '/')
generated <- c(generated, html_supporting)
# _cache peers are always removed
html_cache <- paste0(knitr_root_cache_dir(html_files), '/')
generated <- c(generated, html_cache)
# for rendering in the current directory we need to eliminate
# output files for our inputs (including _files) and the lib dir
if (config$output_dir == ".") {
# .html peers
generated <- c(generated, html_files)
# site_libs dir
generated <- c(generated, "site_libs/")
# for an explicit output_dir just remove the directory
} else {
generated <- c(generated, paste0(config$output_dir, '/'))
}
# filter out by existence
generated[file.exists(file.path(input, generated))]
}
# return site generator
list(
name = config$name,
output_dir = config$output_dir,
render = render,
clean = clean
)
}
# we suppress messages during render so that "Output created" isn't emitted
# (which could result in RStudio previewing the wrong file)
render_current_session <- function(...) suppressMessages(rmarkdown::render(...))
render_new_session <- function(...) {
if (!requireNamespace("callr", quietly = TRUE)) {
stop("The callr package must be installed when `new_session: true`.")
}
callr::r(
function(...) { suppressMessages(rmarkdown::render(...)) },
args = list(...),
block_callback = function(x) cat(x)
)
}
# utility function to copy all files into the _site directory
copy_site_resources <- function(input, encoding = getOption("encoding")) {
# get the site config
config <- site_config(input, encoding)
if (config$output_dir != ".") {
# get the list of files
files <- copyable_site_resources(input = input,
config = config,
encoding = encoding)
# perform the copy
output_dir <- file.path(input, config$output_dir)
file.copy(from = file.path(input, files),
to = output_dir,
recursive = TRUE)
}
}
#' Determine website resource files for a directory
#'
#' Determine which files within a given directory should be copied in
#' order to serve a website from the directory. Attempts to automatically
#' exclude source, data, hidden, and other files not required to serve
#' website content.
#'
#' @inheritParams default_output_format
#'
#' @param site_dir Site directory to analyze
#' @param include Additional files to include (glob wildcards supported)
#' @param exclude Files to exclude (glob wildcards supported)
#' @param recursive \code{TRUE} to return a full recursive file listing;
#' \code{FALSE} to just provide top-level files and directories.
#'
#' @return Character vector of files and directories to copy
#'
#' @export
site_resources <- function(site_dir, include = NULL, exclude = NULL, recursive = FALSE) {
# get the original file list (we'll need it to apply includes)
all_files <- list.files(site_dir, all.files = TRUE)
# excludes:
# - known source/data extensions
# - anything that starts w/ '.' or '_'
# - rsconnect and packrat directories
# - user excludes
extensions <- c("R", "r", "S", "s",
"Rmd", "rmd", "md", "Rmarkdown", "rmarkdown",
"Rproj", "rproj",
"RData", "rdata", "rds")
extensions_regex <- utils::glob2rx(paste0("*.", extensions))
excludes <- c("^rsconnect$", "^packrat$", "^\\..*$", "^_.*$", "^.*_cache$",
extensions_regex,
utils::glob2rx(exclude))
files <- all_files
for (exclude in excludes)
files <- files[!grepl(exclude, files)]
# allow back in anything specified as an explicit "include"
includes <- utils::glob2rx(include)
for (include in includes) {
include_files <- all_files[grepl(include, all_files)]
files <- unique(c(files, include_files))
}
# if this is recursive then we need to blow out the directories
if (recursive) {
recursive_files <- c()
for (file in files) {
file_path <- file.path(site_dir, file)
if (dir_exists(file_path)) {
dir_files <- file.path(list.files(file_path,
full.names = FALSE,
recursive = TRUE))
dir_files <- file.path(file, dir_files)
recursive_files <- c(recursive_files, dir_files)
} else {
recursive_files <- c(recursive_files, file)
}
}
recursive_files
} else {
files
}
}
# utility function to list the files that should be copied
copyable_site_resources <- function(input,
config = site_config(input, encoding),
encoding = getOption("encoding")) {
include <- config$include
exclude <- config$exclude
if (config$output_dir != ".")
exclude <- c(exclude, config$output_dir)
site_resources(input, include, exclude)
}
# utility function to ensure that 'input' is a valid directory
# (converts from file to parent directory as necessary)
input_as_dir <- function(input) {
# ensure the input dir exists
if (!file.exists(input)) {
stop("The specified directory '", normalize_path(input, mustWork = FALSE),
"' does not exist.", call. = FALSE)
}
# convert from file to directory if necessary
if (!dir_exists(input))
input <- dirname(input)
# return it
input
}
# get the path to the site config file
site_config_file <- function(input) {
file.path(input, "_site.yml")
}
site_skeleton <- function(path, ...) {
dir.create(path, recursive = TRUE, showWarnings = FALSE)
file.copy(
list.files(rmarkdown_system_file('rmd', 'site'), full.names = TRUE),
path, overwrite = TRUE
)
}
|