Advertisement
MetB

Untitled

Feb 22nd, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.61 KB | None | 0 0
  1. class HTMLElement:
  2.     def set (self, s, val):
  3.         setattr (self, s, val)
  4.  
  5.     def get (self, s):
  6.         return getattr (self, s)
  7.  
  8.     def __init__ (self, tag, list_of_attr = {}, plain = False):
  9.         self.tag = tag
  10.         self.plain = plain
  11.         self.list_of_attr = []
  12.         self.nested = []
  13.         for attr in list_of_attr:
  14.             self.set (attr, list_of_attr[attr])
  15.             self.list_of_attr.append (attr)
  16.  
  17.     def to_html (self):
  18.         if self.plain:
  19.             return self.tag + '\n'
  20.        
  21.         html = '<' + self.tag
  22.         if self.list_of_attr:
  23.             html += ' '
  24.         html += ' '.join ([attr + '=' + self.get (attr).lower () for attr in self.list_of_attr])
  25.         html += '>\n'
  26.         if self.nested:
  27.             html += ''.join ([element.to_html () for element in self.nested])
  28.             html += '</' + self.tag + '>\n'
  29.         return html
  30.  
  31.  
  32. #HTMLElement is the main logic of this program, it is a rooted tree, where each node remembers its tag, all attributes
  33. #(also we have access to them as to fields) and all directly nested vertices. There are three types of content:
  34. #self-closing tags (as non-leaves), non-self-closing tags and plain text (as leaves). Below you'll see classes of specific
  35. #types of tags with stricter requirements to the structure of the tree (i.e. some tags have to have other tags nested in
  36. #them, while some other are necessarily leaves)
  37.  
  38. class BR (HTMLElement):
  39.     def __init__ (self):
  40.         HTMLElement.__init__ (self, 'br')
  41.  
  42.  
  43. class FormattedText (HTMLElement):
  44.     def __init__ (self, text, list_of_attr = {}):
  45.         HTMLElement.__init__ (self, 'div', list_of_attr)
  46.         self.nested = [HTMLElement (text, {}, True)]
  47.  
  48.  
  49. class LinkedText (HTMLElement):
  50.     def __init__ (self, info):
  51.         HTMLElement.__init__ (self, 'a', info['a_attr'])
  52.         self.nested = [FormattedText (info['text'], info['div_attr'])]
  53.  
  54.  
  55. class FormElement (HTMLElement):
  56.     def __init__ (self, info):
  57.         HTMLElement.__init__ (self, 'form', info['attr'])
  58.         self.nested = []
  59.         for element in info['elements']:
  60.             self.add_element (element['type'], element)
  61.  
  62.     def add_element (self, type, info):
  63.         if type == 'br':
  64.             self.nested.append (BR ())
  65.         elif type == 'input':
  66.             self.nested.append (InputElement (info))
  67.         else:
  68.             info['attr'] = info['attr'] if 'attr' in info else {}
  69.             self.nested.append (SelectElement (info))
  70.  
  71.  
  72. class Option (HTMLElement):
  73.     def __init__ (self, text, list_of_attr = {}):
  74.         print (text, list_of_attr)
  75.         HTMLElement.__init__ (self, 'option', list_of_attr)
  76.         self.nested = [HTMLElement (text, {}, True)]
  77.  
  78.  
  79. class InputElement (HTMLElement):
  80.     def __init__ (self, info):
  81.         HTMLElement.__init__ (self, 'input', info['attr'])
  82.  
  83.  
  84. class SelectElement (HTMLElement):
  85.     def __init__ (self, info):
  86.         HTMLElement.__init__ (self, 'select', info['attr'])
  87.         self.nested = [Option (text, attr) for text, attr in info['options']]
  88.  
  89.     def add_option (self, text, list_of_attr = {}):
  90.         self.options.append (Option (text, list_of_attr))
  91.  
  92.  
  93. class Image (HTMLElement):
  94.     def __init__ (self, list_of_attr = {}):
  95.         HTMLElement.__init__ (self, 'img', list_of_attr)
  96.  
  97.  
  98. class HTMLDocument:
  99.     #expects info['type'] = form/text/image
  100.     #form - a simple HTML form. In ['elements'] there is a list of elements where each element is a dictionary:
  101.     #['type'] is either 'input' or 'select', ['attr'] is the dictionary of attributes (a[2]['attr']['disabled']=true).
  102.     #For 'select' type you also have to
  103.     #specify options in ['options'], where it's a list of tuples (text, attributes), each element in the list
  104.     #describes each option.
  105.     #-------------------
  106.     #text - a block of text, can be styled, be a hyperlinked or both. In ['div_attr'] keep attributes for <div>
  107.     #tag (a['div_attr']['style']="epic"), in ['a_attr'], accodringly, attributes for <a> tag. If none for any of
  108.     #tags, no tag will be created. Keep the plain text in ['text']
  109.     #-------------------
  110.     #image - an image with <img> attributes. Keep attributes by ['attr'] key (img['attr']['src']="circle.png")
  111.     #-------------------
  112.     #br - newline, just a dictionary a['type'] = 'br' will be enough
  113.     def add_element (self, info):
  114.         if 'type' not in info:
  115.             return
  116.  
  117.         if info['type'] == 'br':
  118.             self.items.append (BR ())
  119.         elif info['type'] == 'image':
  120.             self.items.append (Image (info['attr']))
  121.         elif info['type'] == 'text':
  122.             info['div_attr'] = info['div_attr'] if 'div_attr' in info else {}
  123.             info['a_attr'] = info['a_attr'] if 'a_attr' in info else {}
  124.  
  125.             self.items.append (LinkedText (info))
  126.         elif info['type'] == 'form':
  127.             info['attr'] = info['attr'] if 'attr' in info else {}
  128.  
  129.             self.items.append (FormElement (info))
  130.  
  131.     def __init__ (self, input = []):
  132.         self.items = []
  133.         for info in input:
  134.             self.add_element (info)
  135.  
  136.     def translate_to_html (self, filename):
  137.         html = '<html>\n<head></head>\n<body>\n'
  138.         html += ''.join ([item.to_html () for item in self.items])
  139.         html += '</body></html>'
  140.         with open (filename, 'w') as f:
  141.             f.write (html)
  142.  
  143. x = HTMLDocument ()
  144.  
  145. form = {'type' : 'form'}
  146. form['elements'] = []
  147.  
  148. for i in range (3):
  149.     radio = {'type' : 'input'}
  150.     radio['attr'] = {'type' : 'radio', 'name' : 'radio'}
  151.     form['elements'].append (radio)
  152.     form['elements'].append ({'type' : 'br'})
  153.  
  154. sel = {'type' : 'select'}
  155. sel['options'] = [('Choice ' + str (i + 1), {}) for i in range (4)]
  156. form['elements'].append (sel)
  157. form['elements'].append ({'type' : 'br'})
  158.  
  159. inp = {'type' : 'input',
  160.     'attr' : {'type' : 'submit'}
  161. }
  162.  
  163. form['elements'].append (inp)
  164. form['elements'].append ({'type' : 'br'})
  165.  
  166. x.add_element (form)
  167.  
  168. x.add_element ({'type' : 'br'})
  169.  
  170. supertext = {'type' : 'text', 'text' : 'How was it?',
  171.     'a_attr' : {'href' : 'mailto:@gmail.com'}
  172.     'div_attr' : {'style' : 'link'}
  173. }
  174. x.add_element (supertext)
  175.  
  176. x.translate_to_html ('simple_form.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement