2013-04-20 23:27:25 +02:00
|
|
|
|
|
|
|
# Asset Host module
|
|
|
|
class Middleman::Extensions::AssetHost < ::Middleman::Extension
|
|
|
|
option :host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host'
|
|
|
|
|
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
|
|
|
|
|
|
|
# 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.compass_config do |config|
|
|
|
|
if asset_host = extensions[:asset_host].host
|
|
|
|
if asset_host.is_a?(Proc)
|
|
|
|
config.asset_host(&asset_host)
|
|
|
|
else
|
2014-04-29 01:02:18 +02:00
|
|
|
config.asset_host do |_|
|
2013-04-20 23:27:25 +02:00
|
|
|
asset_host
|
2013-04-20 22:32:39 +02:00
|
|
|
end
|
|
|
|
end
|
2013-04-20 21:59:14 +02:00
|
|
|
end
|
2013-05-08 20:37:55 +02:00
|
|
|
end if app.respond_to?(:compass_config)
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def host
|
|
|
|
app.config[:asset_host] || options[:host]
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-12-28 19:14:15 +01:00
|
|
|
helpers do
|
2013-04-20 23:27:25 +02:00
|
|
|
# Override default asset url helper to include asset hosts
|
|
|
|
#
|
|
|
|
# @param [String] path
|
|
|
|
# @param [String] prefix
|
|
|
|
# @return [String]
|
2013-12-28 01:26:31 +01:00
|
|
|
def asset_url(path, prefix='')
|
2013-04-20 23:27:25 +02:00
|
|
|
controller = extensions[:asset_host]
|
|
|
|
|
|
|
|
original_output = super
|
|
|
|
return original_output unless controller.host
|
|
|
|
|
|
|
|
asset_prefix = if controller.host.is_a?(Proc)
|
|
|
|
controller.host.call(original_output)
|
|
|
|
elsif controller.host.is_a?(String)
|
|
|
|
controller.host
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
|
|
|
File.join(asset_prefix, original_output)
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|