-
Notifications
You must be signed in to change notification settings - Fork 32
/
generate_emojis.dart
225 lines (204 loc) · 7.18 KB
/
generate_emojis.dart
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
import 'dart:convert';
import 'package:code_builder/code_builder.dart';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import 'package:string_normalizer/string_normalizer.dart';
import 'package:universal_io/io.dart';
/// Android 14 only supports Unicode 15.0, so we use that for now, even though newer versions are available:
/// https://developer.android.com/guide/topics/resources/internationalization#versioning-nougat
const unicodeVersion = '15_0';
void main() {
final formatter = DartFormatter(pageWidth: 120);
final emitter = DartEmitter(
orderDirectives: true,
useNullSafetySyntax: true,
);
final inputFile = File('../../external/emoji-metadata/emoji_${unicodeVersion}_ordering.json');
final input = json.decode(inputFile.readAsStringSync());
final library = Library((b) {
b.docs.add('// GENERATED CODE - DO NOT MODIFY BY HAND');
b.body.add(
Class(
(b) => b
..name = 'Emoji'
..docs.add('/// Holds the metadata of a Unicode emoji.')
..constructors.add(
Constructor(
(b) => b
..constant = true
..docs.add('/// Creates a new [Emoji].')
..optionalParameters.addAll([
Parameter(
(b) => b
..name = 'base'
..named = true
..toThis = true
..required = true,
),
Parameter(
(b) => b
..name = 'alternates'
..named = true
..toThis = true
..required = true,
),
Parameter(
(b) => b
..name = 'emoticons'
..named = true
..toThis = true
..required = true,
),
Parameter(
(b) => b
..name = 'shortcodes'
..named = true
..toThis = true
..required = true,
),
Parameter(
(b) => b
..name = 'animated'
..named = true
..toThis = true
..required = true,
),
]),
),
)
..fields.addAll([
Field(
(b) => b
..name = 'base'
..type = refer('String')
..modifier = FieldModifier.final$
..docs.add('/// The base emoji symbol.'),
),
Field(
(b) => b
..name = 'alternates'
..type = refer('List<String>')
..modifier = FieldModifier.final$
..docs.add('/// The associated alternate emoji symbols.'),
),
Field(
(b) => b
..name = 'emoticons'
..type = refer('List<String>')
..modifier = FieldModifier.final$
..docs.add('/// The associated emoticons.'),
),
Field(
(b) => b
..name = 'shortcodes'
..type = refer('List<String>')
..modifier = FieldModifier.final$
..docs.add('/// The associated short codes.'),
),
Field(
(b) => b
..name = 'animated'
..type = refer('bool')
..modifier = FieldModifier.final$
..docs.add('/// Whether the emoji can be animated.'),
),
]),
),
);
final names = <String, String>{};
const groupIds = <String, String>{
'Smileys and emotions': 'smileysAndEmotions',
'People': 'people',
'Animals and nature': 'animalsAndNature',
'Food and drink': 'foodAndDrink',
'Travel and places': 'travelAndPlaces',
'Activities and events': 'activitiesAndEvents',
'Objects': 'objects',
'Symbols': 'symbols',
'Flags': 'flags',
};
final emojiGroups = <String, List<String>>{};
for (final group in (input as List).map((t) => t as Map<String, dynamic>)) {
final groupName = group['group'] as String;
final groupId = groupIds[groupName]!;
for (final emoji in (group['emoji'] as List).map((t) => t as Map<String, dynamic>)) {
final base = String.fromCharCodes((emoji['base'] as List).cast<int>());
final alternates = (emoji['alternates'] as List)
.map((alternate) => String.fromCharCodes((alternate as List).cast<int>()))
.toList();
final emoticons = (emoji['emoticons'] as List).cast<String>();
final shortcodes = (emoji['shortcodes'] as List).cast<String>();
final animated = emoji['animated'] as bool;
var name = '';
for (final shortcode in shortcodes) {
name = shortcode.substring(1, shortcode.length - 1).toLowerCase();
name = name
.split('-')
.map(
(t) => switch (t) {
'1' => 'one',
'2' => 'two',
'3' => 'three',
'4' => 'four',
'5' => 'five',
'6' => 'six',
'7' => 'seven',
'8' => 'eight',
'9' => 'nine',
_ => t,
},
)
.mapIndexed((i, t) => i == 0 ? t : '${t.substring(0, 1).toUpperCase()}${t.substring(1)}')
.join();
name = StringNormalizer.normalize(name);
name = name.replaceAll(RegExp('[^a-z]', caseSensitive: false), '');
if (name.isNotEmpty) {
break;
}
}
if (name == 'new') {
name = r'$new';
}
emojiGroups[groupId] ??= [];
emojiGroups[groupId]!.add(name);
// Some emojis are in multiple groups
if (names.containsKey(name)) {
if (names[name] != base) {
throw Exception('Conflicting name $name for different emojis: $base ${names[name]}');
}
continue;
}
names[name] = base;
b.body.add(
Code('''
/// The $base emoji.
const $name = Emoji(
base: '$base',
alternates: [${alternates.isNotEmpty ? "'${alternates.join("', '")}'," : ''}],
emoticons: [${emoticons.isNotEmpty ? "'${emoticons.join("', '").replaceAll("'", r"\'").replaceAll(r'$', r'\$')}'," : ''}],
shortcodes: ['${shortcodes.join("', '")}',],
animated: $animated,
);
'''),
);
}
}
for (final groupId in emojiGroups.keys) {
final emojis = emojiGroups[groupId]!;
b.body.add(
Code('''
/// The "${groupIds.entries.firstWhere((e) => e.value == groupId).key}" emojis.
const ${groupId}Group = [${emojis.join(', ')},];
'''),
);
}
b.body.add(
Code('''
/// All emojis.
const all = [${emojiGroups.values.expand((t) => [...t]).join(', ')},];
'''),
);
});
final output = library.accept(emitter).toString();
File('lib/src/utils/emojis.dart').writeAsStringSync(formatter.format(output));
}