highlight: pass encoding to lexers and formatter
authorChristian Ebert <blacktrash@gmx.net>
Wed, 12 Dec 2007 14:44:59 +0100
changeset 5655 fe38b0a3a928
parent 5654 831e34e17f4f
child 5656 b940260c4291
highlight: pass encoding to lexers and formatter Try to avoid UnicodeDecodeError by: - encoding to local - passing util._encoding as lexer input encoding - passing hgweb.encoding as formatter output encoding
hgext/highlight.py
--- a/hgext/highlight.py	Wed Dec 12 14:36:34 2007 +0100
+++ b/hgext/highlight.py	Wed Dec 12 14:44:59 2007 +0100
@@ -66,19 +66,22 @@
         yield 0, "</div>"
 
 
-def pygments_format(filename, rawtext, forcetext, stripecount, style):
+def pygments_format(filename, rawtext, forcetext, encoding,
+                    stripecount, style):
+    etext = util.tolocal(rawtext)
     if not forcetext:
         try:
-            lexer = guess_lexer_for_filename(filename, rawtext)
+            lexer = guess_lexer_for_filename(filename, etext,
+                                             encoding=util._encoding)
         except ClassNotFound:
-            lexer = TextLexer()
+            lexer = TextLexer(encoding=util._encoding)
     else:
-        lexer = TextLexer()
+        lexer = TextLexer(encoding=util._encoding)
 
     formatter = StripedHtmlFormatter(stripecount, style=style,
-                                     linenos='inline')
+                                     linenos='inline', encoding=encoding)
 
-    return highlight(rawtext, lexer, formatter)
+    return highlight(etext, lexer, formatter)
 
 
 def filerevision_pygments(self, tmpl, fctx):
@@ -109,7 +112,7 @@
 
     style = self.config("web", "pygments_style", "colorful")
 
-    text_formatted = lines(pygments_format(f, text, forcetext,
+    text_formatted = lines(pygments_format(f, text, forcetext, self.encoding,
                                            self.stripecount, style))
 
     # override per-line template