Created
March 29, 2019 08:45
-
-
Save peppy/6f49ac3d204ea3595ee65875944046d4 to your computer and use it in GitHub Desktop.
FontAwesome json -> cs generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("}"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2 issues found in this script:
Xml doc should use
&
for&
characters.Encoding seems not correct. I've seem
Büromöbel
, after search I think it should beBüromöbel
.