From 9cc2f08bb79be8574b359c5ea6ba2432458495ec Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 16 Sep 2022 13:37:49 -0500 Subject: [PATCH] yet another fix for the academic theme (search for all modules in all config files and download them if missing) --- DESCRIPTION | 2 +- R/hugo.R | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9e7cdede..e82ec3ef 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: blogdown Title: Create Blogs and Websites with R Markdown -Version: 1.12.3 +Version: 1.12.4 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Christophe", "Dervieux", role = "aut", email = "cderv@rstudio.com", comment = c(ORCID = "0000-0003-4474-2498")), diff --git a/R/hugo.R b/R/hugo.R index 822bcda1..144b11d9 100644 --- a/R/hugo.R +++ b/R/hugo.R @@ -95,11 +95,15 @@ archetypes = function() { paste0(basename(d), ifelse(utils::file_test('-d', d), '/', '')) } -module_paths = function() { - x = lapply(load_config()[['module']][['imports']], function(x) { - if (is.character(p <- x[['path']])) p +module_paths = function(dir = '.') { + fs = unique(basename(config_files())) + rx = paste0('^', paste(gsub('[.]', '[.]', fs), collapse = '|'), '$') + fs = list_files(dir, rx) + ps = lapply(fs, function(f) { + x = read_config(f)[['module']][['imports']] + lapply(x, function(v) if (is.character(p <- v[['path']])) p) }) - as.character(unlist(x)) + as.character(unlist(ps)) } change_config = function(name, value) { @@ -437,15 +441,8 @@ install_theme = function( # remove the themesDir setting; it is unlikely that you need it change_config('themesDir', NA) # read module:imports:path from config - mods = module_paths() }) - mods = mods[!dir_exists(mods)] - # in case any modules are not downloaded, try to download them - if (length(mods)) { - tmpmod = tempfile(fileext = '.mod') - write_utf8(paste(mods, 'v1'), tmpmod) # dirty hack - download_modules(tmpmod) - } + check_modules('..') }) if (is_theme) if (update_config) { change_config('theme', sprintf('"%s"', theme)) @@ -490,6 +487,26 @@ download_modules = function(mod) { unlink(with_ext(mod, c('.mod', '.sum'))) } +# make sure all modules specified in config files are downloaded +check_modules = function(dir = '.') { + detect = function() { + p = module_paths(dir) + p[!dir_exists(p)] + } + m1 = NULL + # in case any modules are not downloaded, try to download them + while (length(m2 <- detect())) { + if (identical(m1, m2)) { + warning('Failed to download modues (the site may not work):\n', paste(' ', m2, collapse = '\n')) + break + } + f = tempfile(fileext = '.mod') + write_utf8(paste(m2, 'v1'), f) # dirty hack + download_modules(f) + m1 = m2 + } +} + # themes may use config/_default/config.toml, e.g. hugo-academic; in that case, # we remove the config file under the root dir remove_config = function() {