Skip to content

Instantly share code, notes, and snippets.

@anotherlab
Created May 10, 2022 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anotherlab/6a1198ee247e961882b09c40efb43c5f to your computer and use it in GitHub Desktop.
Save anotherlab/6a1198ee247e961882b09c40efb43c5f to your computer and use it in GitHub Desktop.
Python script for collecting Xamarin.Forms or .NET MAUI named font glyphs
#! /usr/bin/env python3
# This script will recursively walk through all of the .xaml files
# in a project and collect the names of the icon font glyphs
# For a line that contains the following markup
# Glyph="{x:Static icons:MaterialDesignIconFont.AccountEditOutline}"/>
# It would return account-edit-outline
#
# Usage:
# WalkThru.py project-folder IconFontAlias
import os
import sys
from glob import glob
# Convert the C# ThisName back to this-name
def DotNetNameToTtfName(glyph):
newGlyph = []
for char in glyph:
if char.isupper():
if len(newGlyph) > 0:
newGlyph.append('-')
newGlyph.append(char.lower())
return ''.join(newGlyph)
if len(sys.argv) < 3:
print("Usage:")
print("WalkThru.py project-folder IconFontAlias")
sys.exit(1)
rootdir = sys.argv[1]
fontPattern = sys.argv[2]
# fontPattern = 'icons:MaterialDesignIconFont'
# Add the object separator to allow matches only on whole names
fontPattern += '.'
ln = len(fontPattern)
glyphs = []
# recursively get all of the xaml file names
files = [f for f in glob(rootdir+'/**/*.xaml', recursive=True) if os.path.isfile(f)]
for f in files:
# read the file
xamlFile = open(f, 'r')
lines = xamlFile.readlines()
xamlFile.close
# check each lines
for l in lines:
match = l.find(fontPattern)
if match != -1:
s = l[match:]
match = s.find('}')
if match != -1:
# trim off the "}" and everything after it
s = s[0:match]
# trim off the fontPattern
s = s[ln:]
# add to the list
if s not in glyphs:
glyphs.append(s)
glyphs.sort()
for g in glyphs:
print(DotNetNameToTtfName(g))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment