From 99952580dedc6d07283fe4cd9523c32b96431d98 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Fri, 22 Feb 2019 13:35:19 -0600 Subject: [PATCH] simplify load_config() a little bit --- R/utils.R | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/R/utils.R b/R/utils.R index 9bcf12a0..a18d0cf2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -141,24 +141,15 @@ opts = knitr:::new_defaults() # read config file and cache the options (i.e. do not read again unless the config is newer) load_config = function() { config = opts$get('config') - + owd = setwd(site_root()); on.exit(setwd(owd), add = TRUE) + f = find_config(); m = file.info(f)[, 'mtime'] # read config only if it has been updated - read_config = function(f, parser) { - if (!is.null(time <- attr(config, 'config_time')) && - time == file.info(f)[, 'mtime']) return(config) - config = parser(f) - attr(config, 'config_time') = file.info(f)[, 'mtime'] - opts$set(config = config) - check_config(config, f) - } - - find_config() - - if (file_exists('config.toml')) - return(read_config('config.toml', parse_toml)) - - if (file_exists('config.yaml')) - return(read_config('config.yaml', yaml_load_file)) + if (identical(attr(config, 'config_time'), m)) return(config) + parser = switch(f, 'config.toml' = parse_toml, 'config.yaml' = yaml_load_file) + config = parser(f) + attr(config, 'config_time') = m + opts$set(config = config) + check_config(config, f) } # check if the user has configured Multilingual Mode for Hugo in config.toml