Skip to content

Commit

Permalink
"Merge pull request #352 from sirex/libsass\n\nLibsass filter, fixes #…
Browse files Browse the repository at this point in the history
…145"

Conflicts:
	requirements-dev.pip
  • Loading branch information
miracle2k committed Sep 13, 2014
2 parents 0d490e2 + cfb41e9 commit 362d2e1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-dev.pip
Expand Up @@ -22,6 +22,7 @@ cssutils
yuicompressor
closure
slimit
libsass

# Python libs that requiring manual installation
#cssprefixer
77 changes: 77 additions & 0 deletions src/webassets/filter/libsass.py
@@ -0,0 +1,77 @@
# coding: utf-8

"""
This filter based on Jesús Jerez <jerezmoreno@gmail.com> code [1].
[1] https://bitbucket.org/jhuss/webassets-libsass
"""

from __future__ import print_function
from __future__ import absolute_import

from webassets.filter import Filter


__all__ = ('LibSass',)


class LibSass(Filter):
"""Converts `Sass <http://sass-lang.com/>`_ markup to real CSS.
Requires the ``libsass`` package (https://pypi.python.org/pypi/libsass)::
pip install libsass
`libsass <http://dahlia.kr/libsass-python>`_ is binding to C/C++
implementation of a Sass compiler `Libsass
<https://github.com/hcatlin/libsass>`_
*Configuration options:*
LIBSASS_STYLE (style)
an optional coding style of the compiled result. choose one of:
`nested` (default), `expanded`, `compact`, `compressed`
LIBSASS_INCLUDES (includes)
an optional list of paths to find @imported SASS/CSS source files
LIBSASS_IMAGES (images)
an optional path to find images
See libsass documentation for full documentation about these configuration
options:
http://hongminhee.org/libsass-python/sass.html#sass.compile
"""
name = 'libsass'
options = {
'style': 'LIBSASS_STYLE',
'includes': 'LIBSASS_INCLUDES',
'images': 'LIBSASS_IMAGES',
}

def setup(self):
super(LibSass, self).setup()

try:
import sass
except ImportError:
raise EnvironmentError('The "libsass" package is not installed.')
else:
self.sass = sass

if not self.style:
self.style = 'nested'

def input(self, _in, out, **kwargs):
source_path = kwargs['source_path']

out.write(
# http://hongminhee.org/libsass-python/sass.html#sass.compile
self.sass.compile(
filename=source_path,
output_style=self.style,
include_paths=(self.includes if self.includes else []),
image_path=(self.images if self.images else '')
)
)
26 changes: 26 additions & 0 deletions tests/test_filters.py
Expand Up @@ -994,6 +994,32 @@ def test_assets(self):
self.get('out.css'),)


class TestLibSass(TempEnvironmentHelper):
default_files = {
'foo.scss': '@import "bar"; a {color: red + green; }',
'bar.scss': 'h1{color:red}'
}

def setup(self):
try:
import sass
self.sass = sass
except ImportError:
raise SkipTest()
TempEnvironmentHelper.setup(self)

def test(self):
self.mkbundle('foo.scss', filters='libsass', output='out.css').build()
assert self.get('out.css') == (
'h1 {\n color: red; }\n\na {\n color: #ff8000; }\n'
)

def test_compressed(self):
libsass = get_filter('libsass', style='compressed')
self.mkbundle('foo.scss', filters=libsass, output='out.css').build()
assert self.get('out.css') == 'h1{color:red;}a{color:#ff8000;}'


class TestCompass(TempEnvironmentHelper):

default_files = {
Expand Down

0 comments on commit 362d2e1

Please sign in to comment.