Show More
@@ -1,62 +1,62 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 Pygments syntax highlighting library: |
|
13 | It depends on the Pygments syntax highlighting library: | |
14 | http://pygments.org/ |
|
14 | http://pygments.org/ | |
15 |
|
15 | |||
16 | There is a single configuration option: |
|
16 | There is a single configuration option: | |
17 |
|
17 | |||
18 | [web] |
|
18 | [web] | |
19 | pygments_style = <style> |
|
19 | pygments_style = <style> | |
20 |
|
20 | |||
21 | The default is 'colorful'. |
|
21 | The default is 'colorful'. | |
22 |
|
22 | |||
23 | -- Adam Hupp <adam@hupp.org> |
|
23 | -- Adam Hupp <adam@hupp.org> | |
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
26 | import highlight |
|
26 | import highlight | |
27 | from mercurial.hgweb import webcommands, webutil, common |
|
27 | from mercurial.hgweb import webcommands, webutil, common | |
28 | from mercurial import extensions |
|
28 | from mercurial import extensions, encoding | |
29 |
|
29 | |||
30 | def filerevision_highlight(orig, web, tmpl, fctx): |
|
30 | def filerevision_highlight(orig, web, tmpl, fctx): | |
31 |
mt = ''.join(tmpl('mimetype', encoding= |
|
31 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) | |
32 | # only pygmentize for mimetype containing 'html' so we both match |
|
32 | # only pygmentize for mimetype containing 'html' so we both match | |
33 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
33 | # 'text/html' and possibly 'application/xhtml+xml' in the future | |
34 | # so that we don't have to touch the extension when the mimetype |
|
34 | # so that we don't have to touch the extension when the mimetype | |
35 | # for a template changes; also hgweb optimizes the case that a |
|
35 | # for a template changes; also hgweb optimizes the case that a | |
36 | # raw file is sent using rawfile() and doesn't call us, so we |
|
36 | # raw file is sent using rawfile() and doesn't call us, so we | |
37 | # can't clash with the file's content-type here in case we |
|
37 | # can't clash with the file's content-type here in case we | |
38 | # pygmentize a html file |
|
38 | # pygmentize a html file | |
39 | if 'html' in mt: |
|
39 | if 'html' in mt: | |
40 | style = web.config('web', 'pygments_style', 'colorful') |
|
40 | style = web.config('web', 'pygments_style', 'colorful') | |
41 | highlight.pygmentize('fileline', fctx, style, tmpl) |
|
41 | highlight.pygmentize('fileline', fctx, style, tmpl) | |
42 | return orig(web, tmpl, fctx) |
|
42 | return orig(web, tmpl, fctx) | |
43 |
|
43 | |||
44 | def annotate_highlight(orig, web, req, tmpl): |
|
44 | def annotate_highlight(orig, web, req, tmpl): | |
45 |
mt = ''.join(tmpl('mimetype', encoding= |
|
45 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) | |
46 | if 'html' in mt: |
|
46 | if 'html' in mt: | |
47 | fctx = webutil.filectx(web.repo, req) |
|
47 | fctx = webutil.filectx(web.repo, req) | |
48 | style = web.config('web', 'pygments_style', 'colorful') |
|
48 | style = web.config('web', 'pygments_style', 'colorful') | |
49 | highlight.pygmentize('annotateline', fctx, style, tmpl) |
|
49 | highlight.pygmentize('annotateline', fctx, style, tmpl) | |
50 | return orig(web, req, tmpl) |
|
50 | return orig(web, req, tmpl) | |
51 |
|
51 | |||
52 | def generate_css(web, req, tmpl): |
|
52 | def generate_css(web, req, tmpl): | |
53 | pg_style = web.config('web', 'pygments_style', 'colorful') |
|
53 | pg_style = web.config('web', 'pygments_style', 'colorful') | |
54 | fmter = highlight.HtmlFormatter(style = pg_style) |
|
54 | fmter = highlight.HtmlFormatter(style = pg_style) | |
55 | req.respond(common.HTTP_OK, 'text/css') |
|
55 | req.respond(common.HTTP_OK, 'text/css') | |
56 | return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] |
|
56 | return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] | |
57 |
|
57 | |||
58 | # monkeypatch in the new version |
|
58 | # monkeypatch in the new version | |
59 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) |
|
59 | extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) | |
60 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
60 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
61 | webcommands.highlightcss = generate_css |
|
61 | webcommands.highlightcss = generate_css | |
62 | webcommands.__all__.append('highlightcss') |
|
62 | webcommands.__all__.append('highlightcss') |
General Comments 0
You need to be logged in to leave comments.
Login now