From 64d86302681cd166b8f1e585ae3048e365ebacf2 Mon Sep 17 00:00:00 2001
From: BBB
Date: Sun, 6 Mar 2016 18:40:38 -0800
Subject: [PATCH 1/4] updating html for step 7 and 8
---
.../Session07/html_project/html_render.py | 79 +++++++++++++++++--
.../Session07/html_project/run_html_render.py | 52 ++++++------
.../html_project/test_html_output6.html | 4 +-
...l_output7.html => test_html_output7a.html} | 5 +-
.../html_project/test_html_output8.html | 27 +++++++
5 files changed, 127 insertions(+), 40 deletions(-)
rename students/Boundb3/Session07/html_project/{test_html_output7.html => test_html_output7a.html} (89%)
create mode 100644 students/Boundb3/Session07/html_project/test_html_output8.html
diff --git a/students/Boundb3/Session07/html_project/html_render.py b/students/Boundb3/Session07/html_project/html_render.py
index e051c53..4901154 100644
--- a/students/Boundb3/Session07/html_project/html_render.py
+++ b/students/Boundb3/Session07/html_project/html_render.py
@@ -14,15 +14,15 @@ class Element(object):
#class attributes
tag = "element tag"
indent = " "
- print("hi")
+ 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
@@ -49,7 +49,7 @@ def append(self, new_content):
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)
+ 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))
@@ -99,6 +99,10 @@ class P (Element):
class Html (Element):
tag = "html"
print("subclass tag = : ",tag)
+
+ def render(self, file_out, current_ind= ""):
+ file_out.write("\n")
+ super().render(file_out,current_ind = current_ind)
pass
class Head (Element):
@@ -164,15 +168,65 @@ class Br (SelfClosingTag):
tag = "br"
pass
-class A (Element):
+class A (OneLineTag):
tag = "a"
+ def __init__(self, link, content=None, **attributes):
+ #self.attributes = "href="
+ #contentstr = "href="https://github.com/+'"'+content+'"> ' + link
+
+
+ Element.__init__(self,content, **attributes) #!!! not quite right - needs the format: link
+ 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="https://github.com/,%20self.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))
- def __init__(self, content=None, link="link",**attributes):
- self.attributes = "href="
- contentstr = "href="https://github.com/+'"'+content+'">' + link
- super().__init__(contentstr,**attributes) #!!! not quite right - needs the format: link
+
+ #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
@@ -192,3 +246,12 @@ def __init__(self,header,content = None,**attributes):
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)
\ No newline at end of file
diff --git a/students/Boundb3/Session07/html_project/run_html_render.py b/students/Boundb3/Session07/html_project/run_html_render.py
index 0eae206..713cb86 100644
--- a/students/Boundb3/Session07/html_project/run_html_render.py
+++ b/students/Boundb3/Session07/html_project/run_html_render.py
@@ -130,7 +130,7 @@ def render_page(page, filename):
'''
# # Step 6
# #########
-
+'''
page = hr.Html()
head = hr.Head()
@@ -152,7 +152,7 @@ def render_page(page, filename):
page.append(body)
render_page(page, "test_html_output6.html")
-
+'''
# # Step 7
# #########
'''
@@ -188,43 +188,43 @@ def render_page(page, filename):
page.append(body)
-render_page(page, "test_html_output7.html")
-'''
+render_page(page, "test_html_output7a.html")
+
# # Step 8
# ########
-
-# page = hr.Html()
+'''
+page = hr.Html()
-# head = hr.Head()
-# head.append( hr.Meta(charset="UTF-8") )
-# head.append(hr.Title("PythonClass = Revision 1087:"))
+head = hr.Head()
+head.append( hr.Meta(charset="UTF-8") )
+head.append(hr.Title("PythonClass = Revision 1087:"))
-# page.append(head)
+page.append(head)
-# body = hr.Body()
+body = hr.Body()
-# body.append( hr.H(2, "PythonClass - Class 6 example") )
+body.append( hr.H(2, "PythonClass - Class 6 example") )
-# 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",
-# style="text-align: center; font-style: oblique;"))
+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",
+ style="text-align: center; font-style: oblique;"))
-# body.append(hr.Hr())
+body.append(hr.Hr())
-# list = hr.Ul(id="TheList", style="line-height:200%")
+list = hr.Ul(id="TheList", style="line-height:200%")
-# list.append( hr.Li("The first item in a list") )
-# list.append( hr.Li("This is the second item", style="color: red") )
+list.append( hr.Li("The first item in a list") )
+list.append( hr.Li("This is the second item", style="color: red") )
-# item = hr.Li()
-# item.append("And this is a ")
-# item.append( hr.A("http://google.com/", "link") )
-# item.append("to google")
+item = hr.Li()
+item.append("And this is a ")
+item.append( hr.A("http://google.com/", "link") )
+item.append("to google")
-# list.append(item)
+list.append(item)
-# body.append(list)
+body.append(list)
-# page.append(body)
+page.append(body)
-# render_page(page, "test_html_output8.html")
+render_page(page, "test_html_output8.html")
diff --git a/students/Boundb3/Session07/html_project/test_html_output6.html b/students/Boundb3/Session07/html_project/test_html_output6.html
index 2ef6bd2..b9f7657 100644
--- a/students/Boundb3/Session07/html_project/test_html_output6.html
+++ b/students/Boundb3/Session07/html_project/test_html_output6.html
@@ -8,9 +8,7 @@
{}
And this is a
-
- href="http://google.com/">link
-
+ link
to google