class Jekyll::Configuration

def add_default_collections

def add_default_collections
  config = clone
  # It defaults to `{}`, so this is only if someone sets it to null manually.
  return config if config["collections"].nil?
  # Ensure we have a hash.
  if config["collections"].is_a?(Array)
    config["collections"] = config["collections"].each_with_object({}) do |collection, hash|
      hash[collection] = {}
    end
  end
  config["collections"] = Utils.deep_merge_hashes(
    { "posts" => {} }, config["collections"]
  ).tap do |collections|
    collections["posts"]["output"] = true
    if config["permalink"]
      collections["posts"]["permalink"] ||= style_to_permalink(config["permalink"])
    end
  end
  config
end

def add_default_excludes

def add_default_excludes
  config = clone
  return config if config["exclude"].nil?
  config["exclude"].concat(DEFAULT_EXCLUDES).uniq!
  config
end

def check_include_exclude(config)

def check_include_exclude(config)
  %w(include exclude).each do |option|
    next unless config.key?(option)
    next if config[option].is_a?(Array)
    raise Jekyll::Errors::InvalidConfigurationError,
          "'#{option}' should be set as an array, but was: #{config[option].inspect}."
  end
end

def check_plugins(config)

is not an Array.
Raises a Jekyll::Errors::InvalidConfigurationError if the config `plugins`

config - the config hash

Private: Checks if the `plugins` config is a String
def check_plugins(config)
  return unless config.key?("plugins")
  return if config["plugins"].is_a?(Array)
  Jekyll.logger.error "'plugins' should be set as an array of gem-names, but was: " \
                      "#{config["plugins"].inspect}. Use 'plugins_dir' instead to set " \
                      "the directory for your non-gemified Ruby plugins."
  raise Jekyll::Errors::InvalidConfigurationError,
        "'plugins' should be set as an array, but was: #{config["plugins"].inspect}."
end

def config_files(override)

Returns an Array of config files

override - the command-line options hash

Public: Generate list of configuration files from the override
def config_files(override)
  # Adjust verbosity quickly
  Jekyll.logger.adjust_verbosity(
    :quiet   => quiet?(override),
    :verbose => verbose?(override)
  )
  # Get configuration from <source>/_config.yml or <source>/<config_file>
  config_files = override["config"]
  if config_files.to_s.empty?
    default = %w(yml yaml toml).find(-> { "yml" }) do |ext|
      File.exist?(Jekyll.sanitized_path(source(override), "_config.#{ext}"))
    end
    config_files = Jekyll.sanitized_path(source(override), "_config.#{default}")
    @default_config_file = true
  end
  Array(config_files)
end

def csv_to_array(csv)

Returns an array of the values contained in the CSV

csv - the string of comma-separated values

Public: Split a CSV string into an array containing its values
def csv_to_array(csv)
  csv.split(",").map(&:strip)
end

def from(user_config)

Returns a Configuration filled with defaults.

user_config - a Hash or Configuration of overrides.

It takes the input, fills in the defaults where values do not exist.
Static: Produce a Configuration ready for use in a Site.
def from(user_config)
  Utils.deep_merge_hashes(DEFAULTS, Configuration[user_config].stringify_keys)
    .add_default_collections.add_default_excludes
end

def get_config_value_with_override(config_key, override)

def get_config_value_with_override(config_key, override)
  override[config_key] || self[config_key] || DEFAULTS[config_key]
end

def quiet(override = {})

def quiet(override = {})
  get_config_value_with_override("quiet", override)
end

def read_config_file(file)

Returns this configuration, overridden by the values in the file

file - the path to the YAML file to be read in

Public: Read configuration and return merged Hash
def read_config_file(file)
  file = File.expand_path(file)
  next_config = safe_load_file(file)
  unless next_config.is_a?(Hash)
    raise ArgumentError, "Configuration file: (INVALID) #{file}".yellow
  end
  Jekyll.logger.info "Configuration file:", file
  next_config
rescue SystemCallError
  if @default_config_file ||= nil
    Jekyll.logger.warn "Configuration file:", "none"
    {}
  else
    Jekyll.logger.error "Fatal:", "The configuration file '#{file}' could not be found."
    raise LoadError, "The Configuration file '#{file}' could not be found."
  end
end

def read_config_files(files)

configuration files
Returns the full configuration, with the defaults overridden by the values in the

files - the list of configuration file paths

Public: Read in a list of configuration files and merge with this hash
def read_config_files(files)
  configuration = clone
  begin
    files.each do |config_file|
      next if config_file.nil? || config_file.empty?
      new_config = read_config_file(config_file)
      configuration = Utils.deep_merge_hashes(configuration, new_config)
    end
  rescue ArgumentError => e
    Jekyll.logger.warn "WARNING:", "Error reading configuration. Using defaults (and options)."
    warn e
  end
  configuration.validate.add_default_collections
end

def safe_load_file(filename)

def safe_load_file(filename)
  case File.extname(filename)
  when %r!\.toml!i
    Jekyll::External.require_with_graceful_fail("tomlrb") unless defined?(Tomlrb)
    Tomlrb.load_file(filename)
  when %r!\.ya?ml!i
    SafeYAML.load_file(filename) || {}
  else
    raise ArgumentError,
          "No parser for '#{filename}' is available. Use a .y(a)ml or .toml file instead."
  end
end

def source(override)

Returns the path to the Jekyll source directory

override - the command-line options hash

Public: Directory of the Jekyll source folder
def source(override)
  get_config_value_with_override("source", override)
end

def stringify_keys

Return a copy of the hash where all its keys are strings

Public: Turn all keys into string
def stringify_keys
  each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v }
end

def style_to_permalink(permalink_style)

def style_to_permalink(permalink_style)
  STYLE_TO_PERMALINK[permalink_style.to_sym] || permalink_style.to_s
end

def validate

Returns the configuration Hash

Public: Ensure the proper options are set in the configuration
def validate
  config = clone
  check_plugins(config)
  check_include_exclude(config)
  config
end

def verbose(override = {})

def verbose(override = {})
  get_config_value_with_override("verbose", override)
end