Class: Middleman::CoreExtensions::InlineURLRewriter::Rack

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

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

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

Returns a new instance of Rack.



54
55
56
57
58
# File 'lib/middleman-core/core_extensions/inline_url_rewriter.rb', line 54

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

Instance Method Details

#call(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/middleman-core/core_extensions/inline_url_rewriter.rb', line 60

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

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

  all_source_exts = @rewriters
                    .reduce([]) { |sum, rewriter| sum + rewriter[:source_extensions] }
                    .flatten
                    .uniq
  source_exts_regex_text = Regexp.union(all_source_exts).to_s

  all_asset_exts = @rewriters
                   .reduce([]) { |sum, rewriter| sum + rewriter[:url_extensions] }
                   .flatten
                   .uniq

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

  return [status, headers, response] unless path =~ /(^\/$)|(#{source_exts_regex_text}$)/
  return [status, headers, response] unless body = ::Middleman::Util.extract_response_text(response)

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

  rewritten = ::Middleman::Util.instrument 'inline_url_rewriter', path: path do
    ::Middleman::Util.rewrite_paths(body, path, all_asset_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

      @rewriters.each do |rewriter|
        uid = rewriter.fetch(:id)

        # Allow upstream request to skip this specific rewriting
        next if env["bypass_inline_url_rewriter_#{uid}"] == 'true'

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

        source_exts = rewriter.fetch(:source_extensions)
        next unless source_exts.include?(::File.extname(path))

        ignore = rewriter.fetch(:ignore)
        next if ignore.any? { |r| should_ignore?(r, full_asset_path) }

        rewrite_ignore = Array(rewriter[:rewrite_ignore] || [])
        next if rewrite_ignore.any? { |i| ::Middleman::Util.path_match(i, path) }

        proc = rewriter.fetch(:proc)

        result = proc.call(asset_path, dirpath, path)
        asset_path = result if result
      end

      asset_path
    end
  end

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

#should_ignore?(validator, value) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/middleman-core/core_extensions/inline_url_rewriter.rb', line 132

def should_ignore?(validator, value)
  if validator.is_a? Regexp
    # Treat as Regexp
    !!(value =~ validator)
  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