Python script to minimize CSS?

I am looking for a simple Python script that can minimize CSS as part of the website deployment process. (Python is the only scripting language supported on the server, and full-size parsers such as CSS Utils are too complex for this project).

Basically, I would like jsmin.py for CSS. The only script without dependencies.

Any ideas?

+42
python css compression minify
Oct 21 '08 at 16:42
source share
6 answers

It seemed to me that this is a good task for me to get into python, which has been under consideration for a long time. I hereby present my first python script:

import sys, re css = open( sys.argv[1] , 'r' ).read() # remove comments - this will break a lot of hacks :-P css = re.sub( r'\s*/\*\s*\*/', "$$HACK1$$", css ) # preserve IE<6 comment hack css = re.sub( r'/\*[\s\S]*?\*/', "", css ) css = css.replace( "$$HACK1$$", '/**/' ) # preserve IE<6 comment hack # url() doesn't need quotes css = re.sub( r'url\((["\'])([^)]*)\1\)', r'url(\2)', css ) # spaces may be safely collapsed as generated content will collapse them anyway css = re.sub( r'\s+', ' ', css ) # shorten collapsable colors: #aabbcc to #abc css = 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 zeros css = 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 that this will work, and bring it out fine on the latest Safari, Opera and Firefox. This will break CSS hacks besides underscores and / ** / hacks! Do not use minifier if you have many hackers (or put them in a separate file).

Any advice on my python appreciated. Please be careful, this is my first time. :-)

+64
Oct 21 '08 at 22:05
source share

There is a YUI CSS compressor port for python.

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

+12
Mar 07
source share

I don't know any ready-made python css minifiers, but, as you said, css utils has an option. After checking and verifying that the license allows this, you can go through the source code and cut out the parts that perform mining. Then paste this into one script and voila! There you go.

As a start, the csscombine function in ... / trunk / src / cssutils / script.py seems to be doing the work of minimizing somewhere around line 361 (I checked revision 1499). Note the boolean argument of the function called "minify".

+1
Oct 21 '08 at 17:18
source share

There is a good cssminifier online tool which also has an API that is pretty simple and easy to use. I made a small python script that places the contents of the CSS file in this tool API, returns miniature CSS and saves it to the file "style.min.css". I like this because it is a little code that can be well integrated into an automatic deployment script:

 import requests f = open("style.css", "r") css_text = f.read() f.close() r = requests.post("http://cssminifier.com/raw", data={"input":css_text}) css_minified = r.text f2 = open("style.min.css", "w") f2.write(css_minified) f2.close() 
+1
May 01 '15 at
source share

If someone has landed on this question and is using Django, the Django Compressor package is usually used for it:

Compresses related and embedded JavaScript or CSS into a single cached file.

  • JS / CSS belong to templates

  • Flexibility

  • He does not interfere

  • Full test suite

+1
Jun 08 '15 at 15:10
source share

In webassets documents you can find links to several compressors and compilers. From this list, I chose pyScss , which also minimizes the resulting CSS.

If you only need a CSS compressor, you can try csscompressor :

Almost accurate port of YUI CSS Compressor. Skips all original unittests.

More general css-html-prettify tool :

StandAlone Asynchronous single-file cross-platform ready-to-use Unicode Python3 Prettifier Beautifier for the web.

+1
Dec 21 '15 at 12:40
source share



All Articles