add extension helpers block, convert cache buster
This commit is contained in:
parent
b12a7bff3d
commit
10e1fd92d6
5 changed files with 72 additions and 77 deletions
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,54 +3,44 @@ 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 3rd-party fastimage library
|
||||
require "middleman-more/extensions/automatic_image_sizes/fastimage"
|
||||
|
||||
# Include methods
|
||||
app.send :include, InstanceMethods
|
||||
end
|
||||
helpers do
|
||||
# Override default image_tag helper to automatically calculate and include
|
||||
# image dimensions.
|
||||
#
|
||||
# @param [String] path
|
||||
# @param [Hash] params
|
||||
# @return [String]
|
||||
def image_tag(path, params={})
|
||||
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
|
||||
params[:alt] ||= ""
|
||||
|
||||
alias :included :registered
|
||||
end
|
||||
real_path = path
|
||||
real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
|
||||
full_path = File.join(source_dir, real_path)
|
||||
|
||||
# Automatic Image Sizes Instance Methods
|
||||
module InstanceMethods
|
||||
|
||||
# Override default image_tag helper to automatically calculate and include
|
||||
# image dimensions.
|
||||
#
|
||||
# @param [String] path
|
||||
# @param [Hash] params
|
||||
# @return [String]
|
||||
def image_tag(path, params={})
|
||||
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
|
||||
params[:alt] ||= ""
|
||||
|
||||
real_path = path
|
||||
real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
|
||||
full_path = File.join(source_dir, real_path)
|
||||
|
||||
if File.exists?(full_path)
|
||||
begin
|
||||
width, height = ::FastImage.size(full_path, :raise_on_failure => true)
|
||||
params[:width] = width
|
||||
params[:height] = height
|
||||
rescue FastImage::UnknownImageType
|
||||
# No message, it's just not supported
|
||||
rescue
|
||||
warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
|
||||
if File.exists?(full_path)
|
||||
begin
|
||||
width, height = ::FastImage.size(full_path, :raise_on_failure => true)
|
||||
params[:width] = width
|
||||
params[:height] = height
|
||||
rescue FastImage::UnknownImageType
|
||||
# No message, it's just not supported
|
||||
rescue
|
||||
warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
super(path, params)
|
||||
super(path, params)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,35 +3,26 @@ module Middleman
|
|||
module Extensions
|
||||
|
||||
# The Cache Buster extension
|
||||
module CacheBuster
|
||||
class CacheBuster < ::Middleman::Extension
|
||||
|
||||
# Setup extension
|
||||
class << self
|
||||
def initialize(app, options_hash={}, &block)
|
||||
super
|
||||
|
||||
# Once registered
|
||||
def registered(app)
|
||||
# Add instance methods to context
|
||||
app.send :include, InstanceMethods
|
||||
|
||||
# After compass is setup, make it use the registered cache buster
|
||||
app.compass_config do |config|
|
||||
config.asset_cache_buster do |path, real_path|
|
||||
real_path = real_path.path if real_path.is_a? File
|
||||
real_path = real_path.gsub(File.join(root, build_dir), source)
|
||||
if File.readable?(real_path)
|
||||
File.mtime(real_path).strftime("%s")
|
||||
else
|
||||
logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
|
||||
end
|
||||
# After compass is setup, make it use the registered cache buster
|
||||
app.compass_config do |config|
|
||||
config.asset_cache_buster do |path, real_path|
|
||||
real_path = real_path.path if real_path.is_a? File
|
||||
real_path = real_path.gsub(File.join(root, build_dir), source)
|
||||
if File.readable?(real_path)
|
||||
File.mtime(real_path).strftime("%s")
|
||||
else
|
||||
logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue