Merge pull request #1285 from bhollis/extensions
Auto activating extensions
This commit is contained in:
commit
18884cac95
|
@ -25,6 +25,15 @@ require 'middleman-core/config_context'
|
||||||
require 'middleman-core/file_renderer'
|
require 'middleman-core/file_renderer'
|
||||||
require 'middleman-core/template_renderer'
|
require 'middleman-core/template_renderer'
|
||||||
|
|
||||||
|
# Rack Request
|
||||||
|
require 'middleman-core/core_extensions/request'
|
||||||
|
|
||||||
|
# Custom Extension API and config.rb handling
|
||||||
|
require 'middleman-core/core_extensions/extensions'
|
||||||
|
|
||||||
|
# Catch and show exceptions at the Rack level
|
||||||
|
require 'middleman-core/core_extensions/show_exceptions'
|
||||||
|
|
||||||
# Core Middleman Class
|
# Core Middleman Class
|
||||||
module Middleman
|
module Middleman
|
||||||
class Application
|
class Application
|
||||||
|
@ -133,21 +142,12 @@ module Middleman
|
||||||
# Handle exceptions
|
# Handle exceptions
|
||||||
include Middleman::CoreExtensions::ShowExceptions
|
include Middleman::CoreExtensions::ShowExceptions
|
||||||
|
|
||||||
# Add Watcher Callbacks
|
|
||||||
include Middleman::CoreExtensions::FileWatcher
|
|
||||||
|
|
||||||
# Activate Data package
|
|
||||||
include Middleman::CoreExtensions::Data
|
|
||||||
|
|
||||||
# Setup custom rendering
|
# Setup custom rendering
|
||||||
include Middleman::CoreExtensions::Rendering
|
include Middleman::CoreExtensions::Rendering
|
||||||
|
|
||||||
# Sitemap Config options and public api
|
# Sitemap Config options and public api
|
||||||
include Middleman::Sitemap
|
include Middleman::Sitemap
|
||||||
|
|
||||||
# Setup external helpers
|
|
||||||
include Middleman::CoreExtensions::ExternalHelpers
|
|
||||||
|
|
||||||
# Reference to Logger singleton
|
# Reference to Logger singleton
|
||||||
def logger
|
def logger
|
||||||
::Middleman::Logger.singleton
|
::Middleman::Logger.singleton
|
||||||
|
@ -168,7 +168,7 @@ module Middleman
|
||||||
delegate :link_to, :image_tag, :asset_path, to: :generic_template_context
|
delegate :link_to, :image_tag, :asset_path, to: :generic_template_context
|
||||||
|
|
||||||
# Initialize the Middleman project
|
# Initialize the Middleman project
|
||||||
def initialize(&block)
|
def initialize
|
||||||
@template_context_class = Class.new(Middleman::TemplateContext)
|
@template_context_class = Class.new(Middleman::TemplateContext)
|
||||||
@generic_template_context = @template_context_class.new(self)
|
@generic_template_context = @template_context_class.new(self)
|
||||||
@config_context = ConfigContext.new(self, @template_context_class)
|
@config_context = ConfigContext.new(self, @template_context_class)
|
||||||
|
@ -179,9 +179,9 @@ module Middleman
|
||||||
# Setup the default values from calls to set before initialization
|
# Setup the default values from calls to set before initialization
|
||||||
self.class.config.load_settings(self.class.superclass.config.all_settings)
|
self.class.config.load_settings(self.class.superclass.config.all_settings)
|
||||||
|
|
||||||
# Parse YAML from templates. Must be before sitemap so sitemap
|
::Middleman::Extensions.auto_activate[:before_sitemap].each do |ext_name|
|
||||||
# extensions see updated frontmatter!
|
activate ext_name
|
||||||
activate :front_matter
|
end
|
||||||
|
|
||||||
# Initialize the Sitemap
|
# Initialize the Sitemap
|
||||||
@sitemap = ::Middleman::Sitemap::Store.new(self)
|
@sitemap = ::Middleman::Sitemap::Store.new(self)
|
||||||
|
@ -193,19 +193,6 @@ module Middleman
|
||||||
|
|
||||||
config[:source] = ENV['MM_SOURCE'] if ENV['MM_SOURCE']
|
config[:source] = ENV['MM_SOURCE'] if ENV['MM_SOURCE']
|
||||||
|
|
||||||
# Built-in extensions
|
|
||||||
activate :default_helpers
|
|
||||||
activate :lorem
|
|
||||||
|
|
||||||
begin
|
|
||||||
activate :compass
|
|
||||||
rescue LoadError
|
|
||||||
# Compass is not available, don't complain about it
|
|
||||||
end
|
|
||||||
|
|
||||||
# Evaluate a passed block if given
|
|
||||||
@config_context.instance_exec(&block) if block_given?
|
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'middleman-core/core_extensions/routing'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
class ConfigContext
|
class ConfigContext
|
||||||
# page routing
|
# page routing
|
||||||
|
|
|
@ -1,44 +1,50 @@
|
||||||
# Rack Request
|
|
||||||
require 'middleman-core/core_extensions/request'
|
|
||||||
|
|
||||||
# File Change Notifier
|
|
||||||
require 'middleman-core/core_extensions/file_watcher'
|
|
||||||
|
|
||||||
# Custom Feature API
|
|
||||||
require 'middleman-core/core_extensions/extensions'
|
|
||||||
|
|
||||||
# Data looks at the data/ folder for YAML files and makes them available
|
|
||||||
# to dynamic requests.
|
|
||||||
require 'middleman-core/core_extensions/data'
|
|
||||||
|
|
||||||
# Parse YAML from templates
|
# Parse YAML from templates
|
||||||
Middleman::Extensions.register :front_matter do
|
Middleman::Extensions.register :front_matter, auto_activate: :before_sitemap do
|
||||||
require 'middleman-core/core_extensions/front_matter'
|
require 'middleman-core/core_extensions/front_matter'
|
||||||
Middleman::CoreExtensions::FrontMatter
|
Middleman::CoreExtensions::FrontMatter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Data looks at the data/ folder for YAML files and makes them available
|
||||||
|
# to dynamic requests.
|
||||||
|
Middleman::Extensions.register :data, auto_activate: :before_sitemap do
|
||||||
|
require 'middleman-core/core_extensions/data'
|
||||||
|
Middleman::CoreExtensions::Data
|
||||||
|
end
|
||||||
|
|
||||||
|
# File Change Notifier
|
||||||
|
Middleman::Extensions.register :file_watcher, auto_activate: :before_sitemap do
|
||||||
|
require 'middleman-core/core_extensions/file_watcher'
|
||||||
|
Middleman::CoreExtensions::FileWatcher
|
||||||
|
end
|
||||||
|
|
||||||
# External helpers looks in the helpers/ folder for helper modules
|
# External helpers looks in the helpers/ folder for helper modules
|
||||||
require 'middleman-core/core_extensions/external_helpers'
|
Middleman::Extensions.register :external_helpers, auto_activate: :before_configuration do
|
||||||
|
require 'middleman-core/core_extensions/external_helpers'
|
||||||
|
Middleman::CoreExtensions::ExternalHelpers
|
||||||
|
end
|
||||||
|
|
||||||
# Extended version of Padrino's rendering
|
# Extended version of Padrino's rendering
|
||||||
require 'middleman-core/core_extensions/rendering'
|
require 'middleman-core/core_extensions/rendering'
|
||||||
|
|
||||||
# Pass custom options to views
|
|
||||||
require 'middleman-core/core_extensions/routing'
|
|
||||||
|
|
||||||
# Catch and show exceptions at the Rack level
|
|
||||||
require 'middleman-core/core_extensions/show_exceptions'
|
|
||||||
|
|
||||||
# Setup default helpers
|
# Setup default helpers
|
||||||
Middleman::Extensions.register :default_helpers do
|
Middleman::Extensions.register :default_helpers, auto_activate: :before_configuration do
|
||||||
require 'middleman-core/core_extensions/default_helpers'
|
require 'middleman-core/core_extensions/default_helpers'
|
||||||
Middleman::CoreExtensions::DefaultHelpers
|
Middleman::CoreExtensions::DefaultHelpers
|
||||||
end
|
end
|
||||||
|
|
||||||
# Compass framework
|
# Compass framework
|
||||||
Middleman::Extensions.register :compass do
|
begin
|
||||||
require 'middleman-core/core_extensions/compass'
|
require 'middleman-core/core_extensions/compass'
|
||||||
Middleman::CoreExtensions::Compass
|
Middleman::Extensions.register :compass, Middleman::CoreExtensions::Compass, auto_activate: :before_configuration
|
||||||
|
rescue LoadError
|
||||||
|
# Compass is not available, don't complain about it
|
||||||
|
end
|
||||||
|
|
||||||
|
# Lorem provides a handful of helpful prototyping methods to generate
|
||||||
|
# words, paragraphs, fake images, names and email addresses.
|
||||||
|
Middleman::Extensions.register :lorem, auto_activate: :before_configuration do
|
||||||
|
require 'middleman-core/extensions/lorem'
|
||||||
|
Middleman::Extensions::Lorem
|
||||||
end
|
end
|
||||||
|
|
||||||
###
|
###
|
||||||
|
@ -103,13 +109,6 @@ Middleman::Extensions.register :directory_indexes do
|
||||||
Middleman::Extensions::DirectoryIndexes
|
Middleman::Extensions::DirectoryIndexes
|
||||||
end
|
end
|
||||||
|
|
||||||
# Lorem provides a handful of helpful prototyping methods to generate
|
|
||||||
# words, paragraphs, fake images, names and email addresses.
|
|
||||||
Middleman::Extensions.register :lorem do
|
|
||||||
require 'middleman-core/extensions/lorem'
|
|
||||||
Middleman::Extensions::Lorem
|
|
||||||
end
|
|
||||||
|
|
||||||
# AutomaticImageSizes inspects the images used in your dynamic templates
|
# AutomaticImageSizes inspects the images used in your dynamic templates
|
||||||
# and automatically adds width and height attributes to their HTML
|
# and automatically adds width and height attributes to their HTML
|
||||||
# elements.
|
# elements.
|
||||||
|
|
|
@ -1,37 +1,35 @@
|
||||||
|
require 'yaml'
|
||||||
|
require 'active_support/json'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
# The data extension parses YAML and JSON files in the data/ directory
|
# The data extension parses YAML and JSON files in the `data/` directory
|
||||||
# and makes them available to config.rb, templates and extensions
|
# and makes them available to `config.rb`, templates and extensions
|
||||||
module Data
|
class Data < Extension
|
||||||
# Extension registered
|
# The regex which tells Middleman which files are for data
|
||||||
class << self
|
MATCHER = /[\w-]+\.(yml|yaml|json)$/
|
||||||
# @private
|
|
||||||
def included(app)
|
|
||||||
# Data formats
|
|
||||||
require 'yaml'
|
|
||||||
require 'active_support/json'
|
|
||||||
|
|
||||||
|
def initialize(app, options_hash={}, &block)
|
||||||
|
super
|
||||||
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
|
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
|
||||||
|
|
||||||
|
# Directly include the #data method instead of using helpers so that this is available immediately
|
||||||
app.send :include, InstanceMethods
|
app.send :include, InstanceMethods
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# Instance methods
|
def before_configuration
|
||||||
module InstanceMethods
|
|
||||||
# Setup data files before anything else so they are available when
|
# Setup data files before anything else so they are available when
|
||||||
# parsing config.rb
|
# parsing config.rb
|
||||||
def initialize
|
app.files.changed MATCHER do |file|
|
||||||
files.changed DataStore.matcher do |file|
|
|
||||||
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
|
||||||
end
|
end
|
||||||
|
|
||||||
files.deleted DataStore.matcher do |file|
|
app.files.deleted MATCHER do |file|
|
||||||
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
|
||||||
end
|
end
|
||||||
|
|
||||||
super
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
# The data object
|
# The data object
|
||||||
#
|
#
|
||||||
# @return [DataStore]
|
# @return [DataStore]
|
||||||
|
@ -42,25 +40,14 @@ module Middleman
|
||||||
|
|
||||||
# The core logic behind the data extension.
|
# The core logic behind the data extension.
|
||||||
class DataStore
|
class DataStore
|
||||||
# Static methods
|
|
||||||
class << self
|
|
||||||
# The regex which tells Middleman which files are for data
|
|
||||||
#
|
|
||||||
# @return [Regexp]
|
|
||||||
def matcher
|
|
||||||
%r{[\w-]+\.(yml|yaml|json)$}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Store static data hash
|
# Store static data hash
|
||||||
#
|
#
|
||||||
# @param [Symbol] name Name of the data, used for namespacing
|
# @param [Symbol] name Name of the data, used for namespacing
|
||||||
# @param [Hash] content The content for this data
|
# @param [Hash] content The content for this data
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def store(name=nil, content=nil)
|
def store(name=nil, content=nil)
|
||||||
@_local_sources ||= {}
|
@local_sources[name.to_s] = content unless name.nil? || content.nil?
|
||||||
@_local_sources[name.to_s] = content unless name.nil? || content.nil?
|
@local_sources
|
||||||
@_local_sources
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Store callback-based data
|
# Store callback-based data
|
||||||
|
@ -69,9 +56,8 @@ module Middleman
|
||||||
# @param [Proc] proc The callback which will return data
|
# @param [Proc] proc The callback which will return data
|
||||||
# @return [Hash]
|
# @return [Hash]
|
||||||
def callbacks(name=nil, proc=nil)
|
def callbacks(name=nil, proc=nil)
|
||||||
@_callback_sources ||= {}
|
@callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
|
||||||
@_callback_sources[name.to_s] = proc unless name.nil? || proc.nil?
|
@callback_sources
|
||||||
@_callback_sources
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Setup data store
|
# Setup data store
|
||||||
|
@ -80,6 +66,8 @@ module Middleman
|
||||||
def initialize(app)
|
def initialize(app)
|
||||||
@app = app
|
@app = app
|
||||||
@local_data = {}
|
@local_data = {}
|
||||||
|
@local_sources = {}
|
||||||
|
@callback_sources = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the internal cache for a given file path
|
# Update the internal cache for a given file path
|
||||||
|
|
|
@ -76,7 +76,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# Override application initialization to load `config.rb` and to call lifecycle hooks.
|
# Override application initialization to load `config.rb` and to call lifecycle hooks.
|
||||||
def initialize
|
def initialize(&block)
|
||||||
super
|
super
|
||||||
|
|
||||||
self.class.inst = self
|
self.class.inst = self
|
||||||
|
@ -86,6 +86,10 @@ module Middleman
|
||||||
|
|
||||||
::Middleman::Extension.clear_after_extension_callbacks
|
::Middleman::Extension.clear_after_extension_callbacks
|
||||||
|
|
||||||
|
::Middleman::Extensions.auto_activate[:before_configuration].each do |ext_name|
|
||||||
|
activate ext_name
|
||||||
|
end
|
||||||
|
|
||||||
if ENV['AUTOLOAD_SPROCKETS'] != 'false'
|
if ENV['AUTOLOAD_SPROCKETS'] != 'false'
|
||||||
begin
|
begin
|
||||||
require 'middleman-sprockets'
|
require 'middleman-sprockets'
|
||||||
|
@ -95,6 +99,9 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Evaluate a passed block if given
|
||||||
|
config_context.instance_exec(&block) if block_given?
|
||||||
|
|
||||||
run_hook :initialized
|
run_hook :initialized
|
||||||
|
|
||||||
run_hook :before_configuration
|
run_hook :before_configuration
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
# Load helpers in helpers/
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
module ExternalHelpers
|
# Load helpers in `helpers/`
|
||||||
# once registered
|
class ExternalHelpers < Extension
|
||||||
def self.included(app)
|
def initialize(app, options_hash={}, &block)
|
||||||
|
super
|
||||||
|
|
||||||
# Setup a default helpers paths
|
# Setup a default helpers paths
|
||||||
app.config.define_setting :helpers_dir, 'helpers', 'Directory to autoload helper modules from'
|
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_glob, '**.rb', 'Glob pattern for matching helper ruby files'
|
||||||
|
@ -11,20 +12,20 @@ module Middleman
|
||||||
basename = File.basename(filename, File.extname(filename))
|
basename = File.basename(filename, File.extname(filename))
|
||||||
basename.camelcase
|
basename.camelcase
|
||||||
}, 'Proc implementing the conversion from helper filename to module name'
|
}, 'Proc implementing the conversion from helper filename to module name'
|
||||||
|
end
|
||||||
|
|
||||||
# After config
|
def after_configuration
|
||||||
app.after_configuration do
|
helpers_path = File.join(app.root, app.config[:helpers_dir])
|
||||||
helpers_path = File.join(root, config[:helpers_dir])
|
|
||||||
next unless File.exist?(helpers_path)
|
|
||||||
|
|
||||||
Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename|
|
if File.exist?(helpers_path)
|
||||||
module_name = config[:helpers_filename_to_module_name_proc].call(filename)
|
Dir[File.join(helpers_path, app.config[:helpers_filename_glob])].each do |filename|
|
||||||
|
module_name = app.config[:helpers_filename_to_module_name_proc].call(filename)
|
||||||
next unless module_name
|
next unless module_name
|
||||||
|
|
||||||
require filename
|
require filename
|
||||||
next unless Object.const_defined?(module_name.to_sym)
|
next unless Object.const_defined?(module_name.to_sym)
|
||||||
|
|
||||||
@template_context_class.send :include, Object.const_get(module_name.to_sym)
|
app.template_context_class.send :include, Object.const_get(module_name.to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
# API for watching file change events
|
require 'pathname'
|
||||||
|
require 'set'
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
module FileWatcher
|
# API for watching file change events
|
||||||
|
class FileWatcher < Extension
|
||||||
|
# Regexes in this list will filter out filenames of files that shouldn't cause file change notifications.
|
||||||
IGNORE_LIST = [
|
IGNORE_LIST = [
|
||||||
/^bin(\/|$)/,
|
/^bin(\/|$)/,
|
||||||
/^\.bundle(\/|$)/,
|
/^\.bundle(\/|$)/,
|
||||||
|
@ -20,39 +24,30 @@ module Middleman
|
||||||
/^tmp\//
|
/^tmp\//
|
||||||
]
|
]
|
||||||
|
|
||||||
# Setup extension
|
def initialize(app, options_hash={}, &block)
|
||||||
class << self
|
super
|
||||||
# Once registered
|
|
||||||
def included(app)
|
|
||||||
require 'pathname'
|
|
||||||
require 'set'
|
|
||||||
|
|
||||||
app.send :include, InstanceMethods
|
|
||||||
|
|
||||||
app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
|
app.config.define_setting :file_watcher_ignore, IGNORE_LIST, 'Regexes for paths that should be ignored when they change.'
|
||||||
|
|
||||||
|
# Directly include the #files method instead of using helpers so that this is available immediately
|
||||||
|
app.send :include, InstanceMethods
|
||||||
|
end
|
||||||
|
|
||||||
# Before parsing config, load the data/ directory
|
# Before parsing config, load the data/ directory
|
||||||
app.before_configuration do
|
def before_configuration
|
||||||
files.reload_path(config[:data_dir])
|
app.files.reload_path(app.config[:data_dir])
|
||||||
end
|
end
|
||||||
|
|
||||||
app.after_configuration do
|
def after_configuration
|
||||||
config[:file_watcher_ignore] << %r{^#{config[:build_dir]}(\/|$)}
|
app.config[:file_watcher_ignore] << %r{^#{app.config[:build_dir]}(\/|$)}
|
||||||
|
app.files.reload_path('.')
|
||||||
end
|
end
|
||||||
|
|
||||||
# After config, load everything else
|
|
||||||
app.ready do
|
|
||||||
files.reload_path('.')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Instance methods
|
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
# Access the file api
|
# Access the file api
|
||||||
# @return [Middleman::CoreExtensions::FileWatcher::API]
|
# @return [Middleman::CoreExtensions::FileWatcher::API]
|
||||||
def files
|
def files
|
||||||
@_files_api ||= API.new(self)
|
@files_api ||= API.new(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
require 'middleman-core/template_context'
|
require 'middleman-core/template_context'
|
||||||
|
|
||||||
# Rendering extension
|
# Rendering extension
|
||||||
# rubocop:disable UnderscorePrefixedVariableName
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
module Rendering
|
module Rendering
|
||||||
|
|
|
@ -7,13 +7,26 @@ module Middleman
|
||||||
# `middleman-core/core_extensions/extensions.rb`.
|
# `middleman-core/core_extensions/extensions.rb`.
|
||||||
module Extensions
|
module Extensions
|
||||||
@registered = {}
|
@registered = {}
|
||||||
|
@auto_activate = {
|
||||||
|
# Activate before the Sitemap is instantiated
|
||||||
|
before_sitemap: [],
|
||||||
|
# Activate the extension before `config.rb` and the `:before_configuration` hook.
|
||||||
|
before_configuration: []
|
||||||
|
}
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
# @api private
|
||||||
# A hash of all registered extensions. Registered extensions are not necessarily active - this
|
# A hash of all registered extensions. Registered extensions are not necessarily active - this
|
||||||
# is the set of all extensions that are known to Middleman.
|
# is the set of all extensions that are known to Middleman.
|
||||||
# @return [Hash{Symbol => Class<Middleman::Extension>, Proc}] A directory of known extensions indexed by the name they were registered under. The value may be a Proc, which can be lazily called to return an extension class.
|
# @return [Hash{Symbol => Class<Middleman::Extension>, Proc}] A directory of known extensions indexed by the name they were registered under. The value may be a Proc, which can be lazily called to return an extension class.
|
||||||
attr_reader :registered
|
attr_reader :registered
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
# A list of extensions that should be automatically loaded at different points in the application startup lifecycle.
|
||||||
|
# Only internal, built-in Middleman extensions should be listed here.
|
||||||
|
# @return [Hash{Symbol => Symbol}] A hash from event name to extension name.
|
||||||
|
attr_reader :auto_activate
|
||||||
|
|
||||||
# Register a new extension. Choose a name which will be
|
# Register a new extension. Choose a name which will be
|
||||||
# used to activate the extension in `config.rb`, like this:
|
# used to activate the extension in `config.rb`, like this:
|
||||||
#
|
#
|
||||||
|
@ -32,16 +45,24 @@ module Middleman
|
||||||
#
|
#
|
||||||
# @param [Symbol] name The name of the extension
|
# @param [Symbol] name The name of the extension
|
||||||
# @param [Class<Middleman::Extension>] extension_class The extension class (Must inherit from {Middleman::Extension})
|
# @param [Class<Middleman::Extension>] extension_class The extension class (Must inherit from {Middleman::Extension})
|
||||||
|
# @option options [Boolean] :auto_activate If this is set to a lifecycle event (:before_configuration or :before_sitemap), this extension will be automatically activated at that point.
|
||||||
|
# This is intended for use with built-in Middleman extensions and should not be used by third-party extensions.
|
||||||
# @yield Instead of passing a module in namespace, you can provide
|
# @yield Instead of passing a module in namespace, you can provide
|
||||||
# a block which returns your extension class. This gives
|
# a block which returns your extension class. This gives
|
||||||
# you the ability to require other files only when the
|
# you the ability to require other files only when the
|
||||||
# extension is first activated.
|
# extension is first activated.
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def register(name, extension_class=nil, &block)
|
def register(name, extension_class=nil, options={}, &block)
|
||||||
raise 'Extension name must be a symbol' unless name.is_a?(Symbol)
|
raise 'Extension name must be a symbol' unless name.is_a?(Symbol)
|
||||||
# If we've already got an extension registered under this name, bail out
|
# If we've already got an extension registered under this name, bail out
|
||||||
raise "There is already an extension registered with the name '#{name}'" if registered.key?(name)
|
raise "There is already an extension registered with the name '#{name}'" if registered.key?(name)
|
||||||
|
|
||||||
|
# If the extension is defined with a block, grab options out of the "extension_class" parameter.
|
||||||
|
if extension_class && block_given? && options.empty? && extension_class.is_a?(Hash)
|
||||||
|
options = extension_class
|
||||||
|
extension_class = nil
|
||||||
|
end
|
||||||
|
|
||||||
registered[name] = if block_given?
|
registered[name] = if block_given?
|
||||||
block
|
block
|
||||||
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
|
elsif extension_class && extension_class.ancestors.include?(::Middleman::Extension)
|
||||||
|
@ -49,6 +70,8 @@ module Middleman
|
||||||
else
|
else
|
||||||
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
|
raise 'You must provide a Middleman::Extension or a block that returns a Middleman::Extension'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@auto_activate[options[:auto_activate]] << name if options[:auto_activate]
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
|
@ -78,6 +101,13 @@ module Middleman
|
||||||
|
|
||||||
extension_class
|
extension_class
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
# A flattened list of all extensions which are automatically activated
|
||||||
|
# @return [Array<Symbol>] A list of extension names which are automatically activated.
|
||||||
|
def auto_activated
|
||||||
|
@auto_activate.values.flatten
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,6 +63,8 @@ module Middleman
|
||||||
extension_config = {}
|
extension_config = {}
|
||||||
|
|
||||||
@middleman.inst.extensions.each do |ext_name, extension|
|
@middleman.inst.extensions.each do |ext_name, extension|
|
||||||
|
next if ::Middleman::Extension.auto_activated.include? ext_name
|
||||||
|
|
||||||
if extension.is_a?(Hash)
|
if extension.is_a?(Hash)
|
||||||
# Multiple instance extension
|
# Multiple instance extension
|
||||||
if extension.size == 1
|
if extension.size == 1
|
||||||
|
@ -72,10 +74,8 @@ module Middleman
|
||||||
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
|
extension_config["#{ext_name} (#{inst})"] = extension_options(ext)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elsif extension.is_a?(::Middleman::Extension)
|
|
||||||
extension_config[ext_name] = extension_options(extension)
|
|
||||||
else
|
else
|
||||||
extension_config[ext_name] = nil
|
extension_config[ext_name] = extension_options(extension)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue