My favorites
|
Sign in
pyoohtml
PyOoHtml - Python Object Oriented HTML
Project Home
Downloads
Source
Checkout
|
Browse
|
Changes
|
r13
Source path:
svn
/
trunk
/
pyoohtml.py
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
"""
PyOoHtml - Python Object Oriented HTML (version 2.0.0 - 2009.04.10)
Public domain (P) 2009 Davide Rognoni
DAVIDE ROGNONI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS, IN NO EVENT SHALL DAVIDE ROGNONI BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
E-mail: davide.rognoni@gmail.com
Web Site: http://pyoohtml.appspot.com/v2
"""
import cgi, types
class Node:
TYPE_COMMENT = "comment"
TYPE_HTML = "html"
TYPE_ROOT = "root"
TYPE_TEXT = "text"
def __init__(self, type=TYPE_ROOT, parent=None):
self.type = type
self.parent = parent
self.child = None
self.name = None
self.chars = None
self.isInline = False
self.isOpen = False
self.attributes = {}
self.nodes = []
def text(self, text, inline=True, escape=True):
n = Node(Node.TYPE_TEXT, self)
self.nodes.append(n)
text = str(text)
if escape:
text = cgi.escape(text)
n.chars = text
n.isInline = inline
return self
def html(self, html, inline=True):
return self.text(html, inline, False)
def comment(self, comment=None):
n = Node(Node.TYPE_COMMENT, self)
self.nodes.append(n)
if comment:
n.text(comment)
self.child = n
return self
else:
return n
def open(self, open=True):
self.isOpen = open
return self
def inline(self, inline=True):
self.isInline = inline
return self
def tag(self, name, *args):
'''name, text, attributes'''
n = Node(Node.TYPE_HTML, self)
self.nodes.append(n)
n.name = name
if len(args) == 1 and type(args[0]) == types.DictType:
n.attributes = args[0]
elif len(args) == 2 and type(args[1]) == types.DictType:
n.attributes = args[1]
if len(args) > 0 and type(args[0]) != types.DictType:
n.isInline = True
n.text(str(args[0]))
self.child = n
return self
else:
return n
def toHtml(self):
return self.__html(0, self.nodes)
def __html(self, level, nodes):
tabs = " "*level*4
out = ""
for n in nodes:
if not n.isInline:
out += "\n%s" % (tabs)
if n.type == Node.TYPE_COMMENT:
out += "<!-- %s -->" % (self.__html(level+1, n.nodes))
elif n.type == Node.TYPE_TEXT:
out += "%s" % (n.chars)
elif n.type == Node.TYPE_HTML:
if len(n.nodes) == 0:
open = "/"
if n.isOpen:
open = ""
out += "<%s%s%s>" % (n.name,
self.__attribs(n.attributes), open)
else:
out += "<%s%s>%s</%s>" % (
n.name, self.__attribs(n.attributes),
self.__html(level+1, n.nodes), n.name)
return out
def __attribs(self, a):
out = ""
for k in a:
if a[k] == True:
out += ' %s' % (k)
elif a[k] == False:
continue
else:
out += ' %s="%s"' % (k, a[k])
return out
def __tabs(self, node, tabs):
if not node.isInline:
return "\n%s" % (tabs)
return ""
TAGS = ["a","abbr","acronym","address","applet","area","b","base","basefont",
"bdo","big","blockquote","body","br","button","caption","center","cite","code",
"col","colgroup","dd","del","dir","div","dfn","dl","dt","em","fieldset","font",
"form","frame","frameset","h1","h2","h3","h4","h5","h6","head","hr","html","i",
"iframe","img","input","ins","isindex","kbd","label","legend","li","link","map",
"menu","meta","noframes","noscript","object","ol","optgroup","option","p",
"param","pre","q","s","samp","script","select","small","span","strike","strong",
"style","sub","sup","table","tbody","td","textarea","tfoot","th","thead",
"title","tr","tt","u","ul","var","xmp"]
for t in TAGS:
funName = t.upper()
exec '''\
def %(funName)s(self, *args):
return self.tag("%(tagName)s", *args)
Node.__dict__["%(funName)s"] = %(funName)s''' % ({"funName": funName, "tagName": t})
Show details
Hide details
Change log
r2
by davide.rognoni on Apr 12, 2009
Diff
[No log message]
Go to:
/trunk/pyoohtml.py
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 3911 bytes, 147 lines
View raw file
Hosted by