forked from Julien-Marcou/Unicode-Emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-unicode-emoji.cjs
More file actions
404 lines (371 loc) Β· 13.7 KB
/
generate-unicode-emoji.cjs
File metadata and controls
404 lines (371 loc) Β· 13.7 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
const fs = require('fs');
const https = require('https');
const readline = require('readline');
const htmlparser2 = require('htmlparser2');
/* ------------- PARAMETERS ------------- */
const unicodeEmojiVersion = '13.1';
const unicodeCldrVersion = '38-1';
const unicodeCldrLocale = 'en';
/* ------------- EMOJIS CONSOLIDATION ------------- */
const categoriesMapping = {
'smileys-emotion': {
default: 'face-emotion'
},
'people-body': {
default: 'person-people',
'body-parts': 'face-emotion',
'hand': 'face-emotion',
},
'animals-nature': {
default: 'animals-nature'
},
'food-drink': {
default: 'food-drink',
},
'travel-places': {
default: 'travel-places',
'sky-weather': 'animals-nature',
'time': 'objects',
},
'activities': {
default: 'activities-events',
},
'objects': {
default: 'objects',
'music': 'activities-events',
},
'symbols': {
default: 'symbols',
},
'flags': {
default: 'flags',
},
};
const overrideCategoriesForEmojis = {
'activities-events': ['π₯', 'ποΈ', 'π½οΈ', 'π¬', 'πΊ', 'π·', 'πΈ', 'πΉ', 'πΌ', 'π¨', 'πͺ', 'βοΈ', 'βοΈ', 'π οΈ', 'π‘οΈ', 'βοΈ', 'π«', 'πͺ', 'πΉ', 'π‘οΈ', 'πͺ', 'π§', 'πͺ'],
'animals-nature': ['π¦', 'π¦', 'π¦', 'π¦', 'π¦ͺ'],
'objects': ['π§Ώ', 'π', 'βοΈ', 'π£'],
'symbols': ['β οΈ', 'β₯οΈ', 'β¦οΈ', 'β£οΈ', 'π³οΈ', 'π¬', 'ποΈβπ¨οΈ', 'π¨οΈ', 'π―οΈ', 'π', 'π', 'π§', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π ', 'π', 'π‘', 'π', 'π’', 'π', 'π£', 'π', 'π€', 'π', 'π₯', 'π', 'π¦', 'π', 'π', 'π', 'π', 'π', 'πΉ'],
};
const missingEmojis = [
'# group: People & Body',
'# subgroup: hands',
'1F91D 1F3FB ; fully-qualified # π€π» E3.0 handshake: light skin tone',
'1F91D 1F3FC ; fully-qualified # π€πΌ E3.0 handshake: medium-light skin tone',
'1F91D 1F3FD ; fully-qualified # π€π½ E3.0 handshake: medium skin tone',
'1F91D 1F3FE ; fully-qualified # π€πΎ E3.0 handshake: medium-dark skin tone',
'1F91D 1F3FF ; fully-qualified # π€πΏ E3.0 handshake: dark skin tone',
];
const missingAnnotations = [
'<annotation cp="π€π»">agreement | hand | handshake | light skin tone | meeting | shake</annotation>',
'<annotation cp="π€π»" type="tts">handshake: light skin tone</annotation>',
'<annotation cp="π€πΌ">agreement | hand | handshake | medium-light skin tone | meeting | shake</annotation>',
'<annotation cp="π€πΌ" type="tts">handshake: medium-light skin tone</annotation>',
'<annotation cp="π€π½">agreement | hand | handshake | medium skin tone | meeting | shake</annotation>',
'<annotation cp="π€π½" type="tts">handshake: medium skin tone</annotation>',
'<annotation cp="π€πΎ">agreement | hand | handshake | medium-dark skin | meeting | shake</annotation>',
'<annotation cp="π€πΎ" type="tts">handshake: medium-dark skin tone</annotation>',
'<annotation cp="π€πΏ">agreement | dark skin tone | hand | handshake | meeting | shake</annotation>',
'<annotation cp="π€πΏ" type="tts">handshake: dark skin tone</annotation>',
];
/* ------------- FILES CONFIG ------------- */
// File paths
const emojisInput = `https://unicode.org/Public/emoji/${unicodeEmojiVersion}/emoji-test.txt`;
const annotationsInput = `https://raw.githubusercontent.com/unicode-org/cldr/release-${unicodeCldrVersion}/common/annotations/${unicodeCldrLocale}.xml`;
const derivedAnnotationsInput = `https://raw.githubusercontent.com/unicode-org/cldr/release-${unicodeCldrVersion}/common/annotationsDerived/${unicodeCldrLocale}.xml`;
const csvOutput = `${__dirname}/unicode-emoji.csv`;
const jsonOutput = `${__dirname}/unicode-emoji.json`;
const jsOutput = `${__dirname}/unicode-emoji.js`;
// Config to parse emojis file
const commentPrefix = '#';
const groupPrefix = '# group:';
const subgroupPrefix = '# subgroup:';
const componentStatus = 'component';
const fullyQualifiedStatus = 'fully-qualified';
const groupsWithVariations = ['people-body'];
const codePointsSeparator= ' ';
const baseDescriptionSeparator = ':';
const qualificationCodePoint = 'FE0F';
// Config to parse annotations file
const annotationTag = 'annotation';
const codePointsAttribute = 'cp';
const typeAttribute = 'type';
const textToSpeechType = 'tts';
const keywordsSeparator = /[|,]/g;
/* ------------- PROCESS ------------- */
// Emojis process
let emojiGroup;
let emojiSubgroup;
let emojiCount = 0;
const emojis = new Map(); // Used to map keywords & text to speech for each emojis
const components = new Map(); // Used to map components for each emoji variations
const baseEmojis = new Map(); // Used to attach variations to base emojis
const results = { components: {}, emojis: [] };
// Annotations process
let isAnnotationTag = false;
let annotationAttributes = null;
let annotationText = null;
let annotationCount = 0;
const annotationsParser = new htmlparser2.Parser({
onopentag: (tag, attributes) => {
isAnnotationTag = tag === annotationTag;
if (isAnnotationTag) {
annotationAttributes = attributes;
annotationText = '';
}
},
ontext: (text) => {
if (isAnnotationTag) {
annotationText += text;
}
},
onclosetag: (tag) => {
if (tag === annotationTag) {
processAnnotationTag(annotationAttributes, annotationText);
isAnnotationTag = false;
annotationAttributes = null;
annotationText = null;
}
},
onend: () => {
isAnnotationTag = false;
annotationAttributes = null;
annotationText = null;
},
}, { decodeEntities: true });
function processEmojiLine(line) {
// Retrieve group/subgroup line
if (line.charAt(0) === commentPrefix) {
if (line.startsWith(groupPrefix)) {
emojiGroup = line
.substr(groupPrefix.length)
.trim()
.replace(/[^A-Za-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.toLowerCase();
}
else if (line.startsWith(subgroupPrefix)) {
emojiSubgroup = line
.substr(subgroupPrefix.length)
.trim()
.replace(/[^A-Za-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.toLowerCase();
}
}
// Retrieve emoji line
else {
const emojiMatch = line.match(/^(.+\b)\s+;\s+([\S]+)\s+#\s([\S]+)\s+E(\S+)\s+(.+)$/);
if (!emojiMatch) {
return;
}
const [_, codePoints, status, renderedEmoji, version, description] = emojiMatch;
if (![componentStatus, fullyQualifiedStatus].includes(status)) {
return;
}
const emoji = {
emoji: renderedEmoji,
description: null,
version: version,
keywords: null,
};
emojis.set(renderedEmoji, emoji);
emojiCount++;
process.stdout.clearLine(-1);
process.stdout.cursorTo(0);
process.stdout.write(`Retrieved ${emojiCount} emojis`);
// Component emoji
if (status === componentStatus) {
emoji.codePoint = codePoints;
if (!(emojiSubgroup in results.components)) {
results.components[emojiSubgroup] = [];
}
results.components[emojiSubgroup].push(emoji);
components.set(codePoints, emoji);
}
// Fully-qualified emoji
else {
emoji.codePoints = codePoints.split(codePointsSeparator);
// Make the unqualified version of the emoji point to the fully-qualified emoji,
// so annotations are later attached to the correct emoji
const unqualifiedRenderedEmoji = String.fromCodePoint(
...emoji.codePoints
.filter(codePoint => codePoint !== qualificationCodePoint)
.map(codePoint => parseInt(codePoint, 16))
);
if (unqualifiedRenderedEmoji !== renderedEmoji) {
emojis.set(unqualifiedRenderedEmoji, emoji);
}
// Map the group/subgroup to a more meaningful category
let category = categoriesMapping[emojiGroup].default;
for (const keyword in categoriesMapping[emojiGroup]) {
if (keyword !== 'default' && emojiSubgroup.includes(keyword)) {
category = categoriesMapping[emojiGroup][keyword];
break;
}
}
for (const overrideCategory in overrideCategoriesForEmojis) {
if (overrideCategoriesForEmojis[overrideCategory].includes(renderedEmoji)) {
category = overrideCategory;
}
}
// Variation of an emoji
const baseDescription = description.split(baseDescriptionSeparator)[0];
if (groupsWithVariations.includes(emojiGroup) && baseEmojis.has(baseDescription)) {
const baseEmoji = baseEmojis.get(baseDescription);
if (!baseEmoji.variations) {
baseEmoji.variations = [];
}
baseEmoji.variations.push(emoji);
}
// Base emoji
else {
emoji.category = category;
emoji.group = emojiGroup;
emoji.subgroup = emojiSubgroup;
results.emojis.push(emoji);
baseEmojis.set(description, emoji);
}
}
}
}
function addComponentsToEmojiVariations() {
for (const baseEmoji of baseEmojis.values()) {
if (baseEmoji.variations) {
for (const emojiVariation of baseEmoji.variations) {
for (const codePoint of emojiVariation.codePoints) {
if (components.has(codePoint)) {
if (!emojiVariation.components) {
emojiVariation.components = [];
}
const component = components.get(codePoint);
if (!emojiVariation.components.includes(component.emoji)) {
emojiVariation.components.push(component.emoji);
}
}
}
}
}
}
}
function processAnnotationTag(attributes, text) {
const renderedEmoji = attributes[codePointsAttribute];
if (!emojis.has(renderedEmoji)) {
return;
}
const emoji = emojis.get(renderedEmoji);
if (attributes[typeAttribute] === textToSpeechType) {
emoji.description = text.trim();
annotationCount++;
process.stdout.clearLine(-1);
process.stdout.cursorTo(0);
process.stdout.write(`Retrieved ${annotationCount} annotations`);
}
else {
emoji.keywords = text.split(keywordsSeparator).map((value) => value.trim());
}
}
function saveResults() {
// Save CSV
let csvResult = 'emoji,description,version,keywords,category,group,subgroup,parent,components\n'
for (const componentGroup in results.components) {
for (const component of results.components[componentGroup]) {
csvResult +=
`${component.emoji},` +
`"${component.description.replace(/"/g, '""')}",` +
`${component.version},` +
`${component.keywords.join('|')},` +
`components,` +
`component,`+
`${componentGroup},,\n`;
}
}
for (const baseEmoji of results.emojis) {
csvResult +=
`${baseEmoji.emoji},` +
`"${baseEmoji.description.replace(/"/g, '""')}",` +
`${baseEmoji.version},` +
`${baseEmoji.keywords.join('|')},` +
`${baseEmoji.category},` +
`${baseEmoji.group},` +
`${baseEmoji.subgroup},,\n`;
if (baseEmoji.variations) {
for (const emojiVariation of baseEmoji.variations) {
csvResult +=
`${emojiVariation.emoji},` +
`"${emojiVariation.description.replace(/"/g, '""')}",` +
`${emojiVariation.version},` +
`${emojiVariation.keywords.join('|')},` +
`${baseEmoji.category},` +
`${baseEmoji.group},` +
`${baseEmoji.subgroup},` +
`${baseEmoji.emoji},` +
`${emojiVariation.components ? emojiVariation.components.join('|') : ''}\n`;
}
}
}
fs.writeFileSync(csvOutput, csvResult);
// Save JSON
fs.writeFileSync(jsonOutput, JSON.stringify(results, null, 2));
// Save JS
for (const componentGroup in results.components) {
results.components[componentGroup].forEach((component) => {
delete component.codePoint;
delete component.keywords;
});
}
results.emojis.forEach((baseEmoji) => {
delete baseEmoji.codePoints;
if (baseEmoji.variations) {
baseEmoji.variations.forEach((emojiVariation) => {
delete emojiVariation.codePoints;
delete emojiVariation.components;
delete emojiVariation.keywords;
})
}
});
fs.writeFileSync(jsOutput, `export default ${JSON.stringify(results)};`);
}
// Retrieve emojis online
process.stdout.write(`Retrieving "Unicode Emoji, Version ${unicodeEmojiVersion}"\n`);
https.get(emojisInput, (emojisResponse) => {
emojisResponse.setEncoding('utf8');
readline.createInterface(emojisResponse).on('line', (line) => {
processEmojiLine(line);
}).on('close', () => {
// Add missing emojis
missingEmojis.forEach((line) => {
processEmojiLine(line);
});
process.stdout.write('\n');
// Data consolidation for emoji variations
addComponentsToEmojiVariations();
// Retrieve annotations online
process.stdout.write(`Retrieving "Common Local Data Repository, Version ${unicodeCldrVersion}" for local "${unicodeCldrLocale}"\n`);
https.get(annotationsInput, (annotationResponse) => {
annotationResponse.setEncoding('utf8');
annotationResponse.on('data', (data) => {
annotationsParser.write(data);
});
annotationResponse.on('end', () => {
// Retrieve derived annotations online
https.get(derivedAnnotationsInput, (derivedAnnotationResponse) => {
derivedAnnotationResponse.setEncoding('utf8');
derivedAnnotationResponse.on('data', (data) => {
annotationsParser.write(data);
});
derivedAnnotationResponse.on('end', () => {
// Add missing annotations
for (const missingAnnotation of missingAnnotations) {
annotationsParser.write(missingAnnotation);
}
annotationsParser.end();
process.stdout.write('\n');
// End of process
saveResults();
});
});
});
});
});
});