From: Cris Luengo Date: Wed, 1 May 2019 22:16:11 +0000 (-0600) Subject: css: allow variables to be defined in terms of other variables. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=4f5b7982d30645e61fba82219306d7895108e2d2;p=blog.git css: allow variables to be defined in terms of other variables. --- diff --git a/css/postprocess.py b/css/postprocess.py index a62e998b..491f6757 100755 --- a/css/postprocess.py +++ b/css/postprocess.py @@ -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--[a-z-]+)\\s*:\\s*(?P[^;]+)\\s*;\\s*(/\\*.*\\*/)?\\s*$") -variable_use_rx = re.compile("^(?P.+)var\\((?P--[a-z-]+)\\)(?P.+)$") +variable_use_rx = re.compile("^(?P.*)var\\((?P--[a-z-]+)\\)(?P.*)$") 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('*/'):