Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created January 16, 2010 05:44
Show Gist options
  • Save tkaemming/278672 to your computer and use it in GitHub Desktop.
Save tkaemming/278672 to your computer and use it in GitHub Desktop.
import os
import tempfile
from compress.filter_base import FilterBase
class LessCSSFilter(FilterBase):
"""
A filter for django-compress that adds LessCSS support.
Based off of "compress.filters.csstidy.CSSTidyFilter".
Supported settings:
* LESSCSS_BINARY: The path to the LessCSS binary. Defaults to 'lessc'.
* LESSCSS_ARGUMENTS: Any additional arguments to pass to the LessCSS
command line. Defaults to None.
"""
def filter_css(self, css):
"""
Compresses a string of CSS files using the LessCSS preprocessor.
"""
from django.conf import settings
BINARY = getattr(settings, 'LESSCSS_BINARY', 'lessc')
ARGUMENTS = getattr(settings, 'LESSCSS_ARGUMENTS', None)
tmp_file = tempfile.NamedTemporaryFile(mode='w+b')
tmp_file.write(css)
tmp_file.flush()
output_file = tempfile.NamedTemporaryFile(mode='w+b')
command = '%s %s %s' % (BINARY, tmp_file.name, output_file.name)
if ARGUMENTS:
command = '%s %s' % (command, ARGUMENTS)
command_output = os.popen(command).read()
filtered_css = output_file.read()
output_file.close()
tmp_file.close()
if self.verbose:
print command_output
return filtered_css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment