Skip to content

Commit 64d8630

Browse files
committed
updating html for step 7 and 8
1 parent 206db36 commit 64d8630

File tree

5 files changed

+127
-40
lines changed

5 files changed

+127
-40
lines changed

students/Boundb3/Session07/html_project/html_render.py

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class Element(object):
1414
#class attributes
1515
tag = "element tag"
1616
indent = " "
17-
print("hi")
17+
print("hi from class element")
1818
#content = []
1919

2020

2121
def __init__(self, content=None, **attributes): ###!!!could i have added the link attribute here??
22+
2223
#store content
2324
self.content = []
2425

25-
2626
# add an attributes dictionary
2727
self.attributes = attributes
2828

@@ -49,7 +49,7 @@ def append(self, new_content):
4949
def render_tag(self, current_ind):
5050
# tag and then content for each class
5151
attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()])
52-
#print("this is self.attributes from render_tag: ", self.attributes)
52+
print("this is self.attributes from render_tag: ", self.attributes)
5353
#indentation + tag + content
5454
tag_str = "{}<{}{}>".format(current_ind, self.__str__(), attrs)
5555
#print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
@@ -99,6 +99,10 @@ class P (Element):
9999
class Html (Element):
100100
tag = "html"
101101
print("subclass tag = : ",tag)
102+
103+
def render(self, file_out, current_ind= ""):
104+
file_out.write("<!DOCTYPE html>\n")
105+
super().render(file_out,current_ind = current_ind)
102106
pass
103107

104108
class Head (Element):
@@ -164,15 +168,65 @@ class Br (SelfClosingTag):
164168
tag = "br"
165169
pass
166170

167-
class A (Element):
171+
class A (OneLineTag):
168172
tag = "a"
169173

174+
def __init__(self, link, content=None, **attributes):
175+
#self.attributes = "href="
176+
#contentstr = "href=" + '"'+content+'"> ' + link
177+
178+
179+
Element.__init__(self,content, **attributes) #!!! not quite right - needs the format: <a href="http://google.com"> link </a>
180+
self.attributes["href"] = link #class guided answer - THANK you!!
181+
#print("a's link is:", self.link)
182+
print("a's content **** is:", self.content)
183+
print("a's kwrgs extract of href** is:", self.attributes["href"])
184+
185+
186+
187+
def render(self, file_out, current_ind= " default indent"):
188+
print("entering a's rendering func ")
189+
tag_str = '{}<{}{}"{}"> '.format(current_ind, self.__str__(),"href=", self.attributes.get("href"))
190+
file_out.write(tag_str)
191+
192+
193+
for con in self.content:
194+
try:
195+
#render it
196+
print("\t--> con in A is: " , con)
197+
file_out.write(con) #was: stuff_str.render(file_out)
198+
except TypeError as e:
199+
print("hit a snag: ", e, "con is: ", con)
200+
con.render(file_out, current_ind + self.indent ) # was: .write(str(stuff_str)) ### watch it if it is self.tag
201+
202+
#write out closing tag
203+
#was:
204+
#stop at the closing html tag
205+
#end_tag = "</{}>".format(self.tag)
206+
#add that tag to the end of the file object
207+
#file_out.write(end_tag)
208+
file_out.write(" </{}>\n".format(self.tag))
170209

171210

172-
def __init__(self, content=None, link="link",**attributes):
173-
self.attributes = "href="
174-
contentstr = "href=" + '"'+content+'">' + link
175-
super().__init__(contentstr,**attributes) #!!! not quite right - needs the format: <a href="http://google.com"> link </a>
211+
212+
#to over write the default __str__
213+
def __str__(self):
214+
return self.ToString()
215+
216+
def ToString(self):
217+
return "a "
218+
219+
220+
# def render_tag(self, current_ind):
221+
# # tag and then content for each class
222+
# attrs = "".join([' {} ="{}"'.format(key,val) for key, val in self.attributes.items()])
223+
# #print("this is self.attributes from render_tag: ", self.attributes)
224+
# #indentation + tag + content
225+
# tag_str = "{}<{}{}".format(current_ind, self.__str__(), attrs) #removed the extra ">" from this tag render
226+
# #print("from render_tag: current_ind is '{}' and tag strg is '{}'".format(current_ind, tag_str))
227+
# print("a's tag string is",tag_str," and self.string is:", self.__str__())
228+
# return tag_str
229+
176230

