Class: Middleman::CoreExtensions::InlineURLRewriter::Rack
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::InlineURLRewriter::Rack
- Extended by:
- Memoist
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/core_extensions/inline_url_rewriter.rb
Constant Summary
Constants included from Contracts
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Rack
constructor
A new instance of Rack.
Methods included from Contracts
Constructor Details
#initialize(app, options = {}) ⇒ Rack
Returns a new instance of Rack.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/middleman-core/core_extensions/inline_url_rewriter.rb', line 56 def initialize(app, ={}) @rack_app = app @middleman_app = .fetch(:middleman_app) @rewriters = .fetch(:rewriters) 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 end |
Instance Method Details
#call(env) ⇒ Object
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 130 131 |
# File 'lib/middleman-core/core_extensions/inline_url_rewriter.rb', line 73 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' 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, @middleman_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 @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| ::Middleman::Util.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 |