Honza Pokorny avatar Honza Pokorny committed 0542b9a

Add support for Less CSS (http://lesscss.org).

Comments (0)

Files changed (2)

mediagenerator/filters/lesscss.py

+from hashlib import sha1
+from mediagenerator.generators.bundles.base import Filter
+from mediagenerator.utils import find_file
+from subprocess import Popen, PIPE
+import os
+
+class LessCSS(Filter):    
+    """
+    LessCSS filter
+    You need node.js and lessc on your path
+    """
+
+    takes_input = False
+
+    def __init__(self, **kwargs):
+        print 'less css'
+        self.config(kwargs, module=None)
+        super(LessCSS, self).__init__(**kwargs)
+        assert self.filetype == 'css', (
+            'LessCSS only supports compilation to css. '
+            'The parent filter expects "%s".' % self.filetype)
+        self._compiled = None
+        self._compiled_hash = None
+        self._mtime = None
+
+    @classmethod
+    def from_default(cls, name):
+        return {'module': name}
+
+    def get_output(self, variation):
+        self._regenerate(debug=False)
+        yield self._compiled
+
+    def get_dev_output(self, name, variation):
+        assert name == self.module
+        self._regenerate(debug=True)
+        return self._compiled
+
+    def get_dev_output_names(self, variation):
+        self._regenerate(debug=True)
+        yield self.module, self._compiled_hash
+
+    def _regenerate(self, debug=False):
+        path = find_file(self.module)
+        mtime = os.path.getmtime(path)
+        if mtime == self._mtime:
+            return
+
+        self._compiled = self._compile(path, debug=debug)
+        self._compiled_hash = sha1(self._compiled).hexdigest()
+        self._mtime = mtime
+
+    def _compile(self, path, debug=False):
+        try:
+            cmd = Popen(['lessc %s' % path], stdin=PIPE, stdout=PIPE, stderr=PIPE,
+                        shell=True, universal_newlines=True)
+            output, error = cmd.communicate(path)
+            assert cmd.wait() == 0, ('LessCSS command returned bad '
+                                     'result:\n%s' % error)
+            return output
+        except Exception, e:
+            raise ValueError("Failed to run LessCSS compiler for this "
+                "file. Please confirm that the \"lessc\" application is "
+                "on your path and that you can run it from your own command "
+                "line.\n"
+                "Error was: %s" % e)

mediagenerator/generators/bundles/settings.py

     'pyva': 'mediagenerator.filters.pyvascript_filter.PyvaScript',
     'sass': 'mediagenerator.filters.sass.Sass',
     'scss': 'mediagenerator.filters.sass.Sass',
+    'less': 'mediagenerator.filters.lesscss.LessCSS',
 })
 
 ROOT_MEDIA_FILTERS = getattr(settings, 'ROOT_MEDIA_FILTERS', {})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.