1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
.. copyright:
Copyright 2011 - 2021
André Malo or his licensors, as applicable
.. license:
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
========================
rCSSmin - CSS Minifier
========================
|**rcssmin**| is a CSS minifier written in python.
.. contents::
:local:
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 :doc:`speed <benchmark>`
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:
- Strings are kept, except that escaped newlines are stripped
- Space/Comments before the very end or before various characters are
stripped: ``:{});=>],!`` (The colon (``:``) is a special case, a single
space is kept if it's outside a ruleset.)
- Space/Comments at the very beginning or after various characters are
stripped: ``{}(=:>[,!``
- Optional space after unicode escapes is kept, resp. replaced by a simple
space
- whitespaces inside ``url()`` definitions are stripped, except if it's a
quoted non-base64 data url
- Comments starting with an exclamation mark (``!``) can be kept optionally.
- All other comments and/or whitespace characters are replaced by a single
space.
- Multiple consecutive semicolons are reduced to one
- The last semicolon within a ruleset is stripped
- CSS Hacks supported:
- IE7 hack (``>/**/``)
- Mac-IE5 hack (``/*\*/.../**/``)
- The boxmodelhack is supported naturally because it relies on valid CSS2
strings
- Between ``:first-line`` and the following comma or curly brace a space is
inserted. (apparently it's needed for IE6)
- Same for ``:first-letter``
rcssmin.c is a reimplementation of rcssmin.py in C and improves runtime up to
factor 50 or so (depending on the input).
.. _YUI compressor: https://github.com/yui/yuicompressor/
.. _the rule list by Isaac Schlueter: https://github.com/isaacs/cssmin/
Supported python versions are 2.7 and 3.6+.
Development Status
~~~~~~~~~~~~~~~~~~
|rcssmin| is stable.
Documentation
~~~~~~~~~~~~~
The module provides a simple function (generated apidoc below), called
``cssmin`` which takes the script as a string and returns the minified
script as a string.
The module additionally provides a "streamy" interface:
.. sourcecode:: console
$ python -mrcssmin <css >minified
This command line interface takes two options:
-b Keep bang-comments (Comments starting with an exclamation mark)
-p Force using the python implementation (not the C implementation)
API Docs
--------
.. autofunction:: rcssmin.cssmin
License
~~~~~~~
|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
<http://www.apache.org/licenses/LICENSE-2.0>`_.
.. placeholder: Download
Bugs
~~~~
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 <https://github.com/ndparker/rcssmin>`_
or send a mail to <rcssmin-bugs@perlig.de>.
Author Information
~~~~~~~~~~~~~~~~~~
|rcssmin| was written and is maintained by André Malo.
Trivia / Fun
~~~~~~~~~~~~
|rCSSmin| analyzes the CSS input using a simple regular expression, which
looks like this:
.. sourcecode:: text
pattern = (
r'([^\\"\047u>@\r\n\f\040\t/;:{}+]+)|(?<=[{}(=:>[,!])((?:[\r\n\f'
r'\040\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)|^((?:[\r\n\f\040'
r'\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)|((?:[\r\n\f\040\t]|('
r'?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))+)(?=(([:{});=>\],!])|$)?)|;'
r'((?:[\r\n\f\040\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*(?:;(?:'
r'[\r\n\f\040\t]|(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/))*)*)(?=(\})?'
r')|(\{)|(\})|((?:(?:\047[^\047\\\r\n\f]*(?:\\[^\r\n\f][^\047'
r'\\\r\n\f]*)*\047)|(?:"[^"\\\r\n\f]*(?:\\[^\r\n\f][^"\\\r\n\f]*'
r')*")))|(?<![^\000-\054\056\057\072-\100\133-\136\140\173-\177]'
r')url\([\r\n\f\040\t]*((?:(?:\047[^\047\\]*(?:\\(?:[^\r]|\r\n?)'
r'[^\047\\]*)*\047)|(?:"[^"\\]*(?:\\(?:[^\r]|\r\n?)[^"\\]*)*"))|'
r'(?:(?:[^\000-\040"\047()\\\177]*(?:(?:\\(?:[0-9a-fA-F]{1,6}(?:'
r'[\040\n\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))[^\000-\040"\047()'
r'\\\177]*)*)(?:(?:[\r\n\f\040\t]+|(?:\\(?:[\n\f]|\r\n?))+)(?:(?'
r':[^\000-\040"\047()\\\177]|(?:\\(?:[0-9a-fA-F]{1,6}(?:[\040\n'
r'\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))|(?:\\(?:[\n\f]|\r\n?)))[^\0'
r'00-\040"\047()\\\177]*(?:(?:\\(?:[0-9a-fA-F]{1,6}(?:[\040\n\t'
r'\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))[^\000-\040"\047()\\\177]*)*)+'
r')*))[\r\n\f\040\t]*\)|(@(?:[mM][eE][dD][iI][aA]|[sS][uU][pP][p'
r'P][oO][rR][tT][sS]|[dD][oO][cC][uU][mM][eE][nN][tT]|(?:-(?:[wW'
r'][eE][bB][kK][iI][tT]|[mM][oO][zZ]|[oO]|[mM][sS])-)?[kK][eE][y'
r'Y][fF][rR][aA][mM][eE][sS]))(?![^\000-\054\056\057\072-\100\13'
r'3-\136\140\173-\177])|((?:>/\*\*/))((?:[\r\n\f\040\t]|(?:/\*[^'
r'*]*\*+(?:[^/*][^*]*\*+)*/))*)|(:[fF][iI][rR][sS][tT]-[lL](?:[i'
r'I][nN][eE]|[eE][tT][tT][eE][rR]))((?:[\r\n\f\040\t]|(?:/\*[^*]'
r'*\*+(?:[^/*][^*]*\*+)*/))*)(?=[{,])|((?:(?:\047[^\047\\\r\n\f]'
r'*(?:\\(?:[^\r]|\r\n?)[^\047\\\r\n\f]*)*\047)|(?:"[^"\\\r\n\f]*'
r'(?:\\(?:[^\r]|\r\n?)[^"\\\r\n\f]*)*")))|((?:\\(?:[0-9a-fA-F]{1'
r',6}(?:[\040\n\t\f]|\r\n?)?|[^\n\r\f0-9a-fA-F]))[^\\"\047u>@\r'
r'\n\f\040\t/;:{}+]*)'
)
Related
~~~~~~~
- `Javascript Minifier rJSmin <http://opensource.perlig.de/rjsmin/>`_
.. vim: ft=rest tw=72
|