-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathhtml_render.py
More file actions
257 lines (193 loc) · 8.51 KB
/
html_render.py
File metadata and controls
257 lines (193 loc) · 8.51 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
"""
B3 version copy - start
Python class example.
"""
# The start of it all:
# Fill it all in here.
class Element(object):
#class attributes
tag = "element tag"
indent = " "
#print("hi from class element")
#content = []
def __init__(self, content=None, **attributes): ###!!!could i have added the link attribute here??
#store content
self.content = []
# add an attributes dictionary
self.attributes = attributes
#check for content
if content is not None:
self.content.append(content)
print("there was content in init. init appended content = {} to self = {} ".format(content,self.tag))
print("\t\tcompleted init call for:", self.tag, "\n" )
def ToString(self):
return self.tag
#to over write the default __str__
def __str__(self):
return self.ToString()
def append(self, new_content):
#add new string content to content list
print("\t\tcalling append function \n appending: appending {} to {} ".format(new_content, self.tag ))
if new_content is not "":
self.content.append(new_content)
#new render tag method
def render_tag(self, current_ind):
# tag and then content for each class
attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()])
print("this is self.attributes from render_tag: ", self.attributes)
#indentation + tag + content
tag_str = "{}<{}{}>".format(current_ind, self.__str__(), attrs)
#print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
return tag_str
def render(self, file_out, current_ind= " default indent"):
print("entering rendering func ")
#print("this is current ind: ", current_ind)
#print("this is self.tag '{}' inside render func and this is self.__str__ '{}' : ".format(self.tag, self.__str__()))
#now render will call the render tag menthod - instead of just a string defined tag
file_out.write(self.render_tag(current_ind)) # the problem here is that the current_ind attribute is carrying text of: html, or htmlbody inside the attribute!!!
print("just finished call to render_tag")
file_out.write("\n")
for con in self.content:
try:
#render it
print("\t--> con is: " , con)
file_out.write(current_ind + self.indent + con+"\n") #was: stuff_str.render(file_out)
except TypeError as e:
print("hit a snag: ", e, "con is: ", con)
con.render(file_out, current_ind + self.indent) # was: .write(str(stuff_str)) ### watch it if it is self.tag
#write out closing tag
#was:
#stop at the closing html tag
#end_tag = "</{}>".format(self.tag)
#add that tag to the end of the file object
#file_out.write(end_tag)
file_out.write("{}</{}>\n".format(current_ind, self.tag))
class Body (Element):
tag = "body"
print("subclass tag = : ",tag)
pass
class P (Element):
#print(super.tag) Why does this not work? (to print out the super's tag)
tag = "p"
print("subclass tag = : ",tag)
pass
class Html (Element):
tag = "html"
print("subclass tag = : ",tag)
def render(self, file_out, current_ind= ""):
file_out.write("<!DOCTYPE html>\n")
super().render(file_out,current_ind = current_ind)
pass
class Head (Element):
tag = "head"
pass
class OneLineTag (Element):
def render(self, file_out, current_ind= " default indent"):
print("entering OneLineTag rendering func ")
#now render will call the render tag menthod
file_out.write(self.render_tag(current_ind))
#print("just finished call to render_tag")
#file_out.write("\n") # B3 - this is not needed in this render subclass
for con in self.content:
try:
#render it
print("\t--> con is: " , con)
file_out.write(current_ind + self.indent + con ) #b-3 removed +"\n" from the write instructions to stay on the same line
except TypeError as e:
print("hit a snag: ", e, "con is: ", con)
con.render(file_out, current_ind + self.indent) # was: .write(str(stuff_str)) ### watch it if it is self.tag
#write out closing tag
file_out.write("{}</{}>\n".format(current_ind, self.tag))
class Title (OneLineTag):
tag = "title"
pass
class SelfClosingTag (Element):
def render(self, file_out, current_ind= " default indent"):
print("entering SelfClosingTag rendering func ")
#write out closing tag
file_out.write("{}<{} /> {}\n".format(current_ind, self.tag, self.attributes))
'''
#comment out this section
#now render will call the render tag menthod
#file_out.write(self.render_tag(current_ind))
#print("just finished call to render_tag")
#file_out.write("\n") # B3 - this is not needed in this render subclass
for con in self.content:
try:
#render it
print("\t--> con is: " , con)
file_out.write(current_ind + self.indent + con) #b-3 removed +"\n" from the write instructions to stay on the same line
except TypeError as e:
print("hit a snag: ", e, "con is: ", con)
con.render(file_out, current_ind + self.indent) # was: .write(str(stuff_str)) ### watch it if it is self.tag
'''
class Hr (SelfClosingTag):
tag = "hr"
pass
class Br (SelfClosingTag):
tag = "br"
pass
class A (OneLineTag):
tag = "a"
def __init__(self, link, content=None, **attributes):
#self.attributes = "href="
#contentstr = "href=" + '"'+content+'"> ' + link
Element.__init__(self,content, **attributes) #!!! not quite right - needs the format: <a href="http://google.com"> link </a>
self.attributes["href"] = link #class guided answer - THANK you!!
#print("a's link is:", self.link)
print("a's content **** is:", self.content)
print("a's kwrgs extract of href** is:", self.attributes["href"])
def render(self, file_out, current_ind= " default indent"):
print("entering a's rendering func ")
tag_str = '{}<{}{}"{}"> '.format(current_ind, self.__str__(),"href=", self.attributes.get("href"))
file_out.write(tag_str)
for con in self.content:
try:
#render it
print("\t--> con in A is: " , con)
file_out.write(con) #was: stuff_str.render(file_out)
except TypeError as e:
print("hit a snag: ", e, "con is: ", con)
con.render(file_out, current_ind + self.indent ) # was: .write(str(stuff_str)) ### watch it if it is self.tag
#write out closing tag
#was:
#stop at the closing html tag
#end_tag = "</{}>".format(self.tag)
#add that tag to the end of the file object
#file_out.write(end_tag)
file_out.write(" </{}>\n".format(self.tag))
#to over write the default __str__
def __str__(self):
return self.ToString()
def ToString(self):
return "a "
# def render_tag(self, current_ind):
# # tag and then content for each class
# attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()])
# #print("this is self.attributes from render_tag: ", self.attributes)
# #indentation + tag + content
# tag_str = "{}<{}{}".format(current_ind, self.__str__(), attrs) #removed the extra ">" from this tag render
# #print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
# print("a's tag string is",tag_str," and self.string is:", self.__str__())
# return tag_str
#self.link = link
pass
class Ul (Element):
tag = "ul"
class Li (Element):
tag = "li"
class H (OneLineTag):
tag = "header"
def __init__(self,header,content = None,**attributes):
switchdict = {1:"h1",2:"h2",3:"h3"}
self.header = switchdict[int(header)]
self.tag = self.header
super().__init__(content,**attributes)
class Meta(SelfClosingTag):
tag = "meta"
def __init__(self,content=None, **attributes):
#dfault for charset
if "charset" not in attributes:
attributes["charset"] = "UTF-8"
SelfClosingTag.__init__(self,content,**attributes)