Skip to content

Commit 784551b

Browse files
committed
added solution to html render assignment.
1 parent b578891 commit 784551b

4 files changed

Lines changed: 557 additions & 4 deletions

File tree

Solutions/Session06/html_render.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
html render code
5+
"""
6+
7+
8+
class Element(object):
9+
10+
tag = 'html'
11+
indent = ' '
12+
13+
def __init__(self, content=None, **attributes):
14+
15+
self.content = []
16+
self.attributes = attributes
17+
18+
if content is not None:
19+
self.content.append(content)
20+
21+
def append(self, content):
22+
self.content.append(content)
23+
24+
def render_tag(self, current_ind):
25+
attrs = "".join([' {}="{}"'.format(key,val) for key, val in self.attributes.items()])
26+
tag_str = "{}<{}{}>".format(current_ind, self.tag, attrs)
27+
return tag_str
28+
29+
def render(self, file_out, current_ind=""):
30+
file_out.write(self.render_tag(current_ind))
31+
file_out.write('\n')
32+
#print "in render:"
33+
#print self.content
34+
for con in self.content:
35+
#print "trying to render:", con
36+
try:
37+
file_out.write( current_ind + self.indent + con+"\n")
38+
except TypeError:
39+
con.render(file_out, current_ind+self.indent)
40+
file_out.write("{}</{}>\n".format(current_ind,self.tag))
41+
42+
43+
class OneLineTag(Element):
44+
def render(self, file_out, current_ind=""):
45+
file_out.write(self.render_tag(current_ind))
46+
for con in self.content:
47+
file_out.write(con)
48+
file_out.write("</{}>\n".format(self.tag))
49+
50+
51+
class Title(OneLineTag):
52+
tag = 'title'
53+
54+
55+
class SelfClosingTag(Element):
56+
def render(self, file_out, current_ind=""):
57+
file_out.write(self.render_tag(current_ind)[:-1])
58+
file_out.write(" />\n")
59+
60+
61+
class Hr(SelfClosingTag):
62+
tag = 'hr'
63+
64+
65+
class A(OneLineTag):
66+
tag = 'a'
67+
def __init__(self, link, content=None, **attributes):
68+
OneLineTag.__init__(self, content, **attributes)
69+
self.attributes["href"] = link
70+
71+
72+
class H(OneLineTag):
73+
def __init__(self, level, content=None, **attributes):
74+
OneLineTag.__init__(self, content, **attributes)
75+
self.tag = "h%i"%(level)
76+
77+
# H(2, "The text of the header")
78+
79+
80+
class Ul(Element):
81+
tag = 'ul'
82+
83+
84+
class Li(Element):
85+
tag = 'li'
86+
87+
88+
class Html(Element):
89+
tag = 'html'
90+
def render(self, file_out, current_ind=""):
91+
file_out.write("<!DOCTYPE html>\n")
92+
Element.render(self, file_out, current_ind=self.indent)
93+
94+
95+
class Body(Element):
96+
tag = 'body'
97+
98+
99+
class P(Element):
100+
tag = 'p'
101+
102+
103+
class Head(Element):
104+
tag = 'head'
105+
106+
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
a simple script can run and test your html rendering classes.
5+
6+
Uncomment the steps as you add to your rendering.
7+
8+
"""
9+
10+
from cStringIO import StringIO
11+
12+
13+
# importing the html_rendering code with a short name for easy typing.
14+
import html_render as hr
15+
reload(hr) # reloading in case you are running this in iPython
16+
# -- we want to make sure the latest version is used
17+
18+
19+
## writing the file out:
20+
def render_page(page, filename):
21+
"""
22+
render the tree of elements
23+
24+
This uses cSstringIO to render to memory, then dump to console and
25+
write to file -- very handy!
26+
"""
27+
28+
f = StringIO()
29+
page.render(f, " ")
30+
31+
f.reset()
32+
33+
print f.read()
34+
35+
f.reset()
36+
open(filename, 'w').write( f.read() )
37+
38+
39+
## Step 1
40+
##########
41+
42+
# page = hr.Element()
43+
44+
# page.append("Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text")
45+
46+
# page.append("And here is another piece of text -- you should be able to add any number")
47+
48+
# render_page(page, "test_html_output1.html")
49+
50+
# ## Step 2
51+
# ##########
52+
53+
# page = hr.Html()
54+
55+
# body = hr.Body()
56+
57+
# 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"))
58+
59+
# body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
60+
61+
# page.append(body)
62+
63+
# render_page(page, "test_html_output2.html")
64+
65+
# # Step 3
66+
# ##########
67+
68+
# page = hr.Html()
69+
70+
# head = hr.Head()
71+
# head.append(hr.Title("PythonClass = Revision 1087:"))
72+
73+
# page.append(head)
74+
75+
# body = hr.Body()
76+
77+
# 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"))
78+
# body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
79+
80+
# page.append(body)
81+
82+
# render_page(page, "test_html_output3.html")
83+
84+
# # Step 4
85+
# ##########
86+
87+
# page = hr.Html()
88+
89+
# head = hr.Head()
90+
# head.append(hr.Title("PythonClass = Revision 1087:"))
91+
92+
# page.append(head)
93+
94+
# body = hr.Body()
95+
96+
# 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",
97+
# style="text-align: center; font-style: oblique;"))
98+
99+
# page.append(body)
100+
101+
# render_page(page, "test_html_output4.html")
102+
103+
# # Step 5
104+
# #########
105+
106+
# page = hr.Html()
107+
108+
# head = hr.Head()
109+
# head.append(hr.Title("PythonClass = Revision 1087:"))
110+
111+
# page.append(head)
112+
113+
# body = hr.Body()
114+
115+
# 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",
116+
# style="text-align: center; font-style: oblique;"))
117+
118+
# body.append(hr.Hr())
119+
120+
# page.append(body)
121+
122+
# render_page(page, "test_html_output5.html")
123+
124+
# # Step 6
125+
# #########
126+
127+
# page = hr.Html()
128+
129+
# head = hr.Head()
130+
# head.append(hr.Title("PythonClass = Revision 1087:"))
131+
132+
# page.append(head)
133+
134+
# body = hr.Body()
135+
136+
# 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",
137+
# style="text-align: center; font-style: oblique;"))
138+
139+
# body.append(hr.Hr())
140+
141+
# body.append("And this is a ")
142+
# body.append( hr.A("http://google.com", "link") )
143+
# body.append("to google")
144+
145+
# page.append(body)
146+
147+
# render_page(page, "test_html_output6.html")
148+
149+
# # Step 7
150+
# #########
151+
152+
# page = hr.Html()
153+
154+
# head = hr.Head()
155+
# head.append(hr.Title("PythonClass = Revision 1087:"))
156+
157+
# page.append(head)
158+
159+
# body = hr.Body()
160+
161+
# body.append( hr.H(2, "PythonClass - Class 6 example") )
162+
163+
# 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",
164+
# style="text-align: center; font-style: oblique;"))
165+
166+
# body.append(hr.Hr())
167+
168+
# list = hr.Ul(id="TheList", style="line-height:200%")
169+
170+
# list.append( hr.Li("The first item in a list") )
171+
# list.append( hr.Li("This is the second item", style="color: red") )
172+
173+
# item = hr.Li()
174+
# item.append("And this is a ")
175+
# item.append( hr.A("http://google.com", "link") )
176+
# item.append("to google")
177+
178+
# list.append(item)
179+
180+
# body.append(list)
181+
182+
# page.append(body)
183+
184+
# render_page(page, "test_html_output7.html")
185+
186+
# # Step 8
187+
# ########
188+
189+
page = hr.Html()
190+
191+
192+
head = hr.Head()
193+
head.append( hr.Meta(charset="UTF-8") )
194+
head.append(hr.Title("PythonClass = Revision 1087:"))
195+
196+
page.append(head)
197+
198+
body = hr.Body()
199+
200+
body.append( hr.H(2, "PythonClass - Class 6 example") )
201+
202+
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",
203+
style="text-align: center; font-style: oblique;"))
204+
205+
body.append(hr.Hr())
206+
207+
list = hr.Ul(id="TheList", style="line-height:200%")
208+
209+
list.append( hr.Li("The first item in a list") )
210+
list.append( hr.Li("This is the second item", style="color: red") )
211+
212+
item = hr.Li()
213+
item.append("And this is a ")
214+
item.append( hr.A("http://google.com", "link") )
215+
item.append("to google")
216+
217+
list.append(item)
218+
219+
body.append(list)
220+
221+
page.append(body)
222+
223+
render_page(page, "test_html_output8.html")
224+
225+
226+
227+

0 commit comments

Comments
 (0)