Created
August 30, 2011 21:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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