-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
209 lines (170 loc) · 7.35 KB
/
Program.cs
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
namespace Generator
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
/// <summary>
/// Source URL of Unicode emoji text file.
/// </summary>
private const string SourceUrl = "https://unicode.org/Public/emoji/latest/emoji-test.txt";
/// <summary>
/// Maximum emoji version to include in output file (last version is probably not supported by browsers/fonts yet).
/// </summary>
private const float MaxVersion = 15.1F;
/// <summary>
/// Output .cs file name/location.
/// </summary>
private const string OutFile = "FullEmojiList.cs";
/// <summary>
/// Set to 'true' to generate long names like `SmileysAndEmotion_GrinningFace` (instead of `GrinningFace`).
/// </summary>
private const bool IncludeGroupInEmojiName = false;
public static async Task Main(string[] args)
{
using var outputFile = File.CreateText(OutFile);
var stat = await new Program().RunAsync(outputFile).ConfigureAwait(false);
Console.WriteLine();
Console.WriteLine("DONE");
Console.WriteLine($" Groups: {stat.groupCount}");
Console.WriteLine($" Emojis: {stat.emojiCount}");
}
private static string FormatName(string source)
{
var parts = source
.Replace(':', '_')
.Replace('-', ' ')
.Replace("&", "And")
.Replace("#", "NumberSign")
.Replace("*", "Asterisk")
.Replace(",", string.Empty)
.Replace("’", string.Empty)
.Split(new[] { ' ', '-', ',', '’', '!', '“', '”', '(', ')', '.' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(x));
return string.Concat(parts);
}
private async Task<(int groupCount, int emojiCount)> RunAsync(TextWriter destination)
{
using var source = await DownloadAsync().ConfigureAwait(false);
await destination.WriteLineAsync("// <auto-generated />");
await destination.WriteLineAsync("namespace System.Text");
await destination.WriteLineAsync("{");
await destination.WriteLineAsync(" /// <summary>");
await destination.WriteLineAsync($" /// Emoji list (from Unicode version {MaxVersion:0.0}).");
await destination.WriteLineAsync(" /// </summary>");
await destination.WriteLineAsync(" /// <remarks>");
await destination.WriteLineAsync($" /// Created from <see href=\"{SourceUrl}\"/> using <see href=\"https://github.com/justdmitry/FullEmojiList\"/> generator at {DateTimeOffset.UtcNow:R}.");
await destination.WriteLineAsync(" /// </remarks>");
await destination.WriteLineAsync(" public static class Emoji");
await destination.WriteLineAsync(" {");
var groupCount = 0;
var emojiCount = 0;
var lastGroup = string.Empty;
var lastSubgroup = string.Empty;
await foreach (var emoji in ParseSourceAsync(source))
{
if (emoji.group != lastGroup)
{
if (groupCount > 0)
{
await destination.WriteLineAsync($" #endregion");
await destination.WriteLineAsync();
}
lastGroup = emoji.group;
groupCount++;
await destination.WriteLineAsync($" #region {emoji.group}");
}
if (IncludeGroupInEmojiName)
{
await destination.WriteLineAsync($" public const string {emoji.group}_{emoji.name} = \"{emoji.value}\";");
}
else
{
await destination.WriteLineAsync($" public const string {emoji.name} = \"{emoji.value}\";");
}
emojiCount++;
}
if (groupCount > 0)
{
await destination.WriteLineAsync($" #endregion");
}
await destination.WriteLineAsync(" }");
await destination.WriteLineAsync("}");
await destination.FlushAsync();
return (groupCount, emojiCount);
}
private async Task<StreamReader> DownloadAsync()
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(SourceUrl).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return new StreamReader(stream);
}
private async IAsyncEnumerable<(string group, string subgroup, string name, string value)> ParseSourceAsync(StreamReader stream)
{
const string groupPrefix = "# group:";
const string subgroupPrefix = "# subgroup:";
const string commentPrefix = "#";
var group = string.Empty;
var subgroup = string.Empty;
while (!stream.EndOfStream)
{
var line = await stream.ReadLineAsync();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line.StartsWith(groupPrefix, StringComparison.Ordinal))
{
group = FormatName(line.Substring(groupPrefix.Length));
continue;
}
if (line.StartsWith(subgroupPrefix, StringComparison.Ordinal))
{
subgroup = FormatName(line.Substring(subgroupPrefix.Length));
continue;
}
if (line.StartsWith(commentPrefix, StringComparison.Ordinal))
{
continue;
}
var emoji = ParseEmoji(line);
if (emoji != null)
{
yield return (group, subgroup, emoji.Value.name, emoji.Value.value);
}
}
}
private (string name, string value)? ParseEmoji(string line)
{
var parts = line.Split(new[] { ';', '#' }, 3);
if (parts[1].Trim() != "fully-qualified")
{
return null;
}
var versionAndName = parts[2].Split('E', 2)[1].Split(' ', 2);
var version = float.Parse(versionAndName[0]);
if (version > MaxVersion)
{
return null;
}
var name = FormatName(versionAndName[1]);
if (!IncludeGroupInEmojiName && char.IsDigit(name[0]))
{
name = "_" + name;
}
var surrogates = parts[0].Trim()
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Select(x => int.Parse(x, NumberStyles.HexNumber))
.Select(x => char.ConvertFromUtf32(x));
var value = string.Concat(surrogates);
return (name, value);
}
}
}