[PATCH] highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding

Christian Ebert blacktrash at gmx.net
Wed Dec 19 03:12:26 CST 2007


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1198055492 -3600
# Node ID 8099eb77b01c6671541dd75a2cfc67fab8cc20e1
# Parent  9d6ad26fab10e06768858ed1a74eff80207cb0f5
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding

This is needed in case util._encoding and hgweb.encoding conflict.
Extreme example:
HGENCODING=UTF-8
[web]
encoding = ascii

Note:
display of files whose encoding differs from HGENCODING or hgweb.encoding
behaves not exactly the same way as with highlight turned off as replacement
chars are either converted or replaced.

diff --git a/hgext/highlight.py b/hgext/highlight.py
--- a/hgext/highlight.py
+++ b/hgext/highlight.py
@@ -66,12 +66,10 @@ class StripedHtmlFormatter(HtmlFormatter
         yield 0, "</div>"
 
 
-def pygments_format(filename, rawtext, forcetext, encoding,
-                    stripecount, style):
-    etext = util.tolocal(rawtext)
+def pygments_format(filename, text, forcetext, stripecount, style):
     if not forcetext:
         try:
-            lexer = guess_lexer_for_filename(filename, etext,
+            lexer = guess_lexer_for_filename(filename, text,
                                              encoding=util._encoding)
         except ClassNotFound:
             lexer = TextLexer(encoding=util._encoding)
@@ -79,9 +77,9 @@ def pygments_format(filename, rawtext, f
         lexer = TextLexer(encoding=util._encoding)
 
     formatter = StripedHtmlFormatter(stripecount, style=style,
-                                     linenos='inline', encoding=encoding)
+                                     linenos='inline', encoding=util._encoding)
 
-    return highlight(etext, lexer, formatter)
+    return highlight(text, lexer, formatter)
 
 
 def filerevision_pygments(self, tmpl, fctx):
@@ -96,6 +94,9 @@ def filerevision_pygments(self, tmpl, fc
 
     mt = mimetypes.guess_type(f)[0]
 
+    # we always want hgweb.encoding
+    util._encoding = self.encoding
+
     if util.binary(text):
         mt = mt or 'application/octet-stream'
         text = "(binary:%s)" % mt
@@ -104,6 +105,9 @@ def filerevision_pygments(self, tmpl, fc
         forcetext = True
     else:
         mt = mt or 'text/plain'
+
+        # encode to hgweb.encoding for lexers and formatter
+        text = util.tolocal(text)
         forcetext = False
 
     def lines(text):
@@ -112,7 +116,7 @@ def filerevision_pygments(self, tmpl, fc
 
     style = self.config("web", "pygments_style", "colorful")
 
-    text_formatted = lines(pygments_format(f, text, forcetext, self.encoding,
+    text_formatted = lines(pygments_format(f, text, forcetext,
                                            self.stripecount, style))
 
     # override per-line template



More information about the Mercurial-devel mailing list