Show More
@@ -1,57 +1,67 b'' | |||||
|
1 | # highlight - syntax highlighting in hgweb, based on Pygments | |||
|
2 | # | |||
|
3 | # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms of the | |||
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |||
|
7 | # | |||
|
8 | # The original module was split in an interface and an implementation | |||
|
9 | # file to defer pygments loading and speedup extension setup. | |||
|
10 | ||||
1 | """syntax highlighting in hgweb, based on Pygments |
|
11 | """syntax highlighting in hgweb, based on Pygments | |
2 |
|
12 | |||
3 | It depends on the pygments syntax highlighting library: |
|
13 | It depends on the pygments syntax highlighting library: | |
4 | http://pygments.org/ |
|
14 | http://pygments.org/ | |
5 |
|
15 | |||
6 | To enable the extension add this to hgrc: |
|
16 | To enable the extension add this to hgrc: | |
7 |
|
17 | |||
8 | [extensions] |
|
18 | [extensions] | |
9 | hgext.highlight = |
|
19 | hgext.highlight = | |
10 |
|
20 | |||
11 | There is a single configuration option: |
|
21 | There is a single configuration option: | |
12 |
|
22 | |||
13 | [web] |
|
23 | [web] | |
14 | pygments_style = <style> |
|
24 | pygments_style = <style> | |
15 |
|
25 | |||
16 | The default is 'colorful'. |
|
26 | The default is 'colorful'. | |
17 |
|
27 | |||
18 | -- Adam Hupp <adam@hupp.org> |
|
28 | -- Adam Hupp <adam@hupp.org> | |
19 | """ |
|
29 | """ | |
20 |
|
30 | |||
21 | import highlight |
|
31 | import highlight | |
22 | from mercurial.hgweb import webcommands, webutil, common |
|
32 | from mercurial.hgweb import webcommands, webutil, common | |
23 | from mercurial import extensions |
|
33 | from mercurial import extensions | |
24 |
|
34 | |||
25 | def filerevision_highlight(orig, web, tmpl, fctx): |
|
35 | def filerevision_highlight(orig, web, tmpl, fctx): | |
26 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
|
36 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) | |
27 | # only pygmentize for mimetype containing 'html' so we both match |
|
37 | # only pygmentize for mimetype containing 'html' so we both match | |
28 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
38 | # 'text/html' and possibly 'application/xhtml+xml' in the future | |
29 | # so that we don't have to touch the extension when the mimetype |
|
39 | # so that we don't have to touch the extension when the mimetype | |
30 | # for a template changes; also hgweb optimizes the case that a |
|
40 | # for a template changes; also hgweb optimizes the case that a | |
31 | # raw file is sent using rawfile() and doesn't call us, so we |
|
41 | # raw file is sent using rawfile() and doesn't call us, so we | |
32 | # can't clash with the file's content-type here in case we |
|
42 | # can't clash with the file's content-type here in case we | |
33 | # pygmentize a html file |
|
43 | # pygmentize a html file | |
34 | if 'html' in mt: |
|
44 | if 'html' in mt: | |
35 | style = web.config('web', 'pygments_style', 'colorful') |
|
45 | style = web.config('web', 'pygments_style', 'colorful') | |
36 | highlight.pygmentize('fileline', fctx, style, tmpl) |
|
46 | highlight.pygmentize('fileline', fctx, style, tmpl) | |
37 | return orig(web, tmpl, fctx) |
|
47 | return orig(web, tmpl, fctx) | |
38 |
|
48 | |||
39 | def annotate_highlight(orig, web, req, tmpl): |
|
49 | def annotate_highlight(orig, web, req, tmpl): | |
40 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
|
50 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) | |
41 | if 'html' in mt: |
|
51 | if 'html' in mt: | |
42 | fctx = webutil.filectx(web.repo, req) |
|
52 | fctx = webutil.filectx(web.repo, req) | |
43 | style = web.config('web', 'pygments_style', 'colorful') |
|
53 | style = web.config('web', 'pygments_style', 'colorful') | |
44 | highlight.pygmentize('annotateline', fctx, style, tmpl) |
|
54 | highlight.pygmentize('annotateline', fctx, style, tmpl) | |
45 | return orig(web, req, tmpl) |
|
55 | return orig(web, req, tmpl) | |
46 |
|
56 | |||
47 | def generate_css(web, req, tmpl): |
|
57 | def generate_css(web, req, tmpl): | |
48 | pg_style = web.config('web', 'pygments_style', 'colorful') |
|
58 | pg_style = web.config('web', 'pygments_style', 'colorful') | |
49 | fmter = highlight.HtmlFormatter(style = pg_style) |
|
59 | fmter = highlight.HtmlFormatter(style = pg_style) | |
50 | req.respond(common.HTTP_OK, 'text/css') |
|
60 | req.respond(common.HTTP_OK, 'text/css') | |
51 | return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] |
|
61 | return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] | |
52 |
|
62 | |||
53 | # monkeypatch in the new version |
|
63 | # monkeypatch in the new version | |
54 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) |
|
64 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) | |
55 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
65 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
56 | webcommands.highlightcss = generate_css |
|
66 | webcommands.highlightcss = generate_css | |
57 | webcommands.__all__.append('highlightcss') |
|
67 | webcommands.__all__.append('highlightcss') |
@@ -1,57 +1,62 b'' | |||||
1 | # highlight extension implementation file |
|
1 | # highlight.py - highlight extension implementation file | |
|
2 | # | |||
|
3 | # Copyright 2007-2009 Adam Hupp <adam@hupp.org> and others | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms of the | |||
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |||
2 | # |
|
7 | # | |
3 | # The original module was split in an interface and an implementation |
|
8 | # The original module was split in an interface and an implementation | |
4 | # file to defer pygments loading and speedup extension setup. |
|
9 | # file to defer pygments loading and speedup extension setup. | |
5 |
|
10 | |||
6 | from mercurial import demandimport |
|
11 | from mercurial import demandimport | |
7 | demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',]) |
|
12 | demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',]) | |
8 |
|
13 | |||
9 | from mercurial import util, encoding |
|
14 | from mercurial import util, encoding | |
10 | from mercurial.templatefilters import filters |
|
15 | from mercurial.templatefilters import filters | |
11 |
|
16 | |||
12 | from pygments import highlight |
|
17 | from pygments import highlight | |
13 | from pygments.util import ClassNotFound |
|
18 | from pygments.util import ClassNotFound | |
14 | from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer |
|
19 | from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer | |
15 | from pygments.formatters import HtmlFormatter |
|
20 | from pygments.formatters import HtmlFormatter | |
16 |
|
21 | |||
17 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' |
|
22 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' | |
18 | 'type="text/css" />') |
|
23 | 'type="text/css" />') | |
19 |
|
24 | |||
20 | def pygmentize(field, fctx, style, tmpl): |
|
25 | def pygmentize(field, fctx, style, tmpl): | |
21 |
|
26 | |||
22 | # append a <link ...> to the syntax highlighting css |
|
27 | # append a <link ...> to the syntax highlighting css | |
23 | old_header = ''.join(tmpl('header')) |
|
28 | old_header = ''.join(tmpl('header')) | |
24 | if SYNTAX_CSS not in old_header: |
|
29 | if SYNTAX_CSS not in old_header: | |
25 | new_header = old_header + SYNTAX_CSS |
|
30 | new_header = old_header + SYNTAX_CSS | |
26 | tmpl.cache['header'] = new_header |
|
31 | tmpl.cache['header'] = new_header | |
27 |
|
32 | |||
28 | text = fctx.data() |
|
33 | text = fctx.data() | |
29 | if util.binary(text): |
|
34 | if util.binary(text): | |
30 | return |
|
35 | return | |
31 |
|
36 | |||
32 | # avoid UnicodeDecodeError in pygments |
|
37 | # avoid UnicodeDecodeError in pygments | |
33 | text = encoding.tolocal(text) |
|
38 | text = encoding.tolocal(text) | |
34 |
|
39 | |||
35 | # To get multi-line strings right, we can't format line-by-line |
|
40 | # To get multi-line strings right, we can't format line-by-line | |
36 | try: |
|
41 | try: | |
37 | lexer = guess_lexer_for_filename(fctx.path(), text[:1024], |
|
42 | lexer = guess_lexer_for_filename(fctx.path(), text[:1024], | |
38 | encoding=encoding.encoding) |
|
43 | encoding=encoding.encoding) | |
39 | except (ClassNotFound, ValueError): |
|
44 | except (ClassNotFound, ValueError): | |
40 | try: |
|
45 | try: | |
41 | lexer = guess_lexer(text[:1024], encoding=encoding.encoding) |
|
46 | lexer = guess_lexer(text[:1024], encoding=encoding.encoding) | |
42 | except (ClassNotFound, ValueError): |
|
47 | except (ClassNotFound, ValueError): | |
43 | lexer = TextLexer(encoding=encoding.encoding) |
|
48 | lexer = TextLexer(encoding=encoding.encoding) | |
44 |
|
49 | |||
45 | formatter = HtmlFormatter(style=style, encoding=encoding.encoding) |
|
50 | formatter = HtmlFormatter(style=style, encoding=encoding.encoding) | |
46 |
|
51 | |||
47 | colorized = highlight(text, lexer, formatter) |
|
52 | colorized = highlight(text, lexer, formatter) | |
48 | # strip wrapping div |
|
53 | # strip wrapping div | |
49 | colorized = colorized[:colorized.find('\n</pre>')] |
|
54 | colorized = colorized[:colorized.find('\n</pre>')] | |
50 | colorized = colorized[colorized.find('<pre>')+5:] |
|
55 | colorized = colorized[colorized.find('<pre>')+5:] | |
51 | coloriter = iter(colorized.splitlines()) |
|
56 | coloriter = iter(colorized.splitlines()) | |
52 |
|
57 | |||
53 | filters['colorize'] = lambda x: coloriter.next() |
|
58 | filters['colorize'] = lambda x: coloriter.next() | |
54 |
|
59 | |||
55 | oldl = tmpl.cache[field] |
|
60 | oldl = tmpl.cache[field] | |
56 | newl = oldl.replace('line|escape', 'line|colorize') |
|
61 | newl = oldl.replace('line|escape', 'line|colorize') | |
57 | tmpl.cache[field] = newl |
|
62 | tmpl.cache[field] = newl |
General Comments 0
You need to be logged in to leave comments.
Login now