Last active
August 29, 2015 14:21
-
-
Save jabney/8b395c983b70157e6492 to your computer and use it in GitHub Desktop.
Python utility for converting Font Awesome cheatsheet to a JSON file
This file contains 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
""" | |
Generate fontawesome icon set in JSON format from website cheatsheet. | |
http://fortawesome.github.io/Font-Awesome/cheatsheet/ | |
Usage: python gen_fontawesome.py | |
Output: font-awesome.json | |
{icon_name: entity_reference, ...} | |
Author: James Abney | |
Date: 2015-05-21 | |
""" | |
import traceback | |
import json | |
import re | |
import urllib.request as http | |
from urllib.error import URLError | |
from bs4 import UnicodeDammit as UD | |
from bs4 import BeautifulSoup as Soup | |
from bs4.element import Tag as Tag | |
# Regular expression for reducing all spans | |
# of whitespace to a single space. | |
RE_WHITESPACE = re.compile(r'\s+') | |
def strip_excess_whitespace(string): | |
""" | |
Return 'string' with newlines and excess spaces removed. | |
""" | |
return RE_WHITESPACE.sub(' ', string).strip() | |
def fetch(url): | |
""" | |
Fetch html from url. | |
""" | |
if not url: | |
return None | |
try: | |
html = None | |
response = http.urlopen(url) | |
except URLError: | |
print('urlopen exception for url %s' % url) | |
traceback.print_exc() | |
else: | |
# Get the byte stream from the http response. | |
bytes_ = response.read() | |
# Convert bytes to unicode text. | |
html = UD(bytes_).unicode_markup | |
return html | |
def generate_font_dict(): | |
""" | |
Fetch cheatsheet html, extract icon references, | |
and convert to dictionary. | |
""" | |
re_extract = re.compile(r'fa-([-a-z0-9]+)(?: \(alias\))? \[(&#x.+?;)\]') | |
html = fetch('http://fortawesome.github.io/Font-Awesome/cheatsheet/') | |
soup = Soup(html) | |
container = soup.find('div', class_='row') | |
icons = {} | |
for child in container.children: | |
if not isinstance(child, Tag): | |
continue | |
text = strip_excess_whitespace(child.get_text()) | |
match = re_extract.search(text) | |
name = match.group(1) | |
entity = match.group(2) | |
icons[name] = entity | |
return icons | |
def main(): | |
""" | |
Get dictionary of icon references and dump to JSON. | |
""" | |
icons = generate_font_dict() | |
with open('font-awesome.json', 'w') as icon_file: | |
json.dump(icons, icon_file) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment