Commits

gbrandl  committed 3e42a61

#371: HTML formatter options can be Unicode strings.

  • Participants
  • Parent commits c97ce1d
  • Branches trunk

Comments (0)

Files changed (3)

 
 - Fixed numpy lexer that it doesn't listen for `*.py` any longer.
 
+- Fixed HTML formatter so that text options can be Unicode
+  strings (#371).
+
 - Unified Diff lexer supports the "udiff" alias now.
 
 - Fix a few issues in Scala lexer (#367).

File pygments/formatters/html.py

 
     def __init__(self, **options):
         Formatter.__init__(self, **options)
+        self.title = self._encodeifneeded(self.title)
         self.nowrap = get_bool_opt(options, 'nowrap', False)
         self.noclasses = get_bool_opt(options, 'noclasses', False)
         self.classprefix = options.get('classprefix', '')
-        self.cssclass = options.get('cssclass', 'highlight')
-        self.cssstyles = options.get('cssstyles', '')
-        self.prestyles = options.get('prestyles', '')
-        self.cssfile = options.get('cssfile', '')
+        self.cssclass = self._encodeifneeded(options.get('cssclass', 'highlight'))
+        self.cssstyles = self._encodeifneeded(options.get('cssstyles', ''))
+        self.prestyles = self._encodeifneeded(options.get('prestyles', ''))
+        self.cssfile = self._encodeifneeded(options.get('cssfile', ''))
         linenos = options.get('linenos', False)
         if linenos == 'inline':
             self.linenos = 2
                          (prefix(''), self.style.highlight_color))
         return '\n'.join(lines)
 
+    def _encodeifneeded(self, value):
+        if not self.encoding or isinstance(value, str):
+            return value
+        return value.encode(self.encoding)
+
     def _wrap_full(self, inner, outfile):
         if self.cssfile:
             if os.path.isabs(self.cssfile):

File tests/test_html_formatter.py

     Pygments HTML formatter tests
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-    :copyright: 2006-2007 by Georg Brandl.
+    :copyright: 2006-2008 by Georg Brandl.
     :license: BSD, see LICENSE for more details.
 """
 
 
 TESTFILE, TESTDIR = support.location(__file__)
 
-tokensource = list(PythonLexer().get_tokens(file(TESTFILE).read()))
+tokensource = list(PythonLexer(encoding='utf-8').get_tokens(file(TESTFILE).read()))
 
 class HtmlFormatterTest(unittest.TestCase):
     def test_correct_output(self):
     def test_external_css(self):
         # test correct behavior
         # CSS should be in /tmp directory
-        fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css')
+        fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css', outencoding='utf-8')
         # CSS should be in TESTDIR (TESTDIR is absolute)
-        fmt2 = HtmlFormatter(full=True, cssfile=join(TESTDIR, 'fmt2.css'))
+        fmt2 = HtmlFormatter(full=True, cssfile=join(TESTDIR, 'fmt2.css'),
+                             outencoding='utf-8')
         tfile = tempfile.NamedTemporaryFile(suffix='.html')
         fmt1.format(tokensource, tfile)
         try:
 
     def test_valid_output(self):
         # test all available wrappers
-        fmt = HtmlFormatter(full=True, linenos=True, noclasses=True)
+        fmt = HtmlFormatter(full=True, linenos=True, noclasses=True,
+                            outencoding='utf-8')
 
         handle, pathname = tempfile.mkstemp('.html')
         tfile = os.fdopen(handle, 'w+b')
                 ret = os.popen('nsgmls -s -c "%s" "%s"' % (catname, pathname)).close()
                 if ret == 32512: raise OSError  # not found
         except OSError:
-            # latex not available
+            # nsgmls not available
             pass
         else:
             self.failIf(ret, 'nsgmls run reported errors')
         sd = fmt.get_style_defs(['.bar', '.baz'])
         fl = sd.splitlines()[0]
         self.assert_('.bar' in fl and '.baz' in fl)
+
+    def test_unicode_options(self):
+        fmt = HtmlFormatter(title=u'Föö',
+                            cssclass=u'bär',
+                            cssstyles=u'div:before { content: \'bäz\' }',
+                            encoding='utf-8')
+        handle, pathname = tempfile.mkstemp('.html')
+        tfile = os.fdopen(handle, 'w+b')
+        fmt.format(tokensource, tfile)
+        tfile.close()