-
Notifications
You must be signed in to change notification settings - Fork 73
/
css-sorter.rb
175 lines (139 loc) · 2.75 KB
/
css-sorter.rb
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
# Note that this script doesn't handle CSS comments very well!
# It can capture comments of the kind
# font-size: 100px; /* because it's gotta be BIG */
# but not
# /* Because it's gotta be BIG: */
# font-size: 100px
# because it has no concept of a multi-line "bundle".
RULE_ORDER = %w(
position
display
flex-direction
flex-wrap
flex-basis flex-grow flex-shrink
align-items
align-self
justify-content
gap
grid-template-columns
grid-column
columns
column-gap
column-fill
column-count
column-width
break-before
break-after
break-inside
page-break-after
top
right
bottom
left
touch-action
scroll-behavior
pointer-events
cursor
content
width min-width max-width
height min-height max-height
transform
overflow overflow-x overflow-y
margin margin-top margin-right margin-bottom margin-left
padding padding-top padding-right padding-bottom padding-left
-webkit-appearance
background
background-color background-image
background-size background-position
background-repeat
border border-top border-right border-bottom border-left
border-radius
border-top-right-radius border-bottom-right-radius
border-bottom-left-radius border-top-left-radius
outline
box-shadow
fill
stroke
stroke-width
opacity
list-style-type
animation
transition
mix-blend-mode
object-fit
font-family
font-size
font-style
font-weight
font-feature-settings
font-variant
font-variant-caps
hyphens
hyphenate-limit-last
letter-spacing
line-height
text-align
text-indent
text-rendering
text-transform
text-decoration
text-decoration-color
text-decoration-thickness
text-underline-offset
text-shadow
white-space
color
z-index
)
puts RULE_ORDER
$infile = File.open("source/css/web.css")
$outfile = File.open("sorted.css", "w")
$current_selector = ""
$current_rules = []
$missed_rules = {}
def sort_and_output_current_rules
$outfile.puts $current_selector
sortable_rules = []
$current_rules.each do |rule|
if rule.match(/^\s*\/\*/)
next
end
if rule.match(/^\s*--/)
$outfile.puts rule
next
end
if rule.length <= 1
next
end
rule_name = rule.split(":").first.strip
rule_order = RULE_ORDER.index(rule_name) || 10000
sortable_rules << {"text" => rule, "order" => rule_order}
end
sortable_rules = sortable_rules.sort_by do |rule|
rule["order"]
end
sortable_rules.each do |rule|
$outfile.puts rule["text"]
end
end
capturing_rules = false
$infile.each_line do |line|
if line.match(/\{$/)
unless line.match(/^\s*@/)
$current_selector = line
$current_rules = []
capturing_rules = true
next
end
end
if capturing_rules && line.match(/\}$/)
sort_and_output_current_rules
capturing_rules = false
end
if capturing_rules
$current_rules << line
else
$outfile.puts line
end
end
$outfile.close