Skip to content

Commit

Permalink
Adding support for cssmin - a pure python port of YUI CSS Compressor,…
Browse files Browse the repository at this point in the history
… for those of us who don't want to install java.

Set property DJANGO_STATIC_ CSSMIN = True  in settings to enable. Requires cssmin install.
  • Loading branch information
Aida Tavakkolie committed Mar 14, 2011
1 parent 3e111c5 commit 0c72221
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions django_static/templatetags/django_static.py
Expand Up @@ -18,15 +18,20 @@
register = template.Library()

try:
from slimmer import (css_slimmer, guessSyntax, html_slimmer, js_slimmer,
xhtml_slimmer)
from slimmer import (css_slimmer, guessSyntax, html_slimmer, js_slimmer, xhtml_slimmer)
slimmer = 'installed'
except ImportError:
slimmer = None
# because if it's import, it's much better to rely on YUI Compressor
# and Google Closure compiler.
#warnings.warn("slimmer is not installed. (easy_install slimmer)")

try:
import cssmin
except ImportError:
warnings.warn("cssmin is not installed. (easy_install cssmin)")


if sys.platform == "win32":
_CAN_SYMLINK = False
else:
Expand Down Expand Up @@ -700,13 +705,14 @@ def has_optimizer(type_):
if type_ == CSS:
if getattr(settings, 'DJANGO_STATIC_YUI_COMPRESSOR', None):
return True
if getattr(settings, 'DJANGO_STATIC_CSSMIN', None):
return True
return slimmer is not None
elif type_ == JS:
if getattr(settings, 'DJANGO_STATIC_CLOSURE_COMPILER', None):
return True
if getattr(settings, 'DJANGO_STATIC_YUI_COMPRESSOR', None):
return True

return slimmer is not None
else:
raise ValueError("Invalid type %r" % type_)
Expand All @@ -715,6 +721,8 @@ def optimize(content, type_):
if type_ == CSS:
if getattr(settings, 'DJANGO_STATIC_YUI_COMPRESSOR', None):
return _run_yui_compressor(content, type_)
if getattr(settings, 'DJANGO_STATIC_CSSMIN', None):
return _run_cssmin(content)
return css_slimmer(content)
elif type_ == JS:
if getattr(settings, 'DJANGO_STATIC_CLOSURE_COMPILER', None):
Expand Down Expand Up @@ -758,3 +766,8 @@ def _run_yui_compressor(code, type_):
return "/* ERRORS WHEN RUNNING YUI COMPRESSOR\n" + stderrdata + '\n*/\n' + code

return stdoutdata


def _run_cssmin(code):
output = cssmin.cssmin(code)
return output
4 changes: 2 additions & 2 deletions settings.py
Expand Up @@ -94,5 +94,5 @@
DJANGO_STATIC = True

#DJANGO_STATIC_CLOSURE_COMPILER = '/home/peterbe/tmp/compiler-latest/compiler.jar'

#DJANGO_STATIC_YUI_COMPRESSOR = '/home/peterbe/tmp/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar'
#DJANGO_STATIC_YUI_COMPRESSOR = '/home/peterbe/tmp/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar'
#DJANGO_STATIC_CSSMIN = True

0 comments on commit 0c72221

Please sign in to comment.