|
1 # highlight - syntax highlighting in hgweb, based on Pygments |
|
2 # |
|
3 # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others |
|
4 # |
|
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. |
|
7 # |
|
8 # The original module was split in an interface and an implementation |
|
9 # file to defer pygments loading and speedup extension setup. |
|
10 |
|
11 """syntax highlighting for hgweb (requires Pygments) |
|
12 |
|
13 It depends on the Pygments syntax highlighting library: |
|
14 http://pygments.org/ |
|
15 |
|
16 There is a single configuration option:: |
|
17 |
|
18 [web] |
|
19 pygments_style = <style> |
|
20 |
|
21 The default is 'colorful'. |
|
22 """ |
|
23 |
|
24 import highlight |
|
25 from mercurial.hgweb import webcommands, webutil, common |
|
26 from mercurial import extensions, encoding |
|
27 |
|
28 def filerevision_highlight(orig, web, tmpl, fctx): |
|
29 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
|
30 # only pygmentize for mimetype containing 'html' so we both match |
|
31 # 'text/html' and possibly 'application/xhtml+xml' in the future |
|
32 # so that we don't have to touch the extension when the mimetype |
|
33 # for a template changes; also hgweb optimizes the case that a |
|
34 # raw file is sent using rawfile() and doesn't call us, so we |
|
35 # can't clash with the file's content-type here in case we |
|
36 # pygmentize a html file |
|
37 if 'html' in mt: |
|
38 style = web.config('web', 'pygments_style', 'colorful') |
|
39 highlight.pygmentize('fileline', fctx, style, tmpl) |
|
40 return orig(web, tmpl, fctx) |
|
41 |
|
42 def annotate_highlight(orig, web, req, tmpl): |
|
43 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
|
44 if 'html' in mt: |
|
45 fctx = webutil.filectx(web.repo, req) |
|
46 style = web.config('web', 'pygments_style', 'colorful') |
|
47 highlight.pygmentize('annotateline', fctx, style, tmpl) |
|
48 return orig(web, req, tmpl) |
|
49 |
|
50 def generate_css(web, req, tmpl): |
|
51 pg_style = web.config('web', 'pygments_style', 'colorful') |
|
52 fmter = highlight.HtmlFormatter(style = pg_style) |
|
53 req.respond(common.HTTP_OK, 'text/css') |
|
54 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] |
|
55 |
|
56 def extsetup(): |
|
57 # monkeypatch in the new version |
|
58 extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight) |
|
59 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) |
|
60 webcommands.highlightcss = generate_css |
|
61 webcommands.__all__.append('highlightcss') |