@@ -1,20 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: ascii -*-
|
||||||
#
|
|
||||||
# Copyright 2011, 2012
|
|
||||||
# Andr\xe9 Malo or his licensors, as applicable
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
r"""
|
r"""
|
||||||
==============
|
==============
|
||||||
CSS Minifier
|
CSS Minifier
|
||||||
@@ -22,8 +7,27 @@ r"""
|
|||||||
|
|
||||||
CSS Minifier.
|
CSS Minifier.
|
||||||
|
|
||||||
The minifier is based on the semantics of the `YUI compressor`_\, which itself
|
The minifier is based on the semantics of the `YUI compressor`_\\, which
|
||||||
is based on `the rule list by Isaac Schlueter`_\.
|
itself is based on `the rule list by Isaac Schlueter`_\\.
|
||||||
|
|
||||||
|
:Copyright:
|
||||||
|
|
||||||
|
Copyright 2011 - 2014
|
||||||
|
Andr\xe9 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.
|
||||||
|
|
||||||
This module is a re-implementation aiming for speed instead of maximum
|
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
|
compression, so it can be used at runtime (rather than during a preprocessing
|
||||||
@@ -51,7 +55,7 @@ Here's a feature list:
|
|||||||
- CSS Hacks supported:
|
- CSS Hacks supported:
|
||||||
|
|
||||||
- IE7 hack (``>/**/``)
|
- IE7 hack (``>/**/``)
|
||||||
- Mac-IE5 hack (``/*\*/.../**/``)
|
- Mac-IE5 hack (``/*\\*/.../**/``)
|
||||||
- The boxmodelhack is supported naturally because it relies on valid CSS2
|
- The boxmodelhack is supported naturally because it relies on valid CSS2
|
||||||
strings
|
strings
|
||||||
- Between ``:first-line`` and the following comma or curly brace a space is
|
- Between ``:first-line`` and the following comma or curly brace a space is
|
||||||
@@ -59,19 +63,22 @@ Here's a feature list:
|
|||||||
- Same for ``:first-letter``
|
- Same for ``:first-letter``
|
||||||
|
|
||||||
rcssmin.c is a reimplementation of rcssmin.py in C and improves runtime up to
|
rcssmin.c is a reimplementation of rcssmin.py in C and improves runtime up to
|
||||||
factor 50 or so (depending on the input).
|
factor 100 or so (depending on the input). docs/BENCHMARKS in the source
|
||||||
|
distribution contains the details.
|
||||||
|
|
||||||
Both python 2 (>= 2.4) and python 3 are supported.
|
Both python 2 (>= 2.4) and python 3 are supported.
|
||||||
|
|
||||||
.. _YUI compressor: https://github.com/yui/yuicompressor/
|
.. _YUI compressor: https://github.com/yui/yuicompressor/
|
||||||
|
|
||||||
.. _the rule list by Isaac Schlueter: https://github.com/isaacs/cssmin/tree/
|
.. _the rule list by Isaac Schlueter: https://github.com/isaacs/cssmin/
|
||||||
"""
|
"""
|
||||||
__author__ = "Andr\xe9 Malo"
|
if __doc__:
|
||||||
__author__ = getattr(__author__, 'decode', lambda x: __author__)('latin-1')
|
# pylint: disable = W0622
|
||||||
|
__doc__ = __doc__.encode('ascii').decode('unicode_escape')
|
||||||
|
__author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
|
||||||
__docformat__ = "restructuredtext en"
|
__docformat__ = "restructuredtext en"
|
||||||
__license__ = "Apache License, Version 2.0"
|
__license__ = "Apache License, Version 2.0"
|
||||||
__version__ = '1.0.2'
|
__version__ = '1.0.5'
|
||||||
__all__ = ['cssmin']
|
__all__ = ['cssmin']
|
||||||
|
|
||||||
import re as _re
|
import re as _re
|
||||||
@@ -89,11 +96,7 @@ def _make_cssmin(python_only=False):
|
|||||||
:Return: Minifier
|
:Return: Minifier
|
||||||
:Rtype: ``callable``
|
:Rtype: ``callable``
|
||||||
"""
|
"""
|
||||||
# pylint: disable = W0612
|
# pylint: disable = R0912, R0914, W0612
|
||||||
# ("unused" variables)
|
|
||||||
|
|
||||||
# pylint: disable = R0911, R0912, R0914, R0915
|
|
||||||
# (too many anything)
|
|
||||||
|
|
||||||
if not python_only:
|
if not python_only:
|
||||||
try:
|
try:
|
||||||
@@ -142,6 +145,7 @@ def _make_cssmin(python_only=False):
|
|||||||
ie7hack = r'(?:>/\*\*/)'
|
ie7hack = r'(?:>/\*\*/)'
|
||||||
|
|
||||||
uri = (r'(?:'
|
uri = (r'(?:'
|
||||||
|
# noqa pylint: disable = C0330
|
||||||
r'(?:[^\000-\040"\047()\\\177]*'
|
r'(?:[^\000-\040"\047()\\\177]*'
|
||||||
r'(?:%(escape)s[^\000-\040"\047()\\\177]*)*)'
|
r'(?:%(escape)s[^\000-\040"\047()\\\177]*)*)'
|
||||||
r'(?:'
|
r'(?:'
|
||||||
@@ -171,6 +175,7 @@ def _make_cssmin(python_only=False):
|
|||||||
post_esc_sub = _re.compile(r'[\r\n\f\t]+').sub
|
post_esc_sub = _re.compile(r'[\r\n\f\t]+').sub
|
||||||
|
|
||||||
main_sub = _re.compile((
|
main_sub = _re.compile((
|
||||||
|
# noqa pylint: disable = C0330
|
||||||
r'([^\\"\047u>@\r\n\f\040\t/;:{}]+)'
|
r'([^\\"\047u>@\r\n\f\040\t/;:{}]+)'
|
||||||
r'|(?<=[{}(=:>+[,!])(%(space)s+)'
|
r'|(?<=[{}(=:>+[,!])(%(space)s+)'
|
||||||
r'|^(%(space)s+)'
|
r'|^(%(space)s+)'
|
||||||
@@ -183,7 +188,15 @@ def _make_cssmin(python_only=False):
|
|||||||
r'%(uri_nl_strings)s'
|
r'%(uri_nl_strings)s'
|
||||||
r'|%(uri)s'
|
r'|%(uri)s'
|
||||||
r')%(spacechar)s*\)'
|
r')%(spacechar)s*\)'
|
||||||
r'|(@[mM][eE][dD][iI][aA])(?!%(nmchar)s)'
|
r'|(@(?:'
|
||||||
|
r'[mM][eE][dD][iI][aA]'
|
||||||
|
r'|[sS][uU][pP][pP][oO][rR][tT][sS]'
|
||||||
|
r'|[dD][oO][cC][uU][mM][eE][nN][tT]'
|
||||||
|
r'|(?:-(?:'
|
||||||
|
r'[wW][eE][bB][kK][iI][tT]|[mM][oO][zZ]|[oO]|[mM][sS]'
|
||||||
|
r')-)?'
|
||||||
|
r'[kK][eE][yY][fF][rR][aA][mM][eE][sS]'
|
||||||
|
r'))(?!%(nmchar)s)'
|
||||||
r'|(%(ie7hack)s)(%(space)s*)'
|
r'|(%(ie7hack)s)(%(space)s*)'
|
||||||
r'|(:[fF][iI][rR][sS][tT]-[lL]'
|
r'|(:[fF][iI][rR][sS][tT]-[lL]'
|
||||||
r'(?:[iI][nN][eE]|[eE][tT][tT][eE][rR]))'
|
r'(?:[iI][nN][eE]|[eE][tT][tT][eE][rR]))'
|
||||||
@@ -196,10 +209,11 @@ def _make_cssmin(python_only=False):
|
|||||||
|
|
||||||
def main_subber(keep_bang_comments):
|
def main_subber(keep_bang_comments):
|
||||||
""" Make main subber """
|
""" Make main subber """
|
||||||
in_macie5, in_rule, at_media = [0], [0], [0]
|
in_macie5, in_rule, at_group = [0], [0], [0]
|
||||||
|
|
||||||
if keep_bang_comments:
|
if keep_bang_comments:
|
||||||
space_sub = space_sub_banged
|
space_sub = space_sub_banged
|
||||||
|
|
||||||
def space_subber(match):
|
def space_subber(match):
|
||||||
""" Space|Comment subber """
|
""" Space|Comment subber """
|
||||||
if match.lastindex:
|
if match.lastindex:
|
||||||
@@ -222,6 +236,7 @@ def _make_cssmin(python_only=False):
|
|||||||
return ''
|
return ''
|
||||||
else:
|
else:
|
||||||
space_sub = space_sub_simple
|
space_sub = space_sub_simple
|
||||||
|
|
||||||
def space_subber(match):
|
def space_subber(match):
|
||||||
""" Space|Comment subber """
|
""" Space|Comment subber """
|
||||||
if match.lastindex:
|
if match.lastindex:
|
||||||
@@ -238,7 +253,7 @@ def _make_cssmin(python_only=False):
|
|||||||
def fn_space_post(group):
|
def fn_space_post(group):
|
||||||
""" space with token after """
|
""" space with token after """
|
||||||
if group(5) is None or (
|
if group(5) is None or (
|
||||||
group(6) == ':' and not in_rule[0] and not at_media[0]):
|
group(6) == ':' and not in_rule[0] and not at_group[0]):
|
||||||
return ' ' + space_sub(space_subber, group(4))
|
return ' ' + space_sub(space_subber, group(4))
|
||||||
return space_sub(space_subber, group(4))
|
return space_sub(space_subber, group(4))
|
||||||
|
|
||||||
@@ -252,34 +267,33 @@ def _make_cssmin(python_only=False):
|
|||||||
return space_sub(space_subber, group(7))
|
return space_sub(space_subber, group(7))
|
||||||
return ';' + space_sub(space_subber, group(7))
|
return ';' + space_sub(space_subber, group(7))
|
||||||
|
|
||||||
def fn_open(group):
|
def fn_open(_):
|
||||||
""" { handler """
|
""" { handler """
|
||||||
# pylint: disable = W0613
|
if at_group[0]:
|
||||||
if at_media[0]:
|
at_group[0] -= 1
|
||||||
at_media[0] -= 1
|
|
||||||
else:
|
else:
|
||||||
in_rule[0] = 1
|
in_rule[0] = 1
|
||||||
return '{'
|
return '{'
|
||||||
|
|
||||||
def fn_close(group):
|
def fn_close(_):
|
||||||
""" } handler """
|
""" } handler """
|
||||||
# pylint: disable = W0613
|
|
||||||
in_rule[0] = 0
|
in_rule[0] = 0
|
||||||
return '}'
|
return '}'
|
||||||
|
|
||||||
def fn_media(group):
|
def fn_at_group(group):
|
||||||
""" @media handler """
|
""" @xxx group handler """
|
||||||
at_media[0] += 1
|
at_group[0] += 1
|
||||||
return group(13)
|
return group(13)
|
||||||
|
|
||||||
def fn_ie7hack(group):
|
def fn_ie7hack(group):
|
||||||
""" IE7 Hack handler """
|
""" IE7 Hack handler """
|
||||||
if not in_rule[0] and not at_media[0]:
|
if not in_rule[0] and not at_group[0]:
|
||||||
in_macie5[0] = 0
|
in_macie5[0] = 0
|
||||||
return group(14) + space_sub(space_subber, group(15))
|
return group(14) + space_sub(space_subber, group(15))
|
||||||
return '>' + space_sub(space_subber, group(15))
|
return '>' + space_sub(space_subber, group(15))
|
||||||
|
|
||||||
table = (
|
table = (
|
||||||
|
# noqa pylint: disable = C0330
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
@@ -294,7 +308,7 @@ def _make_cssmin(python_only=False):
|
|||||||
lambda g: g(11), # string
|
lambda g: g(11), # string
|
||||||
lambda g: 'url(%s)' % uri_space_sub(uri_space_subber, g(12)),
|
lambda g: 'url(%s)' % uri_space_sub(uri_space_subber, g(12)),
|
||||||
# url(...)
|
# url(...)
|
||||||
fn_media, # @media
|
fn_at_group, # @xxx expecting {...}
|
||||||
None,
|
None,
|
||||||
fn_ie7hack, # ie7hack
|
fn_ie7hack, # ie7hack
|
||||||
None,
|
None,
|
||||||
|
Reference in New Issue
Block a user