Show More
@@ -1,67 +1,67 b'' | |||||
1 | # highlight - syntax highlighting in hgweb, based on Pygments |
|
1 | # highlight - syntax highlighting in hgweb, based on Pygments | |
2 | # |
|
2 | # | |
3 | # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others |
|
3 | # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> 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, incorporated herein by reference. |
|
6 | # GNU General Public License version 2, incorporated herein by reference. | |
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 | """syntax highlighting in hgweb, based on Pygments |
|
11 | """syntax highlighting in hgweb, based on Pygments | |
12 |
|
12 | |||
13 |
It depends on the |
|
13 | It depends on the Pygments syntax highlighting library: | |
14 | http://pygments.org/ |
|
14 | http://pygments.org/ | |
15 |
|
15 | |||
16 | To enable the extension add this to hgrc: |
|
16 | To enable the extension add this to hgrc: | |
17 |
|
17 | |||
18 | [extensions] |
|
18 | [extensions] | |
19 | hgext.highlight = |
|
19 | hgext.highlight = | |
20 |
|
20 | |||
21 | There is a single configuration option: |
|
21 | There is a single configuration option: | |
22 |
|
22 | |||
23 | [web] |
|
23 | [web] | |
24 | pygments_style = <style> |
|
24 | pygments_style = <style> | |
25 |
|
25 | |||
26 | The default is 'colorful'. |
|
26 | The default is 'colorful'. | |
27 |
|
27 | |||
28 | -- Adam Hupp <adam@hupp.org> |
|
28 | -- Adam Hupp <adam@hupp.org> | |
29 | """ |
|
29 | """ | |
30 |
|
30 | |||
31 | import highlight |
|
31 | import highlight | |
32 | from mercurial.hgweb import webcommands, webutil, common |
|
32 | from mercurial.hgweb import webcommands, webutil, common | |
33 | from mercurial import extensions |
|
33 | from mercurial import extensions | |
34 |
|
34 | |||
35 | def filerevision_highlight(orig, web, tmpl, fctx): |
|
35 | def filerevision_highlight(orig, web, tmpl, fctx): | |
36 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
|
36 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) | |
37 | # only pygmentize for mimetype containing 'html' so we both match |
|
37 | # only pygmentize for mimetype containing 'html' so we both match | |
38 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
38 | # 'text/html' and possibly 'application/xhtml+xml' in the future | |
39 | # 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 | |
40 | # for a template changes; also hgweb optimizes the case that a |
|
40 | # for a template changes; also hgweb optimizes the case that a | |
41 | # 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 | |
42 | # 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 | |
43 | # pygmentize a html file |
|
43 | # pygmentize a html file | |
44 | if 'html' in mt: |
|
44 | if 'html' in mt: | |
45 | style = web.config('web', 'pygments_style', 'colorful') |
|
45 | style = web.config('web', 'pygments_style', 'colorful') | |
46 | highlight.pygmentize('fileline', fctx, style, tmpl) |
|
46 | highlight.pygmentize('fileline', fctx, style, tmpl) | |
47 | return orig(web, tmpl, fctx) |
|
47 | return orig(web, tmpl, fctx) | |
48 |
|
48 | |||
49 | def annotate_highlight(orig, web, req, tmpl): |
|
49 | def annotate_highlight(orig, web, req, tmpl): | |
50 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
|
50 | mt = ''.join(tmpl('mimetype', encoding=web.encoding)) | |
51 | if 'html' in mt: |
|
51 | if 'html' in mt: | |
52 | fctx = webutil.filectx(web.repo, req) |
|
52 | fctx = webutil.filectx(web.repo, req) | |
53 | style = web.config('web', 'pygments_style', 'colorful') |
|
53 | style = web.config('web', 'pygments_style', 'colorful') | |
54 | highlight.pygmentize('annotateline', fctx, style, tmpl) |
|
54 | highlight.pygmentize('annotateline', fctx, style, tmpl) | |
55 | return orig(web, req, tmpl) |
|
55 | return orig(web, req, tmpl) | |
56 |
|
56 | |||
57 | def generate_css(web, req, tmpl): |
|
57 | def generate_css(web, req, tmpl): | |
58 | pg_style = web.config('web', 'pygments_style', 'colorful') |
|
58 | pg_style = web.config('web', 'pygments_style', 'colorful') | |
59 | fmter = highlight.HtmlFormatter(style = pg_style) |
|
59 | fmter = highlight.HtmlFormatter(style = pg_style) | |
60 | req.respond(common.HTTP_OK, 'text/css') |
|
60 | req.respond(common.HTTP_OK, 'text/css') | |
61 | 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('')] | |
62 |
|
62 | |||
63 | # monkeypatch in the new version |
|
63 | # monkeypatch in the new version | |
64 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) |
|
64 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) | |
65 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
65 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
66 | webcommands.highlightcss = generate_css |
|
66 | webcommands.highlightcss = generate_css | |
67 | webcommands.__all__.append('highlightcss') |
|
67 | webcommands.__all__.append('highlightcss') |
General Comments 0
You need to be logged in to leave comments.
Login now