Skip to content

Instantly share code, notes, and snippets.

@che-wf
Last active March 27, 2019 23:37
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 che-wf/2bba094dce47299d3382758be00842cb to your computer and use it in GitHub Desktop.
Save che-wf/2bba094dce47299d3382758be00842cb to your computer and use it in GitHub Desktop.
Uses Python and lxml to create a less file with classes from an SVG font file
# Install the library
# easy_install lxml
from lxml import etree
filename = "fonts/icons.svg"
tree = etree.parse(open(filename, 'r'))
lessPreStyle = """
@font_family_1: 'icon-font';
[class^="icon-"] {
font-family: @font_family_1 !important;
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
[class*=" icon-"] {
font-family: @font_family_1 !important;
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
"""
font = tree.getroot()[1][0]
for glyph in font.iter('{http://www.w3.org/2000/svg}glyph'):
if 'glyph-name' in glyph.keys():
lessPreStyle += '''\
.icon-{name} {{
&:before {{
content: {unicode};
}}
}}
'''.format(name=glyph.attrib['glyph-name'], unicode=repr(glyph.attrib['unicode']).replace('\\u', '\\'))
file = open("icons.less", "w")
file.write(lessPreStyle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment