-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
69 lines (50 loc) · 1.61 KB
/
parser.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
from bs4 import BeautifulSoup
import sys
import os
from lxml import etree
# input_file = "{filepath}/untitled-font-1/icons-reference.html"
# output_file = "font_icon_mapping.xml"
input_file = ''
output_file = ''
prefix = ''
try:
input_file = str(sys.argv[1])
except:
print "Input file not passed\n"
print "Format : \n \n"
print "python parser.py input_file.html output_file.xml"
sys.exit(1)
try:
output_file = str(sys.argv[2])
except:
print "Output file not passed\n"
print "Format : "
print "python parser.py input_file output_file"
sys.exit(2)
try:
prefix = str(sys.argv[3])
except:
print "Appending default prefix '" + prefix + "'"
soup = BeautifulSoup(open(input_file), "lxml")
div = soup.html.body.div
names = []
chars = []
ul_tag_css_mapping = div.find('ul', 'glyphs css-mapping')
li_tags_css_mapping = ul_tag_css_mapping.find_all('input')
for each in li_tags_css_mapping:
names.append(prefix + each.get('value').replace('-', '_'))
ul_tag_char_mapping = div.find('ul', 'glyphs character-mapping')
li_tags_char_mapping = ul_tag_char_mapping.find_all('input')
for each in li_tags_char_mapping:
chars.append(each.get('value'))
# Create new xml file where all tags will be written
# Open file in writtable mode
file = open(output_file, "w")
root = etree.Element('resources')
# Make a new document tree
doc = etree.ElementTree(root)
for i in range(0, len(names)):
element = etree.SubElement(root, 'string', {'name':names[i], 'translatable':'false'})
element.text = chars[i]
doc.write(file, xml_declaration=True, encoding='utf-8', pretty_print=True)
file.close()