diff --git a/README.rst b/README.rst index b6099a3..9a8cf26 100644 --- a/README.rst +++ b/README.rst @@ -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 `_. diff --git a/django_pyscss/compiler.py b/django_pyscss/compiler.py index cf20595..2a09696 100644 --- a/django_pyscss/compiler.py +++ b/django_pyscss/compiler.py @@ -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 diff --git a/django_pyscss/extension/__init__.py b/django_pyscss/extension/__init__.py index e69de29..9cb1231 100644 --- a/django_pyscss/extension/__init__.py +++ b/django_pyscss/extension/__init__.py @@ -0,0 +1,4 @@ +from __future__ import absolute_import + +from .django import DjangoExtension +from .django_debug import DjangoDebugExtension diff --git a/django_pyscss/extension/django_debug.py b/django_pyscss/extension/django_debug.py new file mode 100644 index 0000000..ed40d7e --- /dev/null +++ b/django_pyscss/extension/django_debug.py @@ -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 diff --git a/testproject/testproject/static/css/namespace_debug.scss b/testproject/testproject/static/css/namespace_debug.scss new file mode 100644 index 0000000..9cfb5ec --- /dev/null +++ b/testproject/testproject/static/css/namespace_debug.scss @@ -0,0 +1,3 @@ +.debug { + visibility: if($debug, visible, hidden); +} diff --git a/tests/test_scss.py b/tests/test_scss.py index c1410e1..18acf83 100644 --- a/tests/test_scss.py +++ b/tests/test_scss.py @@ -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 @@ -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): @@ -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))