doc = xml.dom.minidom.Document()
return self.__todom__(doc).toxml()
-class text(node, unicode):
+class text(node, str):
def __todom__(self, doc):
return doc.createTextNode(self)
class element(node):
def __init__(self, ns, name, ctx):
self.ns = ns
- self.name = unicode(name)
+ self.name = str(name)
self.ctx = ctx
self.attrs = {}
self.children = []
def __call__(self, *children, **attrs):
for child in children:
self.children.append(self.ctx.nodefrom(child))
- for k, v in attrs.iteritems():
- self.attrs[unicode(k)] = unicode(v)
+ for k, v in attrs.items():
+ self.attrs[str(k)] = str(v)
return self
def __todom__(self, doc):
el = doc.createElementNS(self.ns, self.name)
- for k, v in self.attrs.iteritems():
+ for k, v in self.attrs.items():
el.setAttribute(k, v)
for child in self.children:
el.appendChild(child.__todom__(doc))
class context(object):
def __init__(self):
self.nodeconv = {}
- self.nodeconv[str] = lambda ob: text(ob, "utf-8")
- self.nodeconv[unicode] = text
+ self.nodeconv[bytes] = lambda ob: text(ob, "utf-8")
+ self.nodeconv[str] = text
self.nodeconv[int] = text
- self.nodeconv[long] = text
self.nodeconv[float] = text
def nodefrom(self, ob):
-import cons
+from . import cons
def findnsnames(el):
names = {}
def proc(el):
if isinstance(el, cons.element):
if el.ns not in names:
- names[el.ns] = u"n" + unicode(nid[0])
+ names[el.ns] = "n" + str(nid[0])
nid[:] = [nid[0] + 1]
for ch in el.children:
proc(ch)
def quotewrite(self, buf):
for ch in buf:
- if ch == u'&':
- self.write(u"&")
- elif ch == u'<':
- self.write(u"<")
- elif ch == u'>':
- self.write(u">")
+ if ch == '&':
+ self.write("&")
+ elif ch == '<':
+ self.write("<")
+ elif ch == '>':
+ self.write(">")
else:
self.write(ch)
self.quotewrite(el)
def attrval(self, buf):
- qc, qt = (u"'", u"'") if u'"' in buf else (u'"', u""")
+ qc, qt = ("'", "'") if '"' in buf else ('"', """)
self.write(qc)
for ch in buf:
- if ch == u'&':
- self.write(u"&")
- elif ch == u'<':
- self.write(u"<")
- elif ch == u'>':
- self.write(u">")
+ if ch == '&':
+ self.write("&")
+ elif ch == '<':
+ self.write("<")
+ elif ch == '>':
+ self.write(">")
elif ch == qc:
self.write(qt)
else:
def attr(self, k, v):
self.write(k)
- self.write(u'=')
+ self.write('=')
self.attrval(v)
def shorttag(self, el, **extra):
- self.write(u'<' + self.elname(el))
- for k, v in el.attrs.iteritems():
- self.write(u' ')
+ self.write('<' + self.elname(el))
+ for k, v in el.attrs.items():
+ self.write(' ')
self.attr(k, v)
- for k, v in extra.iteritems():
- self.write(u' ')
+ for k, v in extra.items():
+ self.write(' ')
self.attr(k, v)
- self.write(u" />")
+ self.write(" />")
def elname(self, el):
ns = self.nsnames[el.ns]
if ns is None:
return el.name
else:
- return ns + u':' + el.name
+ return ns + ':' + el.name
def starttag(self, el, **extra):
- self.write(u'<' + self.elname(el))
- for k, v in el.attrs.iteritems():
- self.write(u' ')
+ self.write('<' + self.elname(el))
+ for k, v in el.attrs.items():
+ self.write(' ')
self.attr(k, v)
- for k, v in extra.iteritems():
- self.write(u' ')
+ for k, v in extra.items():
+ self.write(' ')
self.attr(k, v)
- self.write(u'>')
+ self.write('>')
def endtag(self, el):
- self.write(u'</' + self.elname(el) + u'>')
+ self.write('</' + self.elname(el) + '>')
def longtag(self, el):
self.starttag(el, **extra)
raise Exception("Unknown object in element tree: " + el)
def start(self):
- self.write(u'<?xml version="1.0" encoding="' + self.charset + u'" ?>\n')
+ self.write('<?xml version="1.0" encoding="' + self.charset + '" ?>\n')
if self.doctype:
- self.write(u'<!DOCTYPE %s PUBLIC "%s" "%s">\n' % (self.root.name,
- self.doctype[0],
- self.doctype[1]))
+ self.write('<!DOCTYPE %s PUBLIC "%s" "%s">\n' % (self.root.name,
+ self.doctype[0],
+ self.doctype[1]))
extra = {}
- for uri, nm in self.nsnames.iteritems():
+ for uri, nm in self.nsnames.items():
if uri is None:
continue
if nm is None:
- extra[u"xmlns"] = uri
+ extra["xmlns"] = uri
else:
- extra[u"xmlns:" + nm] = uri
+ extra["xmlns:" + nm] = uri
self.element(self.root, **extra)
@classmethod
self.col = 0
def write(self, buf):
- for c in buf:
- if c == '\n':
+ for i in range(len(buf)):
+ c = buf[i:i + 1]
+ if c == b'\n':
self.col = 0
else:
self.col += 1
if self.atbol:
return
if self.col != 0:
- self.write('\n')
+ self.write(b'\n')
self.write(indent)
self.atbol = True
class indenter(formatter):
- def __init__(self, indent=u" ", *args, **kw):
+ def __init__(self, indent=" ", *args, **kw):
super(indenter, self).__init__(*args, **kw)
self.out = iwriter(self.out)
self.indent = indent
- self.curind = u""
+ self.curind = ""
def simple(self, el):
for ch in el.children:
reind = False
if not self.simple(el):
sub = self.update(curind=self.curind + self.indent)
- sub.out.indent(sub.curind)
+ sub.reindent()
reind = True
for ch in el.children:
sub.node(ch)
if reind:
- self.out.indent(self.curind)
+ self.reindent()
self.endtag(el)
def element(self, el, **extra):
super(indenter, self).element(el, **extra)
if self.out.col > 80 and self.simple(el):
- self.out.indent(self.curind)
+ self.reindent()
+
+ def reindent(self):
+ self.out.indent(self.curind.encode(self.charset))
def start(self):
super(indenter, self).start()
-import xml.dom.minidom, StringIO
-import cons as _cons
-import util
+import xml.dom.minidom, io
+from . import cons as _cons
+from . import util
dom = xml.dom.minidom.getDOMImplementation()
-ns = u"http://www.w3.org/1999/xhtml"
-doctype = u"-//W3C//DTD XHTML 1.1//EN"
-dtd = u"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
+ns = "http://www.w3.org/1999/xhtml"
+doctype = "-//W3C//DTD XHTML 1.1//EN"
+dtd = "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
class htmlelement(_cons.element):
def __todoc__(self):
head = h.head
if title:
head(h.title(title))
- if isinstance(css, str) or isinstance(css, unicode):
+ if isinstance(css, str) or isinstance(css, bytes):
head(h.link(rel="stylesheet", type="text/css", href=css))
elif css:
for ss in css:
return head
class htmlformatter(util.formatter):
- allowshort = set([u"br", u"hr", u"img", u"input"])
+ allowshort = set(["br", "hr", "img", "input"])
def element(self, el, **extra):
if el.name in self.allowshort:
super(htmlformatter, self).element(el, **extra)
def forreq(req, tree):
# XXX: Use proper Content-Type for clients accepting it.
req.ohead["Content-Type"] = "text/html; charset=utf-8"
- buf = StringIO.StringIO()
+ buf = io.StringIO()
htmlindenter.output(buf, tree, doctype=(doctype, dtd), charset="utf-8")
return [buf.getvalue()]