Skip to content

Commit

Permalink
Improve parser and "material icon" for future.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro committed May 28, 2024
1 parent 60d6627 commit b18a875
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/MaterialSymbolsParser/Model/IconType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MaterialSymbolsParser.Model;

public enum IconType
{
MaterialSymbols = 0,
MaterialIcons = 1
}
7 changes: 4 additions & 3 deletions src/MaterialSymbolsParser/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MaterialSymbolsParser.Service;
using MaterialSymbolsParser.Model;
using MaterialSymbolsParser.Service;

namespace MaterialSymbolsParser;

Expand All @@ -9,8 +10,8 @@ public static async Task Main(string[] args)
var codeGenerator = new CodeGenerationService();
using var client = new IconHttpClientService();
var metadata = await client.ParseIconsAsync().ConfigureAwait(false);
var filteredIcons = Utility.IconFilter.FilterNonMaterialSymbols(metadata!);
var groupedIcons = Utility.IconFilter.GroupIconsByFamilies(filteredIcons);
var filteredIcons = Utility.IconFilter.FilterByFamily(metadata, IconType.MaterialSymbols);
var groupedIcons = Utility.IconFilter.GroupIconsByFamilies(filteredIcons, IconType.MaterialSymbols);

codeGenerator.GenerateCsFilesUsingRoslyn(groupedIcons, folder: "../../../../MudBlazor.Icons.MaterialSymbols");
}
Expand Down
2 changes: 1 addition & 1 deletion src/MaterialSymbolsParser/Service/CodeGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static CompilationUnitSyntax GenerateUsingsAndNamespace(KeyValuePair<str
return SyntaxFactory.CompilationUnit()
.WithUsings(usings)
.WithMembers(SyntaxFactory.SingletonList<MemberDeclarationSyntax>(namespaceDeclaration))
.WithLeadingTrivia(commentTrivia); ;
.WithLeadingTrivia(commentTrivia);
}

private static SyntaxTriviaList AddComment()
Expand Down
6 changes: 5 additions & 1 deletion src/MaterialSymbolsParser/Service/IconHttpClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IconHttpClientService()

}

public async Task<Metadata?> ParseIconsAsync()
public async Task<Metadata> ParseIconsAsync()
{
var json = await _httpClient.GetStringAsync(new Uri("metadata/icons?incomplete=1&key=material_symbols", UriKind.Relative)).ConfigureAwait(false);
using var reader = new StringReader(json);
Expand All @@ -32,6 +32,10 @@ public IconHttpClientService()
var validJson = await reader.ReadToEndAsync().ConfigureAwait(false);

var metadata = JsonSerializer.Deserialize<Metadata>(validJson, _jsonSerializerOptions);
if (metadata is null)
{
throw new InvalidOperationException("Response is null");
}

return metadata;
}
Expand Down
82 changes: 81 additions & 1 deletion src/MaterialSymbolsParser/Utility/FamilyMap.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,84 @@
namespace MaterialSymbolsParser.Utility;
using MaterialSymbolsParser.Model;

namespace MaterialSymbolsParser.Utility;

