forked from mozilla/fxemoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_json.py
72 lines (52 loc) · 1.7 KB
/
generate_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import sys
import natsort
import glob
import json
if __name__ == "__main__":
if len(sys.argv)<1:
print "Please provide arguments."
sys.exit(1)
font_name=sys.argv[1]
icons={}
layers={}
svgs_path=[]
svgs_path_sorted=[]
json_output=[]
data_dir = 'svgs/' + font_name + '/'
file_dir_extension = os.path.join(data_dir, '*.svg')
for file_name in glob.glob(file_dir_extension):
if file_name.endswith('.svg'):
svgs_path.append(file_name.replace('svgs/' + font_name + '/',''))
for files in natsort.natsorted(svgs_path, key=lambda y: y.lower()):
svgs_path_sorted.append(files)
for file in svgs_path_sorted:
if file .find('-') > -1:
file_name=file.replace('.svg','').split('-')
icons[file_name[0]]=file_name[1]
if file .find('.layer') > -1:
#get color from file
raw_svg=open('svgs/' + font_name + '/' + file).read()
color=raw_svg.split('fill="#')[1].split('"')[0]
if len(color) == 3: # short group
value = [c + c for c in color]
color = value[0] + value[1] + value[2]
#get codes and layer index
file_name=file.replace('.svg','').split('layer')
key=file_name[0].replace('.','')
if key not in layers:
layers[key]=[]
layers[key].append({'order': int(file_name[1]) ,'color' : color, 'name':file_name[0] + 'layer' + file_name[1]})
for icon in icons:
icon_glyph={
'icon' : {
'glyph': {
'code' : icon,
'name' : icons[icon]
},'layers' : layers[icon]
}
}
json_output.append(icon_glyph)
f=open(font_name + '.json','w+')
f.write(json.dumps(json_output, sort_keys=True, indent=2, separators=(',', ': ')))
f.close()