mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-18 21:08:15 +08:00
Update emoji support
* Add constants for emojis * Move emoji shortcode rendering to Markup * Add documentation * Add example * Add tests
This commit is contained in:

committed by
Patrik Svensson

parent
090b30f731
commit
eeb3f967b6
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -10,52 +11,72 @@ using Scriban.Runtime;
|
||||
using Spectre.Cli;
|
||||
using Spectre.IO;
|
||||
using Path = Spectre.IO.Path;
|
||||
using SpectreEnvironment = Spectre.IO.Environment;
|
||||
|
||||
namespace Generator.Commands
|
||||
{
|
||||
public sealed class EmojiGeneratorCommand : AsyncCommand<GeneratorCommandSettings>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
private readonly IEnvironment _environment;
|
||||
private readonly IHtmlParser _parser;
|
||||
|
||||
private readonly Dictionary<string, string> _templates = new Dictionary<string, string>
|
||||
{
|
||||
{ "Templates/Emoji.Generated.template", "Emoji.Generated.cs" },
|
||||
{ "Templates/Emoji.Json.template", "emojis.json" },
|
||||
};
|
||||
|
||||
public EmojiGeneratorCommand()
|
||||
{
|
||||
_fileSystem = new FileSystem();
|
||||
_environment = new SpectreEnvironment();
|
||||
_parser = new HtmlParser();
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, GeneratorCommandSettings settings)
|
||||
{
|
||||
var output = new DirectoryPath(settings.Output);
|
||||
|
||||
if (!_fileSystem.Directory.Exists(settings.Output))
|
||||
{
|
||||
_fileSystem.Directory.Create(settings.Output);
|
||||
}
|
||||
|
||||
var templatePath = new FilePath("Templates/Emoji.Generated.template");
|
||||
var stream = await FetchEmojis(settings);
|
||||
var document = await _parser.ParseDocumentAsync(stream);
|
||||
var emojis = Emoji.Parse(document).OrderBy(x => x.Name)
|
||||
.Where(emoji => !emoji.HasCombinators)
|
||||
.ToList();
|
||||
|
||||
var emojis = await FetchEmojis("http://www.unicode.org/emoji/charts/emoji-list.html");
|
||||
// Render all templates
|
||||
foreach (var (templateFilename, outputFilename) in _templates)
|
||||
{
|
||||
var result = await RenderTemplate(new FilePath(templateFilename), emojis);
|
||||
|
||||
var result = await RenderTemplate(templatePath, emojis);
|
||||
|
||||
var outputPath = output.CombineWithFilePath(templatePath.GetFilename().ChangeExtension(".cs"));
|
||||
|
||||
await File.WriteAllTextAsync(outputPath.FullPath, result);
|
||||
var outputPath = output.CombineWithFilePath(outputFilename);
|
||||
await File.WriteAllTextAsync(outputPath.FullPath, result);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyCollection<Emoji>> FetchEmojis(string url)
|
||||
private async Task<Stream> FetchEmojis(GeneratorCommandSettings settings)
|
||||
{
|
||||
using var http = new HttpClient();
|
||||
var input = string.IsNullOrEmpty(settings.Input)
|
||||
? _environment.WorkingDirectory
|
||||
: new DirectoryPath(settings.Input);
|
||||
|
||||
var htmlStream = await http.GetStreamAsync(url);
|
||||
var file = _fileSystem.File.Retrieve(input.CombineWithFilePath("emoji-list.html"));
|
||||
if (!file.Exists)
|
||||
{
|
||||
using var http = new HttpClient();
|
||||
using var httpStream = await http.GetStreamAsync("http://www.unicode.org/emoji/charts/emoji-list.html");
|
||||
using var outStream = file.OpenWrite();
|
||||
|
||||
var document = await _parser.ParseDocumentAsync(htmlStream);
|
||||
await httpStream.CopyToAsync(outStream);
|
||||
}
|
||||
|
||||
return Emoji.Parse(document).OrderBy(x => x.Name).ToList();
|
||||
return file.OpenRead();
|
||||
}
|
||||
|
||||
private static async Task<string> RenderTemplate(Path path, IReadOnlyCollection<Emoji> emojis)
|
||||
@ -63,7 +84,6 @@ namespace Generator.Commands
|
||||
var text = await File.ReadAllTextAsync(path.FullPath);
|
||||
|
||||
var template = Template.Parse(text);
|
||||
|
||||
var templateContext = new TemplateContext
|
||||
{
|
||||
// Because of the insane amount of Emojis,
|
||||
@ -72,9 +92,7 @@ namespace Generator.Commands
|
||||
};
|
||||
|
||||
var scriptObject = new ScriptObject();
|
||||
|
||||
scriptObject.Import(new { Emojis = emojis });
|
||||
|
||||
templateContext.PushGlobal(scriptObject);
|
||||
|
||||
return await template.RenderAsync(templateContext);
|
||||
|
Reference in New Issue
Block a user