Merge pull request #1237 from bhollis/ext

Clean up extensions a bit
This commit is contained in:
Thomas Reynolds 2014-03-30 10:52:30 -07:00
commit f99e333f30
14 changed files with 120 additions and 100 deletions

View file

@ -11,6 +11,6 @@ class ExtensionA < ::Middleman::Extension
option :hola, '', ''
end
ExtensionA.register
Middleman::Extensions.register :extension_a, ExtensionA
activate :extension_a, :hello => "world", :hola => "mundo"

View file

@ -16,7 +16,7 @@ class ExtensionOne < ::Middleman::Extension
end
end
ExtensionOne.register
Middleman::Extensions.register :extension_one, ExtensionOne
class ExtensionTwo < ::Middleman::Extension
helpers do
@ -36,7 +36,7 @@ class ExtensionTwo < ::Middleman::Extension
end
end
ExtensionTwo.register
Middleman::Extensions.register :extension_two, ExtensionTwo
activate :extension_one
activate :extension_two
activate :extension_two

View file

@ -142,10 +142,6 @@ module Middleman
# Setup custom rendering
include Middleman::CoreExtensions::Rendering
# Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter!
register Middleman::CoreExtensions::FrontMatter
# Sitemap Config options and public api
include Middleman::Sitemap
@ -182,6 +178,10 @@ module Middleman
# Setup the default values from calls to set before initialization
self.class.config.load_settings(self.class.superclass.config.all_settings)
# Parse YAML from templates. Must be before sitemap so sitemap
# extensions see updated frontmatter!
activate :front_matter
# Initialize the Sitemap
@sitemap = ::Middleman::Sitemap::Store.new(self)
@ -196,8 +196,10 @@ module Middleman
activate :default_helpers
activate :lorem
if defined?(Middleman::CoreExtensions::Compass)
begin
activate :compass
rescue LoadError
# Compass is not available, don't complain about it
end
# Evaluate a passed block if given

View file

@ -12,7 +12,10 @@ require 'middleman-core/core_extensions/extensions'
require 'middleman-core/core_extensions/data'
# Parse YAML from templates
require 'middleman-core/core_extensions/front_matter'
Middleman::Extensions.register :front_matter do
require 'middleman-core/core_extensions/front_matter'
Middleman::CoreExtensions::FrontMatter
end
# External helpers looks in the helpers/ folder for helper modules
require 'middleman-core/core_extensions/external_helpers'
@ -27,68 +30,97 @@ require 'middleman-core/core_extensions/routing'
require 'middleman-core/core_extensions/show_exceptions'
# Setup default helpers
require 'middleman-core/core_extensions/default_helpers'
require 'middleman-core/core_extensions/i18n'
Middleman::Extensions.register :default_helpers do
require 'middleman-core/core_extensions/default_helpers'
Middleman::CoreExtensions::DefaultHelpers
end
# Compass framework
begin
Middleman::Extensions.register :compass do
require 'middleman-core/core_extensions/compass'
rescue LoadError
Middleman::CoreExtensions::Compass
end
###
# Setup Optional Extensions
###
Middleman::Extensions.register :i18n do
require 'middleman-core/core_extensions/i18n'
Middleman::CoreExtensions::Internationalization
end
# CacheBuster adds a query string to assets in dynamic templates to
# avoid browser caches failing to update to your new content.
require 'middleman-core/extensions/cache_buster'
Middleman::Extensions::CacheBuster.register
Middleman::Extensions.register :cache_buster do
require 'middleman-core/extensions/cache_buster'
Middleman::Extensions::CacheBuster
end
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.
require 'middleman-core/extensions/relative_assets'
Middleman::Extensions::RelativeAssets.register
Middleman::Extensions.register :relative_assets do
require 'middleman-core/extensions/relative_assets'
Middleman::Extensions::RelativeAssets
end
# AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load.
require 'middleman-core/extensions/asset_host'
Middleman::Extensions::AssetHost.register
Middleman::Extensions.register :asset_host do
require 'middleman-core/extensions/asset_host'
Middleman::Extensions::AssetHost
end
# MinifyCss compresses CSS
require 'middleman-core/extensions/minify_css'
Middleman::Extensions::MinifyCss.register
Middleman::Extensions.register :minify_css do
require 'middleman-core/extensions/minify_css'
Middleman::Extensions::MinifyCss
end
# MinifyJavascript compresses JS
require 'middleman-core/extensions/minify_javascript'
Middleman::Extensions::MinifyJavascript.register
Middleman::Extensions.register :minify_javascript do
require 'middleman-core/extensions/minify_javascript'
Middleman::Extensions::MinifyJavascript
end
# GZIP assets and pages during build
require 'middleman-core/extensions/gzip'
Middleman::Extensions::Gzip.register
Middleman::Extensions.register :gzip do
require 'middleman-core/extensions/gzip'
Middleman::Extensions::Gzip
end
# AssetHash appends a hash of the file contents to the assets filename
# to avoid browser caches failing to update to your new content.
require 'middleman-core/extensions/asset_hash'
Middleman::Extensions::AssetHash.register
Middleman::Extensions.register :asset_hash do
require 'middleman-core/extensions/asset_hash'
Middleman::Extensions::AssetHash
end
# Provide Apache-style index.html files for directories
require 'middleman-core/extensions/directory_indexes'
Middleman::Extensions::DirectoryIndexes.register
Middleman::Extensions.register :directory_indexes do
require 'middleman-core/extensions/directory_indexes'
Middleman::Extensions::DirectoryIndexes
end
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
require 'middleman-core/extensions/lorem'
Middleman::Extensions.register :lorem do
require 'middleman-core/extensions/lorem'
Middleman::Extensions::Lorem
end
# AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML
# elements.
require 'middleman-core/extensions/automatic_image_sizes'
Middleman::Extensions::AutomaticImageSizes.register
Middleman::Extensions.register :automatic_image_sizes do
require 'middleman-core/extensions/automatic_image_sizes'
Middleman::Extensions::AutomaticImageSizes
end
# AutomaticAltTags uses the file name of the `image_tag` to generate
# a default `:alt` value.
require 'middleman-core/extensions/automatic_alt_tags'
Middleman::Extensions::AutomaticAltTags.register
Middleman::Extensions.register :automatic_alt_tags do
require 'middleman-core/extensions/automatic_alt_tags'
Middleman::Extensions::AutomaticAltTags
end

