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