Skip to content

Commit

Permalink
use web host for fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
hemang committed May 15, 2012
1 parent 25c6cff commit dadb21a
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/asset_id/asset.rb
Expand Up @@ -21,6 +21,7 @@ class Asset
@@copy = false
@@gzip = false
@@asset_host = false
@@web_host = false

attr_reader :path

Expand All @@ -40,6 +41,9 @@ def self.init(options)

@@asset_host = options[:asset_host] if options[:asset_host]
@@asset_host ||= ''

@@web_host = options[:web_host] if options[:web_host]
@@web_host ||= ''
end

def initialize(path)
Expand All @@ -60,8 +64,8 @@ def self.process!(options={})
end

#replace css images is intentionally before fingerprint
asset.replace_css_images!(:prefix => @@asset_host) if asset.css? && @@replace_images
asset.replace_js_images!(:prefix => @@asset_host) if asset.js? && @@replace_images
asset.replace_css_images!(:asset_host => @@asset_host, :web_host => @@web_host) if asset.css? && @@replace_images
asset.replace_js_images!(:asset_host => @@asset_host, :web_host => @@web_host) if asset.js? && @@replace_images

asset.fingerprint
if options[:debug]
Expand Down Expand Up @@ -201,7 +205,8 @@ def replace_src_tag_images!(options={})
end

def replace_images!(options={})
options[:prefix] ||= ''
options[:asset_host] ||= ''
options[:web_host] || = ''
#defaults to url tag regex
regexp = options[:regexp] || /url\((?:"([^"]*)"|'([^']*)'|([^)]*))\)/mi
data.gsub! regexp do |match|
Expand All @@ -221,19 +226,20 @@ def replace_images!(options={})
original = "#{b4_uri}#{uri}#{after_uri}"

# if the uri appears to begin with a protocol then the asset isn't on the local filesystem
# uri is unchanged for data and font uris
if uri =~ /[a-z]+:\/\//i || uri =~ /data:/i || uri =~ /^data:font/
# uri is unchanged for data and data font uris
if uri =~ /[a-z]+:\/\//i || uri =~ /data:/i
original
else
asset = Asset.new(uri)

puts " - Changing URI #{uri} to #{options[:prefix]}#{asset.fingerprint}" if @@debug
asset = Asset.new(uri)
host = uri=~ /fonts/i ? options[:web_host] : options[:asset_host]

puts " - Changing URI #{uri} to #{host}#{asset.fingerprint}" if @@debug

# TODO: Check the referenced asset is in the asset_paths
# Suggested solution below. But, rescue is probably a better solution in case of nested paths and such
# - https://github.com/KeasInc/asset_id/commit/0fbd108c06ad18f50bfa63073b2a8c5bbac154fb
# - https://github.com/KeasInc/asset_id/commit/14ce9124938c15734ec0c61496fd371de2b8087c
"#{b4_uri}#{options[:prefix]}#{asset.fingerprint}#{suffix}#{after_uri}"
"#{b4_uri}#{host}#{asset.fingerprint}#{suffix}#{after_uri}"
end
rescue Errno::ENOENT => e
puts " - Warning: #{uri} not found" if @@debug
Expand Down

0 comments on commit dadb21a

Please sign in to comment.