Show More
@@ -1,99 +1,98 | |||||
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 the following configuration options:: |
|
16 | There are the following 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 | highlightonlymatchfilename = <bool> (default False) |
|
21 | highlightonlymatchfilename = <bool> (default False) | |
22 |
|
22 | |||
23 | ``highlightonlymatchfilename`` will only highlight files if their type could |
|
23 | ``highlightonlymatchfilename`` will only highlight files if their type could | |
24 | be identified by their filename. When this is not enabled (the default), |
|
24 | be identified by their filename. When this is not enabled (the default), | |
25 | Pygments will try very hard to identify the file type from content and any |
|
25 | Pygments will try very hard to identify the file type from content and any | |
26 | match (even matches with a low confidence score) will be used. |
|
26 | match (even matches with a low confidence score) will be used. | |
27 | """ |
|
27 | """ | |
28 |
|
28 | |||
29 | from __future__ import absolute_import |
|
29 | from __future__ import absolute_import | |
30 |
|
30 | |||
31 | from . import highlight |
|
31 | from . import highlight | |
32 | from mercurial.hgweb import ( |
|
32 | from mercurial.hgweb import ( | |
33 | webcommands, |
|
33 | webcommands, | |
34 | webutil, |
|
34 | webutil, | |
35 | ) |
|
35 | ) | |
36 |
|
36 | |||
37 | from mercurial import ( |
|
37 | from mercurial import ( | |
38 | encoding, |
|
|||
39 | extensions, |
|
38 | extensions, | |
40 | fileset, |
|
39 | fileset, | |
41 | ) |
|
40 | ) | |
42 |
|
41 | |||
43 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
42 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | |
44 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
43 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | |
45 | # be specifying the version(s) of Mercurial they are tested with, or |
|
44 | # be specifying the version(s) of Mercurial they are tested with, or | |
46 | # leave the attribute unspecified. |
|
45 | # leave the attribute unspecified. | |
47 | testedwith = 'ships-with-hg-core' |
|
46 | testedwith = 'ships-with-hg-core' | |
48 |
|
47 | |||
49 | def pygmentize(web, field, fctx, tmpl): |
|
48 | def pygmentize(web, field, fctx, tmpl): | |
50 | style = web.config('web', 'pygments_style', 'colorful') |
|
49 | style = web.config('web', 'pygments_style', 'colorful') | |
51 | expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
50 | expr = web.config('web', 'highlightfiles', "size('<5M')") | |
52 | filenameonly = web.configbool('web', 'highlightonlymatchfilename', False) |
|
51 | filenameonly = web.configbool('web', 'highlightonlymatchfilename', False) | |
53 |
|
52 | |||
54 | ctx = fctx.changectx() |
|
53 | ctx = fctx.changectx() | |
55 | tree = fileset.parse(expr) |
|
54 | tree = fileset.parse(expr) | |
56 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) |
|
55 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) | |
57 | if fctx.path() in fileset.getset(mctx, tree): |
|
56 | if fctx.path() in fileset.getset(mctx, tree): | |
58 | highlight.pygmentize(field, fctx, style, tmpl, |
|
57 | highlight.pygmentize(field, fctx, style, tmpl, | |
59 | guessfilenameonly=filenameonly) |
|
58 | guessfilenameonly=filenameonly) | |
60 |
|
59 | |||
61 | def filerevision_highlight(orig, web, fctx): |
|
60 | def filerevision_highlight(orig, web, fctx): | |
62 | mt = ''.join(web.tmpl('mimetype', encoding=encoding.encoding)) |
|
61 | mt = web.res.headers['Content-Type'] | |
63 | # only pygmentize for mimetype containing 'html' so we both match |
|
62 | # only pygmentize for mimetype containing 'html' so we both match | |
64 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
63 | # 'text/html' and possibly 'application/xhtml+xml' in the future | |
65 | # so that we don't have to touch the extension when the mimetype |
|
64 | # so that we don't have to touch the extension when the mimetype | |
66 | # for a template changes; also hgweb optimizes the case that a |
|
65 | # for a template changes; also hgweb optimizes the case that a | |
67 | # raw file is sent using rawfile() and doesn't call us, so we |
|
66 | # raw file is sent using rawfile() and doesn't call us, so we | |
68 | # can't clash with the file's content-type here in case we |
|
67 | # can't clash with the file's content-type here in case we | |
69 | # pygmentize a html file |
|
68 | # pygmentize a html file | |
70 | if 'html' in mt: |
|
69 | if 'html' in mt: | |
71 | pygmentize(web, 'fileline', fctx, web.tmpl) |
|
70 | pygmentize(web, 'fileline', fctx, web.tmpl) | |
72 |
|
71 | |||
73 | return orig(web, fctx) |
|
72 | return orig(web, fctx) | |
74 |
|
73 | |||
75 | def annotate_highlight(orig, web): |
|
74 | def annotate_highlight(orig, web): | |
76 | mt = ''.join(web.tmpl('mimetype', encoding=encoding.encoding)) |
|
75 | mt = web.res.headers['Content-Type'] | |
77 | if 'html' in mt: |
|
76 | if 'html' in mt: | |
78 | fctx = webutil.filectx(web.repo, web.req) |
|
77 | fctx = webutil.filectx(web.repo, web.req) | |
79 | pygmentize(web, 'annotateline', fctx, web.tmpl) |
|
78 | pygmentize(web, 'annotateline', fctx, web.tmpl) | |
80 |
|
79 | |||
81 | return orig(web) |
|
80 | return orig(web) | |
82 |
|
81 | |||
83 | def generate_css(web): |
|
82 | def generate_css(web): | |
84 | pg_style = web.config('web', 'pygments_style', 'colorful') |
|
83 | pg_style = web.config('web', 'pygments_style', 'colorful') | |
85 | fmter = highlight.HtmlFormatter(style=pg_style) |
|
84 | fmter = highlight.HtmlFormatter(style=pg_style) | |
86 | web.res.headers['Content-Type'] = 'text/css' |
|
85 | web.res.headers['Content-Type'] = 'text/css' | |
87 | web.res.setbodybytes(''.join([ |
|
86 | web.res.setbodybytes(''.join([ | |
88 | '/* pygments_style = %s */\n\n' % pg_style, |
|
87 | '/* pygments_style = %s */\n\n' % pg_style, | |
89 | fmter.get_style_defs(''), |
|
88 | fmter.get_style_defs(''), | |
90 | ])) |
|
89 | ])) | |
91 | return web.res.sendresponse() |
|
90 | return web.res.sendresponse() | |
92 |
|
91 | |||
93 | def extsetup(): |
|
92 | def extsetup(): | |
94 | # monkeypatch in the new version |
|
93 | # monkeypatch in the new version | |
95 | extensions.wrapfunction(webcommands, '_filerevision', |
|
94 | extensions.wrapfunction(webcommands, '_filerevision', | |
96 | filerevision_highlight) |
|
95 | filerevision_highlight) | |
97 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
96 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
98 | webcommands.highlightcss = generate_css |
|
97 | webcommands.highlightcss = generate_css | |
99 | webcommands.__all__.append('highlightcss') |
|
98 | webcommands.__all__.append('highlightcss') |
General Comments 0
You need to be logged in to leave comments.
Login now