class Bridgetown::Configuration

Holds the processed configuration loaded from the YAML config file.

@todo refactor this whole object! Already had to fix obscure

bugs just making minor changes, and all the indirection is
quite hard to decipher. -JW

Constants

CONFIG_FILE_EXTS
CONFIG_FILE_PREFIXES

The modern default config file name is bridgetown.config.EXT, but we also need to check for _config.EXT as a backward-compatibility nod to our progenitor

DEFAULTS

Default options. Overridden by values in bridgetown.config.yml. Strings rather than symbols are used for compatibility with YAML.

DEFAULT_EXCLUDES

Public Class Methods

from(user_config, starting_defaults = DEFAULTS) click to toggle source

Static: Produce a Configuration ready for use in a Site. It takes the input, fills in the defaults where values do not exist.

user_config - a Hash or Configuration of overrides.

Returns a Configuration filled with defaults.

# File lib/bridgetown-core/configuration.rb, line 103
def from(user_config, starting_defaults = DEFAULTS)
  Utils.deep_merge_hashes(starting_defaults.deep_dup, Configuration[user_config])
    .merge_environment_specific_options!
    .add_default_collections
    .add_default_excludes
    .check_include_exclude
end

Public Instance Methods

add_default_collections() click to toggle source
# File lib/bridgetown-core/configuration.rb, line 250
def add_default_collections # rubocop:todo all
  # It defaults to `{}`, so this is only if someone sets it to null manually.
  return self if self[:collections].nil?

  # Ensure we have a hash.
  if self[:collections].is_a?(Array)
    self[:collections] = self[:collections].each_with_object({}) do |collection, hash|
      hash[collection] = {}
    end
  end

  # Setup default collections
  self[:collections][:posts] = {} unless self[:collections][:posts]
  self[:collections][:posts][:output] = true
  self[:collections][:posts][:sort_direction] ||= "descending"

  if self[:content_engine] == "resource"
    self[:permalink] = "pretty" if self[:permalink].blank?
    self[:collections][:pages] = {} unless self[:collections][:pages]
    self[:collections][:pages][:output] = true
    self[:collections][:pages][:permalink] ||= "/:path/"

    self[:collections][:data] = {} unless self[:collections][:data]
    self[:collections][:data][:output] = false

    unless self[:collections][:posts][:permalink]
      self[:collections][:posts][:permalink] = self[:permalink]
    end
  else
    self[:permalink] = "date" if self[:permalink].blank?
    unless self[:collections][:posts][:permalink]
      self[:collections][:posts][:permalink] = style_to_permalink(self[:permalink])
    end
  end

  self
end
add_default_excludes() click to toggle source
# File lib/bridgetown-core/configuration.rb, line 295
def add_default_excludes
  return self if self["exclude"].nil?

  self["exclude"].concat(DEFAULT_EXCLUDES).uniq!
  self
end
check_include_exclude() click to toggle source
# File lib/bridgetown-core/configuration.rb, line 327
def check_include_exclude
  %w(include exclude).each do |option|
    next unless key?(option)
    next if self[option].is_a?(Array)

    raise Bridgetown::Errors::InvalidConfigurationError,
          "'#{option}' should be set as an array, but was: #{self[option].inspect}."
  end

  unless self[:include].include?("_pages") || self[:content_engine] == "resource"
    # add _pages to includes set
    self[:include] << "_pages"
  end

  self
end
config_files(override) click to toggle source

Public: Generate list of configuration files from the override

override - the command-line options hash

Returns an Array of config files

# File lib/bridgetown-core/configuration.rb, line 162
def config_files(override)
  # Adjust verbosity quickly
  Bridgetown.logger.adjust_verbosity(
    quiet: quiet?(override),
    verbose: verbose?(override)
  )

  # Get configuration from <root_dir>/<matching_default_config>
  # or <root_dir>/<config_file> if there's a command line override.
  # By default only the first matching config file will be loaded, but
  # multiple configs can be specified via command line.
  config_files = override["config"]
  if config_files.to_s.empty?
    file_lookups = CONFIG_FILE_PREFIXES.map do |prefix|
      CONFIG_FILE_EXTS.map do |ext|
        Bridgetown.sanitized_path(root_dir(override), "#{prefix}.#{ext}")
      end
    end.flatten.freeze

    found_file = file_lookups.find do |path|
      File.exist?(path)
    end

    config_files = found_file || file_lookups.first
    @default_config_file = true
  end
  Array(config_files)
