mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-18 21:08:15 +08:00
Add Generator command to generate emoji lookup table
This commit is contained in:

committed by
Patrik Svensson

parent
11d331e31d
commit
a7b7d4e556
70
resources/scripts/Generator/Models/Emoji.cs
Normal file
70
resources/scripts/Generator/Models/Emoji.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using AngleSharp.Dom;
|
||||
using AngleSharp.Html.Dom;
|
||||
|
||||
namespace Generator.Models
|
||||
{
|
||||
public class Emoji
|
||||
{
|
||||
private static readonly string[] _headers = { "count", "code", "sample", "name" };
|
||||
|
||||
private Emoji(string code, string name)
|
||||
{
|
||||
Code = code;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Code { get; }
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public static IEnumerable<Emoji> Parse(IHtmlDocument document)
|
||||
{
|
||||
var rows = document
|
||||
.GetNodes<IHtmlTableRowElement>(predicate: row =>
|
||||
row.Cells.Length >= _headers.Length && // Filter out rows that don't have enough cells.
|
||||
row.Cells.All(x => x.LocalName == TagNames.Td)); // We're only interested in td cells, not th.
|
||||
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var dictionary = _headers
|
||||
.Zip(row.Cells, (header, cell) => (header, cell.TextContent.Trim()))
|
||||
.ToDictionary(x => x.Item1, x => x.Item2);
|
||||
|
||||
var code = TransformCode(dictionary["code"]);
|
||||
var name = TransformName(dictionary["name"]);
|
||||
|
||||
yield return new Emoji(code, name);
|
||||
}
|
||||
}
|
||||
|
||||
private static string TransformName(string name)
|
||||
{
|
||||
return name.Replace(":", string.Empty)
|
||||
.Replace(",", string.Empty)
|
||||
.Replace(".", string.Empty)
|
||||
.Replace("\u201c", string.Empty)
|
||||
.Replace("\u201d", string.Empty)
|
||||
.Replace("\u229b", string.Empty)
|
||||
.Trim()
|
||||
.Replace(' ', '_')
|
||||
.ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static string TransformCode(string code)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
|
||||
foreach (var part in code.Split(' '))
|
||||
{
|
||||
builder.Append(part.Length == 6
|
||||
? part.Replace("+", "0000")
|
||||
: part.Replace("+", "000"));
|
||||
}
|
||||
|
||||
return builder.ToString().Replace("U", "\\U");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user