Repository navigation
Expand file tree
/
Copy pathhtml_render.py
More file actions
106 lines (69 loc) · 2.33 KB
/
Copy pathhtml_render.py
File metadata and controls
106 lines (69 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
"""
html render code
"""
class Element(object):
tag = 'html'
indent = ' '
def __init__(self, content=None, **attributes):
self.content = []
self.attributes = attributes
if content is not None:
self.content.append(content)
def append(self, content):
self.content.append(content)
def render_tag(self, current_ind):
attrs = "".join([' {}="{}"'.format(key,val) for key, val in self.attributes.items()])
tag_str = "{}<{}{}>".format(current_ind, self.tag, attrs)
return tag_str
def render(self, file_out, current_ind=""):
file_out.write(self.render_tag(current_ind))
file_out.write('\n')
#print "in render:"
#print self.content
for con in self.content:
#print "trying to render:", con
try:
file_out.write( current_ind + self.indent + con+"\n")
except TypeError:
con.render(file_out, current_ind+self.indent)
file_out.write("{}</{}>\n".format(current_ind,self.tag))
class OneLineTag(Element):
def render(self, file_out, current_ind=""):
file_out.write(self.render_tag(current_ind))
for con in self.content:
file_out.write(con)
file_out.write("</{}>\n".format(self.tag))
class Title(OneLineTag):
tag = 'title'
class SelfClosingTag(Element):
def render(self, file_out, current_ind=""):
file_out.write(self.render_tag(current_ind)[:-1])
file_out.write(" />\n")
class Hr(SelfClosingTag):
tag = 'hr'
class A(OneLineTag):
tag = 'a'
def __init__(self, link, content=None, **attributes):
OneLineTag.__init__(self, content, **attributes)
self.attributes["href"] = link
class H(OneLineTag):
def __init__(self, level, content=None, **attributes):
OneLineTag.__init__(self, content, **attributes)
self.tag = "h%i"%(level)
# H(2, "The text of the header")
class Ul(Element):
tag = 'ul'
class Li(Element):
tag = 'li'
class Html(Element):
tag = 'html'
def render(self, file_out, current_ind=""):
file_out.write("<!DOCTYPE html>\n")
Element.render(self, file_out, current_ind=current_ind)
class Body(Element):
tag = 'body'
class P(Element):
tag = 'p'
class Head(Element):
tag = 'head'