Class: Middleman::Middleware::InlineURLRewriter

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

Constant Summary collapse

IGNORE_DESCRIPTOR =
Or[Regexp, RespondTo[:call], String]

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options = {}) ⇒ InlineURLRewriter

Returns a new instance of InlineURLRewriter.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/middleman-core/middleware/inline_url_rewriter.rb', line 22

def initialize(app, options={})
  @rack_app = app
  @middleman_app = options.fetch(:middleman_app)

  @uid = options.fetch(:id, nil)
  @proc = options.fetch(:proc)

  raise 'InlineURLRewriter requires a :proc to call with inline URL results' unless @proc

  @exts = options.fetch(:url_extensions)

  @source_exts = options.fetch(:source_extensions)
  @source_exts_regex_text = Regexp.union(@source_exts).to_s

  @ignore = options.fetch(:ignore)
end

Instance Method Details

#call(env) ⇒ Object



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
75
76
77
78
79
80
81
# File 'lib/middleman-core/middleware/inline_url_rewriter.rb', line 39

def call(env)
  status, headers, response = @rack_app.call(env)

  # Allow upstream request to skip all rewriting
  return [status, headers, response] if env['bypass_inline_url_rewriter'] == 'true'

  # Allow upstream request to skip this specific rewriting
  if @uid
    uid_key = "bypass_inline_url_rewriter_#{@uid}"
    return [status, headers, response] if env[uid_key] == 'true'
  end

  path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app)

  if path =~ /(^\/$)|(#{@source_exts_regex_text}$)/
    if body = ::Middleman::Util.extract_response_text(response)

      dirpath = Pathname.new(File.dirname(path))

      rewritten = ::Middleman::Util.rewrite_paths(body, path, @exts) do |asset_path|
        uri = ::Addressable::URI.parse(asset_path)

        relative_path = uri.host.nil?

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

        @ignore.none? { |r| should_ignore?(r, full_asset_path) } && @proc.call(asset_path, dirpath, path)
      end

      status, headers, response = ::Rack::Response.new(
        rewritten,
        status,
        headers
      ).finish
    end
  end

  [status, headers, response]
end

#should_ignore?(validator, value) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/middleman-core/middleware/inline_url_rewriter.rb', line 84

def should_ignore?(validator, value)
  if validator.is_a? Regexp
    # Treat as Regexp
    !value.match(validator).nil?
  elsif validator.respond_to? :call
    # Treat as proc
    validator.call(value)
  elsif validator.is_a? String
    # Treat as glob
    File.fnmatch(value, validator)
  else
    # If some unknown thing, don't ignore
    false
  end
end