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