utils.py
changeset 7187 496f51b92154
parent 7140 ba51dac1115d
child 7268 cd14e03124be
child 7277 acd7f0e9f276
--- a/utils.py	Wed Apr 06 10:10:21 2011 +0200
+++ b/utils.py	Wed Apr 06 16:01:19 2011 +0200
@@ -361,17 +361,43 @@
         self.doctype = u''
         # xmldecl and html opening tag
         self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding
-        self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \
-                       'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \
-                       'xml:lang="%s" lang="%s">' % (req.lang, req.lang)
+        self._namespaces = [('xmlns', 'http://www.w3.org/1999/xhtml'),
+                            ('xmlns:cubicweb','http://www.logilab.org/2008/cubicweb')]
+        self._htmlattrs = [('xml:lang', req.lang),
+                           ('lang', req.lang)]
         # keep main_stream's reference on req for easier text/html demoting
         req.main_stream = self
 
+    def add_namespace(self, prefix, uri):
+        self._namespaces.append( (prefix, uri) )
+
+    def set_namespaces(self, namespaces):
+        self._namespaces = namespaces
+
+    def add_htmlattr(self, attrname, attrvalue):
+        self._htmlattrs.append( (attrname, attrvalue) )
+
+    def set_htmlattrs(self, attrs):
+        self._htmlattrs = attrs
+
+    def set_doctype(self, doctype, reset_xmldecl=True):
+        self.doctype = doctype
+        if reset_xmldecl:
+            self.xmldecl = u''
+
     def write(self, data):
         """StringIO interface: this method will be assigned to self.w
         """
         self.body.write(data)
 
+    @property
+    def htmltag(self):
+        attrs = ' '.join('%s="%s"' % (attr, xml_escape(value))
+                         for attr, value in (self._namespaces + self._htmlattrs))
+        if attrs:
+            return '<html %s>' % attrs
+        return '<html>'
+
     def getvalue(self):
         """writes HTML headers, closes </head> tag and writes HTML body"""
         return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype,
@@ -379,7 +405,6 @@
                                                  self.head.getvalue(),
                                                  self.body.getvalue())
 
-
 try:
     # may not be there if cubicweb-web not installed
     if sys.version_info < (2, 6):