chiark / gitweb /
css: allow variables to be defined in terms of other variables.
authorCris Luengo <cris.l.luengo@gmail.com>
Wed, 1 May 2019 22:16:11 +0000 (16:16 -0600)
committerVladimír Vondruš <mosra@centrum.cz>
Sun, 7 Jul 2019 17:39:16 +0000 (19:39 +0200)
css/postprocess.py

index a62e998be911a3ca0db8c94b7767caa3a63cfebf..491f67579eefc1d5ada243c4ce6c098f12c86c59 100755 (executable)
@@ -36,7 +36,7 @@ comment_rx = re.compile("^\\s*(/\\*.*\\*/)?\\s*$")
 comment_start_rx = re.compile("^\\s*(/\\*.*)\\s*$")
 comment_end_rx = re.compile("^\\s*(.*\\*/)\\s*$")
 variable_declaration_rx = re.compile("^\\s*(?P<key>--[a-z-]+)\\s*:\\s*(?P<value>[^;]+)\\s*;\\s*(/\\*.*\\*/)?\\s*$")
-variable_use_rx = re.compile("^(?P<before>.+)var\\((?P<key>--[a-z-]+)\\)(?P<after>.+)$")
+variable_use_rx = re.compile("^(?P<before>.*)var\\((?P<key>--[a-z-]+)\\)(?P<after>.*)$")
 
 def postprocess(files, process_imports, out_file):
     directory = os.path.dirname(files[0])
@@ -67,20 +67,6 @@ def postprocess(files, process_imports, out_file):
                     imported_files += [match.group('file')]
                 continue
 
-            # Variable use, replace with actual value
-            # TODO: more variables on the same line?
-            match = variable_use_rx.match(line)
-            if match and match.group('key') in variables:
-                out.write(match.group('before'))
-                out.write(variables[match.group('key')])
-                # Strip the trailing comment, if there, to save some bytes
-                if match.group('after').endswith('*/'):
-                    out.write(match.group('after')[:match.group('after').rindex('/*')].rstrip())
-                else:
-                    out.write(match.group('after'))
-                out.write("\n")
-                continue
-
             # Opening brace of variable declaration block
             match = opening_brace_rx.match(line)
             if match:
@@ -90,7 +76,17 @@ def postprocess(files, process_imports, out_file):
             # Variable declaration
             match = variable_declaration_rx.match(line)
             if match and in_variable_declarations:
-                variables[match.group('key')] = match.group('value')
+                # Define a variable to equal another
+                key = match.group('key')
+                value = match.group('value')
+                match = variable_use_rx.match(value)
+                if match and match.group('key') in variables:
+                    value = match.group('before') + variables[match.group('key')]
+                    if match.group('after').endswith('*/'):
+                        value += match.group('after')[:match.group('after').rindex('/*')].rstrip()
+                    else:
+                        value += match.group('after')
+                variables[key] = value
                 continue
 
             # Comment or empty line, ignore
@@ -112,11 +108,25 @@ def postprocess(files, process_imports, out_file):
                 continue
 
             # If inside variable declaration block, include also the opening
-            # brace and remeber to put the closing brace there as well
+            # brace and remember to put the closing brace there as well
             if in_variable_declarations:
                 out.write(":root {\n")
                 not_just_variable_declarations = True
 
+            # Variable use, replace with actual value
+            # TODO: more variables on the same line?
+            match = variable_use_rx.match(line)
+            if match and match.group('key') in variables:
+                out.write(match.group('before'))
+                out.write(variables[match.group('key')])
+                # Strip the trailing comment, if there, to save some bytes
+                if match.group('after').endswith('*/'):
+                    out.write(match.group('after')[:match.group('after').rindex('/*')].rstrip())
+                else:
+                    out.write(match.group('after'))
+                out.write("\n")
+                continue
+
             # Something else, copy verbatim to the output. Strip the trailing
             # comment, if there, to save some bytes.
             if line.rstrip().endswith('*/'):