[ create a new paste ] login | about

Link: http://codepad.org/IuB6ixF8    [ raw code | output | fork ]

Python, pasted on Mar 6:
#!/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]))


Output:
1
2
3
4
Traceback (most recent call last):
  Line 47, in <module>
    print generate_html(parse(sys.argv[1]))
IndexError: list index out of range


Create a new paste based on this one


Comments: