forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_document.py
43 lines (39 loc) · 1.37 KB
/
html_document.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
"A HTML document concrete class of AbstractDocument"
from abstract_document import AbstractDocument
class HTMLDocument(AbstractDocument):
"Prints out a HTML formatted document"
@staticmethod
def title(document):
document["title"] = "New HTML Document"
@staticmethod
def text(document, text):
"Putting multiple lines into there own p tags"
lines = text.splitlines()
markup = ""
for line in lines:
markup = markup + " <p>" + f"{line}</p>\n"
document["text"] = markup[:-1]
@staticmethod
def print(document):
"overriding print to output with html tags"
print("<html>")
print(" <head>")
for attribute in document:
if attribute in ["title", "description", "author"]:
print(
f" <{attribute}>{document[attribute]}"
f"</{attribute}>"
)
if attribute == "background_colour":
print(" <style>")
print(" body {")
print(
f" background-color: "
f"{document[attribute]};")
print(" }")
print(" </style>")
print(" </head>")
print(" <body>")
print(f"{document['text']}")
print(" </body>")
print("</html>")