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