##// END OF EJS Templates
highlight: fixes garbled text in non-UTF-8 environment...
Yuya Nishihara -
r9424:799373ff default
parent child Browse files
Show More
@@ -32,26 +32,27 b' def pygmentize(field, fctx, style, tmpl)'
32 32 if util.binary(text):
33 33 return
34 34
35 # avoid UnicodeDecodeError in pygments
36 text = encoding.tolocal(text)
35 # Pygments is best used with Unicode strings:
36 # <http://pygments.org/docs/unicode/>
37 text = text.decode(encoding.encoding, 'replace')
37 38
38 39 # To get multi-line strings right, we can't format line-by-line
39 40 try:
40 lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
41 encoding=encoding.encoding)
41 lexer = guess_lexer_for_filename(fctx.path(), text[:1024])
42 42 except (ClassNotFound, ValueError):
43 43 try:
44 lexer = guess_lexer(text[:1024], encoding=encoding.encoding)
44 lexer = guess_lexer(text[:1024])
45 45 except (ClassNotFound, ValueError):
46 lexer = TextLexer(encoding=encoding.encoding)
46 lexer = TextLexer()
47 47
48 formatter = HtmlFormatter(style=style, encoding=encoding.encoding)
48 formatter = HtmlFormatter(style=style)
49 49
50 50 colorized = highlight(text, lexer, formatter)
51 51 # strip wrapping div
52 52 colorized = colorized[:colorized.find('\n</pre>')]
53 53 colorized = colorized[colorized.find('<pre>')+5:]
54 coloriter = iter(colorized.splitlines())
54 coloriter = (s.encode(encoding.encoding, 'replace')
55 for s in colorized.splitlines())
55 56
56 57 tmpl.filters['colorize'] = lambda x: coloriter.next()
57 58
@@ -121,3 +121,28 b' rm out'
121 121
122 122 echo % errors encountered
123 123 cat errors.log
124
125 cd ..
126 hg init eucjp
127 cd eucjp
128
129 printf '\265\376\n' >> eucjp.txt # Japanese kanji "Kyo"
130
131 hg ci -Ama
132
133 hgserveget () {
134 "$TESTDIR/killdaemons.py"
135 echo % HGENCODING="$1" hg serve
136 HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log
137 cat hg.pid >> $DAEMON_PIDS
138
139 echo % hgweb filerevision, html
140 "$TESTDIR/get-with-headers.py" localhost:$HGPORT "/file/tip/$2" \
141 | grep '<div class="parity0 source">' | $TESTDIR/printrepr.py
142 echo % errors encountered
143 cat errors.log
144 }
145
146 hgserveget euc-jp eucjp.txt
147 hgserveget utf-8 eucjp.txt
148 hgserveget us-ascii eucjp.txt
@@ -538,3 +538,16 b' 200 Script output follows'
538 538 /* pygments_style = fruity */
539 539
540 540 % errors encountered
541 adding eucjp.txt
542 % HGENCODING=euc-jp hg serve
543 % hgweb filerevision, html
544 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> \xb5\xfe</div>
545 % errors encountered
546 % HGENCODING=utf-8 hg serve
547 % hgweb filerevision, html
548 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> \xef\xbf\xbd\xef\xbf\xbd</div>
549 % errors encountered
550 % HGENCODING=us-ascii hg serve
551 % hgweb filerevision, html
552 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> ??</div>
553 % errors encountered
General Comments 0
You need to be logged in to leave comments. Login now