-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimpletree.py
More file actions
executable file
·291 lines (244 loc) · 8.69 KB
/
simpletree.py
File metadata and controls
executable file
·291 lines (244 loc) · 8.69 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from __future__ import absolute_import
from . import _base
from html5lib.constants import voidElements, namespaces, prefixes
from xml.sax.saxutils import escape
# Really crappy basic implementation of a DOM-core like thing
class Node(_base.Node):
type = -1
def __init__(self, name):
self.name = name
self.parent = None
self.value = None
self.childNodes = []
self._flags = []
__init__.func_annotations = {}
def __iter__(self):
for node in self.childNodes:
yield node
for item in node:
yield item
__iter__.func_annotations = {}
def __unicode__(self):
return self.name
__unicode__.func_annotations = {}
def toxml(self):
raise NotImplementedError
toxml.func_annotations = {}
def printTree(self, indent=0):
tree = u'\n|%s%s' % (u' '* indent, unicode(self))
for child in self.childNodes:
tree += child.printTree(indent + 2)
return tree
printTree.func_annotations = {}
def appendChild(self, node):
assert isinstance(node, Node)
if (isinstance(node, TextNode) and self.childNodes and
isinstance(self.childNodes[-1], TextNode)):
self.childNodes[-1].value += node.value
else:
self.childNodes.append(node)
node.parent = self
appendChild.func_annotations = {}
def insertText(self, data, insertBefore=None):
assert isinstance(data, unicode), u"data %s is of type %s expected unicode"%(repr(data), type(data))
if insertBefore is None:
self.appendChild(TextNode(data))
else:
self.insertBefore(TextNode(data), insertBefore)
insertText.func_annotations = {}
def insertBefore(self, node, refNode):
index = self.childNodes.index(refNode)
if (isinstance(node, TextNode) and index > 0 and
isinstance(self.childNodes[index - 1], TextNode)):
self.childNodes[index - 1].value += node.value
else:
self.childNodes.insert(index, node)
node.parent = self
insertBefore.func_annotations = {}
def removeChild(self, node):
try:
self.childNodes.remove(node)
except:
# XXX
raise
node.parent = None
removeChild.func_annotations = {}
def cloneNode(self):
raise NotImplementedError
cloneNode.func_annotations = {}
def hasContent(self):
u"""Return true if the node has children or text"""
return bool(self.childNodes)
hasContent.func_annotations = {}
def getNameTuple(self):
if self.namespace == None:
return namespaces[u"html"], self.name
else:
return self.namespace, self.name
getNameTuple.func_annotations = {}
nameTuple = property(getNameTuple)
class Document(Node):
type = 1
def __init__(self):
Node.__init__(self, None)
__init__.func_annotations = {}
def __unicode__(self):
return u"#document"
__unicode__.func_annotations = {}
def appendChild(self, child):
Node.appendChild(self, child)
appendChild.func_annotations = {}
def toxml(self, encoding=u"utf=8"):
result = u""
for child in self.childNodes:
result += child.toxml()
return result.encode(encoding)
toxml.func_annotations = {}
def hilite(self, encoding=u"utf-8"):
result = u"<pre>"
for child in self.childNodes:
result += child.hilite()
return result.encode(encoding) + u"</pre>"
hilite.func_annotations = {}
def printTree(self):
tree = unicode(self)
for child in self.childNodes:
tree += child.printTree(2)
return tree
printTree.func_annotations = {}
def cloneNode(self):
return Document()
cloneNode.func_annotations = {}
class DocumentFragment(Document):
type = 2
def __unicode__(self):
return u"#document-fragment"
__unicode__.func_annotations = {}
def cloneNode(self):
return DocumentFragment()
cloneNode.func_annotations = {}
class DocumentType(Node):
type = 3
def __init__(self, name, publicId, systemId):
Node.__init__(self, name)
self.publicId = publicId
self.systemId = systemId
__init__.func_annotations = {}
def __unicode__(self):
if self.publicId or self.systemId:
publicId = self.publicId or u""
systemId = self.systemId or u""
return u"""<!DOCTYPE %s "%s" "%s">"""%(
self.name, publicId, systemId)
else:
return u"<!DOCTYPE %s>" % self.name
__unicode__.func_annotations = {}
toxml = __unicode__
def hilite(self):
return u'<code class="markup doctype"><!DOCTYPE %s></code>' % self.name
hilite.func_annotations = {}
def cloneNode(self):
return DocumentType(self.name, self.publicId, self.systemId)
cloneNode.func_annotations = {}
class TextNode(Node):
type = 4
def __init__(self, value):
Node.__init__(self, None)
self.value = value
__init__.func_annotations = {}
def __unicode__(self):
return u"\"%s\"" % self.value
__unicode__.func_annotations = {}
def toxml(self):
return escape(self.value)
toxml.func_annotations = {}
hilite = toxml
def cloneNode(self):
assert isinstance(self.value, unicode)
return TextNode(self.value)
cloneNode.func_annotations = {}
class Element(Node):
type = 5
def __init__(self, name, namespace=None):
Node.__init__(self, name)
self.namespace = namespace
self.attributes = {}
__init__.func_annotations = {}
def __unicode__(self):
if self.namespace == None:
return u"<%s>" % self.name
else:
return u"<%s %s>"%(prefixes[self.namespace], self.name)
__unicode__.func_annotations = {}
def toxml(self):
result = u'<' + self.name
if self.attributes:
for name,value in self.attributes.items():
result += u' %s="%s"' % (name, escape(value,{u'"':u'"'}))
if self.childNodes:
result += u'>'
for child in self.childNodes:
result += child.toxml()
result += u'</%s>' % self.name
else:
result += u'/>'
return result
toxml.func_annotations = {}
def hilite(self):
result = u'<<code class="markup element-name">%s</code>' % self.name
if self.attributes:
for name, value in self.attributes.items():
result += u' <code class="markup attribute-name">%s</code>=<code class="markup attribute-value">"%s"</code>' % (name, escape(value, {u'"':u'"'}))
if self.childNodes:
result += u">"
for child in self.childNodes:
result += child.hilite()
elif self.name in voidElements:
return result + u">"
return result + u'</<code class="markup element-name">%s</code>>' % self.name
hilite.func_annotations = {}
def printTree(self, indent):
tree = u'\n|%s%s' % (u' '*indent, unicode(self))
indent += 2
if self.attributes:
for name, value in sorted(self.attributes.items()):
if isinstance(name, tuple):
name = u"%s %s"%(name[0], name[1])
tree += u'\n|%s%s="%s"' % (u' ' * indent, name, value)
for child in self.childNodes:
tree += child.printTree(indent)
return tree
printTree.func_annotations = {}
def cloneNode(self):
newNode = Element(self.name, self.namespace)
for attr, value in self.attributes.items():
newNode.attributes[attr] = value
return newNode
cloneNode.func_annotations = {}
class CommentNode(Node):
type = 6
def __init__(self, data):
Node.__init__(self, None)
self.data = data
__init__.func_annotations = {}
def __unicode__(self):
return u"<!-- %s -->" % self.data
__unicode__.func_annotations = {}
def toxml(self):
return u"<!--%s-->" % self.data
toxml.func_annotations = {}
def hilite(self):
return u'<code class="markup comment"><!--%s--></code>' % escape(self.data)
hilite.func_annotations = {}
def cloneNode(self):
return CommentNode(self.data)
cloneNode.func_annotations = {}
class TreeBuilder(_base.TreeBuilder):
documentClass = Document
doctypeClass = DocumentType
elementClass = Element
commentClass = CommentNode
fragmentClass = DocumentFragment
def testSerializer(self, node):
return node.printTree()
testSerializer.func_annotations = {}