|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +#import io |
| 4 | + |
| 5 | +class Element(object): |
| 6 | +# tag_indent = {'html':0,'head':4,'meta':8,'title':8,'body':4,'h2':8,'p':8,'hr':8,'ul':8,'li':12} |
| 7 | + tag = 'html' |
| 8 | + indent = ' ' |
| 9 | +# content =[] |
| 10 | + |
| 11 | + def __init__ (self,content=None,**attributes): |
| 12 | + self.cont = [] |
| 13 | + self.attributes = attributes |
| 14 | + if content is not None: |
| 15 | + self.cont.append(content) |
| 16 | + |
| 17 | + def content_add(self,content): |
| 18 | + self.cont.append(content) |
| 19 | + |
| 20 | + def render_tag(self,current_ind): |
| 21 | + attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()]) |
| 22 | + tag_str = "{}<{}{}>".format(current_ind, self.tag, attrs) |
| 23 | + return tag_str |
| 24 | + |
| 25 | + def render(self, file_out,ind = ""): |
| 26 | + file_out.write(self.render_tag(current_ind)) |
| 27 | + file_out.write('\n') |
| 28 | + for con in self.content: |
| 29 | + try: |
| 30 | + file_out.write(current_ind +self.indent+con+'\n') |
| 31 | + except TypeError: |
| 32 | + con.render(file_out, current_ind+self.indent) |
| 33 | + file_out.write("{}</{}>\n".format(current_ind, self.tag)) |
| 34 | + |
| 35 | +class Html(Element): |
| 36 | + def __init__ (self,content=None,**attributes): |
| 37 | + self.tag = 'html' |
| 38 | + |
| 39 | + def render(self, file_out,ind = ""): |
| 40 | + file_out.write('<!DOCTYPE html>') |
| 41 | + super(Html,self).render(self, file_out,ind = "") |
| 42 | + |
| 43 | + |
| 44 | +class Body(Element): |
| 45 | + def __init__ (self,content=None,**attributes): |
| 46 | + self.tag = 'body' |
| 47 | + |
| 48 | +class P(Element): |
| 49 | + def __init__ (self,content=None,**attributes): |
| 50 | + self.tag = 'p' |
| 51 | + |
| 52 | +class Head(Element): |
| 53 | + def __init__ (self,content=None,**attributes): |
| 54 | + self.tag = 'head' |
| 55 | + |
| 56 | +class OneLineTag(Element): |
| 57 | + def render(self, file_out,ind = ""): |
| 58 | + self.render_tag(current_ind) |
| 59 | + file_out.write('<{}>{}'.format(self.tag,current_ind)) |
| 60 | + try: |
| 61 | + file_out.write(current_ind +self.indent+self.content) |
| 62 | + except TypeError: |
| 63 | + con.render(file_out, current_ind+self.indent) |
| 64 | + file_out.write("{}</{}>\n".format(current_ind, self.tag)) |
| 65 | + |
| 66 | +class Title(OneLineTag): |
| 67 | + def __init__ (self,content=None,**attributes): |
| 68 | + self.tag = 'title' |
| 69 | + |
| 70 | +class SelfClosingTag(Element): |
| 71 | + |
| 72 | + def render(self, file_out,ind = ""): |
| 73 | + self.render_tag(current_ind) |
| 74 | + file_out.write('<{}{}/>\n'.format(self.tag,current_ind)) |
| 75 | + |
| 76 | +class meta(SelfClosingTag): |
| 77 | + def render(self, file_out,ind = ""): |
| 78 | + self.render_tag(current_ind) |
| 79 | + file_out.write('<{}{}'.format('meta',current_ind)) |
| 80 | + try: |
| 81 | + file_out.write(current_ind +self.indent+self.content) |
| 82 | + except TypeError: |
| 83 | + con.render(file_out, current_ind+self.indent) |
| 84 | + file_out.write("{}/>\n".format(current_ind)) |
| 85 | + |
| 86 | +class Hr(SelfClosingTag): |
| 87 | + def __init__ (self,content=None,**attributes): |
| 88 | + self.tag = 'hr' |
| 89 | + |
| 90 | + |
| 91 | +class Br(SelfClosingTag): |
| 92 | + def __init__ (self,content=None,**attributes): |
| 93 | + self.tag = 'br' |
| 94 | + |
| 95 | +class A(Element): |
| 96 | + def A__init__ (self,link,content=None): |
| 97 | + super(A, self).__init__(content=None,href=link) |
| 98 | + |
| 99 | +class Ul(Element): |
| 100 | + def __init__ (sself,content=None,**attributes): |
| 101 | + self.tag = 'ul' |
| 102 | + |
| 103 | + |
| 104 | +class Li(Element): |
| 105 | + def __init__ (self,content=None,**attributes): |
| 106 | + self.tag = 'li' |
| 107 | + |
| 108 | +class Header(OneLineTag): |
| 109 | + def __init__(self,hl,content=None): |
| 110 | + self.tag = 'h' + hl |
| 111 | + super(Header,self).__init__(content=None,**attributes) |
| 112 | + |
| 113 | + |
0 commit comments