[PATCH] highlight: pass hgweb.encoding to lexers and formatter

Christian Ebert blacktrash at gmx.net
Tue Dec 11 15:21:23 CST 2007


Hello,

The following is needed to avoid a nasty backtrace when a file
contains non-ascii characters.

Should perhaps be tested in non-utf locale; also I am not
entirely sure if the lexers should get passed util._encoding.
Anyway this gave consistent results re encoding with highlight
turned on and off.

A test script for this extension would be good.

c


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1197407528 -3600
# Node ID 2895a33c4912257625b3b8f88c5ed3fde342507a
# Parent  4d6b630d393953a2ac913232658b3bfb5f05ce93
highlight: pass hgweb.encoding to lexers and formatter

Avoids UnicodeDecodeError.

diff --git a/hgext/highlight.py b/hgext/highlight.py
--- a/hgext/highlight.py
+++ b/hgext/highlight.py
@@ -66,18 +66,19 @@ class StripedHtmlFormatter(HtmlFormatter
         yield 0, "</div>"
 
 
-def pygments_format(filename, rawtext, forcetext=False, stripecount=1,
-                    style='colorful'):
+def pygments_format(filename, rawtext, encoding,
+                    forcetext=False, stripecount=1, style='colorful'):
     if not forcetext:
         try:
-            lexer = guess_lexer_for_filename(filename, rawtext)
+            lexer = guess_lexer_for_filename(filename, rawtext,
+                                             encoding=encoding)
         except ClassNotFound:
-            lexer = TextLexer()
+            lexer = TextLexer(encoding=encoding)
     else:
-        lexer = TextLexer()
+        lexer = TextLexer(encoding=encoding)
 
     formatter = StripedHtmlFormatter(stripecount, style=style,
-                                     linenos='inline')
+                                     linenos='inline', encoding=encoding)
 
     return highlight(rawtext, lexer, formatter)
 
@@ -110,7 +111,7 @@ def filerevision_pygments(self, tmpl, fc
 
     style = self.config("web", "pygments_style", "colorful")
 
-    text_formatted = lines(pygments_format(f, text,
+    text_formatted = lines(pygments_format(f, text, self.encoding,
                                            forcetext=forcetext,
                                            stripecount=self.stripecount,
                                            style=style))



More information about the Mercurial-devel mailing list