Commits

victorlin committed dd83e88

Add genshi.default_encoding and genshi.default_format (original genshi.method) settings

Comments (0)

Files changed (3)

pyramid_genshi/__init__.py

         values.setdefault('_', self.translate)
         stream = self.template.generate(**values)
         method = self.settings.get('genshi.method', 'html')
-        kwargs = dict(method=method)
+        fmt = self.settings.get('genshi.default_format', method)
+        encoding = self.settings.get('genshi.default_encoding', 'utf8')
+        kwargs = {}
         doctype = self.settings.get('genshi.default_doctype', None)
         if doctype is not None:
             kwargs['doctype'] = doctype
-        body = stream.render(**kwargs)
+        body = stream.render(method=fmt, encoding=encoding, **kwargs)
         return body
         
     # implement ITemplateRenderer interface

pyramid_genshi/tests/fixtures/chinese.genshi

+<?xml version="1.0" encoding="UTF-8"?>
+<div xmlns="http://www.w3.org/1999/xhtml"
+     xmlns:py="http://genshi.edgewall.org/">
+中文字
+</div>

pyramid_genshi/tests/test_pyramid_genshi.py

         test_method('text', 
                     '\n')
         
+        def test_format(method, expected):
+            from pyramid.threadlocal import get_current_registry
+            reg = get_current_registry()
+            reg.settings['genshi.default_format'] = method
+            renderer = self.make_one(path, lookup)
+            result = renderer({}, {})
+            self.assertEqual(result, expected)
+            
+        test_format('xml', 
+                    '<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
+        test_format('xhtml', 
+                    '<div xmlns="http://www.w3.org/1999/xhtml">\n</div>')
+        test_format('text', 
+                    '\n')
+        
     def test_render_doctype(self):
         lookup = DummyLookup()
         path = self._get_template_path('minimal.genshi')
                     ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' \
                     '<div>\n</div>')
         
+    def test_render_encoding(self):
+        lookup = DummyLookup()
+        path = self._get_template_path('chinese.genshi')
+        
+        def test_encoding(encoding, expected):
+            from pyramid.threadlocal import get_current_registry
+            reg = get_current_registry()
+            reg.settings['genshi.default_encoding'] = encoding
+            renderer = self.make_one(path, lookup)
+            result = renderer({}, {})
+            self.assertEqual(result, expected)
+            
+        test_encoding('utf8', 
+                    '<div>\n\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97\n</div>')
+        test_encoding('cp950', 
+                    '<div>\n\xa4\xa4\xa4\xe5\xa6r\n</div>')
+        
     def test_i18n_msg(self):
         lookup = DummyLookup()
         def translate(msg):