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 is a single configuration option:: |
16 There are two configuration options:: |
17 |
17 |
18 [web] |
18 [web] |
19 pygments_style = <style> |
19 pygments_style = <style> (default: colorful) |
20 |
20 highlightfiles = <fileset> (default: size('<5M')) |
21 The default is 'colorful'. |
|
22 """ |
21 """ |
23 |
22 |
24 import highlight |
23 import highlight |
25 from mercurial.hgweb import webcommands, webutil, common |
24 from mercurial.hgweb import webcommands, webutil, common |
26 from mercurial import extensions, encoding |
25 from mercurial import extensions, encoding, fileset |
27 # Note for extension authors: ONLY specify testedwith = 'internal' for |
26 # Note for extension authors: ONLY specify testedwith = 'internal' for |
28 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
27 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
29 # 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 |
30 # leave the attribute unspecified. |
29 # leave the attribute unspecified. |
31 testedwith = 'internal' |
30 testedwith = 'internal' |
|
31 |
|
32 def checkfctx(fctx, expr): |
|
33 ctx = fctx.changectx() |
|
34 tree = fileset.parse(expr) |
|
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) |
32 |
41 |
33 def filerevision_highlight(orig, web, req, tmpl, fctx): |
42 def filerevision_highlight(orig, web, req, tmpl, fctx): |
34 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
43 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
35 # only pygmentize for mimetype containing 'html' so we both match |
44 # only pygmentize for mimetype containing 'html' so we both match |
36 # 'text/html' and possibly 'application/xhtml+xml' in the future |
45 # 'text/html' and possibly 'application/xhtml+xml' in the future |
39 # raw file is sent using rawfile() and doesn't call us, so we |
48 # raw file is sent using rawfile() and doesn't call us, so we |
40 # can't clash with the file's content-type here in case we |
49 # can't clash with the file's content-type here in case we |
41 # pygmentize a html file |
50 # pygmentize a html file |
42 if 'html' in mt: |
51 if 'html' in mt: |
43 style = web.config('web', 'pygments_style', 'colorful') |
52 style = web.config('web', 'pygments_style', 'colorful') |
44 highlight.pygmentize('fileline', fctx, style, tmpl) |
53 expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
54 if checkfctx(fctx, expr): |
|
55 highlight.pygmentize('fileline', fctx, style, tmpl) |
45 return orig(web, req, tmpl, fctx) |
56 return orig(web, req, tmpl, fctx) |
46 |
57 |
47 def annotate_highlight(orig, web, req, tmpl): |
58 def annotate_highlight(orig, web, req, tmpl): |
48 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
59 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
49 if 'html' in mt: |
60 if 'html' in mt: |
50 fctx = webutil.filectx(web.repo, req) |
61 fctx = webutil.filectx(web.repo, req) |
51 style = web.config('web', 'pygments_style', 'colorful') |
62 style = web.config('web', 'pygments_style', 'colorful') |
52 highlight.pygmentize('annotateline', fctx, style, tmpl) |
63 expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
64 if checkfctx(fctx, expr): |
|
65 highlight.pygmentize('annotateline', fctx, style, tmpl) |
53 return orig(web, req, tmpl) |
66 return orig(web, req, tmpl) |
54 |
67 |
55 def generate_css(web, req, tmpl): |
68 def generate_css(web, req, tmpl): |
56 pg_style = web.config('web', 'pygments_style', 'colorful') |
69 pg_style = web.config('web', 'pygments_style', 'colorful') |
57 fmter = highlight.HtmlFormatter(style=pg_style) |
70 fmter = highlight.HtmlFormatter(style=pg_style) |