# frozen_string_literal: true module Bridgetown class Configuration < Hash # Default options. Overridden by values in bridgetown.config.yml. # Strings rather than symbols are used for compatibility with YAML. DEFAULTS = { # Where things are "root_dir" => Dir.pwd, "plugins_dir" => "plugins", "source" => File.join(Dir.pwd, "src"), "destination" => File.join(Dir.pwd, "output"), "collections_dir" => "", "cache_dir" => ".bridgetown-cache", "layouts_dir" => "_layouts", "data_dir" => "_data", "components_dir" => "_components", "includes_dir" => "_includes", "collections" => {}, # Handling Reading "include" => [".htaccess", "_redirects", ".well-known"], "exclude" => [], "keep_files" => [".git", ".svn", "_bridgetown"], "encoding" => "utf-8", "markdown_ext" => "markdown,mkdown,mkdn,mkd,md", "strict_front_matter" => false, "slugify_categories" => true, # Filtering Content "limit_posts" => 0, "future" => false, "unpublished" => false, # Conversion "markdown" => "kramdown", "highlighter" => "rouge", "lsi" => false, "excerpt_separator" => "\n\n", "incremental" => false, # Serving "detach" => false, # default to not detaching the server "port" => "4000", "host" => "127.0.0.1", "baseurl" => nil, # this mounts at /, i.e. no subdirectory "show_dir_listing" => false, # Output Configuration "permalink" => "date", "timezone" => nil, # use the local timezone "quiet" => false, "verbose" => false, "defaults" => [], "liquid" => { "error_mode" => "warn", "strict_filters" => false, "strict_variables" => false, }, "kramdown" => { "auto_ids" => true, "toc_levels" => (1..6).to_a, "entity_output" => "as_char", "smart_quotes" => "lsquo,rsquo,ldquo,rdquo", "input" => "GFM", "hard_wrap" => false, "guess_lang" => true, "footnote_nr" => 1, "show_warnings" => false, }, }.each_with_object(Configuration.new) { |(k, v), hsh| hsh[k] = v.freeze }.freeze # 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 CONFIG_FILE_PREFIXES = %w(bridgetown.config _config).freeze CONFIG_FILE_EXTS = %w(yml yaml toml).freeze class << self # 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. def from(user_config) Utils.deep_merge_hashes(DEFAULTS, Configuration[user_config].stringify_keys) .add_default_collections.add_default_excludes end end # Public: Turn all keys into string # # Return a copy of the hash where all its keys are strings def stringify_keys each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v } end def get_config_value_with_override(config_key, override) override[config_key] || self[config_key] || DEFAULTS[config_key] end # 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 def root_dir(override) get_config_value_with_override("root_dir", override) end # Public: Directory of the Bridgetown source folder # # override - the command-line options hash # # Returns the path to the Bridgetown source directory def source(override) get_config_value_with_override("source", override) end def quiet(override = {}) get_config_value_with_override("quiet", override) end alias_method :quiet?, :quiet def verbose(override = {}) get_config_value_with_override("verbose", override) end alias_method :verbose?, :verbose def safe_load_file(filename) case File.extname(filename) when %r!\.toml!i Bridgetown::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 # Public: Generate list of configuration files from the override # # override - the command-line options hash # # Returns an Array of config files def config_files(override) # Adjust verbosity quickly Bridgetown.logger.adjust_verbosity( quiet: quiet?(override), verbose: verbose?(override) ) # Get configuration from / # or / 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 # 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 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.info "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 # 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 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 Bridgetown.logger.warn "WARNING:", "Error reading configuration. Using defaults" \ " (and options)." warn e end configuration.validate.add_default_collections end # Public: Split a CSV string into an array containing its values # # csv - the string of comma-separated values # # Returns an array of the values contained in the CSV def csv_to_array(csv) csv.split(",").map(&:strip) end # Public: Ensure the proper options are set in the configuration # # Returns the configuration Hash def validate config = clone check_include_exclude(config) config end 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 DEFAULT_EXCLUDES = %w( .sass-cache .bridgetown-cache gemfiles Gemfile Gemfile.lock node_modules vendor/bundle/ vendor/cache/ vendor/gems/ vendor/ruby/ ).freeze def add_default_excludes config = clone return config if config["exclude"].nil? config["exclude"].concat(DEFAULT_EXCLUDES).uniq! config end private # rubocop:disable Metrics/CyclomaticComplexity # def style_to_permalink(permalink_style) case permalink_style.to_sym when :pretty "/:categories/:year/:month/:day/:title/" when :simple "/:categories/:title/" when :none "/:categories/:title:output_ext" when :date "/:categories/:year/:month/:day/:title:output_ext" when :ordinal "/:categories/:year/:y_day/:title:output_ext" when :weekdate "/:categories/:year/W:week/:short_day/:title:output_ext" else permalink_style.to_s end end # rubocop:enable Metrics/CyclomaticComplexity # def check_include_exclude(config) %w(include exclude).each do |option| next unless config.key?(option) next if config[option].is_a?(Array) raise Bridgetown::Errors::InvalidConfigurationError, "'#{option}' should be set as an array, but was: #{config[option].inspect}." end end end end