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
--- 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