Skip to content

HTML EXCERCISE #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions Examples/Session07/html_render/html_render.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
#!/usr/bin/env python

#Krishna Bindhu
#Date:3/2/2016
"""
Python class example.
"""


#calss ELEMENT
# The start of it all:
# Fill it all in here.
class Element(object):
"""class attributes"""
tag ='<html>'
indent=''
"""class methods"""
def __init__(self, content=""):
self.content=[]
if content is not None:
self.content.append(content)
def append(self, content):
self.content.append(content)
def render(self, file_out,ind=''):
file_out.write(ind*4+self.tag)
file_out.write('\n')
file_out.write(str(self.content))
file_out.write('\n')
file_out.write(ind*4+"<\"+self.tag+">'')

"""
#subclass html
class Html(Element):
tag='<html>'
def __init__(self, content=""):
super().__init__(content)

def __init__(self, content=None):
pass
def append(self, new_content):
pass
def render(self, file_out, ind=""):
file_out.write("just something as a place holder...")


class Body(Element):
tag='<body>'
def __init__(self, content=""):
super().__init__(content)
def render(self, file_out,ind=''):
file_out.write(ind*8+self.tag+"\n")
file_out.write(self.content)
file_out.write('\n')
file_out.write(ind*8+"<\body>")

class P(Element):
tag='<p>'
def __init__(self, content=""):
super().__init__(content)
def append(self, content):
super(P,self).append(content)
def render(self, file_out,ind=''):
file_out.write(ind*12+self.tag+"\n")
file_out.write(ind*16+self.content)
file_out.write('\n')
file_out.write(ind*8+"<\p>")
"""
15 changes: 7 additions & 8 deletions Examples/Session07/html_render/run_html_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ def render_page(page, filename):

page.append("And here is another piece of text -- you should be able to add any number")

render_page(page, "test_html_output1.html")
render_page(page, "test_html_output1_kr.html")

# ## Step 2
# ##########

# page = hr.Html()

# body = hr.Body()
#page = hr.Html()

# 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"))
#body = hr.Body()

# body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
#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"))

# page.append(body)
#body.append(hr.P("And here is another piece of text -- you should be able to add any number"))
#page.append(body)

# render_page(page, "test_html_output2.html")
#render_page(page, "test_html_output2_krhtml")

# # Step 3
# ##########
Expand Down
8 changes: 3 additions & 5 deletions Examples/Session07/html_render/test_html_output1.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

<html>
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some text
And here is another piece of text -- you should be able to add any number
</html>
<html>
Here is a paragraph of text -- there could be more of them, but this is enough to show that we can do some textAnd here is another piece of text -- you should be able to add any number
<\html>
136 changes: 136 additions & 0 deletions students/kbindhu/session08/html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env python
#Krishna Bindhu
#Date:3/2/2016
"""
Python class example.
"""

#calss ELEMENT
# The start of it all:
# Fill it all in here.

class Element(object):
"""class attributes"""
tag ='html'
"""class methods"""
def __init__(self, content1=None,**kwargs):
#if nothing passed content is an empty list
self.content=[]
self.kwargs=kwargs
if content1 is not None:
#appending strings/objects passed to list
self.content.append(content1)
def append(self, content1):
self.content.append(content1)
def render(self, file_out,ind=' '):
start_tag="<{}>".format(self.tag)
# this for step4 adding additional arguments for tags
for k,v in self.kwargs.items():
start_tag="<{} {}={}>".format(self.tag,k,v)
file_out.write(ind+start_tag)
file_out.write('\n')
# iterating through the list
for i in range(0,len(self.content)):
#checking the member in a list is a string
#if string add 4 spaces and string
if (isinstance(self.content[i], str)):
file_out.write(ind + ' ' + self.content[i])
file_out.write('\n')
else:
# call recursively the render if the content appended is a class and add 4 spaces
self.content[i].render(file_out,ind + ' ')
file_out.write('\n')
end_tag="</{}>".format(self.tag)
file_out.write(ind+end_tag)


#subclass HTML

class Html(Element):
#overiding tag
tag='html'
def render(self, file_out,ind=' '):
file_out.write("<!DOCTYPE html>\n")
super().render(file_out,ind=" ")
#subclass HTML
class Body(Element):
#overiding tag
tag='body'

class P(Element):
#overiding tag
tag='p'
#subclass Head
class Head(Element):
#overiding tag
tag='head'
#One lineTag subclass
class OneLineTag(Element):
#overiding the render method to print content in one line
def render(self, file_out,ind=' '):
start_tag="<{}>".format(self.tag)
file_out.write(ind+start_tag)
# iterating through the list
for i in range(0,len(self.content)):
#checking the member in a list is a string
if (isinstance(self.content[i], str)):
file_out.write(self.content[i])
else:
# call recursively the render if the content appended is a class
self.content[i].render(file_out,ind)
end_tag="</{}>".format(self.tag)
file_out.write(end_tag)
file_out.write('\n')

#subclass Title
class Title(OneLineTag):
tag='title'

#SelfClosing Tag subclass
class SelfClosingTag(Element):
def __init__(self, content=None, **kwargs):
super().__init__(content, **kwargs)
#overirding render method to render end tag alone
def render(self, file_out,ind=' '):
#checking whether arguments are empty(this is done for self closing tags br and hr where there are no arguments)
if(self.kwargs is None):
end_tag="<{} />".format(self.tag)
file_out.write(ind+end_tag)
#this is for meta class wnen there ia argument being passed
else:
for k,v in self.kwargs.items():
end_tag="<{} {}= {}/>".format(self.tag,k,v)
file_out.write(ind+end_tag)
#subclass Hr and Br
class Hr(SelfClosingTag):
tag='hr'
class Hr(SelfClosingTag):
tag='br'

#Anchor class
class A(Element):
tag='a'
def __init__(self, content,link):
super().__init__(content)
#passing link as keywrod argument to element class
self.kwargs["href"]=link

#subclass Unordered List
class Ul(Element):
tag="ul"

#ordered List
class Li(Element):
tag="li"
#subclass H
class H(OneLineTag):
def __init__(self,number,content):
self.tag="h"+str(number)
super().__init__(content)
#subclass Meta
class Meta(SelfClosingTag):
tag='meta'
def __init__(self,content=None,**kwargs):
super().__init__(content)
self.kwargs['charset']="UTF-8"

Loading