comparison emeraldtree/tree.py @ 83:a70598fc5ca6

XML/HTMLWriter: more deduplication, code moved to base class
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 May 2012 01:02:20 +0200
parents 2dc110ff7b19
children 223789ea05b2
comparison
equal deleted inserted replaced
82:2dc110ff7b19 83:a70598fc5ca6
1302 if k: 1302 if k:
1303 k = u':' + k 1303 k = u':' + k
1304 result.append(u' xmlns%s="%s"' % (k, self._escape_attrib(v))) 1304 result.append(u' xmlns%s="%s"' % (k, self._escape_attrib(v)))
1305 return u''.join(result) 1305 return u''.join(result)
1306 1306
1307 1307 def _serialize_element(self, write, elem, qnames, namespaces):
1308 class XMLWriter(MLBaseWriter): 1308 raise NotImplementedError
1309
1310 def _serialize_comment(self, write, elem):
1311 write(u"<!--%s-->" % self._escape_cdata(elem.text))
1312
1313 def _serialize_pi(self, write, elem):
1314 text = self._escape_cdata(elem.target)
1315 if elem.text is not None:
1316 text += ' ' + self._escape_cdata(elem.text)
1317 write(u"<?%s?>" % text)
1318
1319 def _serialize_cdata(self, write, elem):
1320 write(self._escape_cdata(unicode(elem)))
1321
1309 def serialize(self, write, elem, qnames, namespaces={}): 1322 def serialize(self, write, elem, qnames, namespaces={}):
1310 if isinstance(elem, Element): 1323 if isinstance(elem, Element):
1311 tag = qnames[elem.tag] 1324 self._serialize_element(write, elem, qnames, namespaces)
1312 1325 elif isinstance(elem, Comment):
1313 if tag is not None: 1326 self._serialize_comment(write, elem)
1314 attrib_str = self._attrib_string(elem.attrib, qnames) 1327 elif isinstance(elem, ProcessingInstruction):
1315 namespace_str = self._namespace_string(namespaces) 1328 self._serialize_pi(write, elem)
1316 if len(elem): 1329 else:
1317 write(u"<%s%s%s>" % (tag, attrib_str, namespace_str)) 1330 self._serialize_cdata(write, elem)
1318 for e in elem: 1331
1319 self.serialize(write, e, qnames) 1332
1320 write(u"</%s>" % tag) 1333 class XMLWriter(MLBaseWriter):
1321 else: 1334 def _serialize_element(self, write, elem, qnames, namespaces):
1322 write(u"<%s%s%s />" % (tag, attrib_str, namespace_str)) 1335 tag = qnames[elem.tag]
1323 1336
1337 if tag is not None:
1338 attrib_str = self._attrib_string(elem.attrib, qnames)
1339 namespace_str = self._namespace_string(namespaces)
1340 if len(elem):
1341 write(u"<%s%s%s>" % (tag, attrib_str, namespace_str))
1342 for e in elem:
1343 self.serialize(write, e, qnames)
1344 write(u"</%s>" % tag)
1345 else:
1346 write(u"<%s%s%s />" % (tag, attrib_str, namespace_str))
1347
1348 else:
1349 for e in elem:
1350 self.serialize(write, e, qnames)
1351
1352 def serialize_start(self, write):
1353 if self.encoding and self.encoding not in ("utf-8", "us-ascii"):
1354 write(u"<?xml version='1.0' encoding='%s'?>\n" % self.encoding)
1355
1356
1357 class HTMLWriter(MLBaseWriter):
1358 empty_elements = frozenset(("area", "base", "basefont", "br", "col", "frame", "hr",
1359 "img", "input", "isindex", "link", "meta" "param"))
1360
1361 def __init__(self, encoding=None, namespaces={}):
1362 namespaces["http://www.w3.org/1999/xhtml"] = ''
1363 super(HTMLWriter, self).__init__(encoding, namespaces)
1364
1365 def _serialize_element(self, write, elem, qnames, namespaces):
1366 tag = qnames[elem.tag]
1367
1368 if tag is not None:
1369 attrib_str = self._attrib_string(elem.attrib, qnames)
1370 namespace_str = self._namespace_string(namespaces)
1371 write(u"<%s%s%s>" % (tag, attrib_str, namespace_str))
1372 if tag.lower() in ('script', 'style'):
1373 write(u''.join(elem.itertext()))
1324 else: 1374 else:
1325 for e in elem: 1375 for e in elem:
1326 self.serialize(write, e, qnames) 1376 self.serialize(write, e, qnames)
1327 1377 if tag not in self.empty_elements:
1328 elif isinstance(elem, Comment): 1378 write(u"</%s>" % tag)
1329 write(u"<!--%s-->" % self._escape_cdata(elem.text))
1330
1331 elif isinstance(elem, ProcessingInstruction):
1332 text = self._escape_cdata(elem.target)
1333 if elem.text is not None:
1334 text += ' ' + self._escape_cdata(elem.text)
1335 write(u"<?%s?>" % text)
1336 1379
1337 else: 1380 else:
1338 write(self._escape_cdata(unicode(elem))) 1381 for e in elem:
1339 1382 self.serialize(write, e, qnames)
1340 def serialize_start(self, write): 1383
1341 if self.encoding and self.encoding not in ("utf-8", "us-ascii"):
1342 write(u"<?xml version='1.0' encoding='%s'?>\n" % self.encoding)
1343
1344
1345 class HTMLWriter(MLBaseWriter):
1346 empty_elements = frozenset(("area", "base", "basefont", "br", "col", "frame", "hr",
1347 "img", "input", "isindex", "link", "meta" "param"))
1348
1349 def __init__(self, encoding=None, namespaces={}):
1350 namespaces["http://www.w3.org/1999/xhtml"] = ''
1351 super(HTMLWriter, self).__init__(encoding, namespaces)
1352
1353 def serialize(self, write, elem, qnames, namespaces={}):
1354 if isinstance(elem, Element):
1355 tag = qnames[elem.tag]
1356
1357 if tag is not None:
1358 attrib_str = self._attrib_string(elem.attrib, qnames)
1359 namespace_str = self._namespace_string(namespaces)
1360 write(u"<%s%s%s>" % (tag, attrib_str, namespace_str))
1361 if tag.lower() in ('script', 'style'):
1362 write(u''.join(elem.itertext()))
1363 else:
1364 for e in elem:
1365 self.serialize(write, e, qnames)
1366 if tag not in self.empty_elements:
1367 write(u"</%s>" % tag)
1368
1369 else:
1370 for e in elem:
1371 self.serialize(write, e, qnames)
1372
1373 elif isinstance(elem, Comment):
1374 write(u"<!--%s-->" % self._escape_cdata(elem.text))
1375
1376 elif isinstance(elem, ProcessingInstruction):
1377 text = self._escape_cdata(elem.target)
1378 if elem.text is not None:
1379 text += ' ' + self._escape_cdata(elem.text)
1380 write(u"<?%s?>" % text)
1381
1382 else:
1383 write(self._escape_cdata(unicode(elem)))
1384