Created
September 8, 2011 11:05
-
-
Save dchaplinsky/1203153 to your computer and use it in GitHub Desktop.
Update to webassets SassFilter to make it also work as output filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, subprocess | |
| from webassets.filter import Filter | |
| class SassFilter(Filter): | |
| """Converts `Sass <http://sass-lang.com/>`_ markup to real CSS. | |
| """ | |
| name = 'sass' | |
| def __init__(self, scss=False, debug_info=None, as_output=False, includes_dir=None): | |
| super(SassFilter, self).__init__() | |
| self.use_scss = scss | |
| self.debug_info = debug_info | |
| self.as_output = as_output | |
| self.includes_dir = includes_dir | |
| def setup(self): | |
| self.binary = self.get_config('SASS_BIN', what='sass binary', | |
| require=False) | |
| if self.debug_info is None: | |
| self.debug_info = self.get_config('SASS_DEBUG_INFO', require=False) | |
| def _apply_sass(self, _in, out, includes_path): | |
| if includes_path: | |
| old_dir = os.getcwd() | |
| os.chdir(includes_path) | |
| try: | |
| args = [self.binary or 'sass', '--stdin', '--style', 'expanded', | |
| '--no-cache', '--line-comments'] | |
| if not self.debug_info is False: | |
| args.append('--debug-info') | |
| if self.use_scss: | |
| args.append('--scss') | |
| proc = subprocess.Popen(args, | |
| stdin=subprocess.PIPE, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| # shell: necessary on windows to execute | |
| # ruby files, but doesn't work on linux. | |
| shell=(os.name == 'nt')) | |
| stdout, stderr = proc.communicate(_in.read()) | |
| if proc.returncode != 0: | |
| raise Exception(('sass: subprocess had error: stderr=%s, '+ | |
| 'stdout=%s, returncode=%s') % ( | |
| stderr, stdout, proc.returncode)) | |
| elif stderr: | |
| print "sass filter has warnings:", stderr | |
| out.write(stdout) | |
| finally: | |
| if includes_path: | |
| os.chdir(old_dir) | |
| def input(self, _in, out, source_path, output_path): | |
| if self.as_output: | |
| out.write(_in.read()) | |
| else: | |
| self._apply_sass(_in, out, self.includes_dir or os.path.dirname(source_path)) | |
| def output(self, _in, out, **kwargs): | |
| if not self.as_output: | |
| out.write(_in.read()) | |
| else: | |
| self._apply_sass(_in, out, self.includes_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment