Skip to content

Added Session 08 Material #214

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 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
cc37de8
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Nov 28, 2018
40e5f10
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 1, 2018
1418b30
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 3, 2018
d514b18
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 4, 2018
65ac898
Added circle exercise for Session08
Zach-Cooper Dec 4, 2018
e726799
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 5, 2018
4494a60
Updated sorted circle part of circle.py
Zach-Cooper Dec 5, 2018
9f06b63
Finally finished html rendering exercise for session07
Zach-Cooper Dec 7, 2018
4f17d4e
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 10, 2018
a165834
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 11, 2018
6925f0c
Delete html_render.py
Zach-Cooper Dec 11, 2018
3456eeb
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 12, 2018
c42b2ea
Merge branch 'master' of https://github.com/UWPCE-PythonCert-ClassRep…
Zach-Cooper Dec 13, 2018
6f614a4
Added oo_mailroom exercise. Could use some input on the DataCollectio…
Zach-Cooper Dec 14, 2018
d5d2464
Merge branch 'master' of https://github.com/Zach-Cooper/Fall2018-PY210A
Zach-Cooper Dec 14, 2018
0c9cbfd
Realized I deleted my html_render in github so here it is. Thanks
Zach-Cooper Dec 14, 2018
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
142 changes: 142 additions & 0 deletions students/ZachCooper/session07/html_render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python3

import pytest


class Element(object):

tag = "html"
indent = " "

def __init__(self, content=None, **kwargs):
#for key, value in kwargs.items():
#setattr(self, style, value)
# self.style = "text-align: center"
# self.id = "intro"
if content is None:
self.contents = []
else:
self.contents = [content]
self.attributes = kwargs

def append(self, new_content):
self.contents.append(new_content)

def _open_tag(self):
if self.attributes: # only if there are any attributes
open_tag = ["<{}".format(self.tag)]
atts = [f'{key}="{value}"' for key, value in self.attributes.items()]
tag = f"<{self.tag} {' '.join(atts)}>"
else:
tag = f"<{self.tag}>"
return tag

def _close_tag(self):
return f"</{self.tag}>"

def render(self, out_file, cur_ind=""):
out_file.write(cur_ind)
out_file.write(self._open_tag())
out_file.write("\n")
for content in self.contents:
try:
content.render(out_file, cur_ind + self.indent)
content.render(out_file)
except AttributeError:
out_file.write(cur_ind + self.indent)
out_file.write(content)
out_file.write("\n")
out_file.write(cur_ind)
out_file.write(self._close_tag())
out_file.write("\n")


class Html(Element):
tag = "html"

def render(self, out_file, cur_ind=""):
out_file.write(cur_ind + "<!DOCTYPE html>\n")
super().render(out_file, cur_ind)


class Body(Element):
tag = "body"


class P(Element):
tag = "p"


class Head(Element):
tag = "head"


class OneLineTag(Element):

def append(self, content):
if self.contents:
raise TypeError("OneLineTag elements can't have content added")
else:
super().append(content)

def render(self, out_file, cur_ind=""):
out_file.write(cur_ind)
out_file.write(self._open_tag())
out_file.write(self.contents[0])
out_file.write(self._close_tag())
out_file.write("\n")


class Title(OneLineTag):
tag = "title"


class SelfClosingTag(Element):

def __init__(self, content=None, **kwargs):
if content is not None:
raise TypeError("SelfClosingTag cannot contain any content")
super().__init__(content=content, **kwargs)

def render(self, out_file, cur_ind=""):
tag = self._open_tag()[:-1] + " />\n"
out_file.write(cur_ind)
out_file.write(tag)

def append(self, *args):
raise TypeError("You cannot add content to a SelfClosingTag")


class Hr(SelfClosingTag):
tag = "hr"


class Br(SelfClosingTag):
tag = "br"


class A(OneLineTag):
tag = "a"

def __init__(self, link, content=None, **kwargs):
kwargs['href'] = link
super().__init__(content, **kwargs)


class H(OneLineTag):
def __init__(self, num, content=None, **kwargs):
header_tag = "h" + str(num)
super().__init__(content, **kwargs)
self.tag = header_tag


class Ul(Element):
tag = "ul"


class Li(Element):
tag = "li"


class Meta(SelfClosingTag):
tag = "meta"
Loading