开发者

Replace url() relative path with full domain in css files

I'd like to run a script on release that replaces all url() declarations in a css file with the full domain path, because images are hosted on a static web server.

Example

Current:  background-image: url(/images/menu.gif);
Desired:  background-image: url(http://example.com/images/menu.gif);
Current:  background-image: url('/images/menu.gif');
Desired:  background-image: url('http://example.com/images/menu.gif');
Current:  background-image: url("/images/menu.gif");
Desired:  background-image: url("http://example.com/images/menu.gif");

I have concocted a bash script using sed to do just that, but it does not handle url with quotes url(''), or urls that already have a full path.

开发者_开发知识库 STATIC_HOST="http://example.com"
 sed -i '' "s|url(\([^)]*\)|url($STATIC_HOST\1|g" main.css


Try with this

sed "s|\(url(['\"]\?\)\(/[^)]*\)|\1$STATIC_HOST\2|g" main.css

Put the -i option only when you are sure the result is what you were looking for.


|url\(['\"]?([^'\":)]+)['\"]?\)|url($STATIC_HOST\1)|g

where the colon will prevent "http://..." like urls

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