Skip to content

Inject Django's DEBUG setting into sass namespace as $debug #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,30 @@ wish to turn this off you do so:
.. code-block:: python

from django_pyscss import DjangoScssCompiler
from django_pyscss.extensions.django import DjangoExtension
from django_pyscss.extension import DjangoExtension

compiler = DjangoScssCompiler(extensions=[DjangoExtension])

``DjangoScssCompiler`` can optionally be used with an extension that injects Django's
``DEBUG`` setting as a variable into the namespace of your SCSS files.:

.. code-block:: python

from django_pyscss.extension import DjangoExtension, DjangoDebugExtension

compiler = DjangoScssCompiler(extensions=[DjangoExtension, DjangoDebugExtension])

You can then use the ``$debug`` variable anywhere in your SCSS files.:

.. code-block:: scss

.debug {
visibility: if($debug, visible, hidden);
}

Note that you should still omit anything you are not comfortable with people seeing in your
markup and compiled stylesheets, as that content will still be available in the HTML.

For a list of options that ``DjangoScssCompiler`` accepts, please see the
pyScss `API documentation <http://pyscss.readthedocs.org/en/latest/python-api.html#new-api>`_.

Expand Down
2 changes: 1 addition & 1 deletion django_pyscss/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from scss.extension.compass import CompassExtension
from scss.source import SourceFile

from .extension.django import DjangoExtension
from .extension import DjangoExtension
from .utils import find_all_files, get_file_and_storage


Expand Down
4 changes: 4 additions & 0 deletions django_pyscss/extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from __future__ import absolute_import

from .django import DjangoExtension
from .django_debug import DjangoDebugExtension
16 changes: 16 additions & 0 deletions django_pyscss/extension/django_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import absolute_import

from django.conf import settings

from scss.extension import Extension
from scss.namespace import Namespace
from scss.types import Boolean


class DjangoDebugExtension(Extension):
name = 'django_debug'

def __init__(self):
namespace = Namespace()
namespace.set_variable('$debug', Boolean(settings.DEBUG))
self.namespace = namespace
3 changes: 3 additions & 0 deletions testproject/testproject/static/css/namespace_debug.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.debug {
visibility: if($debug, visible, hidden);
}
44 changes: 44 additions & 0 deletions tests/test_scss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import mock

from django.test import TestCase
from django.test.utils import override_settings
from django.conf import settings

from scss.errors import SassImportError
from scss.extension.compass import CompassExtension
from scss.namespace import Namespace
from scss.types import Boolean

from django_pyscss import DjangoScssCompiler
from django_pyscss.extension import DjangoExtension, DjangoDebugExtension

from tests.utils import clean_css, CollectStaticTestCase, NoCollectStaticTestCase

Expand Down Expand Up @@ -43,6 +48,9 @@
with open(os.path.join(settings.BASE_DIR, 'testproject', 'static', 'css', 'dot.file.scss')) as f:
DOT_FILE_CONTENTS = f.read()

with open(os.path.join(settings.BASE_DIR, 'testproject', 'static', 'css', 'namespace_debug.scss')) as f:
NAMESPACE_DEBUG_CONTENTS = f.read()


class CompilerTestMixin(object):
def setUp(self):
Expand Down Expand Up @@ -165,3 +173,39 @@ def test_sprite_images(self):
# pyScss puts a cachebuster query string on the end of the URLs, lets
# just check that it made the file that we expected.
self.assertTrue(re.search(r'url\(/static/scss/assets/images_icons-.+\.png\?_=\d+', actual))


NAMESPACE_DEBUG_TRUE_CONTENTS = """
.debug {
visibility: visible;
}
"""

NAMESPACE_DEBUG_FALSE_CONTENTS = """
.debug {
visibility: hidden;
}
"""


@override_settings(DEBUG=True)
class DebugExtensionTest(TestCase):
def test_debug_namespace(self):
compiler = DjangoScssCompiler(extensions=(DjangoDebugExtension, DjangoExtension, CompassExtension))
actual = compiler.compile_string(NAMESPACE_DEBUG_CONTENTS)
self.assertEqual(clean_css(actual), clean_css(NAMESPACE_DEBUG_TRUE_CONTENTS))

def test_debug_namespace_sass_override(self):
compiler = DjangoScssCompiler(extensions=(DjangoDebugExtension, DjangoExtension, CompassExtension))
actual = compiler.compile_string('$debug:false;\n' + NAMESPACE_DEBUG_CONTENTS)
self.assertEqual(clean_css(actual), clean_css(NAMESPACE_DEBUG_FALSE_CONTENTS))

def test_debug_namespace_kwarg_override(self):
namespace = Namespace()
namespace.set_variable('$debug', Boolean(False))
compiler = DjangoScssCompiler(
extensions=(DjangoDebugExtension, DjangoExtension, CompassExtension),
namespace=namespace,
)
actual = compiler.compile_string(NAMESPACE_DEBUG_CONTENTS)
self.assertEqual(clean_css(actual), clean_css(NAMESPACE_DEBUG_FALSE_CONTENTS))