diff --git a/CHANGELOG.txt b/CHANGELOG.txt index b489264..04d1b09 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 diff --git a/README.rst b/README.rst index 482fa52..48297dd 100644 --- a/README.rst +++ b/README.rst @@ -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 `_. Note that `django-compressor's 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? ~~~~~~~~~~~~~~~~~~~ diff --git a/django_libsass.py b/django_libsass.py index b16599b..e08b308 100644 --- a/django_libsass.py +++ b/django_libsass.py @@ -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(): @@ -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 @@ -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) @@ -59,4 +77,3 @@ def input(self, **kwargs): else: return compile(string=self.content, output_style=OUTPUT_STYLE) - diff --git a/setup.py b/setup.py index 5a8c649..7237863 100644 --- a/setup.py +++ b/setup.py @@ -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='matthew.westcott@torchbox.com', @@ -28,6 +28,6 @@ ], install_requires=[ "django-compressor>=1.3", - "libsass>=0.3.0", + "libsass>=0.7.0", ], )