middleman/middleman-core/lib/middleman-core/core_extensions/extensions.rb

152 lines
4.2 KiB
Ruby
Raw Normal View History

# 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
2011-10-04 02:09:27 +02:00
# with the `app` parameter which is a Middleman::Base context. From here
# 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-11-27 01:23:38 +01:00
# Using for version parsing
require "rubygems"
# Namespace extensions module
module Middleman::CoreExtensions::Extensions
# Register extension
class << self
# @private
2011-11-18 04:56:55 +01:00
def included(app)
2011-11-24 06:59:53 +01:00
# app.set :default_extensions, []
2011-11-09 00:45:22 +01:00
app.define_hook :after_configuration
app.define_hook :before_configuration
app.define_hook :build_config
app.define_hook :development_config
2011-11-28 07:04:19 +01:00
app.extend ClassMethods
2011-11-18 04:56:55 +01:00
app.send :include, InstanceMethods
app.delegate :configure, :to => :"self.class"
end
end
# 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
# Alias `extensions` to access registered extensions
#
# @return [Array<Module]
2011-11-18 04:56:55 +01:00
def extensions
@extensions ||= []
end
# Register a new extension
#
# @param [Array<Module>] new_extensions Extension modules to register
# @return [Array<Module]
def register(extension, options={}, &block)
2011-11-18 04:56:55 +01:00
@extensions ||= []
@extensions += [extension]
extend extension
if extension.respond_to?(:registered)
if extension.method(:registered).arity === 1
extension.registered(self, &block)
else
extension.registered(self, options, &block)
end
2011-11-18 04:56:55 +01:00
end
end
end
# Instance methods
2011-11-18 04:56:55 +01:00
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)
2012-03-11 04:40:04 +01:00
# Make :i18n a no-op
return if ext == :i18n
ext_module = if ext.is_a?(Module)
ext
else
::Middleman::Extensions.load(ext.to_sym)
end
if ext_module.nil?
puts "== Unknown Extension: #{ext}"
else
puts "== Activating: #{ext}" if logging?
self.class.register(ext_module, options, &block)
end
end
# Load features before starting server
2011-11-18 04:56:55 +01:00
def initialize
super
self.class.inst = self
run_hook :before_configuration
2011-12-06 19:28:55 +01:00
# Search the root of the project for required files
$LOAD_PATH.unshift(root)
# Check for and evaluate local configuration
2011-11-21 06:21:19 +01:00
local_config = File.join(root, "config.rb")
if File.exists? local_config
2011-11-08 07:34:02 +01:00
puts "== Reading: Local config" if logging?
instance_eval File.read(local_config), local_config, 1
end
run_hook :build_config if build?
run_hook :development_config if development?
2011-11-18 04:56:55 +01:00
run_hook :after_configuration
# Add in defaults
2011-11-24 06:59:53 +01:00
default_extensions.each do |ext|
2011-11-30 08:43:01 +01:00
activate ext
end
if logging?
self.class.extensions.each do |ext|
2011-11-08 07:34:02 +01:00
puts "== Extension: #{ext}"
end
end
end
end
end