Permalink
Browse files

Merge pull request #184 from sebpiq/develop

Added TemplateFilter + tests and docs.
  • Loading branch information...
2 parents f752c7b + c462c5d commit 37a7d78fe523e3733b23d7b2ce3d215d4fa46fff @jezdez jezdez committed Jan 16, 2012
Showing with 64 additions and 1 deletion.
  1. +10 −0 compressor/conf.py
  2. +12 −0 compressor/filters/template.py
  3. +1 −1 compressor/tests/__init__.py
  4. +24 −0 compressor/tests/filters.py
  5. +17 −0 docs/settings.txt
View
@@ -62,6 +62,8 @@ class CompressorConf(AppConf):
OFFLINE_CONTEXT = {}
# The name of the manifest file (e.g. filename.ext)
OFFLINE_MANIFEST = 'manifest.json'
+ # The Context to be used when TemplateFilter is used
+ TEMPLATE_FILTER_CONTEXT = {}
class Meta:
prefix = 'compress'
@@ -107,6 +109,14 @@ def configure_offline_context(self, value):
value['STATIC_URL'] = settings.STATIC_URL
return value
+ def configure_template_filter_context(self, value):
+ if not value:
+ value = {'MEDIA_URL': settings.MEDIA_URL}
+ # Adds the 1.3 STATIC_URL setting to the context if available
+ if getattr(settings, 'STATIC_URL', None):
+ value['STATIC_URL'] = settings.STATIC_URL
+ return value
+
def configure_precompilers(self, value):
if not isinstance(value, (list, tuple)):
raise ImproperlyConfigured("The COMPRESS_PRECOMPILERS setting "
@@ -0,0 +1,12 @@
+from django.template import Template, Context
+from django.conf import settings
+
+from compressor.filters import FilterBase, FilterError
+
+
+class TemplateFilter(FilterBase):
+
+ def input(self, filename=None, basename=None, **kwargs):
+ template = Template(self.content)
+ context = Context(settings.COMPRESS_TEMPLATE_FILTER_CONTEXT)
+ return template.render(context)
@@ -2,7 +2,7 @@
VerboseTestCase, CacheBackendTestCase)
from compressor.tests.filters import (CssTidyTestCase, PrecompilerTestCase,
CssMinTestCase, CssAbsolutizingTestCase, CssAbsolutizingTestCaseWithHash,
- CssDataUriTestCase)
+ CssDataUriTestCase, TemplateTestCase)
from compressor.tests.jinja2ext import TestJinja2CompressorExtension
from compressor.tests.offline import (
OfflineGenerationBlockSuperTestCase, OfflineGenerationConditionTestCase,
@@ -12,6 +12,7 @@
from compressor.filters.base import CompilerFilter
from compressor.filters.cssmin import CSSMinFilter
from compressor.filters.css_default import CssAbsoluteFilter
+from compressor.filters.template import TemplateFilter
from compressor.tests.base import test_dir
@@ -198,3 +199,26 @@ def test_data_uris(self):
datauri_hash = get_hashed_mtime(os.path.join(settings.COMPRESS_ROOT, 'css/datauri.css'))
out = [u'.add { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJvSURBVDjLpZPrS5NhGIf9W7YvBYOkhlkoqCklWChv2WyKik7blnNris72bi6dus0DLZ0TDxW1odtopDs4D8MDZuLU0kXq61CijSIIasOvv94VTUfLiB74fXngup7nvrnvJABJ/5PfLnTTdcwOj4RsdYmo5glBWP6iOtzwvIKSWstI0Wgx80SBblpKtE9KQs/We7EaWoT/8wbWP61gMmCH0lMDvokT4j25TiQU/ITFkek9Ow6+7WH2gwsmahCPdwyw75uw9HEO2gUZSkfyI9zBPCJOoJ2SMmg46N61YO/rNoa39Xi41oFuXysMfh36/Fp0b7bAfWAH6RGi0HglWNCbzYgJaFjRv6zGuy+b9It96N3SQvNKiV9HvSaDfFEIxXItnPs23BzJQd6DDEVM0OKsoVwBG/1VMzpXVWhbkUM2K4oJBDYuGmbKIJ0qxsAbHfRLzbjcnUbFBIpx/qH3vQv9b3U03IQ/HfFkERTzfFj8w8jSpR7GBE123uFEYAzaDRIqX/2JAtJbDat/COkd7CNBva2cMvq0MGxp0PRSCPF8BXjWG3FgNHc9XPT71Ojy3sMFdfJRCeKxEsVtKwFHwALZfCUk3tIfNR8XiJwc1LmL4dg141JPKtj3WUdNFJqLGFVPC4OkR4BxajTWsChY64wmCnMxsWPCHcutKBxMVp5mxA1S+aMComToaqTRUQknLTH62kHOVEE+VQnjahscNCy0cMBWsSI0TCQcZc5ALkEYckL5A5noWSBhfm2AecMAjbcRWV0pUTh0HE64TNf0mczcnnQyu/MilaFJCae1nw2fbz1DnVOxyGTlKeZft/Ff8x1BRssfACjTwQAAAABJRU5ErkJggg=="); }\n.python { background-image: url("/media/img/python.png?%s"); }\n.datauri { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg=="); }\n' % datauri_hash]
self.assertEqual(out, list(self.css_node.hunks()))
+
+
+class TemplateTestCase(TestCase):
+ def setUp(self):
+ self.old_context = settings.COMPRESS_TEMPLATE_FILTER_CONTEXT
+
+ def tearDown(self):
+ settings.COMPRESS_TEMPLATE_FILTER_CONTEXT = self.old_context
+
+ def test_template_filter(self):
+ settings.COMPRESS_TEMPLATE_FILTER_CONTEXT = {
+ 'stuff': 'thing',
+ 'gimmick': 'bold'
+ }
+ content = """
+ #content {background-image: url("{{ STATIC_URL|default:stuff }}/images/bg.png");}
+ #footer {font-weight: {{ gimmick }};}
+ """
+ input = """
+ #content {background-image: url("thing/images/bg.png");}
+ #footer {font-weight: bold;}
+ """
+ self.assertEqual(input, TemplateFilter(content).input())
View
@@ -138,6 +138,15 @@ Backend settings
.. _`data: URIs`: http://en.wikipedia.org/wiki/Data_URI_scheme
.. _cssmin: http://pypi.python.org/pypi/cssmin/
+ - ``compressor.filters.template.TemplateFilter``
+
+ A filter that renders the CSS content with Django templating system.
+
+ .. attribute:: COMPRESS_TEMPLATE_FILTER_CONTEXT
+
+ The context to render your css files with.
+
+
.. _compress_js_filters:
.. attribute:: COMPRESS_JS_FILTERS
@@ -185,6 +194,14 @@ Backend settings
The arguments passed to the compressor.
+ - ``compressor.filters.template.TemplateFilter``
+
+ A filter that renders the JavaScript code with Django templating system.
+
+ .. attribute:: COMPRESS_TEMPLATE_FILTER_CONTEXT
+
+ The context to render your JavaScript code with.
+
.. _rJSmin: http://opensource.perlig.de/rjsmin/
.. _`Google Closure compiler`: http://code.google.com/closure/compiler/
.. _`YUI compressor`: http://developer.yahoo.com/yui/compressor/

0 comments on commit 37a7d78

Please sign in to comment.