rCSSmin is a CSS minifier written in python.
The minifier is based on the semantics of the YUI compressor, which itself is based on the rule list by Isaac Schlueter.
This module is a re-implementation aiming for speed instead of maximum compression, so it can be used at runtime (rather than during a preprocessing step). rCSSmin does syntactical compression only (removing spaces, comments and possibly semicolons). It does not provide semantic compression (like removing empty blocks, collapsing redundant properties etc). It does, however, support various CSS hacks (by keeping them working as intended).
Here’s a feature list:
rcssmin.c is a reimplementation of rcssmin.py in C and improves runtime up to factor 50 or so (depending on the input).
rCSSmin works with both python 2 (starting with python 2.4) and python 3.
A generated API documentation is available. But you can just look into the module. It provides a simple function, called cssmin which takes the css as a string and returns the minified css as a string.
The module additionally provides a “streamy” interface:
$ python -mrcssmin <css >minified
It takes two options:
-b Keep bang-comments (Comments starting with an exclamation mark) -p Use the python implementation (not the C implementation)
rCSSmin is stable.
rCSSmin is available under the terms and conditions of the “Apache License, Version 2.0.” You’ll find the detailed licensing terms in the root directory of the source distribution package or online at http://www.apache.org/licenses/LICENSE-2.0.
There are hashes (MD5, SHA1 and SHA256) of the download packages stored in the digests file. In order to check the integrity of the downloaded file, use a tool like md5sum (or sha1sum, sha256sum accordingly), e.g.:
$ md5sum -c rcssmin-1.0.6.digests
rcssmin-1.0.6.tar.bz2: OK
rcssmin-1.0.6.tar.gz: OK
rcssmin-1.0.6.tar.xz: OK
rcssmin-1.0.6.zip: OK
In order to check the integrity of the digest file itself, you can check the PGP signature of that file. The file is signed by André Malo, Key-ID 0x8103A37E:
$ gpg --verify rcssmin-1.0.6.digests
gpg: Signature made Sun Oct 11 17:50:18 2015 CEST using DSA key ID 8103A37E
gpg: Good signature from "Andre Malo <nd@apache.org>"
gpg: aka "Andr\xe9\x20Malo <nd@perlig.de>"
gpg: aka "Andre Malo <ndparker@gmx.net>"
No bugs, of course. ;-) But if you’ve found one or have an idea how to improve rCSSmin, feel free to send a pull request on github or send a mail to <rcssmin-bugs@perlig.de>.
rCSSmin was written and is maintained by André Malo.
rCSSmin analyzes the CSS input using a simple regular expression, which looks like this:
pattern = (
r'([^\\"\047u>@\r\n\f\040\t/;:{}+]+)|(?<=[{}(=:>[,!])((?:[\r\n\f\040'
r'\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)|^((?:[\r\n\f\040\t]|(?:/'
r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)|((?:[\r\n\f\040\t]|(?:/\*[^*]*\*'
r'+(?:[^/*][^*]*\*+)*/))+)(?=(([:{});=>\],!])|$)?)|;((?:[\r\n\f\040'
r'\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:;(?:[\r\n\f\040\t]|(?:/'
r'\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*)(?=(\})?)|(\{)|(\})|((?:(?:\047'
r'[^\047\\\r\n\f]*(?:\\[^\r\n\f][^\047\\\r\n\f]*)*\047)|(?:"[^"\\\r'
r'\n\f]*(?:\\[^\r\n\f][^"\\\r\n\f]*)*")))|(?<![^\000-\054\056\057\07'
r'2-\100\133-\136\140\173-\177])url\([\r\n\f\040\t]*((?:(?:\047[^\04'
r'7\\]*(?:\\(?:[^\r]|\r\n?)[^\047\\]*)*\047)|(?:"[^"\\]*(?:\\(?:[^\r'
r']|\r\n?)[^"\\]*)*"))|(?:(?:[^\000-\040"\047()\\\177]*(?:(?:\\(?:[0'
r'-9a-fA-F]{1,6}(?:[\040\n\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))[^\000-'
r'\040"\047()\\\177]*)*)(?:(?:[\r\n\f\040\t]+|(?:\\(?:[\n\f]|\r\n?))'
r'+)(?:(?:[^\000-\040"\047()\\\177]|(?:\\(?:[0-9a-fA-F]{1,6}(?:[\040'
r'\n\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))|(?:\\(?:[\n\f]|\r\n?)))[^\000'
r'-\040"\047()\\\177]*(?:(?:\\(?:[0-9a-fA-F]{1,6}(?:[\040\n\t\f]|\r'
r'\n?)?|[^\n\r\f0-9a-fA-F]))[^\000-\040"\047()\\\177]*)*)+)*))[\r\n'
r'\f\040\t]*\)|(@(?:[mM][eE][dD][iI][aA]|[sS][uU][pP][pP][oO][rR][tT'
r'][sS]|[dD][oO][cC][uU][mM][eE][nN][tT]|(?:-(?:[wW][eE][bB][kK][iI]'
r'[tT]|[mM][oO][zZ]|[oO]|[mM][sS])-)?[kK][eE][yY][fF][rR][aA][mM][eE'
r'][sS]))(?![^\000-\054\056\057\072-\100\133-\136\140\173-\177])|((?'
r':>/\*\*/))((?:[\r\n\f\040\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)'
r'|(:[fF][iI][rR][sS][tT]-[lL](?:[iI][nN][eE]|[eE][tT][tT][eE][rR]))'
r'((?:[\r\n\f\040\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)(?=[{,])|('
r'(?:(?:\047[^\047\\\r\n\f]*(?:\\(?:[^\r]|\r\n?)[^\047\\\r\n\f]*)*\0'
r'47)|(?:"[^"\\\r\n\f]*(?:\\(?:[^\r]|\r\n?)[^"\\\r\n\f]*)*")))|((?:'
r'\\(?:[0-9a-fA-F]{1,6}(?:[\040\n\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))['
r'^\\"\047u>@\r\n\f\040\t/;:{}+]*)'
)