-
Notifications
You must be signed in to change notification settings - Fork 1
/
python.py
86 lines (59 loc) · 1.68 KB
/
python.py
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
from bs4 import BeautifulSoup as bs
class HTMLGenerator:
def __init__(self, content):
self.content = content
def render(self):
# convert the string to a BeautifulSoup object
soup = bs(str(self.content), 'html.parser')
# return the BeautifulSoup object
return soup.prettify()
class BaseTag:
def __init__(self, *args, **kwargs):
self.content = args
self.kwargs = kwargs
self.children = []
self.tag = self.__class__.__name__.lower()
def get_tag(self, tag):
self.tag = tag
def __str__(self):
return self.render()
def render_attributes(self):
return ' '.join(['{}="{}"'.format(k, v) for k, v in self.kwargs.items()])
def render(self):
return self.render_start() + self.render_content() + self.render_end()
def render_start(self):
return '<{} {}>'.format(self.tag, self.render_attributes())
def render_content(self):
return ''.join(map(str, self.content))
def render_end(self):
return '</{}>'.format(self.tag)
class Html(BaseTag):
pass
class Head(BaseTag):
pass
class Body(BaseTag):
pass
class Title(BaseTag):
pass
class Meta(BaseTag):
pass
class Link(BaseTag):
pass
t = HTMLGenerator(
Html(
Head(
Title(
'My first HTML page'
),
Meta(
name='description',
content='This is my first HTML page'
),
Link(
rel='stylesheet',
href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'
)
),
)
)
print(t.render())