2015-04-26 22:01:19 +02:00
|
|
|
require 'addressable/uri'
|
|
|
|
|
2013-04-20 23:33:18 +02:00
|
|
|
# Relative Assets extension
|
|
|
|
class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
|
2016-04-23 00:52:42 +02:00
|
|
|
option :exts, nil, 'List of extensions that get converted to relative paths.'
|
2016-01-27 10:48:36 +01:00
|
|
|
option :sources, %w(.css .htm .html .xhtml), 'List of extensions that are searched for relative assets.'
|
2016-04-23 00:52:42 +02:00
|
|
|
option :ignore, [], 'Regexes of filenames to skip converting to relative paths.'
|
|
|
|
option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites.'
|
|
|
|
option :helpers_only, false, 'Allow only Ruby helpers to change paths.'
|
2014-04-07 21:43:16 +02:00
|
|
|
|
2013-04-20 23:33:18 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2016-04-25 23:21:58 +02:00
|
|
|
return if options[:helpers_only]
|
2016-04-23 00:52:42 +02:00
|
|
|
|
|
|
|
app.rewrite_inline_urls id: :relative_assets,
|
2016-03-22 23:43:48 +01:00
|
|
|
url_extensions: options.exts || app.config[:asset_extensions],
|
2016-01-14 02:16:36 +01:00
|
|
|
source_extensions: options.sources,
|
|
|
|
ignore: options.ignore,
|
|
|
|
rewrite_ignore: options.rewrite_ignore,
|
|
|
|
proc: method(:rewrite_url)
|
2013-04-20 23:33:18 +02:00
|
|
|
end
|
2011-11-21 09:23:16 +01:00
|
|
|
|
2016-04-23 00:52:42 +02:00
|
|
|
def mark_as_relative(file_path, opts, current_resource)
|
|
|
|
result = opts.dup
|
|
|
|
|
|
|
|
valid_exts = options.sources
|
|
|
|
|
|
|
|
return result unless current_resource
|
|
|
|
return result unless valid_exts.include?(current_resource.ext)
|
|
|
|
|
|
|
|
rewrite_ignores = Array(options.rewrite_ignore || [])
|
|
|
|
|
|
|
|
path = current_resource.destination_path
|
|
|
|
return result if rewrite_ignores.any? do |i|
|
|
|
|
::Middleman::Util.path_match(i, path) || ::Middleman::Util.path_match(i, "/#{path}")
|
|
|
|
end
|
|
|
|
|
|
|
|
return result if Array(options.ignore || []).any? do |r|
|
|
|
|
::Middleman::Util.should_ignore?(r, file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
result[:relative] = true unless result.key?(:relative)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-09-17 22:53:43 +02:00
|
|
|
helpers do
|
|
|
|
def asset_url(path, prefix='', options={})
|
2016-04-23 00:52:42 +02:00
|
|
|
super(path, prefix, app.extensions[:relative_assets].mark_as_relative(super, options, current_resource))
|
|
|
|
end
|
2015-09-17 22:53:43 +02:00
|
|
|
|
2016-04-23 00:52:42 +02:00
|
|
|
def asset_path(kind, source, options={})
|
2016-04-25 23:21:58 +02:00
|
|
|
super(kind, source, app.extensions[:relative_assets].mark_as_relative(super, options, current_resource))
|
2015-09-17 22:53:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String, Or[String, Pathname], Any => Maybe[String]
|
2014-04-07 21:43:16 +02:00
|
|
|
def rewrite_url(asset_path, dirpath, request_path)
|
2016-04-22 01:06:26 +02:00
|
|
|
uri = ::Middleman::Util.parse_uri(asset_path)
|
2015-04-26 22:01:19 +02:00
|
|
|
|
|
|
|
return if uri.path[0..0] != '/'
|
|
|
|
|
|
|
|
relative_path = uri.host.nil?
|
2013-04-20 23:33:18 +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
|
2014-03-31 23:15:46 +02:00
|
|
|
|
2014-07-09 15:10:49 +02:00
|
|
|
current_dir = Pathname(request_path).dirname
|
2015-04-26 22:01:19 +02:00
|
|
|
result = Pathname(full_asset_path).relative_path_from(current_dir).to_s
|
|
|
|
|
|
|
|
result
|
2011-11-21 09:23:16 +01:00
|
|
|
end
|
2016-04-22 01:06:26 +02:00
|
|
|
memoize :rewrite_url
|
2012-05-01 06:50:31 +02:00
|
|
|
end
|