-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss-to-json.awk
36 lines (32 loc) · 872 Bytes
/
css-to-json.awk
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
BEGIN { print "{"; firstClass=1 }
END { print "}" }
# Skip nested rules for now.
/@media|@keyframes/ { nested=1 }
nested && /^}/ { nested=0; next }
nested { next }
# Class name
/{/ {
# Remove leading `.` from class name
sub(/^\./, "")
# Remove `\` in .inset-0\.5 etc
sub(/\\/, "")
# Remove any selectors and `{`
# E.g. "p-0 {" -> "p-0"
# E.g. "-space-x-44 > :not([hidden]) ~ :not([hidden]) {" -> "-space-x-44"
sub(/ .*/, "")
print (firstClass ? "" : ",") "\"" $0 "\": ["
firstClass=0
firstProperty=1
}
/}/ {
sub(/}/, "]")
print
}
# CSS property
/^ / {
sub(/^ /, "")
# Escape `"` e.g. in font names
gsub(/"/, "\\\"")
print (firstProperty ? "" : ",") "\"" $0 "\""
firstProperty=0
}