Skip to content

Instantly share code, notes, and snippets.

@peppy
Created March 29, 2019 08:45
Show Gist options
  • Save peppy/6f49ac3d204ea3595ee65875944046d4 to your computer and use it in GitHub Desktop.
Save peppy/6f49ac3d204ea3595ee65875944046d4 to your computer and use it in GitHub Desktop.
FontAwesome json -> cs generation
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Humanizer" Version="2.6.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Humanizer;
namespace metadata
{
class Program
{
static void Main(string[] args)
{
var json = JObject.Parse(File.ReadAllText("icons.json"));
Dictionary<string, List<dynamic>> weights = new Dictionary<string, List<dynamic>>();
foreach (dynamic icon in json)
{
foreach (string s in icon.Value.styles)
{
var upper = s.Pascalize();
if (weights.TryGetValue(upper, out List<dynamic> list))
list.Add(icon);
else
weights.Add(upper, new List<dynamic> { icon });
}
}
foreach (var w in weights)
{
using (StreamWriter sw = File.CreateText($"FontAwesome-{w.Key}"))
{
sw.WriteLine($"public static class FontAwesome{w.Key}");
sw.WriteLine("{");
sw.WriteLine();
sw.WriteLine($"public static IconUsage Get(int icon) => FontAwesome.Get(icon).With(weight: \"{w.Key}\");");
sw.WriteLine();
foreach (var i in w.Value)
{
var name = ((string)i.Key).Replace('-', ' ').Pascalize();
if (char.IsDigit(name[0]))
name = "Icon" + name;
// special cases
switch (name)
{
case "FontAwesome":
name = "IconFontAwesome";
break;
}
sw.WriteLine($"/// <summary>");
sw.WriteLine($"/// {i.Value.label}");
sw.WriteLine($"/// </summary>");
sw.WriteLine($"public static IconUsage {name} => Get(0x{i.Value.unicode});");
sw.WriteLine();
}
sw.WriteLine("}");
}
}
}
}
}
@huoyaoyuan
Copy link

2 issues found in this script:
Xml doc should use &amp; for & characters.
Encoding seems not correct. I've seem Büromöbel, after search I think it should be Büromöbel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment