2011-07-06 19:15:31 +02:00
|
|
|
# Middleman provides an extension API which allows you to hook into the
|
|
|
|
# lifecycle of a page request, or static build, and manipulate the output.
|
|
|
|
# Internal to Middleman, these extensions are called "features," but we use
|
|
|
|
# the exact same API as is made available to the public.
|
|
|
|
#
|
|
|
|
# A Middleman extension looks like this:
|
|
|
|
#
|
|
|
|
# module MyExtension
|
|
|
|
# class << self
|
|
|
|
# def registered(app)
|
|
|
|
# # My Code
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# In your `config.rb`, you must load your extension (if it is not defined in
|
|
|
|
# that file) and call `activate`.
|
|
|
|
#
|
|
|
|
# require "my_extension"
|
|
|
|
# activate MyExtension
|
|
|
|
#
|
|
|
|
# This will call the `registered` method in your extension and provide you
|
2012-04-14 23:04:10 +02:00
|
|
|
# with the `app` parameter which is a Middleman::Application context. From here
|
2011-07-06 19:15:31 +02:00
|
|
|
# you can choose to respond to requests for certain paths or simply attach
|
|
|
|
# Rack middleware to the stack.
|
|
|
|
#
|
|
|
|
# The built-in features cover a wide range of functions. Some provide helper
|
|
|
|
# methods to use in your views. Some modify the output on-the-fly. And some
|
|
|
|
# apply computationally-intensive changes to your final build files.
|
|
|
|
|
2011-12-22 06:52:38 +01:00
|
|
|
# Namespace extensions module
|
2012-05-07 23:41:39 +02:00
|
|
|
module Middleman
|
|
|
|
module CoreExtensions
|
|
|
|
module Extensions
|
2011-07-06 19:15:31 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Register extension
|
|
|
|
class << self
|
|
|
|
# @private
|
2012-05-20 01:49:44 +02:00
|
|
|
def registered(app)
|
2012-05-07 23:41:39 +02:00
|
|
|
# Using for version parsing
|
|
|
|
require "rubygems"
|
|
|
|
|
|
|
|
app.define_hook :after_configuration
|
|
|
|
app.define_hook :before_configuration
|
|
|
|
app.define_hook :build_config
|
|
|
|
app.define_hook :development_config
|
2012-05-24 23:29:29 +02:00
|
|
|
|
|
|
|
if ENV["AUTOLOAD_SPROCKETS"]
|
|
|
|
app.set :autoload_sprockets, (ENV["AUTOLOAD_SPROCKETS"] == "true")
|
|
|
|
else
|
|
|
|
app.set :autoload_sprockets, true
|
|
|
|
end
|
2011-11-28 07:04:19 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
app.extend ClassMethods
|
|
|
|
app.send :include, InstanceMethods
|
|
|
|
app.delegate :configure, :to => :"self.class"
|
|
|
|
end
|
2012-05-20 01:49:44 +02:00
|
|
|
alias :included :registered
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2011-07-06 19:15:31 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Class methods
|
|
|
|
module ClassMethods
|
|
|
|
# Add a callback to run in a specific environment
|
|
|
|
#
|
|
|
|
# @param [String, Symbol] env The environment to run in
|
|
|
|
# @return [void]
|
|
|
|
def configure(env, &block)
|
|
|
|
send("#{env}_config", &block)
|
|
|
|
end
|
2011-11-19 07:57:25 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Alias `extensions` to access registered extensions
|
|
|
|
#
|
|
|
|
# @return [Array<Module>]
|
|
|
|
def extensions
|
|
|
|
@extensions ||= []
|
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Register a new extension
|
|
|
|
#
|
|
|
|
# @param [Module] extension Extension modules to register
|
|
|
|
# @param [Hash] options Per-extension options hash
|
|
|
|
# @return [void]
|
|
|
|
def register(extension, options={}, &block)
|
|
|
|
@extensions ||= []
|
|
|
|
@extensions += [extension]
|
2011-12-25 19:06:45 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
extend extension
|
|
|
|
if extension.respond_to?(:registered)
|
|
|
|
if extension.method(:registered).arity === 1
|
|
|
|
extension.registered(self, &block)
|
|
|
|
else
|
|
|
|
extension.registered(self, options, &block)
|
|
|
|
end
|
|
|
|
end
|
2011-12-25 19:06:45 +01:00
|
|
|
end
|
2011-11-18 04:56:55 +01:00
|
|
|
end
|
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Instance methods
|
|
|
|
module InstanceMethods
|
|
|
|
# This method is available in the project's `config.rb`.
|
|
|
|
# It takes a underscore-separated symbol, finds the appropriate
|
|
|
|
# feature module and includes it.
|
|
|
|
#
|
|
|
|
# activate :lorem
|
|
|
|
#
|
|
|
|
# @param [Symbol, Module] ext Which extension to activate
|
|
|
|
# @return [void]
|
|
|
|
def activate(ext, options={}, &block)
|
|
|
|
ext_module = if ext.is_a?(Module)
|
|
|
|
ext
|
|
|
|
else
|
|
|
|
::Middleman::Extensions.load(ext.to_sym)
|
|
|
|
end
|
2011-07-13 09:38:04 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
if ext_module.nil?
|
2012-07-15 20:04:45 +02:00
|
|
|
logger.warning "== Unknown Extension: #{ext}"
|
2012-05-07 23:41:39 +02:00
|
|
|
else
|
2012-07-15 20:04:45 +02:00
|
|
|
logger.debug "== Activating: #{ext}"
|
2012-05-07 23:41:39 +02:00
|
|
|
self.class.register(ext_module, options, &block)
|
|
|
|
end
|
|
|
|
end
|
2011-07-06 19:15:31 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Load features before starting server
|
|
|
|
def initialize
|
|
|
|
super
|
2011-12-04 20:58:45 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
self.class.inst = self
|
|
|
|
run_hook :before_configuration
|
2011-11-18 01:02:07 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Search the root of the project for required files
|
|
|
|
$LOAD_PATH.unshift(root)
|
2011-12-06 19:28:55 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# Check for and evaluate local configuration
|
|
|
|
local_config = File.join(root, "config.rb")
|
|
|
|
if File.exists? local_config
|
2012-07-15 20:04:45 +02:00
|
|
|
logger.debug "== Reading: Local config"
|
2012-05-07 23:41:39 +02:00
|
|
|
instance_eval File.read(local_config), local_config, 1
|
|
|
|
end
|
2012-05-24 23:29:29 +02:00
|
|
|
|
2012-05-26 23:15:36 +02:00
|
|
|
if autoload_sprockets
|
2012-05-28 22:30:54 +02:00
|
|
|
begin
|
|
|
|
require "middleman-sprockets"
|
|
|
|
activate(:sprockets)
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2012-05-26 23:15:36 +02:00
|
|
|
end
|
2011-11-19 07:57:25 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
run_hook :build_config if build?
|
|
|
|
run_hook :development_config if development?
|
2011-11-19 07:57:25 +01:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
run_hook :after_configuration
|
2011-07-06 19:15:31 +02:00
|
|
|
|
2012-07-15 20:04:45 +02:00
|
|
|
logger.debug "Loaded extensions:"
|
|
|
|
self.class.extensions.each do |ext|
|
|
|
|
logger.debug "== Extension: #{ext}"
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2011-07-13 09:38:04 +02:00
|
|
|
end
|
|
|
|
end
|
2011-07-06 19:15:31 +02:00
|
|
|
end
|
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|