2015-04-26 22:01:19 +02:00
|
|
|
require 'addressable/uri'
|
2014-06-09 23:02:46 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
class Middleman::Extensions::AssetHost < ::Middleman::Extension
|
2014-07-24 06:09:25 +02:00
|
|
|
option :host, nil, 'The asset host to use or a Proc to determine asset host', required: true
|
2014-08-14 19:14:26 +02:00
|
|
|
option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif), 'List of extensions that get cache busters strings appended to them.'
|
2014-04-07 21:43:16 +02:00
|
|
|
option :sources, %w(.htm .html .php .css .js), 'List of extensions that are searched for bustable assets.'
|
|
|
|
option :ignore, [], 'Regexes of filenames to skip adding query strings to'
|
2016-01-09 16:06:47 +01:00
|
|
|
option :rewrite_ignore, [], 'Regexes of filenames to skip processing for host rewrites'
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2016-01-14 02:16:36 +01:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
|
|
|
|
|
|
|
app.rewrite_inline_urls id: :asset_host,
|
|
|
|
url_extensions: options.exts,
|
|
|
|
source_extensions: options.sources,
|
|
|
|
ignore: options.ignore,
|
|
|
|
rewrite_ignore: options.rewrite_ignore,
|
|
|
|
proc: method(:rewrite_url)
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String, Or[String, Pathname], Any => String
|
2014-06-16 18:05:24 +02:00
|
|
|
def rewrite_url(asset_path, dirpath, _request_path)
|
2015-04-26 22:01:19 +02:00
|
|
|
uri = ::Addressable::URI.parse(asset_path)
|
|
|
|
relative_path = uri.path[0..0] != '/'
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2014-04-07 21:43:16 +02:00
|
|
|
full_asset_path = if relative_path
|
|
|
|
dirpath.join(asset_path).to_s
|
|
|
|
else
|
|
|
|
asset_path
|
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2014-04-07 21:43:16 +02:00
|
|
|
asset_prefix = if options[:host].is_a?(Proc)
|
|
|
|
options[:host].call(full_asset_path)
|
|
|
|
elsif options[:host].is_a?(String)
|
|
|
|
options[:host]
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2014-04-07 21:43:16 +02:00
|
|
|
|
|
|
|
File.join(asset_prefix, full_asset_path)
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|