Last active
March 27, 2019 23:37
-
-
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
This file contains hidden or 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
# 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