Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HTMLElement:
- def set (self, s, val):
- setattr (self, s, val)
- def get (self, s):
- return getattr (self, s)
- def __init__ (self, tag, list_of_attr = {}, plain = False):
- self.tag = tag
- self.plain = plain
- self.list_of_attr = []
- self.nested = []
- for attr in list_of_attr:
- self.set (attr, list_of_attr[attr])
- self.list_of_attr.append (attr)
- def to_html (self):
- if self.plain:
- return self.tag + '\n'
- html = '<' + self.tag
- if self.list_of_attr:
- html += ' '
- html += ' '.join ([attr + '=' + self.get (attr).lower () for attr in self.list_of_attr])
- html += '>\n'
- if self.nested:
- html += ''.join ([element.to_html () for element in self.nested])
- html += '</' + self.tag + '>\n'
- return html
- #HTMLElement is the main logic of this program, it is a rooted tree, where each node remembers its tag, all attributes
- #(also we have access to them as to fields) and all directly nested vertices. There are three types of content:
- #self-closing tags (as non-leaves), non-self-closing tags and plain text (as leaves). Below you'll see classes of specific
- #types of tags with stricter requirements to the structure of the tree (i.e. some tags have to have other tags nested in
- #them, while some other are necessarily leaves)
- class BR (HTMLElement):
- def __init__ (self):
- HTMLElement.__init__ (self, 'br')
- class FormattedText (HTMLElement):
- def __init__ (self, text, list_of_attr = {}):
- HTMLElement.__init__ (self, 'div', list_of_attr)
- self.nested = [HTMLElement (text, {}, True)]
- class LinkedText (HTMLElement):
- def __init__ (self, info):
- HTMLElement.__init__ (self, 'a', info['a_attr'])
- self.nested = [FormattedText (info['text'], info['div_attr'])]
- class FormElement (HTMLElement):
- def __init__ (self, info):
- HTMLElement.__init__ (self, 'form', info['attr'])
- self.nested = []
- for element in info['elements']:
- self.add_element (element['type'], element)
- def add_element (self, type, info):
- if type == 'br':
- self.nested.append (BR ())
- elif type == 'input':
- self.nested.append (InputElement (info))
- else:
- info['attr'] = info['attr'] if 'attr' in info else {}
- self.nested.append (SelectElement (info))
- class Option (HTMLElement):
- def __init__ (self, text, list_of_attr = {}):
- print (text, list_of_attr)
- HTMLElement.__init__ (self, 'option', list_of_attr)
- self.nested = [HTMLElement (text, {}, True)]
- class InputElement (HTMLElement):
- def __init__ (self, info):
- HTMLElement.__init__ (self, 'input', info['attr'])
- class SelectElement (HTMLElement):
- def __init__ (self, info):
- HTMLElement.__init__ (self, 'select', info['attr'])
- self.nested = [Option (text, attr) for text, attr in info['options']]
- def add_option (self, text, list_of_attr = {}):
- self.options.append (Option (text, list_of_attr))
- class Image (HTMLElement):
- def __init__ (self, list_of_attr = {}):
- HTMLElement.__init__ (self, 'img', list_of_attr)
- class HTMLDocument:
- #expects info['type'] = form/text/image
- #form - a simple HTML form. In ['elements'] there is a list of elements where each element is a dictionary:
- #['type'] is either 'input' or 'select', ['attr'] is the dictionary of attributes (a[2]['attr']['disabled']=true).
- #For 'select' type you also have to
- #specify options in ['options'], where it's a list of tuples (text, attributes), each element in the list
- #describes each option.
- #-------------------
- #text - a block of text, can be styled, be a hyperlinked or both. In ['div_attr'] keep attributes for <div>
- #tag (a['div_attr']['style']="epic"), in ['a_attr'], accodringly, attributes for <a> tag. If none for any of
- #tags, no tag will be created. Keep the plain text in ['text']
- #-------------------
- #image - an image with <img> attributes. Keep attributes by ['attr'] key (img['attr']['src']="circle.png")
- #-------------------
- #br - newline, just a dictionary a['type'] = 'br' will be enough
- def add_element (self, info):
- if 'type' not in info:
- return
- if info['type'] == 'br':
- self.items.append (BR ())
- elif info['type'] == 'image':
- self.items.append (Image (info['attr']))
- elif info['type'] == 'text':
- info['div_attr'] = info['div_attr'] if 'div_attr' in info else {}
- info['a_attr'] = info['a_attr'] if 'a_attr' in info else {}
- self.items.append (LinkedText (info))
- elif info['type'] == 'form':
- info['attr'] = info['attr'] if 'attr' in info else {}
- self.items.append (FormElement (info))
- def __init__ (self, input = []):
- self.items = []
- for info in input:
- self.add_element (info)
- def translate_to_html (self, filename):
- html = '<html>\n<head></head>\n<body>\n'
- html += ''.join ([item.to_html () for item in self.items])
- html += '</body></html>'
- with open (filename, 'w') as f:
- f.write (html)
- x = HTMLDocument ()
- form = {'type' : 'form'}
- form['elements'] = []
- for i in range (3):
- radio = {'type' : 'input'}
- radio['attr'] = {'type' : 'radio', 'name' : 'radio'}
- form['elements'].append (radio)
- form['elements'].append ({'type' : 'br'})
- sel = {'type' : 'select'}
- sel['options'] = [('Choice ' + str (i + 1), {}) for i in range (4)]
- form['elements'].append (sel)
- form['elements'].append ({'type' : 'br'})
- inp = {'type' : 'input',
- 'attr' : {'type' : 'submit'}
- }
- form['elements'].append (inp)
- form['elements'].append ({'type' : 'br'})
- x.add_element (form)
- x.add_element ({'type' : 'br'})
- supertext = {'type' : 'text', 'text' : 'How was it?',
- 'a_attr' : {'href' : 'mailto:@gmail.com'}
- 'div_attr' : {'style' : 'link'}
- }
- x.add_element (supertext)
- x.translate_to_html ('simple_form.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement