-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml_gen.py
More file actions
264 lines (222 loc) · 6.88 KB
/
html_gen.py
File metadata and controls
264 lines (222 loc) · 6.88 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
Python class example.
Set of classes to build HTML
This version adds a decorator to create a <p> element around any function
that returns a string
"""
class Element(object):
"""
An element with optional attributes and multiple items in the content
"""
tag = ""
indent = " "
def __init__(self, content=None, **attributes):
"""
initialize an element with optional attributes, and any number of sub-elements and content
:param content: content of the element: single string or another element.
an empty string will be ignored
:param [attributes]: optional attributes as keyword parameters.
example:
"""
if not content:
self.children = []
else:
self.children = [content]
self.attributes = attributes
def append(self, element):
self.children.append(element)
def render(self, file_out, ind = ""):
"""
an html rendering method for elements that have attributes and content
"""
file_out.write("\n")
file_out.write(ind)
file_out.write("<%s"%self.tag)
for key, value in self.attributes.items():
file_out.write(' %s="%s"'%(key, value) )
file_out.write(">")
for child in self.children:
try:
child.render(file_out, ind + self.indent)
except AttributeError:
file_out.write("\n")
file_out.write(ind + self.indent)
file_out.write(str(child))
file_out.write("\n")
file_out.write(ind)
file_out.write('</%s>'%self.tag)
class Html(Element):
tag = "html"
## override the render method to add the "<!DOCTYPE html>"
def render(self, file_out, ind = ""):
file_out.write("<!DOCTYPE html>")
# call the superclass render:
Element.render(self, file_out, ind)
class Head(Element):
tag = "head"
class Body(Element):
tag = "body"
class P(Element):
tag = "p"
class Ul(Element):
"""
element for an unordered list
"""
tag = "ul"
class Li(Element):
"""
element for the item in a list
"""
tag = "li"
class SelfClosingTag(Element):
"""
Element with a single tag -- no content, only attributes
"""
def __init__(self, **attributes):
self.attributes = attributes
def render(self, file_out, ind = ""):
"""
an html rendering method for self-closing elements:
attributes, but no content a no closing tag
"""
file_out.write("\n")
file_out.write(ind)
file_out.write("<%s"%self.tag)
for key, value in self.attributes.items():
file_out.write(' %s="%s"'%(key, value) )
file_out.write(" />")
class Meta(SelfClosingTag):
tag = "meta"
class Hr(SelfClosingTag):
tag = "hr"
class OneLineTag(Element):
def render(self, file_out, ind = ""):
"""
an html rendering method for elements that have attributes and content
"""
file_out.write("\n")
file_out.write(ind)
file_out.write("<%s"%self.tag)
for key, value in self.attributes.items():
file_out.write(' %s="%s"'%(key, value) )
file_out.write(">")
for child in self.children:
try:
child.render(file_out)
except AttributeError:
file_out.write(str(child))
file_out.write('</%s>'%self.tag)
class Title(OneLineTag):
tag = "title"
class A(OneLineTag):
"""
element for a link ( <a> tag )
"""
tag = "a"
def __init__(self, link, content):
OneLineTag.__init__(self, content, href=link)
class H(OneLineTag):
"""
class for header tags, the level is specified in a parameter
"""
def __init__(self, level, content, **attributes):
OneLineTag.__init__(self, content, **attributes)
self.tag = "h%i"%level
######
## making a decorator
#def make_a_paragraph(func, *args, **kwargs):
def make_a_paragraph(func):
"""
decorator example -- wrap anything that creates a string in a <P> tag
"""
print "in make_a_paragraph"
def wrapper(*args, **kwargs):
"""
make_a_paragraph decorator
Takes any function that returns a string, and wraps it in a P element
class for rendering an html paragraph
"""
func.__doc__ = "a doctring"
print "in wrapper", kwargs
return P( func(*args), **kwargs )
return wrapper
@make_a_paragraph
def simple_string():
"""
An example function that returns a hard-coded string
"""
return "this is a really simple string"
@make_a_paragraph
def simple_string_from_numbers(limit):
"""
An example function that prints some text from a bunch of numbers
"""
msg = []
for i in range(limit):
msg.append('the next number is: %i ...'%i)
return " ".join(msg)
@make_a_paragraph
def random_sentence(num_words):
"""
An example function that generates random nonsence words
"""
import random
import string
msg = []
for i in xrange(num_words):
length = random.randint(1,10)
word = "".join(random.sample(string.lowercase, length))
msg.append(word)
msg[0] = msg[0].capitalize()
msg[-1] = msg[-1] + "."
return " ".join(msg)
if __name__ == "__main__":
import sys, cStringIO
# page = Html()
#
# head = Head()
# head.append( Meta(charset="UTF-8") )
# head.append(Title("PythonClass = Revision 1087:"))
#
# page.append(head)
#
# body = Body()
#
# body.append( H(2, "PythonClass - Class 6 example") )
#
# body.append(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())
#
# list = Ul(id="TheList", style="line-height:200%")
# list.append( Li("The first item in a list") )
# list.append( Li("This is the second item", style="color: red") )
# item = Li()
# item.append("And this is a ")
# item.append( A("http://google.com", "link") )
# item.append("to google")
# list.append(item)
# body.append(list)
#
# page.append(body)
#
# f = cStringIO.StringIO()
#
# page.render(f)
#
# f.reset()
# print f.read()
#
# f.reset()
# open("test_html.html", 'w').write(f.read())
# Test the @make_a_paragraph decorator
import sys
p = P("some text")
p.render(sys.stdout)
el = simple_string(font="large")
el.render(sys.stdout)
el = simple_string_from_numbers(3)
el.render(sys.stdout)
el = random_sentence(10)
el.render(sys.stdout)