##// END OF EJS Templates
py3: avoid iterating over a literal bytes in highlight...
Denis Laxalde -
r44014:856cce0c stable
parent child Browse files
Show More
@@ -1,101 +1,101 b''
1 1 # highlight.py - highlight extension implementation file
2 2 #
3 3 # Copyright 2007-2009 Adam Hupp <adam@hupp.org> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7 #
8 8 # The original module was split in an interface and an implementation
9 9 # file to defer pygments loading and speedup extension setup.
10 10
11 11 from __future__ import absolute_import
12 12
13 13 from mercurial import demandimport
14 14
15 15 demandimport.IGNORES.update([b'pkgutil', b'pkg_resources', b'__main__'])
16 16
17 17 from mercurial import (
18 18 encoding,
19 19 pycompat,
20 20 )
21 21
22 22 from mercurial.utils import stringutil
23 23
24 24 with demandimport.deactivated():
25 25 import pygments
26 26 import pygments.formatters
27 27 import pygments.lexers
28 28 import pygments.plugin
29 29 import pygments.util
30 30
31 31 for unused in pygments.plugin.find_plugin_lexers():
32 32 pass
33 33
34 34 highlight = pygments.highlight
35 35 ClassNotFound = pygments.util.ClassNotFound
36 36 guess_lexer = pygments.lexers.guess_lexer
37 37 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
38 38 TextLexer = pygments.lexers.TextLexer
39 39 HtmlFormatter = pygments.formatters.HtmlFormatter
40 40
41 41 SYNTAX_CSS = (
42 42 b'\n<link rel="stylesheet" href="{url}highlightcss" type="text/css" />'
43 43 )
44 44
45 45
46 46 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False):
47 47
48 48 # append a <link ...> to the syntax highlighting css
49 49 tmpl.load(b'header')
50 50 old_header = tmpl.cache[b'header']
51 51 if SYNTAX_CSS not in old_header:
52 52 new_header = old_header + SYNTAX_CSS
53 53 tmpl.cache[b'header'] = new_header
54 54
55 55 text = fctx.data()
56 56 if stringutil.binary(text):
57 57 return
58 58
59 59 # str.splitlines() != unicode.splitlines() because "reasons"
60 for c in b"\x0c\x1c\x1d\x1e":
60 for c in b"\x0c", b"\x1c", b"\x1d", b"\x1e":
61 61 if c in text:
62 62 text = text.replace(c, b'')
63 63
64 64 # Pygments is best used with Unicode strings:
65 65 # <http://pygments.org/docs/unicode/>
66 66 text = text.decode(pycompat.sysstr(encoding.encoding), 'replace')
67 67
68 68 # To get multi-line strings right, we can't format line-by-line
69 69 try:
70 70 path = pycompat.sysstr(fctx.path())
71 71 lexer = guess_lexer_for_filename(path, text[:1024], stripnl=False)
72 72 except (ClassNotFound, ValueError):
73 73 # guess_lexer will return a lexer if *any* lexer matches. There is
74 74 # no way to specify a minimum match score. This can give a high rate of
75 75 # false positives on files with an unknown filename pattern.
76 76 if guessfilenameonly:
77 77 return
78 78
79 79 try:
80 80 lexer = guess_lexer(text[:1024], stripnl=False)
81 81 except (ClassNotFound, ValueError):
82 82 # Don't highlight unknown files
83 83 return
84 84
85 85 # Don't highlight text files
86 86 if isinstance(lexer, TextLexer):
87 87 return
88 88
89 89 formatter = HtmlFormatter(nowrap=True, style=pycompat.sysstr(style))
90 90
91 91 colorized = highlight(text, lexer, formatter)
92 92 coloriter = (
93 93 s.encode(pycompat.sysstr(encoding.encoding), 'replace')
94 94 for s in colorized.splitlines()
95 95 )
96 96
97 97 tmpl._filters[b'colorize'] = lambda x: next(coloriter)
98 98
99 99 oldl = tmpl.cache[field]
100 100 newl = oldl.replace(b'line|escape', b'line|colorize')
101 101 tmpl.cache[field] = newl
General Comments 0
You need to be logged in to leave comments. Login now