Class: Middleman::Extensions::AssetHost

Inherits:
Middleman::Extension show all
Defined in:
middleman-core/lib/middleman-core/extensions/asset_host.rb

Constant Summary

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Middleman::Extension

activated_extension, #after_build, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, helpers, #initialize, option

Methods included from Contracts

#Contract

Constructor Details

This class inherits a constructor from Middleman::Extension

Instance Method Details

#after_configurationObject



9
10
11
12
13
14
15
16
17
# File 'middleman-core/lib/middleman-core/extensions/asset_host.rb', line 9

def after_configuration
  app.use ::Middleman::Middleware::InlineURLRewriter,
          id: :asset_host,
          url_extensions: options.exts,
          source_extensions: options.sources,
          ignore: options.ignore,
          middleman_app: app,
          proc: method(:rewrite_url)
end

#rewrite_url(asset_path, dirpath, _request_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'middleman-core/lib/middleman-core/extensions/asset_host.rb', line 20

def rewrite_url(asset_path, dirpath, _request_path)
  relative_path = Pathname.new(asset_path).relative?

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

  asset_prefix = if options[:host].is_a?(Proc)
    options[:host].call(full_asset_path)
  elsif options[:host].is_a?(String)
    options[:host]
  end

  File.join(asset_prefix, full_asset_path)
end