public static class FamilyMap
{
public const string MaterialSymbolsPrefix = "Material Symbols";
public const string MaterialIconsPrefix = "Material Icons";
public const string Outlined = "Outlined";
public const string Rounded = "Rounded";
public const string Sharp = "Sharp";
public const string TwoTone = "Two Tone";
public const string Filled = "Filled";
public const string MaterialSymbolsOutlined = $"{MaterialSymbolsPrefix} {Outlined}";
public const string MaterialSymbolsRounded = $"{MaterialSymbolsPrefix} {Rounded}";
public const string MaterialSymbolsSharp = $"{MaterialSymbolsPrefix} {Sharp}";

public const string MaterialIconsOutlined = $"{MaterialIconsPrefix} {Outlined}";
public const string MaterialIconsFilled = $"{MaterialIconsPrefix}"; // this is not a typo, filled is indeed only using "Material Icons" without "Filled"
public const string MaterialIconsRounded = $"{MaterialIconsPrefix} {Rounded}";
public const string MaterialIconsSharp = $"{MaterialIconsPrefix} {Sharp}";
public const string MaterialIconsTwoTone = $"{MaterialIconsPrefix} {TwoTone}";

public static readonly IReadOnlyList<string> MaterialSymbolsFamilies = new List<string>
{
MaterialSymbolsOutlined,
MaterialSymbolsRounded,
MaterialSymbolsSharp,
};
public static readonly IReadOnlyList<string> MaterialIconsFamilies = new List<string>
{
MaterialIconsOutlined,
MaterialIconsFilled,
MaterialIconsRounded,
MaterialIconsSharp,
MaterialIconsTwoTone
};

public static IReadOnlyList<string> GetFamiliesByIconType(IconType iconType)
{
return iconType switch
{
IconType.MaterialSymbols => MaterialSymbolsFamilies,
IconType.MaterialIcons => MaterialIconsFamilies,
_ => throw new InvalidOperationException($"Mapping for icon type {iconType} not found!")
};
}

public static IconType FromFamilyToIconType(string family)
{
switch (family)
{
// Material Symbols
case MaterialSymbolsOutlined:
case MaterialSymbolsRounded:
case MaterialSymbolsSharp:
return IconType.MaterialSymbols;

// Material Icons
case MaterialIconsOutlined:
case MaterialIconsFilled:
case MaterialIconsRounded:
case MaterialIconsSharp:
case MaterialIconsTwoTone:
return IconType.MaterialIcons;
default:
throw new InvalidOperationException($"Mapping for family name {family} not found!");
}
}

public static bool IsSelectedFamilyByIconType(IconType iconType, string family)
{
return iconType switch
{
IconType.MaterialSymbols => IsMaterialSymbolFamily(family),
IconType.MaterialIcons => IsMaterialIconFamily(family),
_ => throw new InvalidOperationException($"Mapping for icon type {iconType} and family {family} not found!")
};
}

public static bool IsMaterialIconFamily(string family) => family.StartsWith(MaterialIconsPrefix, StringComparison.OrdinalIgnoreCase);

public static bool IsMaterialSymbolFamily(string family) => family.StartsWith(MaterialSymbolsPrefix, StringComparison.OrdinalIgnoreCase);

Expand All @@ -23,9 +87,17 @@ public static string FamilyNameToCsharpClassName(string familyName)
ArgumentNullException.ThrowIfNull(familyName);
return familyName switch
{
// Material Symbols
MaterialSymbolsOutlined => Outlined,
MaterialSymbolsRounded => Rounded,
MaterialSymbolsSharp => Sharp,

// Material Icons
MaterialIconsOutlined => Outlined,
MaterialIconsFilled => Filled,
MaterialIconsRounded => Rounded,
MaterialIconsSharp => Sharp,
MaterialIconsTwoTone => TwoTone,
_ => throw new InvalidOperationException($"Mapping for family name {familyName} not found!")
};
}
Expand All @@ -35,9 +107,17 @@ public static string FamilyNameToHtmlClassName(string familyName)
ArgumentNullException.ThrowIfNull(familyName);
return familyName switch
{
// Material Symbols
MaterialSymbolsOutlined => "material-symbols-outlined",
MaterialSymbolsRounded => "material-symbols-rounded",
MaterialSymbolsSharp => "material-symbols-sharp",

// Material Icons
MaterialIconsOutlined => "material-icons-outlined",
MaterialIconsFilled => "material-icons",
MaterialIconsRounded => "material-icons-round",
MaterialIconsSharp => "material-icons-sharp",
MaterialIconsTwoTone => "material-icons-two-tone",
_ => throw new InvalidOperationException($"Mapping for family name {familyName} not found!")
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/MaterialSymbolsParser/Utility/IconFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace MaterialSymbolsParser.Utility;

public static class IconFilter
{
public static Dictionary<string, IReadOnlyCollection<Icon>> GroupIconsByFamilies(IReadOnlyCollection<Icon> icons)
public static Dictionary<string, IReadOnlyCollection<Icon>> GroupIconsByFamilies(IReadOnlyCollection<Icon> icons, IconType iconType)
{
var groupedIcons = new Dictionary<string, IReadOnlyCollection<Icon>>();

foreach (var family in FamilyMap.MaterialSymbolsFamilies)
foreach (var family in FamilyMap.GetFamiliesByIconType(iconType))
{
groupedIcons[family] = icons
.Where(icon => !icon.UnsupportedFamilies.Contains(family))
Expand All @@ -18,10 +18,10 @@ public static Dictionary<string, IReadOnlyCollection<Icon>> GroupIconsByFamilies
return groupedIcons;
}

public static IReadOnlyCollection<Icon> FilterNonMaterialSymbols(Metadata metadata)
public static IReadOnlyCollection<Icon> FilterByFamily(Metadata metadata, IconType iconType)
{
return metadata.Icons
.Where(icon => !icon.UnsupportedFamilies.Any(FamilyMap.IsMaterialSymbolFamily))
.Where(icon => !icon.UnsupportedFamilies.Any(family => FamilyMap.IsSelectedFamilyByIconType(iconType, family)))
.ToList();
}
}

0 comments on commit b18a875

Please sign in to comment.