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