From 652db17b2d19deb95bfa534fa13286f8b6d2fab0 Mon Sep 17 00:00:00 2001 From: Nolan Stover Date: Wed, 22 Jul 2015 16:33:59 -0600 Subject: [PATCH 1/4] Inject Django's DEBUG setting into sass namespace as $debug --- django_pyscss/compiler.py | 6 +++ .../static/css/namespace_debug.scss | 3 ++ tests/test_scss.py | 39 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 testproject/testproject/static/css/namespace_debug.scss diff --git a/django_pyscss/compiler.py b/django_pyscss/compiler.py index cf20595..dc8d616 100644 --- a/django_pyscss/compiler.py +++ b/django_pyscss/compiler.py @@ -10,7 +10,9 @@ from scss import Compiler, config from scss.extension.compass import CompassExtension +from scss.namespace import Namespace from scss.source import SourceFile +from scss.types import Boolean from .extension.django import DjangoExtension from .utils import find_all_files, get_file_and_storage @@ -30,6 +32,10 @@ class DjangoScssCompiler(Compiler): def __init__(self, **kwargs): kwargs.setdefault('extensions', (DjangoExtension, CompassExtension)) + namespace = kwargs.pop('namespace', Namespace()) + if '$debug' not in namespace.variables: + namespace.set_variable('$debug', Boolean(settings.DEBUG)) + kwargs.update(namespace=namespace) if not os.path.exists(config.ASSETS_ROOT): os.makedirs(config.ASSETS_ROOT) super(DjangoScssCompiler, self).__init__(**kwargs) 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..b1e44b6 100644 --- a/tests/test_scss.py +++ b/tests/test_scss.py @@ -3,9 +3,12 @@ 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.namespace import Namespace +from scss.types import Boolean from django_pyscss import DjangoScssCompiler @@ -43,6 +46,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 +171,36 @@ 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 NamespaceTest(TestCase): + def test_debug_namespace(self): + compiler = DjangoScssCompiler() + actual = compiler.compile_string(NAMESPACE_DEBUG_CONTENTS) + self.assertEqual(clean_css(actual), clean_css(NAMESPACE_DEBUG_TRUE_CONTENTS)) + + def test_debug_namespace_override(self): + compiler = DjangoScssCompiler() + 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_injection(self): + namespace = Namespace() + namespace.set_variable('$debug', Boolean(False)) + compiler = DjangoScssCompiler(namespace=namespace) + actual = compiler.compile_string(NAMESPACE_DEBUG_CONTENTS) + self.assertEqual(clean_css(actual), clean_css(NAMESPACE_DEBUG_FALSE_CONTENTS)) From fa79d3f9c14d83d249fb9f2e1cdf449f257529fb Mon Sep 17 00:00:00 2001 From: Nolan Stover Date: Fri, 24 Jul 2015 10:09:50 -0600 Subject: [PATCH 2/4] Move DEBUG namespace injection into opt-in Extension --- README.rst | 22 +++++++++++++++++++++- django_pyscss/compiler.py | 8 +------- django_pyscss/extension/__init__.py | 4 ++++ django_pyscss/extension/django_debug.py | 16 ++++++++++++++++ tests/test_scss.py | 17 +++++++++++------ 5 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 django_pyscss/extension/django_debug.py diff --git a/README.rst b/README.rst index b6099a3..729b71f 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 omit anything you are not comfortable with people seeing in your +compiled stylesheets, as debug-specific rules and declarations will still be viewable in them. + 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 dc8d616..2a09696 100644 --- a/django_pyscss/compiler.py +++ b/django_pyscss/compiler.py @@ -10,11 +10,9 @@ from scss import Compiler, config from scss.extension.compass import CompassExtension -from scss.namespace import Namespace from scss.source import SourceFile -from scss.types import Boolean -from .extension.django import DjangoExtension +from .extension import DjangoExtension from .utils import find_all_files, get_file_and_storage @@ -32,10 +30,6 @@ class DjangoScssCompiler(Compiler): def __init__(self, **kwargs): kwargs.setdefault('extensions', (DjangoExtension, CompassExtension)) - namespace = kwargs.pop('namespace', Namespace()) - if '$debug' not in namespace.variables: - namespace.set_variable('$debug', Boolean(settings.DEBUG)) - kwargs.update(namespace=namespace) if not os.path.exists(config.ASSETS_ROOT): os.makedirs(config.ASSETS_ROOT) super(DjangoScssCompiler, self).__init__(**kwargs) 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/tests/test_scss.py b/tests/test_scss.py index b1e44b6..18acf83 100644 --- a/tests/test_scss.py +++ b/tests/test_scss.py @@ -7,10 +7,12 @@ 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 @@ -187,20 +189,23 @@ def test_sprite_images(self): @override_settings(DEBUG=True) -class NamespaceTest(TestCase): +class DebugExtensionTest(TestCase): def test_debug_namespace(self): - compiler = DjangoScssCompiler() + 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_override(self): - compiler = DjangoScssCompiler() + 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_injection(self): + def test_debug_namespace_kwarg_override(self): namespace = Namespace() namespace.set_variable('$debug', Boolean(False)) - compiler = DjangoScssCompiler(namespace=namespace) + 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)) From 1d55c2aa212a4a522cda7248fc8c5fae2e120cc8 Mon Sep 17 00:00:00 2001 From: Nolan Stover Date: Fri, 24 Jul 2015 10:17:48 -0600 Subject: [PATCH 3/4] Update README --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 729b71f..ab5f06c 100644 --- a/README.rst +++ b/README.rst @@ -103,7 +103,7 @@ You can then use the ``$debug`` variable anywhere in your SCSS files.: visibility: if($debug, visible, hidden); } -Note that you should omit anything you are not comfortable with people seeing in your +Note that you should omit anything you are not comfortable with people seeing in your html and compiled stylesheets, as debug-specific rules and declarations will still be viewable in them. For a list of options that ``DjangoScssCompiler`` accepts, please see the From 091c1009a8aa04b9331be77029b46a4f65541129 Mon Sep 17 00:00:00 2001 From: Nolan Stover Date: Fri, 24 Jul 2015 10:21:18 -0600 Subject: [PATCH 4/4] Update README --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index ab5f06c..9a8cf26 100644 --- a/README.rst +++ b/README.rst @@ -103,8 +103,8 @@ You can then use the ``$debug`` variable anywhere in your SCSS files.: visibility: if($debug, visible, hidden); } -Note that you should omit anything you are not comfortable with people seeing in your html and -compiled stylesheets, as debug-specific rules and declarations will still be viewable in them. +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 `_.