annotate hgext/highlight/__init__.py @ 7216:292fb2ad2846

extensions: use new wrapper functions
author Matt Mackall <mpm@selenic.com>
date Wed, 22 Oct 2008 17:34:52 -0500
parents 9df67ee30ef5
children 7fc30044b514
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6987
diff changeset
1 """syntax highlighting in hgweb, based on Pygments
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
2
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
3 It depends on the pygments syntax highlighting library:
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
4 http://pygments.org/
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
5
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
6 To enable the extension add this to hgrc:
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
7
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
8 [extensions]
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
9 hgext.highlight =
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
10
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
11 There is a single configuration option:
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
12
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
13 [web]
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
14 pygments_style = <style>
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
15
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
16 The default is 'colorful'.
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
17
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
18 -- Adam Hupp <adam@hupp.org>
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
19 """
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
20
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
21 import highlight
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
22 from mercurial.hgweb import webcommands, webutil, common
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
23 from mercurial import extensions
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
24
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
25 def filerevision_highlight(orig, web, tmpl, fctx):
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
26 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
27 # only pygmentize for mimetype containing 'html' so we both match
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
28 # 'text/html' and possibly 'application/xhtml+xml' in the future
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
29 # so that we don't have to touch the extension when the mimetype
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
30 # for a template changes; also hgweb optimizes the case that a
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
31 # raw file is sent using rawfile() and doesn't call us, so we
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
32 # can't clash with the file's content-type here in case we
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
33 # pygmentize a html file
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
34 if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
35 style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
36 highlight.pygmentize('fileline', fctx, style, tmpl)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
37 return orig(web, tmpl, fctx)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
38
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
39 def annotate_highlight(orig, web, req, tmpl):
6987
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
40 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
41 if 'html' in mt:
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
42 fctx = webutil.filectx(web.repo, req)
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
43 style = web.config('web', 'pygments_style', 'colorful')
d09e813b21e3 highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents: 6938
diff changeset
44 highlight.pygmentize('annotateline', fctx, style, tmpl)
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
45 return orig(web, req, tmpl)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
46
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
47 def generate_css(web, req, tmpl):
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
48 pg_style = web.config('web', 'pygments_style', 'colorful')
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
49 fmter = highlight.HtmlFormatter(style = pg_style)
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
50 req.respond(common.HTTP_OK, 'text/css')
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
51 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
52
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
53 # monkeypatch in the new version
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
54 extensions.wrapfunction(webcommands, '_filerevision', filerevision_highlight)
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7127
diff changeset
55 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
6938
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
56 webcommands.highlightcss = generate_css
ce94b3236ea4 highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff changeset
57 webcommands.__all__.append('highlightcss')