Skip to content

Instantly share code, notes, and snippets.

@gorakhargosh
Created May 16, 2012 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorakhargosh/2709736 to your computer and use it in GitHub Desktop.
Save gorakhargosh/2709736 to your computer and use it in GitHub Desktop.
import re
css_source = """
@import url("../something.css");
@import url(../something.css);
@import url(/some/url.css);
body {
background-image: url("images/file.png");
background-image: url('images/file.png');
background-image: url(../data/images/file.png);
background-image: url(//python-cssembed.googlecode.com/git/cssembed/tests/data/file.png);
background-image: url(http://python-cssembed.googlecode.com/git/cssembed/tests/data/file.png);
background-image: url(https://python-cssembed.googlecode.com/git/cssembed/tests/data/file.png);
background-image: url("http://python-cssembed.googlecode.com/git/cssembed/tests/data/file.png");
background-image: url('https://python-cssembed.googlecode.com/git/cssembed/tests/data/file.png');
background-image: url(missing.gif);
background-image: url(../data/images/missing.gif);
background-image: url(images/file1.png), url(images/file2.png);
background-image: url(images/file1.png), url(images/file2.png)
background-image: url(images/file.png)
}
"""
PATTERN=r'[\s:,]*url\(([^\(\)]*)\)[;,\s]?'
def find_all_urls(source, pattern=PATTERN):
for match in re.finditer(pattern, source, re.MULTILINE | re.IGNORECASE):
s = match.span()
#print dir(match)
print [unquote(s) for s in match.groups()]
#print source[s[0]:s[1]]
def unquote(s):
quote_chars = '\"\''
if s[0] in quote_chars and s[-1] in quote_chars:
return s[1:-1]
else:
return s
if __name__ == '__main__':
find_all_urls(css_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment