@@ -19,6 +19,7 @@ import (
19
19
"sort"
20
20
"strconv"
21
21
"strings"
22
+ "unicode/utf8"
22
23
)
23
24
24
25
const (
@@ -39,6 +40,7 @@ type Emoji struct {
39
40
Description string `json:"description,omitempty"`
40
41
Aliases []string `json:"aliases"`
41
42
UnicodeVersion string `json:"unicode_version,omitempty"`
43
+ SkinTones bool `json:"skin_tones,omitempty"`
42
44
}
43
45
44
46
// Don't include some fields in JSON
@@ -47,6 +49,7 @@ func (e Emoji) MarshalJSON() ([]byte, error) {
47
49
x := emoji (e )
48
50
x .UnicodeVersion = ""
49
51
x .Description = ""
52
+ x .SkinTones = false
50
53
return json .Marshal (x )
51
54
}
52
55
@@ -75,6 +78,7 @@ var replacer = strings.NewReplacer(
75
78
", Description:" , ", " ,
76
79
", Aliases:" , ", " ,
77
80
", UnicodeVersion:" , ", " ,
81
+ ", SkinTones:" , ", " ,
78
82
)
79
83
80
84
var emojiRE = regexp .MustCompile (`\{Emoji:"([^"]*)"` )
@@ -102,18 +106,20 @@ func generate() ([]byte, error) {
102
106
return nil , err
103
107
}
104
108
105
- var re = regexp .MustCompile (`keycap|registered|copyright` )
106
- tmp := data [:0 ]
109
+ var skinTones = make (map [string ]string )
107
110
108
- // filter out emoji that require greater than max unicode version
111
+ skinTones ["\U0001f3fb " ] = "Light Skin Tone"
112
+ skinTones ["\U0001f3fc " ] = "Medium-Light Skin Tone"
113
+ skinTones ["\U0001f3fd " ] = "Medium Skin Tone"
114
+ skinTones ["\U0001f3fe " ] = "Medium-Dark Skin Tone"
115
+ skinTones ["\U0001f3ff " ] = "Dark Skin Tone"
116
+
117
+ var tmp Gemoji
118
+
119
+ //filter out emoji that require greater than max unicode version
109
120
for i := range data {
110
121
val , _ := strconv .ParseFloat (data [i ].UnicodeVersion , 64 )
111
122
if int (val ) <= maxUnicodeVersion {
112
- // remove these keycaps for now they really complicate matching since
113
- // they include normal letters in them
114
- if re .MatchString (data [i ].Description ) {
115
- continue
116
- }
117
123
tmp = append (tmp , data [i ])
118
124
}
119
125
}
@@ -123,7 +129,6 @@ func generate() ([]byte, error) {
123
129
return data [i ].Aliases [0 ] < data [j ].Aliases [0 ]
124
130
})
125
131
126
- aliasPairs := make ([]string , 0 )
127
132
aliasMap := make (map [string ]int , len (data ))
128
133
129
134
for i , e := range data {
@@ -135,7 +140,6 @@ func generate() ([]byte, error) {
135
140
continue
136
141
}
137
142
aliasMap [a ] = i
138
- aliasPairs = append (aliasPairs , ":" + a + ":" , e .Emoji )
139
143
}
140
144
}
141
145
@@ -149,6 +153,43 @@ func generate() ([]byte, error) {
149
153
data [i ].Aliases = append (data [i ].Aliases , "laugh" )
150
154
}
151
155
156
+ // write a JSON file to use with tribute (write before adding skin tones since we can't support them there yet)
157
+ file , _ := json .Marshal (data )
158
+ _ = ioutil .WriteFile ("assets/emoji.json" , file , 0644 )
159
+
160
+ // Add skin tones to emoji that support it
161
+ var (
162
+ s []string
163
+ newEmoji string
164
+ newDescription string
165
+ newData Emoji
166
+ )
167
+
168
+ for i := range data {
169
+ if data [i ].SkinTones {
170
+ for k , v := range skinTones {
171
+ s = strings .Split (data [i ].Emoji , "" )
172
+
173
+ if utf8 .RuneCountInString (data [i ].Emoji ) == 1 {
174
+ s = append (s , k )
175
+ } else {
176
+ // insert into slice after first element because all emoji that support skin tones
177
+ // have that modifer placed at this spot
178
+ s = append (s , "" )
179
+ copy (s [2 :], s [1 :])
180
+ s [1 ] = k
181
+ }
182
+
183
+ newEmoji = strings .Join (s , "" )
184
+ newDescription = data [i ].Description + ": " + v
185
+ newAlias := data [i ].Aliases [0 ] + "_" + strings .ReplaceAll (v , " " , "_" )
186
+
187
+ newData = Emoji {newEmoji , newDescription , []string {newAlias }, "12.0" , false }
188
+ data = append (data , newData )
189
+ }
190
+ }
191
+ }
192
+
152
193
// add header
153
194
str := replacer .Replace (fmt .Sprintf (hdr , gemojiURL , data ))
154
195
@@ -162,10 +203,6 @@ func generate() ([]byte, error) {
162
203
return "{" + strconv .QuoteToASCII (s )
163
204
})
164
205
165
- // write a JSON file to use with tribute
166
- file , _ := json .Marshal (data )
167
- _ = ioutil .WriteFile ("assets/emoji.json" , file , 0644 )
168
-
169
206
// format
170
207
return format .Source ([]byte (str ))
171
208
}
0 commit comments