-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlComponent.py
88 lines (63 loc) · 2.65 KB
/
HtmlComponent.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
import logging
from HtmlSource import HtmlSource
class HtmlComponent:
def __init__(self, componentType, contents="", classes=[]):
self.componentType = componentType
self.contents = contents
self.classes = classes
self.indentation = 0
self.subelements = []
self.hasRendered = False
if componentType is "html":
self.head = HtmlComponent("head")
self.add(self.head)
def render(self):
if self.contents is None:
self.contents = ""
for subelement in self.subelements:
self.contents += "\n" + self._properIndent(subelement.render(), self.indentation)
classPortion = ""
if self.classes is None:
self.classes = []
elif not self.classes == []:
classPortion = " class=\"" + " ".join(str(x) for x in self.classes) + "\""
self.hasRendered = True
for i in range(len(self.subelements)):
self.subelements[i].render()
componentBody = ""
if not self.contents == "":
componentBody = "\n\t" + self.contents + "\n"
return "<" + self.componentType + classPortion + ">" + componentBody + "</" + self.componentType + ">\n"
def appendToContents(self, contents):
if self.contents is None:
self.contents = ""
self.contents += contents
self.hasRendered = False
def clearContents(self):
self.contents = ""
self.hasRendered = False
def setClasses(self, classes):
self.classes = classes
self.hasRendered = False
def add(self, html_component):
if self.contents is None:
self.contents = ""
if self.hasRendered:
logging.warning("Parent component has already been rendered, so added component will not be visible.")
html_component.indentation = self.indentation + 1
self.subelements.append(html_component)
self.hasRendered = False
def addSource(self, filelink):
dependency = HtmlSource(filelink)
self.head.appendToContents(dependency.render())
self.hasRendered = False
def addManualSource(self, manualLine):
dependency = HtmlSource(None, manualLine)
self.head.add(dependency)
self.hasRendered = False
def _properIndent(self, componentString, indentationLevel):
individualComponentLines = componentString.split("\n")
tabs = "\t"*indentationLevel
for i in range(len(individualComponentLines) - 1):
individualComponentLines[i] = tabs + individualComponentLines[i]
return "\n".join(str(x) for x in individualComponentLines)