-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
80 lines (61 loc) · 2 KB
/
Copy pathbuilder.py
File metadata and controls
80 lines (61 loc) · 2 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
class HtmlElement:
indent_size = 2
def __init__(self, name="", text=""):
self.name = name
self.text = text
self.elements = []
def __str(self, indent):
lines = []
i = " " * (indent * self.indent_size)
lines.append(f"{i}<{self.name}>")
if self.text:
i1 = " " * ((indent + 1) * self.indent_size)
lines.append(f"{i1}{self.text}")
for e in self.elements:
lines.append(e.__str(indent + 1))
lines.append(f"{i}</{self.name}>")
return "\n".join(lines)
def __str__(self):
return self.__str(0)
@staticmethod
def create(name):
return HtmlBuilder(name)
class HtmlBuilder:
__root = HtmlElement()
def __init__(self, root_name):
self.root_name = root_name
self.__root.name = root_name
# not fluent
def add_child(self, child_name, child_text):
self.__root.elements.append(HtmlElement(child_name, child_text))
# fluent
def add_child_fluent(self, child_name, child_text):
self.__root.elements.append(HtmlElement(child_name, child_text))
return self
def clear(self):
self.__root = HtmlElement(name=self.root_name)
def __str__(self):
return str(self.__root)
# if you want to build a simple HTML paragraph using a list
hello = "hello"
parts = ["<p>", hello, "</p>"]
print("".join(parts))
# now I want an HTML list with 2 words in it
words = ["hello", "world"]
parts = ["<ul>"]
for w in words:
parts.append(f" <li>{w}</li>")
parts.append("</ul>")
print("\n".join(parts))
# ordinary non-fluent builder
# builder = HtmlBuilder('ul')
builder = HtmlElement.create("ul")
builder.add_child("li", "hello")
builder.add_child("li", "world")
print("Ordinary builder:")
print(builder)
# fluent builder
builder.clear()
builder.add_child_fluent("li", "hello").add_child_fluent("li", "world")
print("Fluent builder:")
print(builder)