Skip to content

Commit

Permalink
Merge branch 'apocquet-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
gasman committed Aug 24, 2015
2 parents 73cd05c + ae66f39 commit 7253a36
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.4 (24.08.2015)
~~~~~~~~~~~~~~~~
* Added support for custom functions (Alexandre Pocquet)
* Added a `static` function to generate paths to assets such as images and fonts (Alexandre Pocquet)

0.3 (27.04.2015)
~~~~~~~~~~~~~~~~
* Enabled source comments when DEBUG is True; can be overridden with the LIBSASS_SOURCE_COMMENTS setting
Expand Down
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,22 @@ The following settings can be used to control django-libsass's behaviour:

* ``LIBSASS_SOURCE_COMMENTS`` - whether to enable SASS source comments (adds comments about source lines). Defaults to ``True`` when Django's ``DEBUG`` is ``True``, ``False`` otherwise.
* ``LIBSASS_OUTPUT_STYLE`` - SASS output style. Options are ``'nested'``, ``'expanded'``, ``'compact'`` and ``'compressed'``, although as of libsass 3.0.2 only ``'nested'`` and ``'compressed'`` are implemented. Default is 'nested'. See `SASS documentation for output styles <http://sass-lang.com/documentation/file.SASS_REFERENCE.html#output_style>`_. Note that `django-compressor's settings <http://django-compressor.readthedocs.org/en/latest/settings/>`_ may also affect the formatting of the resulting CSS.
* ``LIBSASS_CUSTOM_FUNCTIONS`` - A mapping of custom functions to be made available within the SASS compiler. By default, a ``static`` function is provided, analogous to Django's ``static`` template tag.

Custom functions
~~~~~~~~~~~~~~~~

The SASS compiler can be extended with custom Python functions defined in the ``LIBSASS_CUSTOM_FUNCTIONS`` setting. By default, a ``static`` function is provided, for generating static paths to resources such as images and fonts::

.foo {
background: url(static("myapp/image/bar.png"));
}

If your ``STATIC_URL`` is '/static/', this will be rendered as::

.foo {
background: url("/static/myapp/image/bar.png"));
}

Why django-libsass?
~~~~~~~~~~~~~~~~~~~
Expand Down
19 changes: 18 additions & 1 deletion django_libsass.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from django.conf import settings
from django.contrib.staticfiles.finders import get_finders
from django.templatetags.static import static as django_static

import sass
from compressor.filters.base import FilterBase


def static(path):
"""
Use the Django builtin static file resolver to return an absolute path
usable as CSS url() argument. Sass equivalent of the 'static' template
tag.
"""
return '"{}"'.format(django_static(path))


OUTPUT_STYLE = getattr(settings, 'LIBSASS_OUTPUT_STYLE', 'nested')
SOURCE_COMMENTS = getattr(settings, 'LIBSASS_SOURCE_COMMENTS', settings.DEBUG)
CUSTOM_FUNCTIONS = getattr(settings, 'LIBSASS_CUSTOM_FUNCTIONS', {'static': static})


def get_include_paths():
Expand Down Expand Up @@ -35,6 +47,7 @@ def get_include_paths():

INCLUDE_PATHS = None # populate this on first call to 'compile'


def compile(**kwargs):
"""Perform sass.compile, but with the appropriate include_paths for Django added"""
global INCLUDE_PATHS
Expand All @@ -43,6 +56,11 @@ def compile(**kwargs):

kwargs = kwargs.copy()
kwargs['include_paths'] = (kwargs.get('include_paths') or []) + INCLUDE_PATHS

custom_functions = CUSTOM_FUNCTIONS.copy()
custom_functions.update(kwargs.get('custom_functions', {}))
kwargs['custom_functions'] = custom_functions

return sass.compile(**kwargs)


Expand All @@ -59,4 +77,3 @@ def input(self, **kwargs):
else:
return compile(string=self.content,
output_style=OUTPUT_STYLE)

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='django-libsass',
version='0.3',
version='0.4',
description="A django-compressor filter to compile SASS files using libsass",
author='Matt Westcott',
author_email='[email protected]',
Expand All @@ -28,6 +28,6 @@
],
install_requires=[
"django-compressor>=1.3",
"libsass>=0.3.0",
"libsass>=0.7.0",
],
)

0 comments on commit 7253a36

Please sign in to comment.