View file

@ -73,5 +73,3 @@ class Middleman::CoreExtensions::Compass < ::Middleman::Extension
end
end
end
Middleman::CoreExtensions::Compass.register

View file

@ -259,5 +259,3 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
end
end
end
Middleman::CoreExtensions::DefaultHelpers.register

View file

@ -62,19 +62,6 @@ module Middleman
def configure(env, &block)
send("#{env}_config", &block)
end
# 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)
if extension.ancestors.include?(::Middleman::Extension)
extension.new(self, options, &block)
else
$stderr.puts "!! Tried to register old-style extension: #{extension}"
end
end
end
# Instance methods
@ -88,26 +75,19 @@ module Middleman
# @param [Symbol, Module] ext Which extension to activate
# @return [void]
def activate(ext, options={}, &block)
if extension = ::Middleman::Extensions.load(ext)
if extension.ancestors.include?(::Middleman::Extension)
logger.debug "== Activating: #{ext}"
extension = ::Middleman::Extensions.load(ext)
logger.debug "== Activating: #{ext}"
if extension.supports_multiple_instances?
extensions[ext] ||= {}
key = "instance_#{extensions[ext].keys.length}"
extensions[ext][key] = extension.new(self.class, options, &block)
else
if extensions[ext]
logger.error "== #{ext} already activated."
else
extensions[ext] = extension.new(self.class, options, &block)
end
end
else
logger.error "!! Tried to activate old-style extension: #{ext}"
end
if extension.supports_multiple_instances?
extensions[ext] ||= {}
key = "instance_#{extensions[ext].keys.length}"
extensions[ext][key] = extension.new(self.class, options, &block)
else
logger.error "!! Unknown Extension: #{ext}"
if extensions[ext]
raise "#{ext} has already been activated and cannot be re-activated."
else
extensions[ext] = extension.new(self.class, options, &block)
end
end
end

View file

