This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
235 lines (186 loc) · 4.68 KB
/
match.go
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
package main
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
)
var classesComponents = map[string]string{
"b": "bottom",
"t": "top",
"c": "color",
"s": "size",
"r": "radius",
"m": "margin",
"p": "padding",
"w": "width",
"f": "flex",
"text": "font",
"a": "align",
"j": "justify",
"i": "items",
"bk": "background",
"d": "display",
"pos": "position",
"rel": "relative",
"abs": "absolute",
"full": "100%",
"ov": "overflow",
"sh": "shadow",
}
func parseError(s string) error {
return errors.New("could not map class:" + s)
}
func makeClass(prefix, class, attr string) (string, string, error) {
if attr == "" {
return "", "", parseError(class)
}
if !strings.Contains(attr, ":") {
return "", "", parseError(class)
}
var suffix string
if prefix != "" {
suffix = strings.Replace(prefix, ":", "", -1)
suffix = suffix + "\\:"
}
class = strings.Replace(class, ".", "\\.", -1)
class = strings.Replace(class, "~", "\\~", -1)
class = strings.Replace(class, "%", "\\%", -1)
if isMedia(suffix) {
return makeMedia(class, prefix, attr, suffix)
}
return "." + suffix + class + prefix + "{" + attr + ";}", "", nil
}
func match(str string) (string, string, error) {
var style string
prefixSplit := strings.Split(str, ":")
var prefix = ""
if len(prefixSplit) > 1 {
prefix = ":" + prefixSplit[0]
}
// states
str = strings.Replace(str, "hover:", "", -1)
str = strings.Replace(str, "focus:", "", -1)
str = strings.Replace(str, "disabled:", "", -1)
str = strings.Replace(str, "sm:", "", -1)
str = strings.Replace(str, "md:", "", -1)
str = strings.Replace(str, "lg:", "", -1)
str = strings.Replace(str, "xl:", "", -1)
str = strings.Replace(str, "2xl:", "", -1)
str = preReplace(str)
// split class into tokens
splitClass := strings.Split(str, "-")
var sep string
var previousToken string
for i, token := range splitClass {
newtoken, ok := classesComponents[token]
if ok {
token = newtoken
}
if len(splitClass) > 1 && i == 0 {
// special case for shadow
// e.g.: sh-sm
shadow, ok := makeShadow(token, splitClass[i+1])
if ok {
return makeClass(prefix, str, shadow)
}
}
if len(splitClass)-1 > i && i > 0 {
// catches use of x, y, b, t
// e.g.: p-t-2 or p-x-2
newtoken, valid := makeXYTB(token, splitClass[i+1], previousToken)
if valid {
return makeClass(prefix, str, newtoken)
}
}
// last should always be the attribute value
if !isColor(token) && len(splitClass) > 1 && i == len(splitClass)-1 {
// numbers are treated as rem
// except 50 and 100 as %
// and shrink and grow as int, e.g.: flex-grow:1
if isNumber(token) {
token = convertNumber(token, splitClass[i-1])
}
style += ":" + token
return makeClass(prefix, str, style)
}
previousToken = token
if sep != "-" && i > 0 {
sep = "-"
}
// special handling of colors
color, err := makeColor(splitClass[i:])
// it's a color
if err == nil {
style += color
return makeClass(prefix, str, style)
}
// it's a "simple color"
// e.g.: bk-white
if errors.Is(err, errSimpleColor) {
// text-white converts to font-white
// we want color: white
if token == "font" {
style += color
return makeClass(prefix, str, style)
}
// results in e.g.: background-color: white;
style += token + "-" + color
return makeClass(prefix, str, style)
}
// it's not a color
if errors.Is(err, errNotColor) {
style += sep + token
continue
}
if err != nil {
return "", "", err
}
return makeClass(prefix, str, style)
}
return makeClass(prefix, str, style)
}
func isNumber(s string) bool {
if strings.Contains(s, "~") {
return true
}
_, err := strconv.Atoi(s)
return err == nil
}
func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}
func convertNumber(s, previous string) string {
isStrict := strings.Contains(s, "~")
if previous == "grow" || previous == "shrink" {
return s
}
if !isStrict && (s == "50" || s == "100") {
return s + "%"
}
s = strings.Replace(s, "~", "", -1)
num, _ := strconv.ParseFloat(s, 64)
// we divide by 4
// if it's not a strict number
if !isStrict {
num = roundFloat(num/4, 2)
}
return fmt.Sprintf("%vrem", num)
}
func makeXYTB(token, next, previous string) (string, bool) {
if token == "x" {
return previous + "-left:" + next + ";" + previous + "-right:" + next, true
}
if token == "y" {
return previous + "-top:" + next + ";" + previous + "-bottom:" + next, true
}
if token == "t" {
return previous + "-top:" + next + ";", true
}
if token == "b" {
return previous + "-bottom:" + next + ";", true
}
return "", false
}