-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_1.py
More file actions
62 lines (48 loc) · 1.56 KB
/
gen_1.py
File metadata and controls
62 lines (48 loc) · 1.56 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
"""
Python class example.
This module does some more consolidating...
"""
class Element(object):
"""
An element with multiple items in the content
"""
tag = "html"
indent = " "
def __init__(self, content=None):
"""
initialize an element 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
example:
"""
if not content:
self.children = []
else:
self.children = [content]
def append(self, element):
self.children.append(element)
def render(self, file_out, ind = ""):
"""
an html rendering method for elements that have content
"""
file_out.write("\n")
file_out.write(ind)
file_out.write("<%s"%self.tag)
file_out.write(">")
for child in self.children:
file_out.write("\n")
file_out.write(ind + self.indent)
file_out.write(child)
file_out.write("\n")
file_out.write(ind)
file_out.write('</%s>'%self.tag)
if __name__ == "__main__":
import sys, cStringIO
page = Element()
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")
f = cStringIO.StringIO()
page.render(f)
f.reset()
print f.read()
f.reset()
open("test_html.html", 'w').write(f.read())