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