Class: Middleman::Extensions::AssetHost

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

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Middleman::Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Middleman::Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, #after_extension_activated, after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ AssetHost

Returns a new instance of AssetHost.



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

def initialize(app, options_hash={}, &block)
  super

  app.rewrite_inline_urls id: :asset_host,
                          url_extensions: options.exts || app.config[:asset_extensions],
                          source_extensions: options.sources,
                          ignore: options.ignore,
                          rewrite_ignore: options.rewrite_ignore,
                          proc: method(:rewrite_url)
end

Instance Method Details

#rewrite_url(asset_path, dirpath, _request_path) ⇒ Object



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

def rewrite_url(asset_path, dirpath, _request_path)
  uri = ::Addressable::URI.parse(asset_path)
  relative_path = uri.path[0..0] != '/'

  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