Class: Bridgetown::Configuration
- Inherits:
-
HashWithDotAccess::Hash
- Object
- HashWithDotAccess::Hash
- Bridgetown::Configuration
- Defined in:
- bridgetown-core/lib/bridgetown-core/configuration.rb,
bridgetown-core/lib/bridgetown-core/configuration/configuration_dsl.rb
Overview
The primary configuration object for a Bridgetown project
Defined Under Namespace
Classes: ConfigurationDSL, Initializer, Preflight, SourceManifest
Constant Summary collapse
- REQUIRE_DENYLIST =
Built-in initializer list which isn’t Gem-backed:
%i(external_sources parse_routes ssr wikilinks)
- DEFAULTS =
Default options. Overridden by values in bridgetown.config.yml or initializers. Strings rather than symbols are used for compatibility with YAML.
{ # Where things are "root_dir" => Dir.pwd, "source" => "src", "destination" => "output", "plugins_dir" => "plugins", "server_dir" => "server", "collections_dir" => "", "cache_dir" => ".bridgetown-cache", "layouts_dir" => "_layouts", "components_dir" => "_components", "islands_dir" => "_islands", "partials_dir" => "_partials", "collections" => {}, "taxonomies" => { category: { key: "categories", title: "Category" }, tag: { key: "tags", title: "Tag" }, }, "autoload_paths" => [], "inflector" => Bridgetown::Foundation::Inflector.new, "eager_load_paths" => [], "autoloader_collapsed_paths" => [], "additional_watch_paths" => [], "defaults" => [], # 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_mode" => "pretty", # Filtering Content "future" => false, "unpublished" => false, "ruby_in_front_matter" => true, # Conversion "template_engine" => "erb", "markdown" => "kramdown", "highlighter" => "rouge", "support_data_as_view_methods" => true, # Output Configuration "base_path" => "/", "available_locales" => [:en], "default_locale" => :en, "prefix_default_locale" => false, "permalink" => "pretty", "timezone" => nil, # use the local timezone "quiet" => false, "verbose" => false, "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, "include_extraction_tags" => false, "mark_highlighting" => true, }, "development" => { "fast_refresh" => true, }, }.each_with_object(Configuration.new) { |(k, v), hsh| hsh[k] = v.freeze }.freeze
- DEFAULT_EXCLUDES =
%w( .sass-cache/ .bridgetown-cache/ tmp/ gemfiles Gemfile Gemfile.lock gems.rb gems.locked node_modules/ config/ vendor/bundle/ vendor/cache/ vendor/gems/ vendor/ruby/ ).freeze
Instance Attribute Summary collapse
-
#initializers ⇒ Hash<Symbol, Initializer>
-
#roda_initializers ⇒ Array<Proc>
-
#source_manifests ⇒ Set<SourceManifest>
Class Method Summary collapse
-
.from(user_config, starting_defaults = DEFAULTS) ⇒ Configuration
Produce a Configuration ready for use in a Site.
Instance Method Summary collapse
-
#add_default_collections ⇒ Object
rubocop:todo all.
-
#add_destination_paths ⇒ Object
(also: #add_default_excludes)
-
#check_include_exclude ⇒ Object
-
#config_files(override) ⇒ Array<string>
Generate list of configuration files from the override.
-
#get_config_value_with_override(config_key, override) ⇒ Object
-
#initialize_roda_app(app) ⇒ Object
-
#initializers_dsl(context:) ⇒ Object
-
#merge_environment_specific_options! ⇒ Object
Merge in environment-specific options, if present.
-
#quiet(override = {}) ⇒ Object
(also: #quiet?)
-
#read_config_file(file) ⇒ Hash
Read configuration and return merged Hash.
-
#read_config_files(files) ⇒ Hash
Read in a list of configuration files and merge with this hash.
-
#root_dir(override = {}) ⇒ String
Directory of the top-level root where config files are located.
-
#run_initializers!(context:) ⇒ Object
-
#safe_load_file(filename) ⇒ Object
-
#setup_load_paths!(appending: false) ⇒ Object
rubocop:todo Metrics.
-
#setup_locales ⇒ Object
-
#setup_post_init! ⇒ Object
-
#should_execute_inline_ruby? ⇒ Boolean
-
#source(override = {}) ⇒ String
Directory of the Bridgetown source folder.
-
#uses_postcss? ⇒ Boolean
Whether or not PostCSS is being used to process stylesheets.
-
#verbose(override = {}) ⇒ Object
(also: #verbose?)
Methods inherited from HashWithDotAccess::Hash
Instance Attribute Details
#initializers ⇒ Hash<Symbol, Initializer>
130 131 132 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 130 def initializers @initializers end |
#roda_initializers ⇒ Array<Proc>
193 194 195 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 193 def roda_initializers @roda_initializers ||= [] end |
#source_manifests ⇒ Set<SourceManifest>
188 189 190 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 188 def source_manifests @source_manifests ||= Set.new end |
Class Method Details
.from(user_config, starting_defaults = DEFAULTS) ⇒ Configuration
Produce a Configuration ready for use in a Site. It takes the input, fills in the defaults where values do not exist.
140 141 142 143 144 145 146 147 148 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 140 def from(user_config, starting_defaults = DEFAULTS) Utils.deep_merge_hashes(starting_defaults.deep_dup, Configuration.new(user_config)) . .setup_load_paths! .setup_locales .add_default_collections .add_destination_paths .check_include_exclude end |
Instance Method Details
#add_default_collections ⇒ Object
rubocop:todo all
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 359 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" self[:collections][:pages] = {} unless self[:collections][:pages] self[:collections][:pages][:output] = true self[:collections][:data] = {} unless self[:collections][:data] self[:collections][:data][:output] = false self end |
#add_destination_paths ⇒ Object Also known as: add_default_excludes
391 392 393 394 395 396 397 398 399 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 391 def add_destination_paths self["keep_files"] << "_bridgetown" unless self["keep_files"].nil?.! && self["keep_files"].include?("_bridgetown") return self if self["exclude"].nil? self["exclude"].concat(DEFAULT_EXCLUDES).uniq! self end |
#check_include_exclude ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 408 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 self end |
#config_files(override) ⇒ Array<string>
Generate list of configuration files from the override
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 243 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"] config_files = "bridgetown.config.yml" if config_files.to_s.empty? Array(config_files) end |
#get_config_value_with_override(config_key, override) ⇒ Object
203 204 205 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 203 def get_config_value_with_override(config_key, override) override[config_key] || self[config_key] || DEFAULTS[config_key] end |
#initialize_roda_app(app) ⇒ Object
197 198 199 200 201 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 197 def initialize_roda_app(app) roda_initializers.each do |initializer| initializer.(app) end end |
#initializers_dsl(context:) ⇒ Object
151 152 153 154 155 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 151 def initializers_dsl(context:) ConfigurationDSL.new(scope: self, data: self).tap do |dsl| dsl.instance_variable_set(:@context, context) end end |
#merge_environment_specific_options! ⇒ Object
Merge in environment-specific options, if present
307 308 309 310 311 312 313 314 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 307 def self[Bridgetown.environment]&.each_key do |k| self[k] = self[Bridgetown.environment][k] end delete(Bridgetown.environment) self end |
#quiet(override = {}) ⇒ Object Also known as: quiet?
223 224 225 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 223 def quiet(override = {}) get_config_value_with_override("quiet", override) end |
#read_config_file(file) ⇒ Hash
Read configuration and return merged Hash
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 287 def read_config_file(file) default_config_file = file == "bridgetown.config.yml" file = File.(file) # We don't care if default config is missing, we can return blank: return {} if !File.exist?(file) && default_config_file file_config = safe_load_file(file) unless file_config.is_a?(Hash) raise ArgumentError, "Configuration file: (INVALID) #{file}".yellow end Bridgetown.logger.debug "Configuration file:", file file_config rescue SystemCallError Bridgetown.logger.error "Fatal:", "The configuration file '#{file}' could not be found." raise LoadError, "missing configuration file" end |
#read_config_files(files) ⇒ Hash
Read in a list of configuration files and merge with this hash
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 264 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 = {}) ⇒ String
Directory of the top-level root where config files are located
211 212 213 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 211 def root_dir(override = {}) get_config_value_with_override("root_dir", override) end |
#run_initializers!(context:) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 157 def run_initializers!(context:) initializers_file = File.join(root_dir, "config", "initializers.rb") unless File.file?(initializers_file) setup_load_paths! appending: true return end load initializers_file return unless initializers # no initializers have been set up init_init = initializers[:init] return unless init_init && !init_init.completed Bridgetown.logger.debug "Initializing:", "Running initializers with `#{context}' context in:" Bridgetown.logger.debug "", initializers_file self.init_params = {} dsl = initializers_dsl(context:) dsl.instance_exec(dsl, &init_init.block) dsl._run_builtins! setup_post_init! self end |
#safe_load_file(filename) ⇒ Object
233 234 235 236 237 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 233 def safe_load_file(filename) YAMLParser.load_file(filename) || {} rescue Psych::DisallowedClass => e raise "Unable to parse `#{File.basename(filename)}'. #{e.}" end |
#setup_load_paths!(appending: false) ⇒ Object
rubocop:todo Metrics
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 316 def setup_load_paths!(appending: false) # rubocop:todo Metrics self[:root_dir] = File.(self[:root_dir]) self[:source] = File.(self[:source], self[:root_dir]) self[:destination] = File.(self[:destination], self[:root_dir]) unless appending autoload_paths.unshift({ path: File.(self[:plugins_dir], self[:root_dir]), eager: true, }) autoload_paths.unshift({ path: File.(self[:islands_dir], self[:source]), eager: true, }) end autoload_paths.map! do |load_path| if load_path.is_a?(Hash) = File.(load_path[:path], self[:root_dir]) self[:eager_load_paths] << if load_path[:eager] next end File.(load_path, self[:root_dir]) end autoloader_collapsed_paths.map! do |collapsed_path| File.(collapsed_path, self[:root_dir]) end additional_watch_paths.map! do |collapsed_path| File.(collapsed_path, self[:root_dir]) end self end |
#setup_locales ⇒ Object
353 354 355 356 357 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 353 def setup_locales self.default_locale = default_locale.to_sym available_locales.map!(&:to_sym) self end |
#setup_post_init! ⇒ Object
183 184 185 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 183 def setup_post_init! add_destination_paths.setup_load_paths! appending: true end |
#should_execute_inline_ruby? ⇒ Boolean
403 404 405 406 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 403 def should_execute_inline_ruby? ENV["BRIDGETOWN_RUBY_IN_FRONT_MATTER"] != "false" && self["ruby_in_front_matter"] end |
#source(override = {}) ⇒ String
Directory of the Bridgetown source folder
219 220 221 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 219 def source(override = {}) get_config_value_with_override("source", override) end |
#uses_postcss? ⇒ Boolean
Whether or not PostCSS is being used to process stylesheets.
423 424 425 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 423 def uses_postcss? File.exist?(Bridgetown.sanitized_path(root_dir, "postcss.config.js")) end |
#verbose(override = {}) ⇒ Object Also known as: verbose?
228 229 230 |
# File 'bridgetown-core/lib/bridgetown-core/configuration.rb', line 228 def verbose(override = {}) get_config_value_with_override("verbose", override) end |