Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created August 30, 2011 21:30
Show Gist options
  • Save carlhoerberg/1182116 to your computer and use it in GitHub Desktop.
Save carlhoerberg/1182116 to your computer and use it in GitHub Desktop.
A sprockets processor which rewrites the relative urls in a css when concatenating css files
require 'sprockets'
require 'pathname'
require 'uri'
module Sprockets
class UrlRewriter < Processor
def evaluate(context, locals)
rel = Pathname.new(context.logical_path).parent
data.gsub /url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/ do |url|
return url if URI.parse($1).absolute?
new_path = rel.join Pathname.new($1)
"url(#{new_path})"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment