make assets a core ext
This commit is contained in:
parent
59a07d4cc0
commit
118d23be41
|
@ -76,6 +76,9 @@ module Middleman
|
||||||
# Custom Feature API
|
# Custom Feature API
|
||||||
autoload :Features, "middleman/core_extensions/features"
|
autoload :Features, "middleman/core_extensions/features"
|
||||||
|
|
||||||
|
# Asset Path Pipeline
|
||||||
|
autoload :Assets, "middleman/core_extensions/assets"
|
||||||
|
|
||||||
# DefaultHelpers are the built-in dynamic template helpers.
|
# DefaultHelpers are the built-in dynamic template helpers.
|
||||||
autoload :DefaultHelpers, "middleman/core_extensions/default_helpers"
|
autoload :DefaultHelpers, "middleman/core_extensions/default_helpers"
|
||||||
|
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
module Middleman
|
|
||||||
module Assets
|
|
||||||
@@asset_handler_map = []
|
|
||||||
@@asset_handler_stack = []
|
|
||||||
|
|
||||||
def self.register(handler_name, &block)
|
|
||||||
if block_given?
|
|
||||||
@@asset_handler_stack << block
|
|
||||||
@@asset_handler_map << handler_name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_url(path, prefix="", request=nil)
|
|
||||||
@@asset_handler_stack.last.call(path, prefix, request)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.before(position, *args)
|
|
||||||
current_index = @@asset_handler_map.index(position)
|
|
||||||
return nil unless current_index
|
|
||||||
|
|
||||||
previous = current_index - 1
|
|
||||||
if (previous >= 0) && (previous < @@asset_handler_map.length)
|
|
||||||
@@asset_handler_stack[previous].call(*args)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Middleman::Assets.register :base do |path, prefix, request|
|
|
||||||
path.include?("://") ? path : File.join(Middleman::Server.http_prefix || "/", prefix, path)
|
|
||||||
end
|
|
54
lib/middleman/core_extensions/assets.rb
Normal file
54
lib/middleman/core_extensions/assets.rb
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
module Middleman::CoreExtensions::Assets
|
||||||
|
class << self
|
||||||
|
def registered(app)
|
||||||
|
app.extend ClassMethods
|
||||||
|
|
||||||
|
app.helpers Helpers
|
||||||
|
|
||||||
|
app.register_asset_handler :base do |path, prefix, request|
|
||||||
|
path.include?("://") ? path : File.join(app.http_prefix || "/", prefix, path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
alias :included :registered
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def register_asset_handler(handler_name, &block)
|
||||||
|
@asset_handler_map ||= []
|
||||||
|
@asset_handler_stack ||= []
|
||||||
|
|
||||||
|
if block_given?
|
||||||
|
@asset_handler_stack << block
|
||||||
|
@asset_handler_map << handler_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def asset_handler_get_url(path, prefix="", request=nil)
|
||||||
|
@asset_handler_map ||= []
|
||||||
|
@asset_handler_stack ||= []
|
||||||
|
|
||||||
|
@asset_handler_stack.last.call(path, prefix, request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_asset_handler(position, *args)
|
||||||
|
@asset_handler_map ||= []
|
||||||
|
@asset_handler_stack ||= []
|
||||||
|
|
||||||
|
current_index = @asset_handler_map.index(position)
|
||||||
|
return nil unless current_index
|
||||||
|
|
||||||
|
previous = current_index - 1
|
||||||
|
if (previous >= 0) && (previous < @asset_handler_map.length)
|
||||||
|
@asset_handler_stack[previous].call(*args)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Helpers
|
||||||
|
def asset_url(path, prefix="")
|
||||||
|
self.class.asset_handler_get_url(path, prefix, request)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -56,10 +56,6 @@ module Middleman::CoreExtensions::DefaultHelpers
|
||||||
classes.join(' ')
|
classes.join(' ')
|
||||||
end
|
end
|
||||||
|
|
||||||
def asset_url(path, prefix="")
|
|
||||||
Middleman::Assets.get_url(path, prefix, request)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Padrino's asset handling needs to pass through ours
|
# Padrino's asset handling needs to pass through ours
|
||||||
def asset_path(kind, source)
|
def asset_path(kind, source)
|
||||||
return source if source =~ /^http/
|
return source if source =~ /^http/
|
||||||
|
|
|
@ -2,17 +2,17 @@ module Middleman::Features::AssetHost
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
app.after_feature_init do
|
app.after_feature_init do
|
||||||
if Middleman::Server.asset_host.is_a?(Proc)
|
if app.asset_host.is_a?(Proc)
|
||||||
::Compass.configuration.asset_host(&Middleman::Server.asset_host)
|
::Compass.configuration.asset_host(&app.asset_host)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Middleman::Assets.register :asset_host do |path, prefix, request|
|
app.register_asset_handler :asset_host do |path, prefix, request|
|
||||||
original_output = Middleman::Assets.before(:asset_host, path, prefix, request)
|
original_output = app.before_asset_handler(:asset_host, path, prefix, request)
|
||||||
|
|
||||||
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
|
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
|
||||||
|
|
||||||
asset_prefix = Middleman::Server.asset_host.call(original_output)
|
asset_prefix = app.asset_host.call(original_output)
|
||||||
|
|
||||||
File.join(asset_prefix, original_output)
|
File.join(asset_prefix, original_output)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
module Middleman::Features::CacheBuster
|
module Middleman::Features::CacheBuster
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
Middleman::Assets.register :cache_buster do |path, prefix, request|
|
app.register_asset_handler :cache_buster do |path, prefix, request|
|
||||||
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request)
|
http_path = app.before_asset_handler(:cache_buster, path, prefix, request)
|
||||||
|
|
||||||
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
|
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
|
||||||
http_path
|
http_path
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
|
prefix = app.images_dir if prefix == app.http_images_path
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
|
|
||||||
real_path_static = File.join(Middleman::Server.views, prefix, path)
|
real_path_static = File.join(app.views, prefix, path)
|
||||||
|
|
||||||
if File.readable?(real_path_static)
|
if File.readable?(real_path_static)
|
||||||
http_path << "?" + File.mtime(real_path_static).strftime("%s")
|
http_path << "?" + File.mtime(real_path_static).strftime("%s")
|
||||||
elsif Middleman::Server.environment == :build
|
elsif app.environment == :build
|
||||||
real_path_dynamic = File.join(Middleman::Server.root, Middleman::Server.build_dir, prefix, path)
|
real_path_dynamic = File.join(app.root, app.build_dir, prefix, path)
|
||||||
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
|
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ module Middleman::Features::CacheBuster
|
||||||
::Compass.configuration do |config|
|
::Compass.configuration do |config|
|
||||||
config.asset_cache_buster do |path, real_path|
|
config.asset_cache_buster do |path, real_path|
|
||||||
real_path = real_path.path if real_path.is_a? File
|
real_path = real_path.path if real_path.is_a? File
|
||||||
real_path = real_path.gsub(File.join(Middleman::Server.root, Middleman::Server.build_dir), Middleman::Server.views)
|
real_path = real_path.gsub(File.join(app.root, app.build_dir), app.views)
|
||||||
if File.readable?(real_path)
|
if File.readable?(real_path)
|
||||||
File.mtime(real_path).strftime("%s")
|
File.mtime(real_path).strftime("%s")
|
||||||
else
|
else
|
||||||
|
|
|
@ -3,20 +3,20 @@ module Middleman::Features::RelativeAssets
|
||||||
def registered(app)
|
def registered(app)
|
||||||
::Compass.configuration.relative_assets = true
|
::Compass.configuration.relative_assets = true
|
||||||
|
|
||||||
Middleman::Assets.register :relative_assets do |path, prefix, request|
|
app.register_asset_handler :relative_assets do |path, prefix, request|
|
||||||
begin
|
begin
|
||||||
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
|
prefix = app.images_dir if prefix == app.http_images_path
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
|
|
||||||
if path.include?("://")
|
if path.include?("://")
|
||||||
Middleman::Assets.before(:relative_assets, path, prefix, request)
|
app.before_asset_handler(:relative_assets, path, prefix, request)
|
||||||
elsif path[0,1] == "/"
|
elsif path[0,1] == "/"
|
||||||
path
|
path
|
||||||
else
|
else
|
||||||
path = File.join(prefix, path) if prefix.length > 0
|
path = File.join(prefix, path) if prefix.length > 0
|
||||||
request_path = request.path_info.dup
|
request_path = request.path_info.dup
|
||||||
request_path << Middleman::Server.index_file if path.match(%r{/$})
|
request_path << app.index_file if path.match(%r{/$})
|
||||||
request_path.gsub!(%r{^/}, '')
|
request_path.gsub!(%r{^/}, '')
|
||||||
parts = request_path.split('/')
|
parts = request_path.split('/')
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module Middleman::Features::TinySrc
|
module Middleman::Features::TinySrc
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
Middleman::Assets.register :tiny_src do |path, prefix, request|
|
app.register_asset_handler :tiny_src do |path, prefix, request|
|
||||||
original_output = Middleman::Assets.before(:tiny_src, path, prefix, request)
|
original_output = app.before_asset_handler(:tiny_src, path, prefix, request)
|
||||||
"http://i.tinysrc.mobi/#{original_output}"
|
"http://i.tinysrc.mobi/#{original_output}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,6 +36,9 @@ module Middleman
|
||||||
# Setup custom rendering
|
# Setup custom rendering
|
||||||
register Middleman::CoreExtensions::Rendering
|
register Middleman::CoreExtensions::Rendering
|
||||||
|
|
||||||
|
# Setup asset path pipeline
|
||||||
|
register Middleman::CoreExtensions::Assets
|
||||||
|
|
||||||
# Activate built-in helpers
|
# Activate built-in helpers
|
||||||
register Middleman::CoreExtensions::DefaultHelpers
|
register Middleman::CoreExtensions::DefaultHelpers
|
||||||
|
|
||||||
|
@ -175,5 +178,3 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require "middleman/assets"
|
|
Loading…
Reference in a new issue