add extension helpers block, convert cache buster

This commit is contained in:
Thomas Reynolds 2013-04-20 13:32:39 -07:00
parent b12a7bff3d
commit 10e1fd92d6
5 changed files with 72 additions and 77 deletions

View file

@ -105,6 +105,7 @@ module Middleman
class Extension
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false
class << self
def config
@ -114,11 +115,21 @@ module Middleman
def option(key, default=nil, description=nil)
config.define_setting(key, default, description)
end
def helpers(&block)
self.defined_helpers ||= []
m = Module.new
m.module_eval(&block)
self.defined_helpers << m
end
end
attr_accessor :app, :options
def initialize(klass, options_hash={})
@_helpers = []
@options = self.class.config.dup
@options.finalize!
@ -131,6 +142,10 @@ module Middleman
ext = self
klass.initialized do
ext.app = self
(ext.class.defined_helpers || []).each do |m|
ext.app.class.send(:include, m)
end
end
klass.after_configuration(&method(:after_configuration))

View file

@ -51,16 +51,6 @@ module Middleman
# No line-comments in test mode (changing paths mess with sha1)
compass_config.line_comments = false if ENV["TEST"]
if extensions[:asset_host] && asset_host = extensions[:asset_host].host
if asset_host.is_a?(Proc)
compass_config.asset_host(&asset_host)
else
compass_config.asset_host do |asset|
asset_host
end
end
end
end
# Call hook

View file

@ -11,16 +11,25 @@ module Middleman
# Backwards compatible API
app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
app.send :include, InstanceMethods
app.compass_config do |config|
if asset_host = extensions[:asset_host].host
if asset_host.is_a?(Proc)
config.asset_host(&asset_host)
else
config.asset_host do |asset|
asset_host
end
end
end
end
end
def host
app.config[:asset_host] || options[:host]
end
# Asset Host Instance Methods
module InstanceMethods
helpers do
# Override default asset url helper to include asset hosts
#
# @param [String] path

View file

@ -3,26 +3,15 @@ module Middleman
module Extensions
# Automatic Image Sizes extension
module AutomaticImageSizes
class AutomaticImageSizes < ::Middleman::Extension
# Setup extension
class << self
def initialize(app, options_hash={}, &block)
super
# Once registered
def registered(app)
# Include 3rd-party fastimage library
require "middleman-more/extensions/automatic_image_sizes/fastimage"
# Include methods
app.send :include, InstanceMethods
end
alias :included :registered
end
# Automatic Image Sizes Instance Methods
module InstanceMethods
helpers do
# Override default image_tag helper to automatically calculate and include
# image dimensions.
#
@ -56,3 +45,4 @@ module Middleman
end
end
end
end

View file

@ -3,15 +3,10 @@ module Middleman
module Extensions
# The Cache Buster extension
module CacheBuster
class CacheBuster < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
# Add instance methods to context
app.send :include, InstanceMethods
def initialize(app, options_hash={}, &block)
super
# After compass is setup, make it use the registered cache buster
app.compass_config do |config|
@ -26,12 +21,8 @@ module Middleman
end
end
end
alias :included :registered
end
# Cache buster instance methods
module InstanceMethods
helpers do
# asset_url override if we're using cache busting
# @param [String] path
# @param [String] prefix