Source

tgext.scss / tgext / scss / middleware.py

Diff from to

tgext/scss/middleware.py

 from compiler import Scss
 from tg import config
 
+from logging import getLogger
+
+log = getLogger('tgext.scss')
+
+try:
+    import libsass
+    USE_LIBSASS = True
+except LookupError:
+    log.warning('libsass not found, falling back to plain Python SCSS compiler')
+    USE_LIBSASS = False
+
 class SCSSMiddleware(object):
     def __init__(self, app):
         self.app = app
         try:
             self.root_directory = os.path.normcase(os.path.abspath((config['paths']['static_files'])))
         except KeyError:
-            self.root_directory = os.path.normcase(os.path.abspath((config['pylons.paths']['static_files'])))            
+            self.root_directory = os.path.normcase(os.path.abspath((config['pylons.paths']['static_files'])))
+
+        include_paths = [os.path.join(self.root_directory, ip) for ip in config.get('tgext.scss.include_paths', [])]
+
+        if USE_LIBSASS:
+            INCLUDES_JOIN_CHAR = ':'
+        else:
+            INCLUDES_JOIN_CHAR = ','
+        self.include_paths = INCLUDES_JOIN_CHAR.join([self.root_directory] + include_paths)
 
     def convert(self, text):
-        p = Scss(scss_opts=dict(compress=True, root_path=self.root_directory))
-        return p.compile(text)
+        if USE_LIBSASS:
+            res = libsass.compile(text, self.include_paths)
+            if res[0]:
+               return res[1]
+            else:
+                raise RuntimeError(res[1])
+        else:
+            p = Scss(scss_opts=dict(compress=True, root_path=self.include_paths))
+            return p.compile(text)
 
     def get_scss_content(self, path):
         f = open(path)