Skip to content

Instantly share code, notes, and snippets.

@kuhess
Last active November 25, 2021 22:31
Show Gist options
  • Save kuhess/5205073 to your computer and use it in GitHub Desktop.
Save kuhess/5205073 to your computer and use it in GitHub Desktop.
Font-Awesome css to XeLaTeX mapping package (based on the work of sway https://gist.github.com/sway/3101743)
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
#
# This script gets the UTF-8 codes of the icons from the font-awesome.css and write a LaTeX mapping table in fontawesome.sty
# To use it, it must be placed at the root of the Font-Awesome repository.
#
# To use the generated .sty, you just have to put \usepackage{fontawesome} in your document
# Then, all the font-awesome symbols can be used with commands like \iconGithubAlt{} for the icon-github-alt
#
# To make the compiler able to find this package, you have two solutions:
# 1. THE UGLY WAY: copy and paste it into your working folder
# 2. THE BETTER WAY: create a fontawesome package in your local LaTeX package tree (on Mac for example it must be placed in ~/Library/texmf/tex/latex/fontawesome/fontawesome.sty)
#
# Note: you must have the font installed on your system, and compile your document with XeLaTex (or LuaLaTeX).
import re
def hyphen2camel(hyphen):
words = hyphen.split('-')
capitalized_words = map(str.capitalize, words)
camel = ''.join(capitalized_words)
return camel
css_file = open('css/font-awesome.css', 'r')
sty_file = open('fontawesome.sty', 'w')
header = '''\\usepackage{fontspec}
\\usepackage{relsize}
\\newfontfamily{\\FA}{FontAwesome}
'''
sty_file.write(header)
template = '\\def\\%s{{\\smaller[3] \\FA \\symbol{"%s}}~}\n'
pattern = r'\.icon-(\w+[-\w]*):.*\"\\(.*)\"'
regex = re.compile(pattern)
for line in css_file:
result = regex.search(line)
if result is not None:
name = ''.join(['icon', hyphen2camel(result.group(1))])
code = str.upper(result.group(2))
sty_file.write(template % (name, code))
css_file.close()
sty_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment