middleman/middleman-core/lib/middleman-core/extensions/relative_assets.rb

39 lines
1.3 KiB
Ruby
Raw Normal View History

2013-04-20 23:33:18 +02:00
# Relative Assets extension
class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
2015-02-11 00:09:40 +01:00
option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2), 'List of extensions that get cache busters strings appended to them.'
option :sources, %w(.htm .html .css), 'List of extensions that are searched for relative assets.'
option :ignore, [], 'Regexes of filenames to skip adding query strings to'
2013-04-20 23:33:18 +02:00
def initialize(app, options_hash={}, &block)
super
require 'middleman-core/middleware/inline_url_rewriter'
end
def after_configuration
app.use ::Middleman::Middleware::InlineURLRewriter,
2014-06-16 18:05:24 +02:00
id: :asset_hash,
url_extensions: options.exts,
source_extensions: options.sources,
ignore: options.ignore,
middleman_app: app,
proc: method(:rewrite_url)
2013-04-20 23:33:18 +02:00
end
2014-07-03 04:04:34 +02:00
Contract String, Or[String, Pathname], Any => Maybe[String]
def rewrite_url(asset_path, dirpath, request_path)
relative_path = Pathname.new(asset_path).relative?
2013-04-20 23:33:18 +02:00
full_asset_path = if relative_path
dirpath.join(asset_path).to_s
else
asset_path
end
2014-07-02 20:05:57 +02:00
return unless !full_asset_path.include?('//') && !asset_path.start_with?('data:')
2014-07-09 15:10:49 +02:00
current_dir = Pathname(request_path).dirname
2014-07-02 20:05:57 +02:00
Pathname(full_asset_path).relative_path_from(current_dir).to_s
end
end