Skip to content

Commit 945735d

Browse files
committed
global-var support for less
1 parent 1791dc6 commit 945735d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

static_precompiler/compilers/less.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from static_precompiler.exceptions import StaticCompilationError
22
from 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
44
from static_precompiler.utils import run_command, convert_urls
55
import os
66
import posixpath
77
import re
8+
import hashlib
89

910

1011
class 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:

static_precompiler/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
SCSS_USE_COMPASS = getattr(settings, "SCSS_USE_COMPASS", False)
4747
LESS_EXECUTABLE = getattr(settings, "LESS_EXECUTABLE", "lessc")
4848

49+
LESS_GLOBAL_VARS = getattr(settings, "LESS_GLOBAL_VARS", [])
50+
4951
PREPEND_STATIC_URL = getattr(settings, 'STATIC_PRECOMPILER_PREPEND_STATIC_URL', False)
5052

5153
DISABLE_AUTO_COMPILE = getattr(settings, 'STATIC_PRECOMPILER_DISABLE_AUTO_COMPILE', False)

0 commit comments

Comments
 (0)