1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Clean up extensions a bit. Removes newest form of registering extensions, more consistently sets and uses an extension's ext_name, and makes a lot of things errors instead of just log messages in hopes that people can't get too far with a messed-up config.

This commit is contained in:
Ben Hollis 2014-03-29 14:29:42 -07:00
parent 10eca91311
commit a6c37f3dd3
11 changed files with 107 additions and 75 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

View file

@ -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

@ -27,68 +27,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

@ -88,8 +88,7 @@ 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)
extension = ::Middleman::Extensions.load(ext)
logger.debug "== Activating: #{ext}"
if extension.supports_multiple_instances?
@ -98,17 +97,11 @@ module Middleman
extensions[ext][key] = extension.new(self.class, options, &block)
else
if extensions[ext]
logger.error "== #{ext} already activated."
raise "#{ext} has already been activated and cannot be re-activated."
else
extensions[ext] = extension.new(self.class, options, &block)
end
end
else
logger.error "!! Tried to activate old-style extension: #{ext}"
end
else
logger.error "!! Unknown Extension: #{ext}"
end
end
# Access activated extensions

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