Skip to content

Commit 129cf50

Browse files
committed
support custom build functions for build_site()
1 parent d26e72f commit 129cf50

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

R/render.R

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#' option \code{getOption('blogdown.method')} when it is set.
2929
#' @param run_hugo Whether to run \code{hugo_build()} after R Markdown files are
3030
#' compiled.
31+
#' @param build_fun Function used to build the Rmd files.
3132
#' @note This function recompiles all R Markdown files by default, even if the
3233
#' output files are newer than the source files. If you want to build the site
3334
#' without rebuilding all R Markdown files, you should use
3435
#' \code{\link{hugo_build}()} instead.
3536
#' @export
3637
build_site = function(
37-
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE
38+
local = FALSE, method = c('html', 'custom'), run_hugo = TRUE,
39+
build_fun = getOption('blogdown.build_rmds', build_rmds)
3840
) {
3941
if (missing(method)) method = getOption('blogdown.method', method)
4042
method = match.arg(method)
@@ -44,7 +46,7 @@ build_site = function(
4446
if (local && length(files)) {
4547
files = getOption('blogdown.files_filter', timestamp_filter)(files)
4648
}
47-
build_rmds(files)
49+
build_fun(files)
4850
if (run_hugo) on.exit(hugo_build(local), add = TRUE)
4951
invisible()
5052
}
@@ -163,3 +165,18 @@ encode_paths = function(x, deps, parent, base = '/', to_md = FALSE) {
163165
dirs_rename(libs, to, clean = TRUE)
164166
x
165167
}
168+
169+
build_rmds_parallel = function(files) {
170+
on.exit(if (exists("cl")) parallel::stopCluster(cl), add = TRUE)
171+
no_cores = getOption('blogdown.parallelcores', parallel::detectCores())
172+
# revert to legacy where parallelization irrelevant
173+
valid_parallel = isTRUE(as.integer(no_cores) > 1L) && length(files) > 1L
174+
if (!valid_parallel) build_rmds(files)
175+
cl = parallel::makeCluster(no_cores)
176+
# propagate blogdown-relevant options to the nodes
177+
opts = options()
178+
blogdown_opts = opts[grepl('^blogdown\\.', names(opts))]
179+
if (length(blogdown_opts) > 0L) parallel::clusterCall(cl, options, blogdown_opts)
180+
files = parallel::clusterSplit(cl = cl, seq = files)
181+
parallel::parLapply(cl = cl, X = files, fun = build_rmds)
182+
}

man/build_site.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)