#!/usr/bin/env python
import sys
def parse(f):
data = []
buff = open(f).read()
for items in buff.split(':'):
# check items
x = items.find('(')
li = []
if x > -1:
for p_items in items[x+1:-1].split(','):
# the key
k = p_items.strip()[1]
# the text for the option
li.append((k,
''.join([i for i in p_items if i != '[' and i != ']']).strip()))
data.append((items.strip().split()[0],li))
# no options
else:
if items.strip():
data.append((items.strip(),[]))
return data
def generate_html(data):
html = '<html>\n<body>\n<form>\n'
for (k, v) in data:
if not len(v):
html += '{}:\n<input type="text" name="{}"/>\n<br/>\n'.format(k,k.lower())
else:
html += '{}:\n'.format(k)
if len(v) < 5:
for o in v:
html += '<input type="radio" name="{}" value="{}"/> {}\n'.format(
k.lower(), o[0].lower(), o[1])
elif len(v) >= 5:
html += '<select name="{}">\n'.format(k.lower())
for o in v:
html += '<option value="{}">{}</option>\n'.format(o[0].lower(), o[1])
html += '</select>\n'
html += '<br/>\n'
html+= '<input type="submit" value="Submit"/>\n</form>\n</body>\n</html>\n'
return html
if __name__ == '__main__':
print generate_html(parse(sys.argv[1]))