From 129cf50a046d0800f8c5ba2d92d1b90dc7c888ed Mon Sep 17 00:00:00 2001 From: Jozef Date: Fri, 14 Jun 2019 20:12:14 +0000 Subject: [PATCH] support custom build functions for build_site() --- R/render.R | 21 +++++++++++++++++++-- man/build_site.Rd | 5 ++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/R/render.R b/R/render.R index ec9c486d..36316943 100644 --- a/R/render.R +++ b/R/render.R @@ -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) @@ -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() } @@ -163,3 +165,18 @@ encode_paths = function(x, deps, parent, base = '/', to_md = FALSE) { dirs_rename(libs, to, clean = TRUE) x } + +build_rmds_parallel = function(files) { + on.exit(if (exists("cl")) parallel::stopCluster(cl), add = TRUE) + no_cores = getOption('blogdown.parallelcores', parallel::detectCores()) + # revert to legacy where parallelization irrelevant + valid_parallel = isTRUE(as.integer(no_cores) > 1L) && length(files) > 1L + 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) +} diff --git a/man/build_site.Rd b/man/build_site.Rd index 80bb5f27..d884f682 100644 --- a/man/build_site.Rd +++ b/man/build_site.Rd @@ -4,7 +4,8 @@ \alias{build_site} \title{Build a website} \usage{ -build_site(local = FALSE, method = c("html", "custom"), run_hugo = TRUE) +build_site(local = FALSE, method = c("html", "custom"), run_hugo = TRUE, + build_fun = getOption("blogdown.build_rmds", build_rmds)) } \arguments{ \item{local}{Whether to build the website locally to be served via @@ -16,6 +17,8 @@ option \code{getOption('blogdown.method')} when it is set.} \item{run_hugo}{Whether to run \code{hugo_build()} after R Markdown files are compiled.} + +\item{build_fun}{Function used to build the Rmd files.} } \description{ Compile all Rmd files and build the site through Hugo.