comparison emeraldtree/tree.py @ 56:644bf7185b57

Tree - Make Element writable
author Bastian Blank <bblank@thinkmo.de>
date Tue, 27 Oct 2009 15:47:37 +0100
parents 6d2aaba7ac4d
children 2461a04b352e
comparison
equal deleted inserted replaced
55:125ce968352d 56:644bf7185b57
367 if isinstance(e, Element): 367 if isinstance(e, Element):
368 for s in e.itertext(): 368 for s in e.itertext():
369 yield s 369 yield s
370 elif isinstance(e, basestring): 370 elif isinstance(e, basestring):
371 yield e 371 yield e
372
373 def write(self, write, encoding=None, namespaces={}, method=None):
374 if not method or method == "xml":
375 Writer = XMLWriter
376 elif method == "html":
377 Writer = HTMLWriter
378 else:
379 Writer = TextWriter
380
381 Writer(encoding, namespaces).write(write, self)
382
372 383
373 ## 384 ##
374 # Subelement factory. This function creates an element instance, and 385 # Subelement factory. This function creates an element instance, and
375 # appends it to an existing element. 386 # appends it to an existing element.
376 # <p> 387 # <p>
1317 empty_elements = frozenset(("area", "base", "basefont", "br", "col", "frame", "hr", 1328 empty_elements = frozenset(("area", "base", "basefont", "br", "col", "frame", "hr",
1318 "img", "input", "isindex", "link", "meta" "param")) 1329 "img", "input", "isindex", "link", "meta" "param"))
1319 1330
1320 def __init__(self, encoding=None, namespaces={}): 1331 def __init__(self, encoding=None, namespaces={}):
1321 namespaces["http://www.w3.org/1999/xhtml"] = '' 1332 namespaces["http://www.w3.org/1999/xhtml"] = ''
1322 super(HTTPWriter, self).__init__(encoding, namespaces) 1333 super(HTMLWriter, self).__init__(encoding, namespaces)
1323 1334
1324 def serialize(self, write, elem, qnames, namespaces={}): 1335 def serialize(self, write, elem, qnames, namespaces={}):
1325 if isinstance(elem, Element): 1336 if isinstance(elem, Element):
1326 tag = qnames[elem.tag] 1337 tag = qnames[elem.tag]
1327 1338
1353 1364
1354 if tag.lower() in ('script', 'style'): 1365 if tag.lower() in ('script', 'style'):
1355 write(self._encode(''.join(elem.itertext()))) 1366 write(self._encode(''.join(elem.itertext())))
1356 else: 1367 else:
1357 for e in elem: 1368 for e in elem:
1358 self.serialize(write, e, encoding, qnames) 1369 self.serialize(write, e, qnames)
1359 1370
1360 if tag not in HTML_EMPTY: 1371 if tag not in self.empty_elements:
1361 write("</" + tag + ">") 1372 write("</" + tag + ">")
1362 1373
1363 else: 1374 else:
1364 for e in elem: 1375 for e in elem:
1365 self.serialize(write, e, encoding, qnames) 1376 self.serialize(write, e, qnames)
1366 1377
1367 elif isinstance(elem, Comment): 1378 elif isinstance(elem, Comment):
1368 write("<!--%s-->" % self._escape_cdata(elem.text)) 1379 write("<!--%s-->" % self._escape_cdata(elem.text))
1369 1380
1370 elif isinstance(elem, ProcessingInstruction): 1381 elif isinstance(elem, ProcessingInstruction):