##// END OF EJS Templates
highlight: ignore Unicode's extra linebreaks (issue4291)...
Matt Mackall -
r23613:7b8ff3fd default
parent child Browse files
Show More
@@ -1,62 +1,67 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 mercurial import demandimport
12 12 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__'])
13 13 from mercurial import util, encoding
14 14
15 15 from pygments import highlight
16 16 from pygments.util import ClassNotFound
17 17 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
18 18 from pygments.formatters import HtmlFormatter
19 19
20 20 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
21 21 'type="text/css" />')
22 22
23 23 def pygmentize(field, fctx, style, tmpl):
24 24
25 25 # append a <link ...> to the syntax highlighting css
26 26 old_header = tmpl.load('header')
27 27 if SYNTAX_CSS not in old_header:
28 28 new_header = old_header + SYNTAX_CSS
29 29 tmpl.cache['header'] = new_header
30 30
31 31 text = fctx.data()
32 32 if util.binary(text):
33 33 return
34 34
35 # str.splitlines() != unicode.splitlines() because "reasons"
36 for c in "\x0c\x1c\x1d\x1e":
37 if c in text:
38 text = text.replace(c, '')
39
35 40 # Pygments is best used with Unicode strings:
36 41 # <http://pygments.org/docs/unicode/>
37 42 text = text.decode(encoding.encoding, 'replace')
38 43
39 44 # To get multi-line strings right, we can't format line-by-line
40 45 try:
41 46 lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
42 47 stripnl=False)
43 48 except (ClassNotFound, ValueError):
44 49 try:
45 50 lexer = guess_lexer(text[:1024], stripnl=False)
46 51 except (ClassNotFound, ValueError):
47 52 lexer = TextLexer(stripnl=False)
48 53
49 54 formatter = HtmlFormatter(style=style)
50 55
51 56 colorized = highlight(text, lexer, formatter)
52 57 # strip wrapping div
53 58 colorized = colorized[:colorized.find('\n</pre>')]
54 59 colorized = colorized[colorized.find('<pre>') + 5:]
55 60 coloriter = (s.encode(encoding.encoding, 'replace')
56 61 for s in colorized.splitlines())
57 62
58 63 tmpl.filters['colorize'] = lambda x: coloriter.next()
59 64
60 65 oldl = tmpl.cache[field]
61 66 newl = oldl.replace('line|escape', 'line|colorize')
62 67 tmpl.cache[field] = newl
General Comments 0
You need to be logged in to leave comments. Login now