The project i’m currently working on has seen various coders, from various backgrounds. After succesfully writing a clean Capistrano recipe in order minimize css and js files, I’m asking myself how to recognize various url patterns spread around our css codebase, to rewrite them all at once before they go live.
The patterns i’m looking for are
url('../../images/
url(../images/
url("../../../images/
url(images/
So basically, here’s an input example :
.test{background:url('/images/test.jpg') no-repeat top left}.pouet{background:url("../../images/bg.png")}
Note that in some cases, we have quotes, in others not…
And what i’m trying to obtain
.test{background:url('http://www.cdnImages.com/images/test.jpg') no-repeat top left}.pouet{background:url("http://www.cdnImages.com/images/bg.png")}
All these must be replaced by my cdn url. The most puzzling thing is how to do so in a way that does not allow any mistakes.
I’ve come up with a regexp that fits my needs : rubular permalink
I was looking at the sed command, using it as follows
sed '/url([^\/](.*?)images/http:\/\/www.cdnImages.com' myFile.css
but that doesn’t seem to do the job.
My current research lead me to this
find #{current_release}/public/static/css/ -name '*.css' | xargs sed -i 's@url\([^\/](.*?)images|static\/@#{images_cdn}@g'
and, while the regular expression perfectly fits the need (check here to see the catched output, there seems to be something wrong somewhere.
Any idea ? Thanks a lot.
I finally came up with the perfect command, that fits the need :
works fast and safe !