Commits

Jan-Jaap Driessen committed f897b11 Merge

Merge with LessCSS changes.

  • Participants
  • Parent commits 7fd5b62, 3499c24
  • Branches compilers-n-compressors

Comments (0)

Files changed (4)

   and the name of the compiler.In non-development mode, the filesystem is
   used to find a compiled version of a resource. 
 
-- Implemented a CoffeeScript and SCSS compiler. The optional dependencies
+- Implemented a CoffeeScript, LessCSS and SCSS compiler. The optional dependencies
   can be installed using the 'fanstatic[compilers]' requirement.
   Base functionality of these compilers is shared in a baseclass. 
   Compilers do not know about Resources or Library, only about filenames.

File fanstatic/compiler.py

             f.write(p.stdout.read())
         finally:
             f.close()
+
+
+class LessCSS(Compiler):
+    source_extension = 'less'
+    target_extension = 'css'
+
+    @staticmethod
+    def available():
+        try:
+            check = subprocess.check_call('which lessc', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        except subprocess.CalledProcessError:
+            return False
+        if check == 0:
+            return True
+        return False
+
+    def __call__(self, path):
+        cmd = 'lessc %s' % path
+        
+        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        p.wait()
+        if p.returncode != 0:
+            raise CompilerException(p.stderr.read())
+
+        f = open(self.target_path(path), 'wb')
+        try:
+            f.write(p.stdout.read())
+        finally:
+            f.close()
+

File fanstatic/test_compile.py

 from fanstatic import Library, Resource, NeededResources, LibraryRegistry, Publisher
 from fanstatic.core import CompiledNotFoundError, TooManyCompiledFoundError
 from fanstatic.core import UnknownCompilerExtensionError
-from fanstatic.compiler import Compiler, CoffeeScript, SCSS
+from fanstatic.compiler import Compiler, CoffeeScript, SCSS, LessCSS
 from fanstatic.compiler import CompilerException
 import fanstatic.registry
 
 a = 'Ṱ';
 '''
 
+@pytest.mark.skipif('not LessCSS.available()')
+def test_lesscsscompiler(tmpdir):
+    style = tmpdir.join('style.less')
+    # from http://lesscss.org :
+    style.write('''\
+@color: #4D926F;
+
+#header {
+  color: @color;
+}
+h2 {
+  color: @color;
+}''')
+
+    compiler = LessCSS()
+    compiler(str(style))
+    assert open(compiler.target_path(str(style))).read() == '''\
+#header {
+  color: #4d926f;
+}
+h2 {
+  color: #4d926f;
+}
+'''
+    with_syntax_error = tmpdir.join('error.less')
+    with_syntax_error.write('.frop {')
+    # If the input is invalid, should raise a compiler error.
+    with pytest.raises(CompilerException):
+        compiler(str(with_syntax_error))
+
 
 @pytest.mark.skipif('not SCSS.available()')
 def test_scsscompiler(tmpdir):
             ],
         'fanstatic.compilers': [
             'scss = fanstatic.compiler:SCSS',
+            'lesscss = fanstatic.compiler:LessCSS',
             'coffeescript = fanstatic.compiler:CoffeeScript',
             ],
     })