Replace hooks
This commit is contained in:
parent
b9f0330869
commit
86bbcb7676
6 changed files with 37 additions and 56 deletions
|
@ -9,9 +9,6 @@ require 'i18n'
|
||||||
require 'active_support/json'
|
require 'active_support/json'
|
||||||
require 'active_support/core_ext/integer/inflections'
|
require 'active_support/core_ext/integer/inflections'
|
||||||
|
|
||||||
# Simple callback library
|
|
||||||
require 'hooks'
|
|
||||||
|
|
||||||
# Our custom logger
|
# Our custom logger
|
||||||
require 'middleman-core/logger'
|
require 'middleman-core/logger'
|
||||||
|
|
||||||
|
@ -21,6 +18,7 @@ require 'middleman-core/sitemap/store'
|
||||||
|
|
||||||
require 'middleman-core/configuration'
|
require 'middleman-core/configuration'
|
||||||
|
|
||||||
|
require 'middleman-core/callback_manager'
|
||||||
require 'middleman-core/extension_manager'
|
require 'middleman-core/extension_manager'
|
||||||
require 'middleman-core/core_extensions'
|
require 'middleman-core/core_extensions'
|
||||||
|
|
||||||
|
@ -56,31 +54,6 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Uses callbacks
|
|
||||||
include Hooks
|
|
||||||
include Hooks::InstanceHooks
|
|
||||||
|
|
||||||
define_hook :initialized
|
|
||||||
define_hook :after_configuration
|
|
||||||
define_hook :before_configuration
|
|
||||||
|
|
||||||
# Before request hook
|
|
||||||
define_hook :before
|
|
||||||
|
|
||||||
# Ready (all loading and parsing of extensions complete) hook
|
|
||||||
define_hook :ready
|
|
||||||
|
|
||||||
# Runs before the build is started
|
|
||||||
define_hook :before_build
|
|
||||||
|
|
||||||
# Runs after the build is finished
|
|
||||||
define_hook :after_build
|
|
||||||
|
|
||||||
define_hook :before_shutdown
|
|
||||||
|
|
||||||
define_hook :before_render
|
|
||||||
define_hook :after_render
|
|
||||||
|
|
||||||
# Which host preview should start on.
|
# Which host preview should start on.
|
||||||
# @return [Fixnum]
|
# @return [Fixnum]
|
||||||
config.define_setting :host, '0.0.0.0', 'The preview server host'
|
config.define_setting :host, '0.0.0.0', 'The preview server host'
|
||||||
|
@ -209,6 +182,20 @@ module Middleman
|
||||||
# Search the root of the project for required files
|
# Search the root of the project for required files
|
||||||
$LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root)
|
$LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root)
|
||||||
|
|
||||||
|
@callbacks = ::Middleman::CallbackManager.new
|
||||||
|
@callbacks.install_methods!(self, [
|
||||||
|
:initialized,
|
||||||
|
:after_configuration,
|
||||||
|
:before_configuration,
|
||||||
|
:before,
|
||||||
|
:ready,
|
||||||
|
:before_build,
|
||||||
|
:after_build,
|
||||||
|
:before_shutdown,
|
||||||
|
:before_render,
|
||||||
|
:after_render
|
||||||
|
])
|
||||||
|
|
||||||
@middleware = Set.new
|
@middleware = Set.new
|
||||||
@mappings = Set.new
|
@mappings = Set.new
|
||||||
|
|
||||||
|
@ -244,9 +231,9 @@ module Middleman
|
||||||
|
|
||||||
@extensions.auto_activate(:before_configuration)
|
@extensions.auto_activate(:before_configuration)
|
||||||
|
|
||||||
run_hook :initialized
|
execute_callbacks :initialized
|
||||||
|
|
||||||
run_hook :before_configuration
|
execute_callbacks :before_configuration
|
||||||
|
|
||||||
evaluate_configuration
|
evaluate_configuration
|
||||||
|
|
||||||
|
@ -270,10 +257,10 @@ module Middleman
|
||||||
|
|
||||||
@extensions.activate_all
|
@extensions.activate_all
|
||||||
|
|
||||||
run_hook :after_configuration
|
execute_callbacks :after_configuration
|
||||||
config_context.execute_callbacks(:after_configuration)
|
config_context.execute_callbacks(:after_configuration)
|
||||||
|
|
||||||
run_hook :ready
|
execute_callbacks :ready
|
||||||
@config_context.execute_callbacks(:ready)
|
@config_context.execute_callbacks(:ready)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -365,7 +352,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
def shutdown!
|
def shutdown!
|
||||||
run_hook :before_shutdown
|
execute_callbacks :before_shutdown
|
||||||
end
|
end
|
||||||
|
|
||||||
# Work around this bug: http://bugs.ruby-lang.org/issues/4521
|
# Work around this bug: http://bugs.ruby-lang.org/issues/4521
|
||||||
|
|
|
@ -49,7 +49,7 @@ module Middleman
|
||||||
@has_error = false
|
@has_error = false
|
||||||
@events = {}
|
@events = {}
|
||||||
|
|
||||||
@app.run_hook :before_build, self
|
@app.execute_callbacks :before_build, [self]
|
||||||
|
|
||||||
queue_current_paths if @cleaning
|
queue_current_paths if @cleaning
|
||||||
prerender_css
|
prerender_css
|
||||||
|
@ -61,7 +61,7 @@ module Middleman
|
||||||
::Middleman::Profiling.report('build')
|
::Middleman::Profiling.report('build')
|
||||||
|
|
||||||
# Run hooks
|
# Run hooks
|
||||||
@app.run_hook :after_build, self
|
@app.execute_callbacks :after_build, [self]
|
||||||
@app.config_context.execute_callbacks(:after_build, [self])
|
@app.config_context.execute_callbacks(:after_build, [self])
|
||||||
|
|
||||||
!@has_error
|
!@has_error
|
||||||
|
|
|
@ -25,8 +25,10 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
install_target.define_singleton_method(:execute_callbacks) do |keys, *args|
|
install_target.define_singleton_method(:execute_callbacks) do |keys, *args|
|
||||||
manager.execute(keys, args, self)
|
manager.execute(keys, args[0], self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
install_target.define_singleton_method(:callbacks_for, &method(:callbacks_for))
|
||||||
end
|
end
|
||||||
|
|
||||||
Contract Or[Symbol, ArrayOf[Symbol]], Proc => Any
|
Contract Or[Symbol, ArrayOf[Symbol]], Proc => Any
|
||||||
|
@ -40,10 +42,13 @@ module Middleman
|
||||||
|
|
||||||
Contract Or[Symbol, ArrayOf[Symbol]], Maybe[ArrayOf[Any]], Maybe[RespondTo[:instance_exec]] => Any
|
Contract Or[Symbol, ArrayOf[Symbol]], Maybe[ArrayOf[Any]], Maybe[RespondTo[:instance_exec]] => Any
|
||||||
def execute(keys, args=[], scope=self)
|
def execute(keys, args=[], scope=self)
|
||||||
immutable_keys = keys.is_a?(Symbol) ? keys : ::Hamster::Vector.new(keys)
|
callbacks_for(keys).each { |b| scope.instance_exec(*args, &b) }
|
||||||
|
end
|
||||||
|
|
||||||
callbacks = @callbacks.get(immutable_keys)
|
Contract Or[Symbol, ArrayOf[Symbol]] => ::Hamster::Set
|
||||||
callbacks && callbacks.each { |b| scope.instance_exec(*args, &b) }
|
def callbacks_for(keys)
|
||||||
|
immutable_keys = keys.is_a?(Symbol) ? keys : ::Hamster::Vector.new(keys)
|
||||||
|
@callbacks.get(immutable_keys) || ::Hamster.set
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,14 +59,10 @@ module Middleman
|
||||||
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
|
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
|
||||||
|
|
||||||
template_class = ::Tilt[path]
|
template_class = ::Tilt[path]
|
||||||
|
|
||||||
# Allow hooks to manipulate the template before render
|
# Allow hooks to manipulate the template before render
|
||||||
@app.class.callbacks_for_hook(:before_render).each do |callback|
|
content = @app.callbacks_for(:before_render).reduce(content) do |sum, callback|
|
||||||
newbody = if callback.respond_to?(:call)
|
callback.call(content, path, locs, template_class) || sum
|
||||||
callback.call(body, path, locs, template_class)
|
|
||||||
elsif callback.respond_to?(:evaluate)
|
|
||||||
callback.evaluate(self, body, path, locs, template_class)
|
|
||||||
end
|
|
||||||
body = newbody if newbody # Allow the callback to return nil to skip it
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read compiled template from disk or cache
|
# Read compiled template from disk or cache
|
||||||
|
@ -80,14 +76,8 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
# Allow hooks to manipulate the result after render
|
# Allow hooks to manipulate the result after render
|
||||||
@app.class.callbacks_for_hook(:after_render).each do |callback|
|
content = @app.callbacks_for(:after_render).reduce(content) do |sum, callback|
|
||||||
# Uber::Options::Value doesn't respond to call
|
callback.call(content, path, locs, template_class) || sum
|
||||||
newcontent = if callback.respond_to?(:call)
|
|
||||||
callback.call(content, path, locs, template_class)
|
|
||||||
elsif callback.respond_to?(:evaluate)
|
|
||||||
callback.evaluate(self, content, path, locs, template_class)
|
|
||||||
end
|
|
||||||
content = newcontent if newcontent # Allow the callback to return nil to skip it
|
|
||||||
end
|
end
|
||||||
|
|
||||||
output = ::ActiveSupport::SafeBuffer.new ''
|
output = ::ActiveSupport::SafeBuffer.new ''
|
||||||
|
|
|
@ -85,7 +85,7 @@ module Middleman
|
||||||
full_request_path = File.join(env['SCRIPT_NAME'], request_path) # Path including rack mount
|
full_request_path = File.join(env['SCRIPT_NAME'], request_path) # Path including rack mount
|
||||||
|
|
||||||
# Run before callbacks
|
# Run before callbacks
|
||||||
@middleman.run_hook :before
|
@middleman.execute_callbacks :before
|
||||||
|
|
||||||
# Get the resource object for this path
|
# Get the resource object for this path
|
||||||
resource = @middleman.sitemap.find_resource_by_destination_path(request_path.gsub(' ', '%20'))
|
resource = @middleman.sitemap.find_resource_by_destination_path(request_path.gsub(' ', '%20'))
|
||||||
|
|
|
@ -24,7 +24,6 @@ Gem::Specification.new do |s|
|
||||||
s.add_dependency('rack', ['>= 1.4.5', '< 2.0'])
|
s.add_dependency('rack', ['>= 1.4.5', '< 2.0'])
|
||||||
s.add_dependency('tilt', ['~> 1.4.1'])
|
s.add_dependency('tilt', ['~> 1.4.1'])
|
||||||
s.add_dependency('erubis')
|
s.add_dependency('erubis')
|
||||||
s.add_dependency('hooks', ['~> 0.3'])
|
|
||||||
|
|
||||||
# Helpers
|
# Helpers
|
||||||
s.add_dependency('activesupport', ['~> 4.2.0'])
|
s.add_dependency('activesupport', ['~> 4.2.0'])
|
||||||
|
|
Loading…
Reference in a new issue