summaryrefslogtreecommitdiff
path: root/django_pyscss/compressor.py
blob: 68aec24893baf843004cf2d4c08d4f7768d8043e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from __future__ import absolute_import

from compressor.filters import FilterBase
from compressor.conf import settings

from django_pyscss import DjangoScssCompiler


class DjangoScssFilter(FilterBase):
    compiler = DjangoScssCompiler()

    def __init__(self, content, attrs=None, filter_type=None, filename=None, **kwargs):
        # It looks like there is a bug in django-compressor because it expects
        # us to accept attrs.
        super(DjangoScssFilter, self).__init__(content, filter_type, filename, **kwargs)
        try:
            # this is a link tag which means there is an SCSS file being
            # referenced.
            href = attrs['href']
        except KeyError:
            # this is a style tag which means this is inline SCSS.
            self.filename = None
        else:
            self.filename = href.replace(settings.STATIC_URL, '')

    def input(self, **kwargs):
        return self.compiler.compile_string(self.content, filename=self.filename)