Show More
@@ -1,108 +1,108 b'' | |||
|
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 | |
|
30 | 30 | from . import highlight |
|
31 | 31 | from mercurial.hgweb import ( |
|
32 | 32 | webcommands, |
|
33 | 33 | webutil, |
|
34 | 34 | ) |
|
35 | 35 | |
|
36 | 36 | from mercurial import ( |
|
37 | 37 | extensions, |
|
38 | 38 | pycompat, |
|
39 | 39 | ) |
|
40 | 40 | |
|
41 | 41 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
42 | 42 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
43 | 43 | # be specifying the version(s) of Mercurial they are tested with, or |
|
44 | 44 | # leave the attribute unspecified. |
|
45 | 45 | testedwith = b'ships-with-hg-core' |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | def pygmentize(web, field, fctx, tmpl): |
|
49 | 49 | style = web.config(b'web', b'pygments_style', b'colorful') |
|
50 | 50 | expr = web.config(b'web', b'highlightfiles', b"size('<5M')") |
|
51 | 51 | filenameonly = web.configbool(b'web', b'highlightonlymatchfilename', False) |
|
52 | 52 | |
|
53 | 53 | ctx = fctx.changectx() |
|
54 | 54 | m = ctx.matchfileset(fctx.repo().root, expr) |
|
55 | 55 | if m(fctx.path()): |
|
56 | 56 | highlight.pygmentize( |
|
57 | 57 | field, fctx, style, tmpl, guessfilenameonly=filenameonly |
|
58 | 58 | ) |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | def filerevision_highlight(orig, web, fctx): |
|
62 | 62 | mt = web.res.headers[b'Content-Type'] |
|
63 | 63 | # only pygmentize for mimetype containing 'html' so we both match |
|
64 | 64 | # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
65 | 65 | # so that we don't have to touch the extension when the mimetype |
|
66 | 66 | # for a template changes; also hgweb optimizes the case that a |
|
67 | 67 | # raw file is sent using rawfile() and doesn't call us, so we |
|
68 | 68 | # can't clash with the file's content-type here in case we |
|
69 | 69 | # pygmentize a html file |
|
70 | 70 | if b'html' in mt: |
|
71 | 71 | pygmentize(web, b'fileline', fctx, web.tmpl) |
|
72 | 72 | |
|
73 | 73 | return orig(web, fctx) |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | def annotate_highlight(orig, web): |
|
77 | 77 | mt = web.res.headers[b'Content-Type'] |
|
78 | 78 | if b'html' in mt: |
|
79 | 79 | fctx = webutil.filectx(web.repo, web.req) |
|
80 | 80 | pygmentize(web, b'annotateline', fctx, web.tmpl) |
|
81 | 81 | |
|
82 | 82 | return orig(web) |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | def generate_css(web): |
|
86 | 86 | pg_style = web.config(b'web', b'pygments_style', b'colorful') |
|
87 | 87 | fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style)) |
|
88 | 88 | web.res.headers[b'Content-Type'] = b'text/css' |
|
89 | 89 | style_defs = fmter.get_style_defs(pycompat.sysstr(b'')) |
|
90 | 90 | web.res.setbodybytes( |
|
91 | 91 | b''.join( |
|
92 | 92 | [ |
|
93 | 93 | b'/* pygments_style = %s */\n\n' % pg_style, |
|
94 | 94 | pycompat.bytestr(style_defs), |
|
95 | 95 | ] |
|
96 | 96 | ) |
|
97 | 97 | ) |
|
98 | 98 | return web.res.sendresponse() |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | def extsetup(ui): |
|
102 | 102 | # monkeypatch in the new version |
|
103 | 103 | extensions.wrapfunction( |
|
104 |
webcommands, |
|
|
104 | webcommands, '_filerevision', filerevision_highlight | |
|
105 | 105 | ) |
|
106 |
extensions.wrapfunction(webcommands, |
|
|
106 | extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) | |
|
107 | 107 | webcommands.highlightcss = generate_css |
|
108 | 108 | webcommands.__all__.append(b'highlightcss') |
General Comments 0
You need to be logged in to leave comments.
Login now