class SprocketsRelativeUrl::Processor

Constants

CSS_URL_REGEXP

Public Instance Methods

evaluate(context, *) click to toggle source
# File lib/sprockets_relative_url/processor.rb, line 15
def evaluate(context, *)
  data.gsub(CSS_URL_REGEXP) do
    path = Regexp.last_match[:path]
    next path if should_skip?(path)

    uri = URI.parse(path)
    next path if uri.absolute?

    uri.path = root_relative_path(context, uri.path)
    uri.to_s
  end
end

Private Instance Methods

root_relative_path(context, path) click to toggle source
# File lib/sprockets_relative_url/processor.rb, line 30
def root_relative_path(context, path)
  decoded = URI.decode(path)
  asset_full_path = context.pathname.parent.join(decoded)
  asset = context.environment.find_asset(asset_full_path)

  return path if asset.nil?

  encoded = URI.encode(asset.logical_path)
  context.asset_path(encoded)
end
should_skip?(path) click to toggle source
# File lib/sprockets_relative_url/processor.rb, line 41
def should_skip?(path)
  path.start_with?('/') || path.include?('\\')
end