Created
May 17, 2012 08:36
-
-
Save gorakhargosh/2717416 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); | |
/* @import url(/some/url.css); */ | |
body { | |
background-image: url("images/fileA.png"); | |
/* background-image: url("images/fileDISCARD.png"); */ | |
background-image: url('images/fileB.png'); | |
background-image:url("images/fileC.png");background-image:url('images/fileD.png'); | |
background-image: url(../data/images/fileE.png); | |
background-image: url(//python-cssembed.googlecode.com/git/cssembed/tests/data/file1.png); | |
background-image: url(http://python-cssembed.googlecode.com/git/cssembed/tests/data/file2.png); | |
background-image: url(https://python-cssembed.googlecode.com/git/cssembed/tests/data/file3.png); | |
background-image: url("http://python-cssembed.googlecode.com/git/cssembed/tests/data/file4.png"); | |
background-image: url('https://python-cssembed.googlecode.com/git/cssembed/tests/data/file5.png'); | |
background-image: url(missing.gif); | |
background-image: url(../data/images/missing.gif); | |
background-image: url(images/fileX.png), url(images/fileY.png); | |
background-image: url(images/fileX.png), url(images/fileY.png) | |
background-image: url(images/fileX.png),url(images/fileY.png); | |
background-image: url(images/file.png) | |
} | |
""" | |
#PATTERN=r'[\s:,]*url\(([^\(\)]*)\)[;,\s]?' | |
PATTERN=r'[:,]\s*url\(([^\(\)]*)\)' | |
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(i) for i in match.groups()] | |
#print source[s[0]:s[1]] | |
#print(re.findall(pattern, source, re.MULTILINE | re.IGNORECASE)) | |
def unquote(s): | |
quote_chars = '\"\'' | |
if s[0] in quote_chars: | |
if s[0] == s[-1]: | |
return s[1:-1] | |
else: | |
raise ValueError("Mismatched quotes around %s: " % s) | |
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