This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
css_explore.py
383 lines (305 loc) · 10.6 KB
/
css_explore.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from __future__ import annotations
import argparse
import json
import os.path
import re
import shlex
import subprocess
import sys
from typing import Any
from typing import NamedTuple
from typing import Sequence
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Protocol
else:
Protocol = object
NENV_PATH = '.nenv-css-explore'
ACTIVATE = shlex.quote(f'{NENV_PATH}/bin/activate')
CSS_PROG = '''\
const css = require('css');
const fs = require('fs');
const src = fs.readFileSync('/dev/stdin').toString('UTF-8');
console.log(JSON.stringify(css.parse(src, {silent: false})));
'''
class CalledProcessError(ValueError):
def __init__(self, returncode: int, out: str, err: str) -> None:
super().__init__(
f'Unexpected returncode ({returncode})\n'
f'stdout:\n{out}\n'
f'stderr:\n{err}\n',
)
def _check_keys(d: dict[str, Any], keys: tuple[str, ...]) -> None:
# All things have these keys
keys = keys + ('position', 'type')
assert set(d) <= set(keys), (set(d), set(keys))
def indent(text: str) -> str:
lines = text.splitlines()
return '\n'.join(' ' + line for line in lines) + '\n'
NUM = r'(\d*(?:\.\d*)?)'
HEXDIGIT = '[0-9a-fA-F]'
COLORS_TO_SHORT_COLORS = (
('black', '#000'),
('white', '#fff'),
)
COLOR_TO_SHORT_RE_PATTERN = r'\b{}\b'
COLOR_RE = re.compile(fr'#({HEXDIGIT})\1({HEXDIGIT})\2({HEXDIGIT})\3')
COLOR_RE_SUB = r'#\1\2\3'
COMMA_RE = re.compile(r'(,\s*)')
COMMA_RE_SUB = ', '
FLOAT_RE = re.compile(r'(?<!\d)(\.\d+)')
FLOAT_RE_SUB = r'0\1'
POINT_ZERO_RE = re.compile(r'(\d)\.0+px')
POINT_ZERO_SUB = r'\1px'
QUOTE_RE = re.compile(r'"([^\'"]*)"')
QUOTE_RE_SUB = r"'\1'"
RELATION_RE = re.compile(r'\s*([+>])\s*')
RELATION_RE_SUB = r' \1 '
RGBA_RE = re.compile(fr'rgba\({NUM},\s*{NUM},\s*{NUM},\s*{NUM}\)')
RGBA_RE_SUB = r'rgba(\1, \2, \3, \4)'
SLASH_RE = re.compile(r'\s*/\s*')
SLASH_RE_SUB = ' / '
SPACES_RE = re.compile('[ ]+')
SPACES_RE_SUB = ' '
UNICODE_ESC_RE = re.compile(r'\\[A-Fa-f0-9]{4}\s*')
def norm_unicode_escapes(value: str) -> str:
matches = UNICODE_ESC_RE.findall(value)
for match in matches:
value = value.replace(match, chr(int(match[1:].rstrip(), 16)))
return value
class Settings(NamedTuple):
ignore_charset: bool = False
ignore_comments: bool = False
ignore_empty_rules: bool = False
class CSSNode(Protocol):
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> CSSNode: ...
def to_text(self, settings: Settings) -> str: ...
class Property(NamedTuple):
name: str
value: str
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Property:
assert dct['type'] == 'declaration', dct['type']
_check_keys(dct, ('property', 'value'))
value = dct['value']
value = COLOR_RE.sub(COLOR_RE_SUB, value)
value = COMMA_RE.sub(COMMA_RE_SUB, value)
value = FLOAT_RE.sub(FLOAT_RE_SUB, value)
value = POINT_ZERO_RE.sub(POINT_ZERO_SUB, value)
value = QUOTE_RE.sub(QUOTE_RE_SUB, value)
value = RGBA_RE.sub(RGBA_RE_SUB, value)
for color, replace in COLORS_TO_SHORT_COLORS:
value = re.sub(
COLOR_TO_SHORT_RE_PATTERN.format(color),
replace,
value,
)
# Only normalize slashes in font declarations for shorthand
if dct['property'] == 'font':
value = SLASH_RE.sub(SLASH_RE_SUB, value)
value = SPACES_RE.sub(SPACES_RE_SUB, value)
value = norm_unicode_escapes(value)
return cls(dct['property'], value)
def to_text(self, settings: Settings) -> str:
return f' {self.name}: {self.value};\n'
class Charset(NamedTuple):
charset: str
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Charset:
_check_keys(dct, ('charset',))
return cls(dct['charset'])
def to_text(self, settings: Settings) -> str:
if settings.ignore_charset:
return ''
else:
return f'@charset {self.charset};\n'
class Comment(NamedTuple):
comment: str
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Comment:
_check_keys(dct, ('comment',))
return cls(dct['comment'])
def to_text(self, settings: Settings) -> str:
if settings.ignore_comments:
return ''
else:
return f'/*{self.comment}*/\n'
class Document(NamedTuple):
vendor: str
name: str
rules: tuple[CSSNode, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Document:
_check_keys(dct, ('vendor', 'document', 'rules'))
rules = tuple(generic_to_node(node_dict) for node_dict in dct['rules'])
return cls(dct.get('vendor', ''), dct['document'], rules)
def to_text(self, settings: Settings) -> str:
return '@{}document {} {{\n{}}}\n'.format(
self.vendor,
self.name,
indent(''.join(rule.to_text(settings) for rule in self.rules)),
)
class Import(NamedTuple):
value: str
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Import:
_check_keys(dct, ('import',))
return cls(dct['import'])
def to_text(self, settings: Settings) -> str:
return f'@import {self.value};\n'
class KeyFrame(NamedTuple):
values: str
properties: tuple[Property, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> KeyFrame:
_check_keys(dct, ('declarations', 'values'))
properties = tuple(
Property.from_dict(property_dict)
for property_dict in dct['declarations']
)
return cls(', '.join(dct['values']), properties)
def to_text(self, settings: Settings) -> str:
return '{} {{\n{}}}\n'.format(
self.values,
''.join(prop.to_text(settings) for prop in self.properties),
)
class KeyFrames(NamedTuple):
vendor: str
name: str
keyframes: tuple[KeyFrame, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> KeyFrames:
_check_keys(dct, ('vendor', 'name', 'keyframes'))
keyframes = tuple(
KeyFrame.from_dict(keyframe_dict)
for keyframe_dict in dct['keyframes']
)
return cls(dct.get('vendor', ''), dct['name'], keyframes)
def to_text(self, settings: Settings) -> str:
return '@{}keyframes {} {{\n{}}}\n'.format(
self.vendor,
self.name,
indent(
''.join(
keyframe.to_text(settings) for keyframe in self.keyframes
),
),
)
class MediaQuery(NamedTuple):
media: str
rules: tuple[CSSNode, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> MediaQuery:
_check_keys(dct, ('media', 'rules'))
media = dct['media']
media = COMMA_RE.sub(COMMA_RE_SUB, media)
rules = tuple(generic_to_node(node_dict) for node_dict in dct['rules'])
return cls(media, rules)
def to_text(self, settings: Settings) -> str:
return '@media {} {{\n{}}}\n'.format(
self.media,
indent(''.join(rule.to_text(settings) for rule in self.rules)),
)
class Rule(NamedTuple):
selectors: str
properties: tuple[Property, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Rule:
_check_keys(dct, ('selectors', 'declarations'))
selectors = [
RELATION_RE.sub(RELATION_RE_SUB, selector)
for selector in dct['selectors']
]
properties = tuple(
Property.from_dict(property_dict)
for property_dict in dct['declarations']
)
return cls(', '.join(sorted(selectors)), properties)
def to_text(self, settings: Settings) -> str:
if settings.ignore_empty_rules and not self.properties:
return ''
return '{} {{\n{}}}\n'.format(
self.selectors,
''.join(prop.to_text(settings) for prop in self.properties),
)
class Supports(NamedTuple):
supports: str
rules: tuple[CSSNode, ...]
@classmethod
def from_dict(cls, dct: dict[str, Any]) -> Supports:
_check_keys(dct, ('supports', 'rules'))
rules = tuple(generic_to_node(node_dict) for node_dict in dct['rules'])
return cls(dct['supports'], rules)
def to_text(self, settings: Settings) -> str:
return '@supports {} {{\n{}}}\n'.format(
self.supports,
indent(''.join(rule.to_text(settings) for rule in self.rules)),
)
def require_nodeenv() -> None:
# Make it in the current directory, whatevs.
if os.path.exists(os.path.join(NENV_PATH, 'installed')):
return
subprocess.check_call(
(sys.executable, '-m', 'nodeenv', NENV_PATH, '--prebuilt'),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.check_call(
('bash', '-c', f'. {ACTIVATE} && npm install -g css'),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# Atomically indicate we've installed
open(f'{NENV_PATH}/installed', 'w').close()
TO_NODE_TYPES: dict[str, type[CSSNode]] = {
'charset': Charset,
'comment': Comment,
'document': Document,
'import': Import,
'keyframes': KeyFrames,
'media': MediaQuery,
'rule': Rule,
'supports': Supports,
}
def generic_to_node(node_dict: dict[str, Any]) -> CSSNode:
return TO_NODE_TYPES[node_dict['type']].from_dict(node_dict)
def format_css(
contents: str,
*,
ignore_charset: bool = False,
ignore_comments: bool = False,
ignore_empty_rules: bool = False,
) -> str:
require_nodeenv()
proc = subprocess.Popen(
('sh', '-c', f'. {ACTIVATE} && node -e {shlex.quote(CSS_PROG)}'),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='UTF-8',
)
out, err = proc.communicate(contents)
if proc.returncode:
raise CalledProcessError(proc.returncode, out, err)
sheet = json.loads(out)['stylesheet']
rules = tuple(generic_to_node(rule_dict) for rule_dict in sheet['rules'])
return ''.join(
rule.to_text(
Settings(
ignore_charset=ignore_charset,
ignore_comments=ignore_comments,
ignore_empty_rules=ignore_empty_rules,
),
)
for rule in rules
)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args(argv)
contents = open(args.filename).read()
print(format_css(contents).rstrip())
return 0
if __name__ == '__main__':
raise SystemExit(main())