Class: Middleman::Extensions::RelativeAssets
- Inherits:
-
Middleman::Extension
- Object
- Middleman::Extension
- Middleman::Extensions::RelativeAssets
- Defined in:
- middleman-core/lib/middleman-core/extensions/relative_assets.rb
Overview
Relative Assets extension
Constant Summary
Constants included from Contracts
Instance Attribute Summary
Attributes inherited from Middleman::Extension
Instance Method Summary collapse
-
#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ RelativeAssets
constructor
A new instance of RelativeAssets.
- #manipulate_resource_list_container!(resource_list) ⇒ Object
- #mark_as_relative(file_path, opts, current_resource) ⇒ Object
- #rewrite_url(asset_path, dirpath, request_path) ⇒ Object
Methods inherited from Middleman::Extension
activated_extension, #add_exposed_to_context, #after_build, #after_configuration, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources
Methods included from Contracts
Constructor Details
#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ RelativeAssets
Returns a new instance of RelativeAssets.
11 12 13 14 15 16 17 |
# File 'middleman-core/lib/middleman-core/extensions/relative_assets.rb', line 11 def initialize(app, = ::Middleman::EMPTY_HASH, &block) super require 'set' @set_of_exts = Set.new(.exts || app.config[:asset_extensions]) @set_of_sources = Set.new .sources end |
Instance Method Details
#manipulate_resource_list_container!(resource_list) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'middleman-core/lib/middleman-core/extensions/relative_assets.rb', line 20 def manipulate_resource_list_container!(resource_list) return if .helpers_only resource_list.by_extensions(@set_of_sources).each do |r| next if Array(.rewrite_ignore || []).any? do |i| ::Middleman::Util.path_match(i, "/#{r.destination_path}") end r.add_filter ::Middleman::InlineURLRewriter.new(:relative_assets, app, r, url_extensions: @set_of_exts, ignore: .ignore, proc: method(:rewrite_url)) end end |
#mark_as_relative(file_path, opts, current_resource) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'middleman-core/lib/middleman-core/extensions/relative_assets.rb', line 37 def mark_as_relative(file_path, opts, current_resource) result = opts.dup valid_exts = .sources return result unless current_resource return result unless valid_exts.include?(current_resource.ext) rewrite_ignores = Array(.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(.ignore || []).any? do |r| ::Middleman::Util.should_ignore?(r, file_path) end result[:relative] = true unless result.key?(:relative) result end |
#rewrite_url(asset_path, dirpath, request_path) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'middleman-core/lib/middleman-core/extensions/relative_assets.rb', line 72 def rewrite_url(asset_path, dirpath, request_path) uri = ::Middleman::Util.parse_uri(asset_path) return if uri.path[0..0] != '/' relative_path = uri.host.nil? full_asset_path = if relative_path dirpath.join(asset_path).to_s else asset_path end current_dir = Pathname(request_path).dirname Pathname(full_asset_path).relative_path_from(current_dir).to_s end |