Skip to content

Commit

Permalink
support custom build functions for build_site()
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefhajnala committed Jun 14, 2019
1 parent d26e72f commit 102e98f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 23 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
#' option \code{getOption('blogdown.method')} when it is set.
#' @param run_hugo Whether to run \code{hugo_build()} after R Markdown files are
#' compiled.
#' @param build_fun Function used to build the Rmd files.
#' @note This function recompiles all R Markdown files by default, even if the
#' output files are newer than the source files. If you want to build the site
#' without rebuilding all R Markdown files, you should use
#' \code{\link{hugo_build}()} instead.
#' @export
build_site = function(
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE,
build_fun = getOption('blogdown.build_rmds', build_rmds)
) {
if (missing(method)) method = getOption('blogdown.method', method)
method = match.arg(method)
Expand All @@ -44,7 +46,7 @@ build_site = function(
if (local && length(files)) {
files = getOption('blogdown.files_filter', timestamp_filter)(files)
}
build_rmds(files)
build_fun(files)
if (run_hugo) on.exit(hugo_build(local), add = TRUE)
invisible()
}
Expand Down Expand Up @@ -163,3 +165,22 @@ encode_paths = function(x, deps, parent, base = '/', to_md = FALSE) {
dirs_rename(libs, to, clean = TRUE)
x
}

build_rmds_parallel = function(
files,
no_cores = getOption('blogdown.parallelcores', parallel::detectCores())
) {
on.exit(if (exists("cl")) parallel::stopCluster(cl), add = TRUE)
# revert to legacy where parallelization irrelevant
valid_parallel = isTRUE(as.integer(no_cores) > 1L) &&
length(files) > 1L &&
requireNamespace("parallel", quietly = TRUE)
if (!valid_parallel) build_rmds(files)
cl = parallel::makeCluster(no_cores)
# propagate blogdown-relevant options to the nodes
opts = options()
blogdown_opts = opts[grepl('^blogdown\\.', names(opts))]
if (length(blogdown_opts) > 0L) parallel::clusterCall(cl, options, blogdown_opts)
files = parallel::clusterSplit(cl = cl, seq = files)
parallel::parLapply(cl = cl, X = files, fun = build_rmds)
}
5 changes: 4 additions & 1 deletion man/build_site.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 102e98f

Please sign in to comment.