1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#!/usr/bin/env python3
#
# This file is part of m.css.
#
# Copyright © 2017, 2018, 2019, 2020, 2021, 2022, 2023
# Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import argparse
import re
import os
import sys
import_rx = re.compile("^@import url\\('(?P<file>[^']+)'\\);$")
opening_brace_rx = re.compile("^\\s*:root\s*{\\s*$")
closing_brace_rx = re.compile("^\\s*}\\s*$")
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>.*)$")
def postprocess(files, process_imports, out_file):
directory = os.path.dirname(files[0])
if not out_file:
basename, ext = os.path.splitext(files[0])
out_file = basename + ".compiled" + ext
variables = {}
imported_files = []
def parse(f):
nonlocal variables, imported_files
not_just_variable_declarations = False
in_variable_declarations = False
in_comment = False
for line in f:
# In comment and the comment is not ending yet, ignore
if in_comment:
if comment_end_rx.match(line):
in_comment = False
continue
# Import statement: add the file to additionally processed files
# unless it's disabled
match = import_rx.match(line)
if match:
if process_imports:
imported_files += [match.group('file')]
continue
# Opening brace of variable declaration block
match = opening_brace_rx.match(line)
if match:
in_variable_declarations = True
continue
# Variable declaration
match = variable_declaration_rx.match(line)
if match and in_variable_declarations:
# 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
if comment_rx.match(line):
continue
# Comment start line, ignore this and the next lines
if comment_start_rx.match(line):
in_comment = True
continue
# Closing brace of variable declaration block. If it was not just
# variable declarations, put the closing brace to the output as
# well.
match = closing_brace_rx.match(line)
if match and in_variable_declarations:
if not_just_variable_declarations: out.write("}\n")
in_variable_declarations = False
continue
# If inside variable declaration block, include also the opening
# 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('*/'):
out.write(line[:line.rindex('/*')].rstrip() + '\n')
else:
out.write(line)
with open(out_file, mode='w', encoding='utf8') as out:
# Put a helper comment and a license blob on top
out.write("""/* Generated using `./postprocess.py {}`. Do not edit. */
/*
This file is part of m.css.
Copyright © 2017, 2018, 2019, 2020, 2021, 2022, 2023
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
""".format(' '.join(sys.argv[1:])))
# Parse the top-level file
with open(files[0], encoding='utf8') as f: parse(f)
# Now open the imported files and parse them as well. Not doing any
# recursive parsing.
for i, file in enumerate(imported_files + files[1:]):
if i: out.write('\n')
with open(file, encoding='utf8') as f: parse(f)
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=r"""
Postprocessor for removing @import statements and variables from CSS files.
Combines all files into a new *.compiled.css file. The basename is taken
implicitly from the first argument. The -o option can override the output
filename.""")
parser.add_argument('files', nargs='+', help="input CSS file(s)")
parser.add_argument('--no-import', help="ignore @import statements", action='store_true')
parser.add_argument('-o', '--output', help="output file", default='')
args = parser.parse_args()
exit(postprocess(args.files, not args.no_import, args.output))
|