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