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