-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLAB_instuctions.txt
More file actions
134 lines (84 loc) · 4.87 KB
/
LAB_instuctions.txt
File metadata and controls
134 lines (84 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Instructions for html renderer exercise:
Goal: a set of classes to render html pages.
we'll try to get to all the features required to render:
sample_html.html
Step 1:
Create an "Element" class for rendering an html element (xml element).
It should have class attributes for the tag name ("html" first) and the
indentation (spaces to indent for pretty printing)
the constructor signature should look like:
Element(content=None)
where content is a string
It should have an "append" method that can add another string to the content
It should have a render(file_out, ind = "") method that renders the tag
and the strings in the content.
file_out could be any file-like object (i.e have a write() method ).
"ind" is a string with the indentation level in it -- i.e the amount that
the tag should be indented (for pretty printing). (maybe 4 spaces per level).
The amount of indentation should be set by the class attribute: "indent"
you can test with sys.stdout to print to the console, and/or use a
cStringIO.sStringIO object to store it in a string - or pass a file
you should now be able to render an html tag with text in it as contents.
Step 2:
Create a couple subclasses of Element, for a <body> tag and <p> tag. All you
should have to do is override the "tag" class attribute.
Now you can render a few different types of element.
Extend the Element.render() method so that it can render other elements
inside the tag in addition to strings. Simple recursion should
do it. i.e. it can call the render() method of the elements it contains.
figure out a way to deal with the fact the the contents elements could be
either simple strings or Elements with render methods...(there are a few
ways to handle that...)
You should now be able to render a basic web page with an html tag around
the whole thing, and body tag inside, and multiple <p> tags inside that,
with text inside that
Step 3:
Create a <head> element -- simple subclass.
Create a OneLineTag subclass of Element:
It should override the render method, to render everything on one line --
for the simple tags, like:
<title> PythonClass - Class 6 example </title>
Create a Title subclass of OneLineTag class for the title.
You should now be able to render an html doc with a head element, with a
title element in that, and a body element with some <P> elements and some text.
Step 4:
Extend the Element class to accept a set of attributes as keywords to the
constructor, ie.:
Element("some text content", id="TheList", style="line-height:200%")
( remember **kwargs ? )
The render method will need to be extended to render the attributes properly.
You can now render some <p> tags (and others) with attributes
Step 5:
Create a "SelfClosingTag" subclass of Element, to render tags like:
<hr /> and <br /> (horizontal rule and line break).
You will need to override the render method to render just the one tag and
attributes, if any.
Create a couple subclasses of SelfClosingTag for and <hr /> and <br />
Step 6:
Create a A class for an anchor (link) element. Its constructor should look like:
A(self, link, content) -- where link is the link, and content is what you see. It can be called like so:
A("http://google.com", "link")
You should be able to subclass from Element, and only override the __init__
- Calling the Element __init__ from the A __init__
You can now add a link to your web page.
Step 7:
Create Ul class for an unordered list (really simple subclass of Element)
Create Li class for an element in a list (also really simple)
add a list to your web page.
Create a Header class -- this one should take an integer argument for the
header level. i.e <h1>, <h2>, <h3>, called like:
H(2, "The text of the header") for an <h2> header
It can subclass from OneLineTag -- overriding the __init__, then calling
the superclass __init__
Step 8:
Update the Html element class to render the "<!DOCTYPE html>" tag at the
head of the page, before the html element.
You can do this by subclassing Element, overriding render(), but then
calling the Element render from the new render.
Create a subclass of SelfClosingTag for <meta charset="UTF-8" /> (like
for <hr /> and <br /> and add the meta element to the beginning of
the head element to give your document an encoding.
The doctype and encoding are HTML 5 and you can check this at:
validator.w3.org.
You now have a pretty full-featured html renderer -- play with it, add some
new tags, etc....Maybe integrate it into your http server...