@ -32,7 +32,6 @@ module Middleman::CoreExtensions
end
def after_configuration
app.extensions[:frontmatter] = self
app.ignore %r{\.frontmatter$}
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
@ -45,7 +44,7 @@ module Middleman::CoreExtensions
[:layout, :layout_engine].each do |opt|
data[opt] = fmdata[opt] unless fmdata[opt].nil?
end
if fmdata[:renderer_options]
data[:renderer_options] = {}
fmdata[:renderer_options].each do |k, v|
@ -71,7 +70,7 @@ module Middleman::CoreExtensions
# @private
# @return [Hash]
def raw_data
app.extensions[:frontmatter].data(source_file).first
app.extensions[:front_matter].data(source_file).first
end
# This page's frontmatter

View file

@ -201,5 +201,3 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end
end
end
Middleman::CoreExtensions::Internationalization.register(:i18n)

View file

@ -33,14 +33,6 @@ module Middleman
self.defined_helpers += m
end
def extension_name
self.ext_name || self.name.underscore.split('/').last.to_sym
end
def register(n=self.extension_name)
::Middleman::Extensions.register(n, self)
end
def activate
new(::Middleman::Application)
end
@ -56,7 +48,7 @@ module Middleman
end
def activated_extension(instance)
name = instance.class.extension_name
name = instance.class.ext_name
return unless @_extension_activation_callbacks && @_extension_activation_callbacks[name]
@_extension_activation_callbacks[name].each do |block|
block.arity == 1 ? block.call(instance) : block.call()
@ -136,7 +128,7 @@ module Middleman
end
if ext.respond_to?(:manipulate_resource_list)
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
ext.app.sitemap.register_resource_list_manipulator(ext.class.ext_name, ext)
end
end
end

View file

@ -13,7 +13,16 @@ module Middleman
# activate :my_extension
#
# Provide your extension module either as the namespace
# parameter, or return it from the block:
# parameter:
#
# Middleman::Extensions.register(:my_extension, MyExtension)
#
# Or return it from a block:
#
# Middleman::Extensions.register(:my_extension) do
# require 'my_extension'
# MyExtension
# end
#
# @param [Symbol] name The name of the extension
# @param [Module] namespace The extension module
@ -22,34 +31,49 @@ module Middleman
# you the ability to require other files only when the
# extension is activated.
def register(name, namespace=nil, &block)
# If we've already got a matching extension that passed the
# version check, bail out.
return if registered.has_key?(name.to_sym) && !registered[name.to_sym].is_a?(String)
# If we've already got an extension registered under this name, bail out
if registered.has_key?(name.to_sym)
raise "There is already an extension registered with the name '#{name}'"
end
registered[name.to_sym] = if block_given?
block
elsif namespace
elsif namespace && namespace.ancestors.include?(::Middleman::Extension)
namespace
else
raise "You must provide a Middleman::Extension or a block that returns a Middleman::Extension"
end
end
# Load an extension by name, evaluating block definition if necessary.
def load(name)
name = name.to_sym
return nil unless registered.has_key?(name)
unless registered.has_key?(name)
raise "Unknown Extension: #{name}. Check the name and make sure you have referenced the extension's gem in your Gemfile."
end
extension = registered[name]
if extension.is_a?(Proc)
extension = extension.call() || nil
extension = extension.call()
registered[name] = extension
end
if !extension.ancestors.include?(::Middleman::Extension)
raise "Tried to activate old-style extension: #{name}. They are no longer supported."
end
# Set the extension's name to whatever it was registered as.
extension.ext_name = name
extension
end
end
end
# Where to look in gems for extensions to auto-register
# Where to look in gems for extensions to auto-register. Since most extensions are
# called out in a Gemfile, this is really only useful for template extensions that get
# used by "middleman init".
EXTENSION_FILE = File.join('lib', 'middleman_extension.rb') unless const_defined?(:EXTENSION_FILE)
class << self

View file

@ -174,5 +174,3 @@ class Middleman::Extensions::Lorem < ::Middleman::Extension
end
end
end
Middleman::Extensions::Lorem.register

View file

@ -84,8 +84,8 @@ module Middleman
# @param [String] path
# @return [String]
def get_template_data_for_file
if @app.extensions[:frontmatter]
@app.extensions[:frontmatter].template_data_for_file(@path)
if @app.extensions[:front_matter]
@app.extensions[:front_matter].template_data_for_file(@path)
else
File.read(File.expand_path(@path, source_dir))
end
@ -116,4 +116,4 @@ module Middleman
end
end
end
end
end

View file

@ -1,6 +1,5 @@
# Core Sitemap Extensions
module Middleman
module Sitemap
# Setup Extension