Created
May 16, 2012 11:41
-
-
Save gorakhargosh/2709736 to your computer and use it in GitHub Desktop.
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
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