make assets a core ext

This commit is contained in:
Thomas Reynolds 2011-07-06 10:40:17 -07:00
parent 59a07d4cc0
commit 118d23be41
9 changed files with 79 additions and 58 deletions

View file

@ -76,6 +76,9 @@ module Middleman
# Custom Feature API
autoload :Features, "middleman/core_extensions/features"
# Asset Path Pipeline
autoload :Assets, "middleman/core_extensions/assets"
# DefaultHelpers are the built-in dynamic template helpers.
autoload :DefaultHelpers, "middleman/core_extensions/default_helpers"

View file

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

View 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

View file

@ -56,10 +56,6 @@ module Middleman::CoreExtensions::DefaultHelpers
classes.join(' ')
end
def asset_url(path, prefix="")
Middleman::Assets.get_url(path, prefix, request)
end
# Padrino's asset handling needs to pass through ours
def asset_path(kind, source)
return source if source =~ /^http/

View file

@ -2,17 +2,17 @@ module Middleman::Features::AssetHost
class << self
def registered(app)
app.after_feature_init do
if Middleman::Server.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&Middleman::Server.asset_host)
if app.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&app.asset_host)
end
end
Middleman::Assets.register :asset_host do |path, prefix, request|
original_output = Middleman::Assets.before(:asset_host, path, prefix, request)
app.register_asset_handler :asset_host do |path, prefix, request|
original_output = app.before_asset_handler(:asset_host, path, prefix, request)
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)
end

View file

@ -1,23 +1,23 @@
module Middleman::Features::CacheBuster
class << self
def registered(app)
Middleman::Assets.register :cache_buster do |path, prefix, request|
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request)
app.register_asset_handler :cache_buster do |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))
http_path
else
begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
prefix = app.images_dir if prefix == app.http_images_path
rescue
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)
http_path << "?" + File.mtime(real_path_static).strftime("%s")
elsif Middleman::Server.environment == :build
real_path_dynamic = File.join(Middleman::Server.root, Middleman::Server.build_dir, prefix, path)
elsif app.environment == :build
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)
end
@ -29,7 +29,7 @@ module Middleman::Features::CacheBuster
::Compass.configuration 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(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)
File.mtime(real_path).strftime("%s")
else

View file

@ -3,20 +3,20 @@ module Middleman::Features::RelativeAssets
def registered(app)
::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
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
prefix = app.images_dir if prefix == app.http_images_path
rescue
end
if path.include?("://")
Middleman::Assets.before(:relative_assets, path, prefix, request)
app.before_asset_handler(:relative_assets, path, prefix, request)
elsif path[0,1] == "/"
path
else
path = File.join(prefix, path) if prefix.length > 0
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{^/}, '')
parts = request_path.split('/')

View file

@ -1,8 +1,8 @@
module Middleman::Features::TinySrc
class << self
def registered(app)
Middleman::Assets.register :tiny_src do |path, prefix, request|
original_output = Middleman::Assets.before(:tiny_src, path, prefix, request)
app.register_asset_handler :tiny_src do |path, prefix, request|
original_output = app.before_asset_handler(:tiny_src, path, prefix, request)
"http://i.tinysrc.mobi/#{original_output}"
end
end

View file

@ -36,6 +36,9 @@ module Middleman
# Setup custom rendering
register Middleman::CoreExtensions::Rendering
# Setup asset path pipeline
register Middleman::CoreExtensions::Assets
# Activate built-in helpers
register Middleman::CoreExtensions::DefaultHelpers
@ -174,6 +177,4 @@ module Middleman
end
end
end
end
require "middleman/assets"
end