Show More
@@ -1,81 +1,77 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 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
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 for hgweb (requires Pygments) |
|
11 | """syntax highlighting for hgweb (requires 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 are two configuration options:: |
|
16 | There are two configuration options:: | |
17 |
|
17 | |||
18 | [web] |
|
18 | [web] | |
19 | pygments_style = <style> (default: colorful) |
|
19 | pygments_style = <style> (default: colorful) | |
20 | highlightfiles = <fileset> (default: size('<5M')) |
|
20 | highlightfiles = <fileset> (default: size('<5M')) | |
21 | """ |
|
21 | """ | |
22 |
|
22 | |||
23 | import highlight |
|
23 | import highlight | |
24 | from mercurial.hgweb import webcommands, webutil, common |
|
24 | from mercurial.hgweb import webcommands, webutil, common | |
25 | from mercurial import extensions, encoding, fileset |
|
25 | from mercurial import extensions, encoding, fileset | |
26 | # Note for extension authors: ONLY specify testedwith = 'internal' for |
|
26 | # Note for extension authors: ONLY specify testedwith = 'internal' for | |
27 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
27 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | |
28 | # be specifying the version(s) of Mercurial they are tested with, or |
|
28 | # be specifying the version(s) of Mercurial they are tested with, or | |
29 | # leave the attribute unspecified. |
|
29 | # leave the attribute unspecified. | |
30 | testedwith = 'internal' |
|
30 | testedwith = 'internal' | |
31 |
|
31 | |||
32 | def checkfctx(fctx, expr): |
|
32 | def checkfctx(fctx, expr): | |
33 | ctx = fctx.changectx() |
|
33 | ctx = fctx.changectx() | |
34 | tree = fileset.parse(expr) |
|
34 | tree = fileset.parse(expr) | |
35 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) |
|
35 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) | |
36 | repo = ctx.repo() |
|
|||
37 | # To allow matching file names in the fileset in hgweb directory mode. |
|
|||
38 | # See issue4568. |
|
|||
39 | object.__setattr__(repo, 'getcwd', lambda: repo.root) |
|
|||
40 | return fctx.path() in fileset.getset(mctx, tree) |
|
36 | return fctx.path() in fileset.getset(mctx, tree) | |
41 |
|
37 | |||
42 | def filerevision_highlight(orig, web, req, tmpl, fctx): |
|
38 | def filerevision_highlight(orig, web, req, tmpl, fctx): | |
43 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
|
39 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) | |
44 | # only pygmentize for mimetype containing 'html' so we both match |
|
40 | # only pygmentize for mimetype containing 'html' so we both match | |
45 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
41 | # 'text/html' and possibly 'application/xhtml+xml' in the future | |
46 | # so that we don't have to touch the extension when the mimetype |
|
42 | # so that we don't have to touch the extension when the mimetype | |
47 | # for a template changes; also hgweb optimizes the case that a |
|
43 | # for a template changes; also hgweb optimizes the case that a | |
48 | # raw file is sent using rawfile() and doesn't call us, so we |
|
44 | # raw file is sent using rawfile() and doesn't call us, so we | |
49 | # can't clash with the file's content-type here in case we |
|
45 | # can't clash with the file's content-type here in case we | |
50 | # pygmentize a html file |
|
46 | # pygmentize a html file | |
51 | if 'html' in mt: |
|
47 | if 'html' in mt: | |
52 | style = web.config('web', 'pygments_style', 'colorful') |
|
48 | style = web.config('web', 'pygments_style', 'colorful') | |
53 | expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
49 | expr = web.config('web', 'highlightfiles', "size('<5M')") | |
54 | if checkfctx(fctx, expr): |
|
50 | if checkfctx(fctx, expr): | |
55 | highlight.pygmentize('fileline', fctx, style, tmpl) |
|
51 | highlight.pygmentize('fileline', fctx, style, tmpl) | |
56 | return orig(web, req, tmpl, fctx) |
|
52 | return orig(web, req, tmpl, fctx) | |
57 |
|
53 | |||
58 | def annotate_highlight(orig, web, req, tmpl): |
|
54 | def annotate_highlight(orig, web, req, tmpl): | |
59 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
|
55 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) | |
60 | if 'html' in mt: |
|
56 | if 'html' in mt: | |
61 | fctx = webutil.filectx(web.repo, req) |
|
57 | fctx = webutil.filectx(web.repo, req) | |
62 | style = web.config('web', 'pygments_style', 'colorful') |
|
58 | style = web.config('web', 'pygments_style', 'colorful') | |
63 | expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
59 | expr = web.config('web', 'highlightfiles', "size('<5M')") | |
64 | if checkfctx(fctx, expr): |
|
60 | if checkfctx(fctx, expr): | |
65 | highlight.pygmentize('annotateline', fctx, style, tmpl) |
|
61 | highlight.pygmentize('annotateline', fctx, style, tmpl) | |
66 | return orig(web, req, tmpl) |
|
62 | return orig(web, req, tmpl) | |
67 |
|
63 | |||
68 | def generate_css(web, req, tmpl): |
|
64 | def generate_css(web, req, tmpl): | |
69 | pg_style = web.config('web', 'pygments_style', 'colorful') |
|
65 | pg_style = web.config('web', 'pygments_style', 'colorful') | |
70 | fmter = highlight.HtmlFormatter(style=pg_style) |
|
66 | fmter = highlight.HtmlFormatter(style=pg_style) | |
71 | req.respond(common.HTTP_OK, 'text/css') |
|
67 | req.respond(common.HTTP_OK, 'text/css') | |
72 | return ['/* pygments_style = %s */\n\n' % pg_style, |
|
68 | return ['/* pygments_style = %s */\n\n' % pg_style, | |
73 | fmter.get_style_defs('')] |
|
69 | fmter.get_style_defs('')] | |
74 |
|
70 | |||
75 | def extsetup(): |
|
71 | def extsetup(): | |
76 | # monkeypatch in the new version |
|
72 | # monkeypatch in the new version | |
77 | extensions.wrapfunction(webcommands, '_filerevision', |
|
73 | extensions.wrapfunction(webcommands, '_filerevision', | |
78 | filerevision_highlight) |
|
74 | filerevision_highlight) | |
79 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
75 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
80 | webcommands.highlightcss = generate_css |
|
76 | webcommands.highlightcss = generate_css | |
81 | webcommands.__all__.append('highlightcss') |
|
77 | webcommands.__all__.append('highlightcss') |
General Comments 0
You need to be logged in to leave comments.
Login now