177231
#self.link = link
178232
pass
@@ -192,3 +246,12 @@ def __init__(self,header,content = None,**attributes):
192246
self.header = switchdict[int(header)]
193247
self.tag = self.header
194248
super().__init__(content,**attributes)
249+
250+
class Meta(SelfClosingTag):
251+
tag = "meta"
252+
253+
def __init__(self,content=None, **attributes):
254+
#dfault for charset
255+
if "charset" not in attributes:
256+
attributes["charset"] = "UTF-8"
257+
SelfClosingTag.__init__(self,content,**attributes)

students/Boundb3/Session07/html_project/run_html_render.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def render_page(page, filename):
130130
'''
131131
# # Step 6
132132
# #########
133-
133+
'''
134134
page = hr.Html()
135135
136136
head = hr.Head()
@@ -152,7 +152,7 @@ def render_page(page, filename):
152152
page.append(body)
153153
154154
render_page(page, "test_html_output6.html")
155-
155+
'''
156156
# # Step 7
157157
# #########
158158
'''
@@ -188,43 +188,43 @@ def render_page(page, filename):
188188
189189
page.append(body)
190190
191-
render_page(page, "test_html_output7.html")
192-
'''
191+
render_page(page, "test_html_output7a.html")
192+
193193
# # Step 8
194194
# ########
195-
196-
# page = hr.Html()
195+
'''
196+
page = hr.Html()
197197

198198

199-
# head = hr.Head()
200-
# head.append( hr.Meta(charset="UTF-8") )
201-
# head.append(hr.Title("PythonClass = Revision 1087:"))
199+
head = hr.Head()
200+
head.append( hr.Meta(charset="UTF-8") )
201+
head.append(hr.Title("PythonClass = Revision 1087:"))
202202

203-
# page.append(head)
203+
page.append(head)
204204

205-
# body = hr.Body()
205+
body = hr.Body()
206206

207-
# body.append( hr.H(2, "PythonClass - Class 6 example") )
207+
body.append( hr.H(2, "PythonClass - Class 6 example") )
208208

209-
# body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
210-
# style="text-align: center; font-style: oblique;"))
209+
body.append(hr.P("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text",
210+
style="text-align: center; font-style: oblique;"))
211211

212-
# body.append(hr.Hr())
212+
body.append(hr.Hr())
213213

214-
# list = hr.Ul(id="TheList", style="line-height:200%")
214+
list = hr.Ul(id="TheList", style="line-height:200%")
215215

216-
# list.append( hr.Li("The first item in a list") )
217-
# list.append( hr.Li("This is the second item", style="color: red") )
216+
list.append( hr.Li("The first item in a list") )
217+
list.append( hr.Li("This is the second item", style="color: red") )
218218

219-
# item = hr.Li()
220-
# item.append("And this is a ")
221-
# item.append( hr.A("http://google.com/", "link") )
222-
# item.append("to google")
219+
item = hr.Li()
220+
item.append("And this is a ")
221+
item.append( hr.A("http://google.com/", "link") )
222+
item.append("to google")
223223

224-
# list.append(item)
224+
list.append(item)
225225

226-
# body.append(list)
226+
body.append(list)
227227

228-
# page.append(body)
228+
page.append(body)
229229

230-
# render_page(page, "test_html_output8.html")
230+
render_page(page, "test_html_output8.html")

students/Boundb3/Session07/html_project/test_html_output6.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
</p>
99
<hr /> {}
1010
And this is a
11-
<a>
12-
href="http://google.com">link
13-
</a>
11+
<a href="http://google.com"> link </a>
1412
to google
1513
</body>
1614
</html>

students/Boundb3/Session07/html_project/test_html_output7.html renamed to students/Boundb3/Session07/html_project/test_html_output7a.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!DOCTYPE html>
12
<html>
23
<head>
34
<title> PythonClass = Revision 1087: </title>
@@ -17,9 +18,7 @@ <h2> PythonClass - Class 6 example </h2>
1718
</li>
1819
<li>
1920
And this is a
20-
<a>
21-
href="http://google.com">, link
22-
</a>
21+
<a href="http://google.com"> link </a>
2322
to google
2423
</li>
2524
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta /> {'charset': 'UTF-8'}
5+
<title> PythonClass = Revision 1087: </title>
6+
</head>
7+
<body>
8+
<h2> PythonClass - Class 6 example </h2>
9+
<p style ="text-align: center; font-style: oblique;">
10+
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
11+
</p>
12+
<hr /> {}
13+
<ul style ="line-height:200%" id ="TheList">
14+
<li>
15+
The first item in a list
16+
</li>
17+
<li style ="color: red">
18+
This is the second item
19+
</li>
20+
<li>
21+
And this is a
22+
<a href="http://google.com"> link </a>
23+
to google
24+
</li>
25+
</ul>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)