end
get_config_value_with_override(config_key, override) click to toggle source
# File lib/bridgetown-core/configuration.rb, line 112
def get_config_value_with_override(config_key, override)
  override[config_key] || self[config_key] || DEFAULTS[config_key]
end
merge_environment_specific_options!() click to toggle source

Merge in environment-specific options, if present

# File lib/bridgetown-core/configuration.rb, line 242
def merge_environment_specific_options!
  self[Bridgetown.environment]&.each_key do |k|
    self[k] = self[Bridgetown.environment][k]
  end
  delete(Bridgetown.environment)
  self
end
quiet(override = {}) click to toggle source
# File lib/bridgetown-core/configuration.rb, line 134
def quiet(override = {})
  get_config_value_with_override("quiet", override)
end
Also aliased as: quiet?
quiet?(override = {})
Alias for: quiet
read_config_file(file) click to toggle source

Public: Read configuration and return merged Hash

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

Returns this configuration, overridden by the values in the file

# File lib/bridgetown-core/configuration.rb, line 221
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

  Bridgetown.logger.debug "Configuration file:", file
  next_config
rescue SystemCallError
  if @default_config_file ||= nil
    Bridgetown.logger.warn "Configuration file:", "none"
    {}
  else
    Bridgetown.logger.error "Fatal:", "The configuration file '#{file}' could not be found."
    raise LoadError, "The Configuration file '#{file}' could not be found."
  end
end
read_config_files(files) click to toggle source

Public: Read in a list of configuration files and merge with this hash

files - the list of configuration file paths

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

# File lib/bridgetown-core/configuration.rb, line 197
def read_config_files(files)
  config = self

  begin
    files.each do |config_file|
      next if config_file.nil? || config_file.empty?

      new_config = read_config_file(config_file)
      config = Utils.deep_merge_hashes(self, new_config)
    end
  rescue ArgumentError => e
    Bridgetown.logger.warn "WARNING:", "Error reading configuration. Using defaults" \
                            " (and options)."
    warn e
  end

  config
end
root_dir(override = "") click to toggle source

Public: Directory of the top-level root where config files are located

override - the command-line options hash

Returns the path to the Bridgetown root directory

# File lib/bridgetown-core/configuration.rb, line 121
def root_dir(override = "")
  get_config_value_with_override("root_dir", override)
end
safe_load_file(filename) click to toggle source
# File lib/bridgetown-core/configuration.rb, line 144
def safe_load_file(filename)
  case File.extname(filename)
  when %r!\.toml!i
    Bridgetown::Utils::RequireGems.require_with_graceful_fail("tomlrb") unless defined?(Tomlrb)
    Tomlrb.load_file(filename)
  when %r!\.ya?ml!i
    YAMLParser.load_file(filename) || {}
  else
    raise ArgumentError,
          "No parser for '#{filename}' is available. Use a .y(a)ml or .toml file instead."
  end
end
should_execute_inline_ruby?() click to toggle source
# File lib/bridgetown-core/configuration.rb, line 302
def should_execute_inline_ruby?
  ENV["BRIDGETOWN_RUBY_IN_FRONT_MATTER"] != "false" &&
    self["ruby_in_front_matter"]
end
source(override = "") click to toggle source

Public: Directory of the Bridgetown source folder

override - the command-line options hash

Returns the path to the Bridgetown source directory

# File lib/bridgetown-core/configuration.rb, line 130
def source(override = "")
  get_config_value_with_override("source", override)
end
uses_postcss?() click to toggle source

Whether or not PostCSS is being used to process stylesheets.

@return [Boolean] true if `postcss.config.js` exists, false if not

# File lib/bridgetown-core/configuration.rb, line 347
def uses_postcss?
  File.exist?(Bridgetown.sanitized_path(root_dir, "postcss.config.js"))
end
verbose(override = {}) click to toggle source
# File lib/bridgetown-core/configuration.rb, line 139
def verbose(override = {})
  get_config_value_with_override("verbose", override)
end
Also aliased as: verbose?
verbose?(override = {})
Alias for: verbose