comparison hgext/highlight.py @ 5748:5957c7b5894a

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.
author Christian Ebert <blacktrash@gmx.net>
date Wed, 19 Dec 2007 10:11:32 +0100
parents fe38b0a3a928
children 31726c27e40e
comparison
equal deleted inserted replaced
5747:f75ca1b0c81e 5748:5957c7b5894a
64 64
65 yield 0, "</pre>" 65 yield 0, "</pre>"
66 yield 0, "</div>" 66 yield 0, "</div>"
67 67
68 68
69 def pygments_format(filename, rawtext, forcetext, encoding, 69 def pygments_format(filename, text, forcetext, stripecount, style):
70 stripecount, style):
71 etext = util.tolocal(rawtext)
72 if not forcetext: 70 if not forcetext:
73 try: 71 try:
74 lexer = guess_lexer_for_filename(filename, etext, 72 lexer = guess_lexer_for_filename(filename, text,
75 encoding=util._encoding) 73 encoding=util._encoding)
76 except ClassNotFound: 74 except ClassNotFound:
77 lexer = TextLexer(encoding=util._encoding) 75 lexer = TextLexer(encoding=util._encoding)
78 else: 76 else:
79 lexer = TextLexer(encoding=util._encoding) 77 lexer = TextLexer(encoding=util._encoding)
80 78
81 formatter = StripedHtmlFormatter(stripecount, style=style, 79 formatter = StripedHtmlFormatter(stripecount, style=style,
82 linenos='inline', encoding=encoding) 80 linenos='inline', encoding=util._encoding)
83 81
84 return highlight(etext, lexer, formatter) 82 return highlight(text, lexer, formatter)
85 83
86 84
87 def filerevision_pygments(self, tmpl, fctx): 85 def filerevision_pygments(self, tmpl, fctx):
88 """Reimplement hgweb.filerevision to use syntax highlighting""" 86 """Reimplement hgweb.filerevision to use syntax highlighting"""
89 f = fctx.path() 87 f = fctx.path()
94 fl = fctx.filelog() 92 fl = fctx.filelog()
95 n = fctx.filenode() 93 n = fctx.filenode()
96 94
97 mt = mimetypes.guess_type(f)[0] 95 mt = mimetypes.guess_type(f)[0]
98 96
97 # we always want hgweb.encoding
98 util._encoding = self.encoding
99
99 if util.binary(text): 100 if util.binary(text):
100 mt = mt or 'application/octet-stream' 101 mt = mt or 'application/octet-stream'
101 text = "(binary:%s)" % mt 102 text = "(binary:%s)" % mt
102 103
103 # don't parse (binary:...) as anything 104 # don't parse (binary:...) as anything
104 forcetext = True 105 forcetext = True
105 else: 106 else:
106 mt = mt or 'text/plain' 107 mt = mt or 'text/plain'
108
109 # encode to hgweb.encoding for lexers and formatter
110 text = util.tolocal(text)
107 forcetext = False 111 forcetext = False
108 112
109 def lines(text): 113 def lines(text):
110 for line in text.splitlines(True): 114 for line in text.splitlines(True):
111 yield {"line": line} 115 yield {"line": line}
112 116
113 style = self.config("web", "pygments_style", "colorful") 117 style = self.config("web", "pygments_style", "colorful")
114 118
115 text_formatted = lines(pygments_format(f, text, forcetext, self.encoding, 119 text_formatted = lines(pygments_format(f, text, forcetext,
116 self.stripecount, style)) 120 self.stripecount, style))
117 121
118 # override per-line template 122 # override per-line template
119 tmpl.cache['fileline'] = '#line#' 123 tmpl.cache['fileline'] = '#line#'
120 124