Skip to content

Commit

Permalink
simplify load_config() a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Feb 22, 2019
1 parent 4fb9bfd commit 9995258
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9995258

Please sign in to comment.