[PATCH 2 of 2] highlight: pass encoding to lexers and formatter

Christian Ebert blacktrash at gmx.net
Wed Dec 12 08:12:39 CST 2007


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1197467099 -3600
# Node ID 2545e73ad25fc4ec9f2883045c733c7aa5fca30b
# Parent  2b500c9c96ecc0c7f10a2f26128da2ea079ceb62
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

diff --git a/hgext/highlight.py b/hgext/highlight.py
--- a/hgext/highlight.py
+++ b/hgext/highlight.py
@@ -66,19 +66,22 @@ class StripedHtmlFormatter(HtmlFormatter
         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 @@ def filerevision_pygments(self, tmpl, fc
 
     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



More information about the Mercurial-devel mailing list