Created
May 31, 2013 00:01
-
-
Save mishudark/5682184 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Copyright (c) , mishudark <[email protected]>. This file is | |
| # licensed under the Affero General Public License version 3 or later. See | |
| # the COPYRIGHT file. | |
| import fileinput | |
| import linecache | |
| import re | |
| file_css = 'style.css' | |
| sprite_css = 'sprites.css' | |
| class_prefix = 'sprite-images-' | |
| regex = re.compile(r'background:\s{0,1}url\((?:[A-Za-z0-9-_]+\/)*(?P<url>[A-Za-z0-9-_]+)\.[a-z]+\)') | |
| regex_width = re.compile(r'width:\s{0,1}') | |
| regex_height = re.compile(r'height:\s{0,1}') | |
| regex_start = re.compile(r'{') | |
| regex_class_name = re.compile(r'\.(?P<class>.+){(?P<style>.+)}') | |
| clases = '' | |
| clase = '' | |
| css = {} | |
| width = '' | |
| height = '' | |
| flag = False | |
| f = open(sprite_css,'r') | |
| lines = f.readlines() | |
| for line in lines: | |
| r = regex_class_name.search(line) | |
| if r: | |
| css[r.group('class').replace(class_prefix,'')] = r.group('style') | |
| f.close() | |
| for line in fileinput.input(file_css, inplace=1): | |
| #start of class or id | |
| if regex_start.search(line): | |
| #restart values | |
| height = '' | |
| width = '' | |
| flag = False | |
| clase = line.replace('{','') | |
| print line, | |
| continue | |
| #end of class or id | |
| if line.strip() == '}': | |
| if flag: | |
| if width: | |
| print '/*' | |
| print width, | |
| print '*/' | |
| if height: | |
| print '/*' | |
| print height, | |
| print '*/' | |
| else: | |
| if width: | |
| print width, | |
| if height: | |
| print height, | |
| print line | |
| #restart values | |
| height = '' | |
| width = '' | |
| clase = '' | |
| flag = False | |
| continue | |
| #search background name | |
| name = regex.search(line) | |
| if not name: | |
| h = regex_height.search(line) | |
| if h: | |
| height = line | |
| continue | |
| w = regex_width.search(line) | |
| if w: | |
| width = line | |
| continue | |
| print line, | |
| continue | |
| name = name.group('url') | |
| name = re.sub(r'[^\w\-_]', '', name) | |
| replace = css.get(name,None) | |
| #print replace if replace else line | |
| if replace: | |
| flag = True | |
| clases = clases + clase.replace('\n','') + ',' | |
| print '/*' | |
| print line, | |
| print '*/' | |
| replace = replace.split(';') | |
| l = len(replace) | |
| for i in range(l-1): | |
| if i == 0: | |
| print replace[i] + ' !important;' | |
| else: | |
| print replace[i] + ';' | |
| else: | |
| print line | |
| print clases |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment