34 lines
834 B
Python
34 lines
834 B
Python
class HtmlElement:
|
|
tag = "html"
|
|
indent = " "
|
|
def __init__(self, content=None, **kwargs):
|
|
if content is None:
|
|
self.contents = []
|
|
else:
|
|
self.contents = [content]
|
|
self.attributes = kwargs
|
|
def append(self, new_content):
|
|
self.contents.append(new_content)
|
|
def render(self, cur_ind=""):
|
|
print(cur_ind + f"<{self.tag}>")
|
|
for content in self.contents:
|
|
try:
|
|
content.render(cur_ind + self.indent)
|
|
except AttributeError:
|
|
print(cur_ind + self.indent + content)
|
|
print(cur_ind + f"</{self.tag}>")
|
|
|
|
class Body(HtmlElement):
|
|
tag = "body"
|
|
|
|
class Paragraph(HtmlElement):
|
|
tag = "p"
|
|
|
|
|
|
|
|
para = Paragraph("hello")
|
|
body = Body(para)
|
|
body.append("world")
|
|
doc = HtmlElement(body)
|
|
doc.render()
|