From c213bd19dfb1b5b804d60fb83902c77f97b1d357 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 12 Jan 2016 16:03:23 -0800 Subject: [PATCH] Move block run --- .../lib/middleman-core/application.rb | 34 ++-- .../middleman-core/core_extensions/data.rb | 3 +- .../core_extensions/default_helpers.rb | 4 +- .../core_extensions/external_helpers.rb | 17 +- .../middleman-core/core_extensions/routing.rb | 4 +- .../core_extensions/show_exceptions.rb | 8 +- .../lib/middleman-core/extension.rb | 17 ++ .../lib/middleman-core/extension_manager.rb | 2 + .../lib/middleman-core/extensions.rb | 12 ++ .../lib/middleman-core/renderers/less.rb | 5 +- .../lib/middleman-core/renderers/markdown.rb | 10 +- .../lib/middleman-core/renderers/sass.rb | 21 ++- .../lib/middleman-core/renderers/stylus.rb | 6 +- .../lib/middleman-core/util/data.rb | 168 +++++++++--------- 14 files changed, 161 insertions(+), 150 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 88b6385a..be603df4 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -70,27 +70,27 @@ module Middleman # Which port preview should start on. # @return [Fixnum] - config.define_setting :port, 4567, 'The preview server port' + define_setting :port, 4567, 'The preview server port' # Which server name should be used # @return [NilClass, String] - config.define_setting :server_name, nil, 'The server name of preview server' + define_setting :server_name, nil, 'The server name of preview server' # Which bind address the preview server should use # @return [NilClass, String] - config.define_setting :bind_address, nil, 'The bind address of the preview server' + define_setting :bind_address, nil, 'The bind address of the preview server' # Whether to serve the preview server over HTTPS. # @return [Boolean] - config.define_setting :https, false, 'Serve the preview server over SSL/TLS' + define_setting :https, false, 'Serve the preview server over SSL/TLS' # The (optional) path to the SSL cert to use for the preview server. # @return [String] - config.define_setting :ssl_certificate, nil, 'Path to an X.509 certificate to use for the preview server' + define_setting :ssl_certificate, nil, 'Path to an X.509 certificate to use for the preview server' # The (optional) private key for the certificate in :ssl_certificate. # @return [String] - config.define_setting :ssl_private_key, nil, "Path to an RSA private key for the preview server's certificate" + define_setting :ssl_private_key, nil, "Path to an RSA private key for the preview server's certificate" # Name of the source directory # @return [String] @@ -214,6 +214,7 @@ module Middleman @callbacks.install_methods!(self, [ :initialized, :configure, + :before_extensions, :before_sitemap, :before_configuration, :after_configuration, @@ -245,6 +246,8 @@ module Middleman ::Middleman::FileRenderer.cache.clear ::Middleman::TemplateRenderer.cache.clear + execute_callbacks(:before_extensions) + @extensions = ::Middleman::ExtensionManager.new(self) # Evaluate a passed block if given @@ -257,13 +260,6 @@ module Middleman ::Middleman::Extension.clear_after_extension_callbacks - after_configuration_eval(&method(:prune_tilt_templates)) - - start_lifecycle - end - - # Boot the app. - def start_lifecycle # Before config is parsed, before extensions get to it. execute_callbacks(:initialized) @@ -273,10 +269,6 @@ module Middleman # Eval config. evaluate_configuration! - if Object.const_defined?(:Encoding) - Encoding.default_external = config[:encoding] - end - # Run any `configure` blocks for the current environment. execute_callbacks([:configure, config[:environment]]) @@ -286,6 +278,12 @@ module Middleman # Post parsing, pre-extension callback execute_callbacks(:after_configuration_eval) + if Object.const_defined?(:Encoding) + Encoding.default_external = config[:encoding] + end + + prune_tilt_templates! + # After extensions have worked after_config execute_callbacks(:after_configuration) @@ -317,7 +315,7 @@ module Middleman end # Clean up missing Tilt exts - def prune_tilt_templates + def prune_tilt_templates! ::Tilt.mappings.each do |key, _| begin ::Tilt[".#{key}"] diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index f1762e43..22d571be 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -8,6 +8,8 @@ module Middleman class Data < Extension attr_reader :data_store + define_setting :data_dir, 'data', 'The directory data files are stored in' + # Make the internal `data_store` method available as `app.data` expose_to_application data: :data_store @@ -21,7 +23,6 @@ module Middleman super @data_store = DataStore.new(app, DATA_FILE_MATCHER) - app.config.define_setting :data_dir, 'data', 'The directory data files are stored in' start_watching(app.config[:data_dir]) end diff --git a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb index 3187bf22..be6371c1 100644 --- a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb @@ -17,6 +17,8 @@ class Padrino::Helpers::OutputHelpers::ErbHandler end class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension + define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones' + def initialize(app, options_hash={}, &block) super @@ -30,8 +32,6 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::RenderHelpers ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::NumberHelpers # ::Middleman::TemplateContext.send :include, ::Padrino::Helpers::TranslationHelpers - - app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones' end # The helpers diff --git a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb index 7f662aec..b4be1a68 100644 --- a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb @@ -2,17 +2,12 @@ module Middleman module CoreExtensions # Load helpers in `helpers/` class ExternalHelpers < Extension - def initialize(app, options_hash={}, &block) - super - - # Setup a default helpers paths - app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from' - app.config.define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files' - app.config.define_setting :helpers_filename_to_module_name_proc, proc { |filename| - basename = File.basename(filename, File.extname(filename)) - basename.camelcase - }, 'Proc implementing the conversion from helper filename to module name' - end + define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from' + define_setting :helpers_filename_glob, '**.rb', 'Glob pattern for matching helper ruby files' + define_setting :helpers_filename_to_module_name_proc, proc { |filename| + basename = File.basename(filename, File.extname(filename)) + basename.camelcase + }, 'Proc implementing the conversion from helper filename to module name' def after_configuration helpers_path = File.join(app.root, app.config[:helpers_dir]) diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index cf024d7d..c5655dfe 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -24,8 +24,8 @@ module Middleman normalized_path = '/' + ::Middleman::Util.strip_leading_slash(normalized_path) if normalized_path.is_a?(String) resources - .select { |r| ::Middleman::Util.path_match(normalized_path, "/#{r.path}")} - .each { |r| r.add_metadata(metadata) } + .select { |r| ::Middleman::Util.path_match(normalized_path, "/#{r.path}") } + .each { |r| r.add_metadata(metadata) } resources end diff --git a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb index b6407100..fec4f7bf 100644 --- a/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb +++ b/middleman-core/lib/middleman-core/core_extensions/show_exceptions.rb @@ -3,13 +3,7 @@ require 'rack/showexceptions' # Support rack/showexceptions during development module Middleman::CoreExtensions class ShowExceptions < ::Middleman::Extension - def initialize(app, options_hash={}, &block) - super - - return if app.config.defines_setting? :show_exceptions - - app.config.define_setting :show_exceptions, ENV['TEST'] ? false : true, 'Whether to catch and display exceptions' - end + define_setting :show_exceptions, ENV['TEST'] ? false : true, 'Whether to catch and display exceptions' def ready app.use ::Rack::ShowExceptions if !app.build? && app.config[:show_exceptions] diff --git a/middleman-core/lib/middleman-core/extension.rb b/middleman-core/lib/middleman-core/extension.rb index 5fd80a76..a94a9e12 100644 --- a/middleman-core/lib/middleman-core/extension.rb +++ b/middleman-core/lib/middleman-core/extension.rb @@ -135,6 +135,23 @@ module Middleman config.define_setting(key, default, description, options) end + # @api private + # @return [Middleman::Configuration::ConfigurationManager] The defined global options for this extension. + def global_config + @_global_config ||= ::Middleman::Configuration::ConfigurationManager.new + end + + # Add an global option to this extension. + # @see Middleman::Configuration::ConfigurationManager#define_setting + # @example + # option :compress, false, 'Whether to compress the output' + # @param [Symbol] key The name of the option + # @param [Object] default The default value for the option + # @param [String] description A human-readable description of what the option does + def define_setting(key, default=nil, description=nil, options={}) + global_config.define_setting(key, default, description, options) + end + # Short-hand for simple Sitemap manipulation # @example A generator which returns an array of resources # resources :make_resources diff --git a/middleman-core/lib/middleman-core/extension_manager.rb b/middleman-core/lib/middleman-core/extension_manager.rb index 4e2040eb..cc297b38 100644 --- a/middleman-core/lib/middleman-core/extension_manager.rb +++ b/middleman-core/lib/middleman-core/extension_manager.rb @@ -9,6 +9,8 @@ module Middleman @app = app @activated = {} + ::Middleman::Extensions.load_settings(@app) + manager = self { diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index 42fbdb2d..c295efe3 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -120,6 +120,18 @@ module Middleman app.extensions.activate descriptor[:name] end end + + def load_settings(app) + registered.each do |name, _| + begin + ext = load(name) + unless ext.global_config.all_settings.empty? + app.config.load_settings(ext.global_config.all_settings) + end + rescue LoadError + end + end + end end end end diff --git a/middleman-core/lib/middleman-core/renderers/less.rb b/middleman-core/lib/middleman-core/renderers/less.rb index a2f94a0c..715033e7 100644 --- a/middleman-core/lib/middleman-core/renderers/less.rb +++ b/middleman-core/lib/middleman-core/renderers/less.rb @@ -4,12 +4,11 @@ module Middleman module Renderers # Sass renderer class Less < ::Middleman::Extension + define_setting :less, {}, 'LESS compiler options' + def initialize(app, options={}, &block) super - # Default less options - app.config.define_setting :less, {}, 'LESS compiler options' - # Tell Tilt to use it as well (for inline sass blocks) ::Tilt.register 'less', LocalLoadingLessTemplate ::Tilt.prefer(LocalLoadingLessTemplate) diff --git a/middleman-core/lib/middleman-core/renderers/markdown.rb b/middleman-core/lib/middleman-core/renderers/markdown.rb index 6a285a89..e8acb45a 100644 --- a/middleman-core/lib/middleman-core/renderers/markdown.rb +++ b/middleman-core/lib/middleman-core/renderers/markdown.rb @@ -2,14 +2,8 @@ module Middleman module Renderers # Markdown renderer class Markdown < ::Middleman::Extension - # Once registered - def initialize(app, options={}, &block) - super - - # Set our preference for a markdown engine - app.config.define_setting :markdown_engine, :kramdown, 'Preferred markdown engine' - app.config.define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines' - end + define_setting :markdown_engine, :kramdown, 'Preferred markdown engine' + define_setting :markdown_engine_prefix, ::Tilt, 'The parent module for markdown template engines' # Once configuration is parsed def after_configuration diff --git a/middleman-core/lib/middleman-core/renderers/sass.rb b/middleman-core/lib/middleman-core/renderers/sass.rb index 4e1efe0a..f71f8a9d 100644 --- a/middleman-core/lib/middleman-core/renderers/sass.rb +++ b/middleman-core/lib/middleman-core/renderers/sass.rb @@ -1,9 +1,17 @@ require 'sass' + module Middleman module Renderers # Sass renderer class Sass < ::Middleman::Extension + + opts = { output_style: :nested } + opts[:line_comments] = false if ENV['TEST'] + define_setting :sass, opts, 'Sass engine options' + define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files' + define_setting :sass_source_maps, nil, 'Whether to inline sourcemap into Sass' + # Setup extension def initialize(app, options={}, &block) super @@ -12,15 +20,6 @@ module Middleman app.files.ignore :sass_cache, :source, /(^|\/)\.sass-cache\// - opts = { output_style: :nested } - opts[:line_comments] = false if ENV['TEST'] - - # Default sass options - app.config.define_setting :sass, opts, 'Sass engine options' - - app.config.define_setting :sass_assets_paths, [], 'Paths to extra SASS/SCSS files' - app.config.define_setting :sass_source_maps, app.development?, 'Whether to inline sourcemap into Sass' - # Tell Tilt to use it as well (for inline sass blocks) ::Tilt.register 'sass', SassPlusCSSFilenameTemplate ::Tilt.prefer(SassPlusCSSFilenameTemplate) @@ -75,7 +74,7 @@ module Middleman ctx = @context more_opts = { - load_paths: ::Sass.load_paths | ctx.config[:sass_assets_paths], + load_paths: ::Sass.load_paths | ctx.app.config[:sass_assets_paths], filename: eval_file, line: line, syntax: syntax, @@ -85,7 +84,7 @@ module Middleman ) } - if ctx.config[:sass_source_maps] + if ctx.app.config[:sass_source_maps] || (ctx.app.config[:sass_source_maps].nil? && ctx.app.development?) more_opts[:source_map_file] = '.' more_opts[:source_map_embed] = true more_opts[:source_map_contents] = true diff --git a/middleman-core/lib/middleman-core/renderers/stylus.rb b/middleman-core/lib/middleman-core/renderers/stylus.rb index 19bc0f17..c116aa3f 100644 --- a/middleman-core/lib/middleman-core/renderers/stylus.rb +++ b/middleman-core/lib/middleman-core/renderers/stylus.rb @@ -4,11 +4,7 @@ require 'stylus/tilt' module Middleman module Renderers class Stylus < ::Middleman::Extension - def initialize(app, options={}, &block) - super - - app.config.define_setting :styl, {}, 'Stylus config options' - end + define_setting :styl, {}, 'Stylus config options' end end end diff --git a/middleman-core/lib/middleman-core/util/data.rb b/middleman-core/lib/middleman-core/util/data.rb index 0b3d0967..c1dc1cd3 100644 --- a/middleman-core/lib/middleman-core/util/data.rb +++ b/middleman-core/lib/middleman-core/util/data.rb @@ -5,98 +5,102 @@ require 'middleman-core/util' require 'middleman-core/contracts' require 'backports/2.1.0/array/to_h' -module Middleman::Util::Data - include Contracts +module Middleman + module Util + module Data + include Contracts - module_function + module_function - # Get the frontmatter and plain content from a file - # @param [String] path - # @return [Array] - Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]] - def parse(full_path, frontmatter_delims, known_type=nil) - return [{}, nil] if Middleman::Util.binary?(full_path) + # Get the frontmatter and plain content from a file + # @param [String] path + # @return [Array] + Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]] + def parse(full_path, frontmatter_delims, known_type=nil) + return [{}, nil] if Middleman::Util.binary?(full_path) - # Avoid weird race condition when a file is renamed - begin - content = File.read(full_path) - rescue EOFError, IOError, Errno::ENOENT - return [{}, nil] - end + # Avoid weird race condition when a file is renamed + begin + content = File.read(full_path) + rescue EOFError, IOError, Errno::ENOENT + return [{}, nil] + end - start_delims, stop_delims = frontmatter_delims - .values - .flatten(1) - .transpose - .map(&Regexp.method(:union)) + start_delims, stop_delims = frontmatter_delims + .values + .flatten(1) + .transpose + .map(&Regexp.method(:union)) - match = / - \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)? - (?#{start_delims})[ ]*\r?\n - (?.*?)[ ]*\r?\n? - ^(?#{stop_delims})[ ]*\r?\n? - \r?\n? - (?.*) - /mx.match(content) || {} + match = / + \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)? + (?#{start_delims})[ ]*\r?\n + (?.*?)[ ]*\r?\n? + ^(?#{stop_delims})[ ]*\r?\n? + \r?\n? + (?.*) + /mx.match(content) || {} - unless match[:frontmatter] - case known_type - when :yaml - return [parse_yaml(content, full_path), nil] - when :json - return [parse_json(content, full_path), nil] + unless match[:frontmatter] + case known_type + when :yaml + return [parse_yaml(content, full_path), nil] + when :json + return [parse_json(content, full_path), nil] + end + end + + case [match[:start], match[:stop]] + when *frontmatter_delims[:yaml] + [ + parse_yaml(match[:frontmatter], full_path), + match[:additional_content] + ] + when *frontmatter_delims[:json] + [ + parse_json("{#{match[:frontmatter]}}", full_path), + match[:additional_content] + ] + else + [ + {}, + content + ] + end end - end - case [match[:start], match[:stop]] - when *frontmatter_delims[:yaml] - [ - parse_yaml(match[:frontmatter], full_path), - match[:additional_content] - ] - when *frontmatter_delims[:json] - [ - parse_json("{#{match[:frontmatter]}}", full_path), - match[:additional_content] - ] - else - [ - {}, - content - ] - end - end + # Parse YAML frontmatter out of a string + # @param [String] content + # @return [Hash] + Contract String, Pathname, Bool => Hash + def parse_yaml(content, full_path) + symbolize_recursive(YAML.load(content) || {}) + rescue StandardError, Psych::SyntaxError => error + warn "YAML Exception parsing #{full_path}: #{error.message}" + {} + end - # Parse YAML frontmatter out of a string - # @param [String] content - # @return [Hash] - Contract String, Pathname, Bool => Hash - def parse_yaml(content, full_path) - symbolize_recursive(YAML.load(content) || {}) - rescue StandardError, Psych::SyntaxError => error - warn "YAML Exception parsing #{full_path}: #{error.message}" - {} - end + # Parse JSON frontmatter out of a string + # @param [String] content + # @return [Hash] + Contract String, Pathname => Hash + def parse_json(content, full_path) + symbolize_recursive(JSON.parse(content) || {}) + rescue StandardError => error + warn "JSON Exception parsing #{full_path}: #{error.message}" + {} + end - # Parse JSON frontmatter out of a string - # @param [String] content - # @return [Hash] - Contract String, Pathname => Hash - def parse_json(content, full_path) - symbolize_recursive(JSON.parse(content) || {}) - rescue StandardError => error - warn "JSON Exception parsing #{full_path}: #{error.message}" - {} - end - - def symbolize_recursive(value) - case value - when Hash - value.map { |k, v| [k.to_sym, symbolize_recursive(v)] }.to_h - when Array - value.map { |v| symbolize_recursive(v) } - else - value + def symbolize_recursive(value) + case value + when Hash + value.map { |k, v| [k.to_sym, symbolize_recursive(v)] }.to_h + when Array + value.map { |v| symbolize_recursive(v) } + else + value + end + end end end end