Skip to content

Commit

Permalink
Switch pre-commit formatting to include python
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
devgg committed Jul 4, 2018
1 parent 242750d commit 6f66041
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/prettier/prettier
# Use old version until https://github.com/prettier/prettier/issues/4637 is fixed
rev: 1.12.1
hooks:
- id: prettier
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.6
15 changes: 4 additions & 11 deletions package.json
@@ -1,7 +1,8 @@
{
"name": "fonticon",
"version": "1.0.0",
"description": "Tool for creating favicons and images from [Font Awesome](http://fontawesome.io/) icons. The generated icon can be previewed live in the browser.",
"description":
"Tool for creating favicons and images from [Font Awesome](http://fontawesome.io/) icons. The generated icon can be previewed live in the browser.",
"private": true,
"directories": {
"lib": "src",
Expand All @@ -12,18 +13,13 @@
"build": "webpack --display-modules",
"start:dev": "webpack-dev-server",
"build:prod": "webpack --display-modules --mode=production",
"precommit": "pretty-quick --staged"
"postinstall": "pip install pre-commit && pre-commit install"
},
"repository": {
"type": "git",
"url": "git+https://github.com/devgg/fonticon.git"
},
"keywords": [
"favicon",
"Font Awesome",
"icon",
"fonticon"
],
"keywords": ["favicon", "Font Awesome", "icon", "fonticon"],
"author": "Florian Gauger",
"license": "MIT",
"bugs": {
Expand All @@ -35,9 +31,6 @@
"css-loader": "^0.28.11",
"file-loader": "^1.1.11",
"html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3",
"prettier": "^1.13.7",
"pretty-quick": "^1.6.0",
"style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.12.1",
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
@@ -0,0 +1,2 @@
[tool.black]
line-length = 120
68 changes: 34 additions & 34 deletions util/import_icon_data.py
Expand Up @@ -9,8 +9,11 @@

from PIL import ImageFont

css_url = 'https://use.fontawesome.com/releases/v5.1.0/css/all.css'
icons_json_url = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/advanced-options/metadata/icons.json'
css_url = "https://use.fontawesome.com/releases/v5.1.0/css/all.css"
icons_json_url = (
"https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/advanced-options/metadata/icons.json"
)


def load_fonts(css_url):
with urllib.request.urlopen(css_url) as url:
Expand All @@ -20,36 +23,38 @@ def load_fonts(css_url):
font_solid = None
font_regular = None
for rule in rules:
if rule.type == 'at-rule' and rule.at_keyword == 'font-face':
if rule.type == "at-rule" and rule.at_keyword == "font-face":
for component in rule.content:
if component.type == 'url' and component.value.endswith('.woff'):
if component.type == "url" and component.value.endswith(".woff"):
font_url = urllib.parse.urljoin(css_url, component.value)
fd = urllib.request.urlopen(font_url)
font = fd.read()
if 'fa-solid-900' in component.value:
if "fa-solid-900" in component.value:
font_solid = font
elif 'fa-regular-400' in component.value:
elif "fa-regular-400" in component.value:
font_regular = font
else:
print('Warning font "{0}" found in import but unused.'.format(font_url));
print('Warning font "{0}" found in import but unused.'.format(font_url))
if font_solid == None or font_regular == None:
print('Error one of the required fonts was not found')
print("Error one of the required fonts was not found")
sys.exit()
return (font_solid, font_regular)


font_solid, font_regular = load_fonts(css_url)


def get_style(style):
if style == 'solid':
return ('fas', font_solid_path)
elif style == 'regular':
return ('far', font_regular_path)
if style == "solid":
return ("fas", font_solid_path)
elif style == "regular":
return ("far", font_regular_path)
else:
print('Warning: style {0} unkown.'.format(style))
print("Warning: style {0} unkown.".format(style))


def get_max_size(font, icon):
font_size = 1200;
font_size = 1200
max_size = 1024
width = max_size + 1
height = max_size + 1
Expand All @@ -67,12 +72,13 @@ def get_max_size(font, icon):
width, height = f.getsize(icon)
return font_size


with tempfile.TemporaryDirectory() as tmp_dir:
font_solid_path = tmp_dir + 'solid.woff'
with open(font_solid_path, 'wb') as output:
font_solid_path = tmp_dir + "solid.woff"
with open(font_solid_path, "wb") as output:
output.write(font_solid)
font_regular_path = tmp_dir + 'regular.woff'
with open(font_regular_path, 'wb') as output:
font_regular_path = tmp_dir + "regular.woff"
with open(font_regular_path, "wb") as output:
output.write(font_regular)

with urllib.request.urlopen(icons_json_url) as url:
Expand All @@ -81,27 +87,21 @@ def get_max_size(font, icon):
result = []
idx = 0
for i, (icon_name, icon) in enumerate(icons_json.items()):
if icon_name != 'font-awesome-logo-full':
for style in icon['styles']:
if style != 'brands':
if icon_name != "font-awesome-logo-full":
for style in icon["styles"]:
if style != "brands":
style, font = get_style(style)
font
uni = ('\\u' + icon['unicode']).encode().decode('unicode-escape')
result_icon = {
'ix': idx,
'id': icon_name,
'st': style,
'uc': uni,
'si': get_max_size(font, uni),
}
search_terms = icon['search']['terms']
uni = ("\\u" + icon["unicode"]).encode().decode("unicode-escape")
result_icon = {"ix": idx, "id": icon_name, "st": style, "uc": uni, "si": get_max_size(font, uni)}
search_terms = icon["search"]["terms"]
if search_terms:
result_icon['se'] = search_terms
result_icon["se"] = search_terms
result.append(result_icon)
idx += 1
print('{0}/{1} icons processed.'.format(i + 1, len(icons_json)), end="\r")
print("{0}/{1} icons processed.".format(i + 1, len(icons_json)), end="\r")

js_icons = 'export default ' + json.dumps(result, separators=(',', ':'))
js_icons = "export default " + json.dumps(result, separators=(",", ":"))

with open('src/js/generated/icons.js', 'w') as f:
with open("src/js/generated/icons.js", "w") as f:
f.write(js_icons)

0 comments on commit 6f66041

Please sign in to comment.