Class: Middleman::InlineURLRewriter

Inherits:
Filter
  • Object
show all
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/inline_url_filter.rb

Constant Summary

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Attribute Summary

Attributes inherited from Filter

#after_filter, #filter_name

Instance Method Summary collapse

Methods included from Contracts

#Contract

Methods inherited from Filter

#<=>

Constructor Details

#initialize(filter_name, app, resource, options_hash = ::Middleman::EMPTY_HASH) ⇒ InlineURLRewriter

Returns a new instance of InlineURLRewriter



13
14
15
16
17
18
# File 'middleman-core/lib/middleman-core/inline_url_filter.rb', line 13

def initialize(filter_name, app, resource, options_hash = ::Middleman::EMPTY_HASH)
  super(filter_name, options_hash)

  @app = app
  @resource = resource
end

Instance Method Details

#execute_filter(body) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'middleman-core/lib/middleman-core/inline_url_filter.rb', line 35

def execute_filter(body)
  path = "/#{@resource.destination_path}"
  dirpath = ::Pathname.new(File.dirname(path))

  ::Middleman::Util.instrument 'inline_url_filter', path: path do
    vertices = ::Hamster::Set.empty

    new_content = ::Middleman::Util.rewrite_paths(body, path, @options.fetch(:url_extensions), @app) do |asset_path|
      uri = ::Middleman::Util.parse_uri(asset_path)

      relative_path = uri.host.nil?
      full_asset_path = if relative_path
                          dirpath.join(asset_path).to_s
                        else
                          asset_path
                        end

      exts = @options.fetch(:url_extensions)
      next unless exts.include?(::File.extname(asset_path))

      next if @options.fetch(:ignore).any? { |r| ::Middleman::Util.should_ignore?(r, full_asset_path) }

      result = @options.fetch(:proc).call(asset_path, dirpath, path)

      if result
        if @options.fetch(:create_dependencies, false)
          vertices <<= ::Middleman::Dependencies::FileVertex.from_resource(
            target_resource(asset_path, dirpath)
          )
        end

        asset_path = result
      end

      asset_path
    end

    [new_content, vertices]
  end
end

#target_resource(asset_path, dirpath) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'middleman-core/lib/middleman-core/inline_url_filter.rb', line 21

def target_resource(asset_path, dirpath)
  uri = ::Middleman::Util.parse_uri(asset_path)
  relative_path = !uri.path.start_with?('/')

  full_asset_path = if relative_path
                      dirpath.join(asset_path).to_s
                    else
                      asset_path
                    end

  @app.sitemap.by_destination_path(full_asset_path) || @app.sitemap.by_path(full_asset_path)
end