add extension helpers block, convert cache buster
This commit is contained in:
parent
b12a7bff3d
commit
10e1fd92d6
|
@ -105,6 +105,7 @@ module Middleman
|
||||||
|
|
||||||
class Extension
|
class Extension
|
||||||
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
|
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
|
||||||
|
class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def config
|
def config
|
||||||
|
@ -114,11 +115,21 @@ module Middleman
|
||||||
def option(key, default=nil, description=nil)
|
def option(key, default=nil, description=nil)
|
||||||
config.define_setting(key, default, description)
|
config.define_setting(key, default, description)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def helpers(&block)
|
||||||
|
self.defined_helpers ||= []
|
||||||
|
|
||||||
|
m = Module.new
|
||||||
|
m.module_eval(&block)
|
||||||
|
self.defined_helpers << m
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :app, :options
|
attr_accessor :app, :options
|
||||||
|
|
||||||
def initialize(klass, options_hash={})
|
def initialize(klass, options_hash={})
|
||||||
|
@_helpers = []
|
||||||
|
|
||||||
@options = self.class.config.dup
|
@options = self.class.config.dup
|
||||||
@options.finalize!
|
@options.finalize!
|
||||||
|
|
||||||
|
@ -131,6 +142,10 @@ module Middleman
|
||||||
ext = self
|
ext = self
|
||||||
klass.initialized do
|
klass.initialized do
|
||||||
ext.app = self
|
ext.app = self
|
||||||
|
|
||||||
|
(ext.class.defined_helpers || []).each do |m|
|
||||||
|
ext.app.class.send(:include, m)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
klass.after_configuration(&method(:after_configuration))
|
klass.after_configuration(&method(:after_configuration))
|
||||||
|
|
|
@ -51,16 +51,6 @@ module Middleman
|
||||||
|
|
||||||
# No line-comments in test mode (changing paths mess with sha1)
|
# No line-comments in test mode (changing paths mess with sha1)
|
||||||
compass_config.line_comments = false if ENV["TEST"]
|
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
|
end
|
||||||
|
|
||||||
# Call hook
|
# Call hook
|
||||||
|
|
|
@ -11,16 +11,25 @@ module Middleman
|
||||||
|
|
||||||
# Backwards compatible API
|
# 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.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
|
end
|
||||||
|
|
||||||
def host
|
def host
|
||||||
app.config[:asset_host] || options[:host]
|
app.config[:asset_host] || options[:host]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Asset Host Instance Methods
|
helpers do
|
||||||
module InstanceMethods
|
|
||||||
|
|
||||||
# Override default asset url helper to include asset hosts
|
# Override default asset url helper to include asset hosts
|
||||||
#
|
#
|
||||||
# @param [String] path
|
# @param [String] path
|
||||||
|
|
|
@ -3,54 +3,44 @@ module Middleman
|
||||||
module Extensions
|
module Extensions
|
||||||
|
|
||||||
# Automatic Image Sizes extension
|
# Automatic Image Sizes extension
|
||||||
module AutomaticImageSizes
|
class AutomaticImageSizes < ::Middleman::Extension
|
||||||
|
|
||||||
# Setup extension
|
def initialize(app, options_hash={}, &block)
|
||||||
class << self
|
super
|
||||||
|
|
||||||
# Once registered
|
# Include 3rd-party fastimage library
|
||||||
def registered(app)
|
require "middleman-more/extensions/automatic_image_sizes/fastimage"
|
||||||
# Include 3rd-party fastimage library
|
|
||||||
require "middleman-more/extensions/automatic_image_sizes/fastimage"
|
|
||||||
|
|
||||||
# Include methods
|
helpers do
|
||||||
app.send :include, InstanceMethods
|
# Override default image_tag helper to automatically calculate and include
|
||||||
end
|
# 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
|
real_path = path
|
||||||
end
|
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
|
if File.exists?(full_path)
|
||||||
module InstanceMethods
|
begin
|
||||||
|
width, height = ::FastImage.size(full_path, :raise_on_failure => true)
|
||||||
# Override default image_tag helper to automatically calculate and include
|
params[:width] = width
|
||||||
# image dimensions.
|
params[:height] = height
|
||||||
#
|
rescue FastImage::UnknownImageType
|
||||||
# @param [String] path
|
# No message, it's just not supported
|
||||||
# @param [Hash] params
|
rescue
|
||||||
# @return [String]
|
warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
|
||||||
def image_tag(path, params={})
|
end
|
||||||
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}"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
super(path, params)
|
super(path, params)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,35 +3,26 @@ module Middleman
|
||||||
module Extensions
|
module Extensions
|
||||||
|
|
||||||
# The Cache Buster extension
|
# The Cache Buster extension
|
||||||
module CacheBuster
|
class CacheBuster < ::Middleman::Extension
|
||||||
|
|
||||||
# Setup extension
|
def initialize(app, options_hash={}, &block)
|
||||||
class << self
|
super
|
||||||
|
|
||||||
# Once registered
|
# After compass is setup, make it use the registered cache buster
|
||||||
def registered(app)
|
app.compass_config do |config|
|
||||||
# Add instance methods to context
|
config.asset_cache_buster do |path, real_path|
|
||||||
app.send :include, InstanceMethods
|
real_path = real_path.path if real_path.is_a? File
|
||||||
|
real_path = real_path.gsub(File.join(root, build_dir), source)
|
||||||
# After compass is setup, make it use the registered cache buster
|
if File.readable?(real_path)
|
||||||
app.compass_config do |config|
|
File.mtime(real_path).strftime("%s")
|
||||||
config.asset_cache_buster do |path, real_path|
|
else
|
||||||
real_path = real_path.path if real_path.is_a? File
|
logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
|
||||||
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :included :registered
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Cache buster instance methods
|
helpers do
|
||||||
module InstanceMethods
|
|
||||||
|
|
||||||
# asset_url override if we're using cache busting
|
# asset_url override if we're using cache busting
|
||||||
# @param [String] path
|
# @param [String] path
|
||||||
# @param [String] prefix
|
# @param [String] prefix
|
||||||
|
|
Loading…
Reference in a new issue