##// END OF EJS Templates
highlight: put pygments import inside demandimport.deactivated...
Augie Fackler -
r32908:661025fd default
parent child Browse files
Show More
@@ -1,88 +1,89 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 import pygments
14 import pygments.formatters
15 import pygments.lexers
16 import pygments.util
17
18 from mercurial import demandimport
13 from mercurial import demandimport
19 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__'])
14 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__'])
20
15
21 from mercurial import (
16 from mercurial import (
22 encoding,
17 encoding,
23 util,
18 util,
24 )
19 )
25
20
21 with demandimport.deactivated():
22 import pygments
23 import pygments.formatters
24 import pygments.lexers
25 import pygments.util
26
26 highlight = pygments.highlight
27 highlight = pygments.highlight
27 ClassNotFound = pygments.util.ClassNotFound
28 ClassNotFound = pygments.util.ClassNotFound
28 guess_lexer = pygments.lexers.guess_lexer
29 guess_lexer = pygments.lexers.guess_lexer
29 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
30 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
30 TextLexer = pygments.lexers.TextLexer
31 TextLexer = pygments.lexers.TextLexer
31 HtmlFormatter = pygments.formatters.HtmlFormatter
32 HtmlFormatter = pygments.formatters.HtmlFormatter
32
33
33 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
34 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
34 'type="text/css" />')
35 'type="text/css" />')
35
36
36 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False):
37 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False):
37
38
38 # append a <link ...> to the syntax highlighting css
39 # append a <link ...> to the syntax highlighting css
39 old_header = tmpl.load('header')
40 old_header = tmpl.load('header')
40 if SYNTAX_CSS not in old_header:
41 if SYNTAX_CSS not in old_header:
41 new_header = old_header + SYNTAX_CSS
42 new_header = old_header + SYNTAX_CSS
42 tmpl.cache['header'] = new_header
43 tmpl.cache['header'] = new_header
43
44
44 text = fctx.data()
45 text = fctx.data()
45 if util.binary(text):
46 if util.binary(text):
46 return
47 return
47
48
48 # str.splitlines() != unicode.splitlines() because "reasons"
49 # str.splitlines() != unicode.splitlines() because "reasons"
49 for c in "\x0c\x1c\x1d\x1e":
50 for c in "\x0c\x1c\x1d\x1e":
50 if c in text:
51 if c in text:
51 text = text.replace(c, '')
52 text = text.replace(c, '')
52
53
53 # Pygments is best used with Unicode strings:
54 # Pygments is best used with Unicode strings:
54 # <http://pygments.org/docs/unicode/>
55 # <http://pygments.org/docs/unicode/>
55 text = text.decode(encoding.encoding, 'replace')
56 text = text.decode(encoding.encoding, 'replace')
56
57
57 # To get multi-line strings right, we can't format line-by-line
58 # To get multi-line strings right, we can't format line-by-line
58 try:
59 try:
59 lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
60 lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
60 stripnl=False)
61 stripnl=False)
61 except (ClassNotFound, ValueError):
62 except (ClassNotFound, ValueError):
62 # guess_lexer will return a lexer if *any* lexer matches. There is
63 # guess_lexer will return a lexer if *any* lexer matches. There is
63 # no way to specify a minimum match score. This can give a high rate of
64 # no way to specify a minimum match score. This can give a high rate of
64 # false positives on files with an unknown filename pattern.
65 # false positives on files with an unknown filename pattern.
65 if guessfilenameonly:
66 if guessfilenameonly:
66 return
67 return
67
68
68 try:
69 try:
69 lexer = guess_lexer(text[:1024], stripnl=False)
70 lexer = guess_lexer(text[:1024], stripnl=False)
70 except (ClassNotFound, ValueError):
71 except (ClassNotFound, ValueError):
71 # Don't highlight unknown files
72 # Don't highlight unknown files
72 return
73 return
73
74
74 # Don't highlight text files
75 # Don't highlight text files
75 if isinstance(lexer, TextLexer):
76 if isinstance(lexer, TextLexer):
76 return
77 return
77
78
78 formatter = HtmlFormatter(nowrap=True, style=style)
79 formatter = HtmlFormatter(nowrap=True, style=style)
79
80
80 colorized = highlight(text, lexer, formatter)
81 colorized = highlight(text, lexer, formatter)
81 coloriter = (s.encode(encoding.encoding, 'replace')
82 coloriter = (s.encode(encoding.encoding, 'replace')
82 for s in colorized.splitlines())
83 for s in colorized.splitlines())
83
84
84 tmpl.filters['colorize'] = lambda x: next(coloriter)
85 tmpl.filters['colorize'] = lambda x: next(coloriter)
85
86
86 oldl = tmpl.cache[field]
87 oldl = tmpl.cache[field]
87 newl = oldl.replace('line|escape', 'line|colorize')
88 newl = oldl.replace('line|escape', 'line|colorize')
88 tmpl.cache[field] = newl
89 tmpl.cache[field] = newl
General Comments 0
You need to be logged in to leave comments. Login now