Python script for minifying CSS? [closed] Python script for minifying CSS? [closed] python python

Python script for minifying CSS? [closed]


This seemed like a good task for me to get into python, which has been pending for a while. I hereby present my first ever python script:

import sys, rewith open( sys.argv[1] , 'r' ) as f:    css = f.read()# remove comments - this will break a lot of hacks :-Pcss = re.sub( r'\s*/\*\s*\*/', "$$HACK1$$", css ) # preserve IE<6 comment hackcss = re.sub( r'/\*[\s\S]*?\*/', "", css )css = css.replace( "$$HACK1$$", '/**/' ) # preserve IE<6 comment hack# url() doesn't need quotescss = re.sub( r'url\((["\'])([^)]*)\1\)', r'url(\2)', css )# spaces may be safely collapsed as generated content will collapse them anywaycss = re.sub( r'\s+', ' ', css )# shorten collapsable colors: #aabbcc to #abccss = re.sub( r'#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3(\s|;)', r'#\1\2\3\4', css )# fragment values can loose zeroscss = re.sub( r':\s*0(\.\d+([cm]m|e[mx]|in|p[ctx]))\s*;', r':\1;', css )for rule in re.findall( r'([^{]+){([^}]*)}', css ):    # we don't need spaces around operators    selectors = [re.sub( r'(?<=[\[\(>+=])\s+|\s+(?=[=~^$*|>+\]\)])', r'', selector.strip() ) for selector in rule[0].split( ',' )]    # order is important, but we still want to discard repetitions    properties = {}    porder = []    for prop in re.findall( '(.*?):(.*?)(;|$)', rule[1] ):        key = prop[0].strip().lower()        if key not in porder: porder.append( key )        properties[ key ] = prop[1].strip()    # output rule if it contains any declarations    if properties:        print "%s{%s}" % ( ','.join( selectors ), ''.join(['%s:%s;' % (key, properties[key]) for key in porder])[:-1] ) 

I believe this to work, and output it tests fine on recent Safari, Opera, and Firefox. It will break CSS hacks other than the underscore & /**/ hacks! Do not use a minifier if you have a lot of hacks going on (or put them in a separate file).

Any tips on my python appreciated. Please be gentle though, it's my first time. :-)


There is a port of YUI's CSS compressor available for python.

Here is its project page on PyPi:http://pypi.python.org/pypi/cssmin/0.1.1


There is a nice online tool cssminifier which has also an API which is pretty simple and easy to use.I made a small python script that posts the CSS file content to that tool's API, returns the minifed CSS and saves it into a file "style.min.css". I like it because it is a small code that may be nicely integrated in an automated deployment script:

import requestsf = open("style.css", "r")css_text = f.read()f.close()r = requests.post("http://cssminifier.com/raw", data={"input":css_text})css_minified = r.textf2 = open("style.min.css", "w")f2.write(css_minified)f2.close()