11from static_precompiler .exceptions import StaticCompilationError
22from static_precompiler .compilers .base import BaseCompiler
3- from static_precompiler .settings import LESS_EXECUTABLE
3+ from static_precompiler .settings import LESS_EXECUTABLE , LESS_GLOBAL_VARS
44from static_precompiler .utils import run_command , convert_urls
55import os
66import posixpath
77import re
8+ import hashlib
89
910
1011class LESS (BaseCompiler ):
@@ -16,6 +17,22 @@ class LESS(BaseCompiler):
1617
1718 IMPORT_RE = re .compile (r"@import\s+(.+?)\s*;" , re .DOTALL )
1819 IMPORT_ITEM_RE = re .compile (r"([\"'])(.+?)\1" )
20+ VARIABLE_RE = re .compile (r".*@\{(.+?)\}.*" )
21+
22+ def get_output_filename (self , source_filename ):
23+ """ Return the name of compiled file based on the name of source file.
24+
25+ :param source_filename: name of a source file
26+ :type source_filename: str
27+ :returns: str
28+
29+ """
30+ vars_hash = ""
31+ if len (LESS_GLOBAL_VARS ):
32+ vars_str = ";" .join ([k + "=" + v for (k , v ) in LESS_GLOBAL_VARS ])
33+ vars_hash = hashlib .md5 (vars_str ).hexdigest () + "."
34+
35+ return source_filename [:- len (self .input_extension )] + vars_hash + self .output_extension
1936
2037 def should_compile (self , source_path , from_management = False ):
2138 # Do not compile the files that start with "_" if run from management
@@ -27,8 +44,10 @@ def compile_file(self, source_path):
2744 full_source_path = self .get_full_source_path (source_path )
2845 args = [
2946 LESS_EXECUTABLE ,
30- full_source_path ,
3147 ]
48+ for var in LESS_GLOBAL_VARS :
49+ args .append ("--global-var=%s=%s" % var )
50+ args .append (full_source_path )
3251 # `cwd` is a directory containing `source_path`. Ex: source_path = '1/2/3', full_source_path = '/abc/1/2/3' -> cwd = '/abc'
3352 cwd = os .path .normpath (os .path .join (full_source_path , * ([".." ] * len (source_path .split ("/" )))))
3453 out , errors = run_command (args , None , cwd = cwd )
@@ -64,6 +83,10 @@ def find_imports(self, source):
6483 imports = set ()
6584 for import_string in self .IMPORT_RE .findall (source ):
6685 import_string = import_string .strip ()
86+ match = self .VARIABLE_RE .match (import_string )
87+ # TODO: variable in @import
88+ if match :
89+ continue
6790 if import_string .startswith ("(css)" ):
6891 continue
6992 if "url(" in import_string :
0 commit comments