##// END OF EJS Templates
Merge with crew-stable.
Dirkjan Ochtman -
r6511:5c1bb575 merge default
parent child Browse files
Show More
@@ -1,100 +1,100 b''
1 1 """
2 2 This is Mercurial extension for syntax highlighting in the file
3 3 revision view of hgweb.
4 4
5 5 It depends on the pygments syntax highlighting library:
6 6 http://pygments.org/
7 7
8 8 To enable the extension add this to hgrc:
9 9
10 10 [extensions]
11 11 hgext.highlight =
12 12
13 13 There is a single configuration option:
14 14
15 15 [web]
16 16 pygments_style = <style>
17 17
18 18 The default is 'colorful'.
19 19
20 20 -- Adam Hupp <adam@hupp.org>
21 21 """
22 22
23 23 from mercurial import demandimport
24 24 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
25 25
26 26 from mercurial.hgweb import webcommands, webutil, common
27 27 from mercurial import util
28 28 from mercurial.templatefilters import filters
29 29
30 30 from pygments import highlight
31 31 from pygments.util import ClassNotFound
32 32 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
33 33 from pygments.formatters import HtmlFormatter
34 34
35 35 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
36 36 'type="text/css" />')
37 37
38 38 def pygmentize(field, fctx, style, tmpl):
39 39
40 40 # append a <link ...> to the syntax highlighting css
41 41 old_header = ''.join(tmpl('header'))
42 42 if SYNTAX_CSS not in old_header:
43 43 new_header = old_header + SYNTAX_CSS
44 44 tmpl.cache['header'] = new_header
45 45
46 46 text = fctx.data()
47 47 if util.binary(text):
48 48 return
49 49
50 50 # To get multi-line strings right, we can't format line-by-line
51 51 try:
52 52 lexer = guess_lexer_for_filename(fctx.path(), text,
53 53 encoding=util._encoding)
54 except ClassNotFound:
54 except (ClassNotFound, ValueError):
55 55 try:
56 56 lexer = guess_lexer(text, encoding=util._encoding)
57 except ClassNotFound:
57 except (ClassNotFound, ValueError):
58 58 lexer = TextLexer(encoding=util._encoding)
59 59
60 60 formatter = HtmlFormatter(style=style, encoding=util._encoding)
61 61
62 62 colorized = highlight(text, lexer, formatter)
63 63 # strip wrapping div
64 64 colorized = colorized[:colorized.find('\n</pre>')]
65 65 colorized = colorized[colorized.find('<pre>')+5:]
66 66 coloriter = iter(colorized.splitlines())
67 67
68 68 filters['colorize'] = lambda x: coloriter.next()
69 69
70 70 oldl = tmpl.cache[field]
71 71 newl = oldl.replace('line|escape', 'line|colorize')
72 72 tmpl.cache[field] = newl
73 73
74 74 web_filerevision = webcommands._filerevision
75 75 web_annotate = webcommands.annotate
76 76
77 77 def filerevision_highlight(web, tmpl, fctx):
78 78 style = web.config('web', 'pygments_style', 'colorful')
79 79 pygmentize('fileline', fctx, style, tmpl)
80 80 return web_filerevision(web, tmpl, fctx)
81 81
82 82 def annotate_highlight(web, req, tmpl):
83 83 fctx = webutil.filectx(web.repo, req)
84 84 style = web.config('web', 'pygments_style', 'colorful')
85 85 pygmentize('annotateline', fctx, style, tmpl)
86 86 return web_annotate(web, req, tmpl)
87 87
88 88 def generate_css(web, req, tmpl):
89 89 pg_style = web.config('web', 'pygments_style', 'colorful')
90 90 fmter = HtmlFormatter(style = pg_style)
91 91 req.respond(common.HTTP_OK, 'text/css')
92 92 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
93 93
94 94
95 95 # monkeypatch in the new version
96 96
97 97 webcommands._filerevision = filerevision_highlight
98 98 webcommands.annotate = annotate_highlight
99 99 webcommands.highlightcss = generate_css
100 100 webcommands.__all__.append('highlightcss')
General Comments 0
You need to be logged in to leave comments. Login now