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])
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:
# 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
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('*/'):