X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsp%2Fxhtml.py;h=17c56788729b63cbb631a2a3db547a4dbb441edf;hb=26648796c1d6c3c528ad7cdb108ecb086cfa5784;hp=abf4b965e83ac9e44139b7b6940dc9db3e3553cb;hpb=313cc8f23e7a5656a6d2ad9e56e94e0e119c8f85;p=wrw.git diff --git a/wrw/sp/xhtml.py b/wrw/sp/xhtml.py index abf4b96..17c5678 100644 --- a/wrw/sp/xhtml.py +++ b/wrw/sp/xhtml.py @@ -1,11 +1,11 @@ -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): @@ -14,7 +14,15 @@ class htmlelement(_cons.element): doc.appendChild(self.__todom__(doc)) return doc +class xhtmlcontext(_cons.context): + attrmap = {"klass": "class"} + + def addattr(self, node, k, v): + k = str(k) + node.attrs[self.attrmap.get(k, k)] = str(v) + def cons(ctx=None): + if ctx is None: ctx = xhtmlcontext() return _cons.constructor(ns, htmlelement, ctx) def head(title=None, css=None): @@ -22,7 +30,7 @@ def head(title=None, css=None): 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: @@ -30,7 +38,7 @@ def head(title=None, css=None): return head class htmlformatter(util.formatter): - allowshort = set([u"br", u"hr", u"img", u"input"]) + allowshort = set(["br", "hr", "img", "input", "meta", "link"]) def element(self, el, **extra): if el.name in self.allowshort: super(htmlformatter, self).element(el, **extra) @@ -43,7 +51,7 @@ class htmlindenter(util.indenter, htmlformatter): 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.BytesIO() htmlindenter.output(buf, tree, doctype=(doctype, dtd), charset="utf-8") return [buf.getvalue()]