forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_render.py
More file actions
155 lines (107 loc) · 3.31 KB
/
Copy pathhtml_render.py
File metadata and controls
155 lines (107 loc) · 3.31 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
"""A class-based system for rendering html."""
# This is the framework for the base class
class Element(object):
tag = "html"
indent = " "
def __init__(self, content=None, **kwargs):
self.contents = []
if content is not None:
self.contents.append(content)
self.attributes = kwargs
def append(self, new_content):
self.contents.append(new_content)
def _open_tag(self):
if self.attributes: # only if there are any attributes
open_tag = ["<{}".format(self.tag)]
atts = [f'{key}="{value}"' for key, value in self.attributes.items()]
tag = f"<{self.tag} {' '.join(atts)}>"
else:
tag = f"<{self.tag}>"
return tag
def _close_tag(self):
return f"</{self.tag}>"
def render(self, out_file, cur_ind=""):
# loop through the list of contents:
out_file.write(cur_ind)
out_file.write(self._open_tag())
out_file.write("\n")
for content in self.contents:
try:
content.render(out_file, cur_ind + self.indent)
except AttributeError:
out_file.write(cur_ind + self.indent)
out_file.write(content)
out_file.write("\n")
out_file.write(cur_ind)
out_file.write(self._close_tag())
out_file.write("\n")
class Body(Element):
tag = 'body'
class P(Element):
tag = 'p'
class Html(Element):
tag = 'html'
def render(self, out_file, cur_ind=""):
out_file.write(cur_ind)
out_file.write("<!DOCTYPE html>\n")
super().render(out_file, cur_ind)
class Head(Element):
tag = 'head'
class OneLineTag(Element):
def append(self, content):
if self.contents:
raise TypeError("OneLineTag elements can not have content added")
else:
super().append(content)
def render(self, out_file, cur_ind=""):
out_file.write(cur_ind)
out_file.write(self._open_tag())
out_file.write(self.contents[0])
out_file.write(self._close_tag())
out_file.write("\n")
class Title(OneLineTag):
tag = "title"
class SelfClosingTag(Element):
def __init__(self, content=None, **kwargs):
if content is not None:
raise TypeError("SelfClosingTag can not contain any content")
super().__init__(content=content, **kwargs)
def render(self, out_file, cur_ind=""):
tag = self._open_tag()[:-1] + " />\n"
out_file.write(cur_ind)
out_file.write(tag)
def append(self, *args):
raise TypeError("You can not add content to a SelfClosingTag")
class Hr(SelfClosingTag):
tag = "hr"
class Br(SelfClosingTag):
tag = "br"
class A(OneLineTag):
tag = 'a'
def __init__(self, link, content=None, **kwargs):
kwargs['href'] = link
super().__init__(content, **kwargs)
class H(OneLineTag):
"""
section head
"""
tag = "H"
def __init__(self, level, *args, **kwargs):
self.tag = "h" + str(int(level))
super().__init__(*args, **kwargs)
class Ul(Element):
"""
unordered list
"""
tag = "ul"
class Li(Element):
"""
list element
"""
tag = "li"
class Meta(SelfClosingTag):
"""
metadata tag
"""
tag = "meta"