-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path186 html doc.py
88 lines (62 loc) · 2.54 KB
/
186 html doc.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
87
88
class Tag(object):
def __init__(self, name, contents):
self.start_tag = '<{}>'.format(name)
self.end_tag = '</{}>'.format(name)
self.contents = contents
def __str__(self):
return "{0.start_tag}{0.contents}{0.end_tag}".format(self)
def display(self, file=None):
print(self, file=file)
class DocType(Tag):
def __init__(self):
super().__init__('!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
'"http://www.w3.org/TR/html4/strict.dtd', '')
self.end_tag = '' # DOCTYPE doesn't have an end_tag
class Head(Tag):
def __init__(self, title=None):
super().__init__('head', '') # contents need to be added separately
if title:
self._title_tag = Tag('title', title)
self.contents = str(self._title_tag)
class Body(Tag):
def __init__(self):
super().__init__('body', '') # contents need to be added separately
self._body_contents = []
def add_tag(self, name, contents):
new_tag = Tag(name, contents)
self._body_contents.append(new_tag)
def display(self, file=None):
for tag in self._body_contents:
self.contents += str(tag)
super().display(file=file)
class HtmlDoc(object):
def __init__(self, doc_type, head, body):
self._doc_type = doc_type
self._head = head
self._body = body
def add_tag(self, name, contents):
self._body.add_tag(name, contents)
def display(self, file=None):
self._doc_type.display(file=file)
print("<html>", file=file)
self._head.display(file=file)
self._body.display(file=file)
print("</html>", file=file)
if __name__ == "__main__":
# my_page = HtmlDoc("Strona domowa")
# my_page.add_tag("H1", "Main heading")
# my_page.add_tag("H2", "Subheading")
# my_page.add_tag("p", "Paragraph")
# with open("e:\\index.html", "w") as index:
# my_page.display(index)
new_body = Body()
new_body.add_tag("H1", "agregation")
new_body.add_tag("p", "W przeciwieństwie do <strong>compozycji </strong>"
"agregacja używa istniejących funkcji")
new_body.add_tag("p", "Objekt compozycji nie przejmuje praw nad objektem"
"z którego powstał, usuwając go nie niszczymy obiektów")
new_docType = DocType()
new_header = Head("Agregation document")
my_page = HtmlDoc(new_docType, new_header, new_body)
with open("index3.html", "w") as index:
my_page.display(file=index)