Class: Sprockets::UrlRewriter

Inherits:
Processor
  • Object
show all
Defined in:
lib/sprockets-urlrewriter.rb

Instance Method Summary collapse

Instance Method Details

#evaluate(context, locals) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sprockets-urlrewriter.rb', line 7

def evaluate(context, locals) 
  # FIXME: Accessing logical_path instance variable directly instead of
  # using public method, since the public method incorrectly chops paths
  # with periods in them: https://github.com/sstephenson/sprockets/pull/299
  #
  # logical_path = context.logical_path
  logical_path = context.instance_variable_get(:@logical_path)

  rel = Pathname.new(logical_path).parent
  data.gsub /(:|\s)url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/i do |url| 
    next url if URI.parse($2).absolute?
    new_path = rel.join Pathname.new($2)
    "#{$1}url(#{new_path}#{$3})"
  end 
end