author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
Sat, 18 Oct 2008 16:56:39 +0200 | |
changeset 7127 | 9df67ee30ef5 |
parent 6987 | d09e813b21e3 |
child 7216 | 292fb2ad2846 |
permissions | -rw-r--r-- |
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 |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
23 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
24 |
web_filerevision = webcommands._filerevision |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
25 |
web_annotate = webcommands.annotate |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
26 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
27 |
def filerevision_highlight(web, tmpl, fctx): |
6987
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
28 |
mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
29 |
# 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
|
30 |
# '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
|
31 |
# 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
|
32 |
# 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
|
33 |
# 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
|
34 |
# 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
|
35 |
# pygmentize a html file |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
36 |
if 'html' in mt: |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
37 |
style = web.config('web', 'pygments_style', 'colorful') |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
38 |
highlight.pygmentize('fileline', fctx, style, tmpl) |
6938
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
39 |
return web_filerevision(web, tmpl, fctx) |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
40 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
41 |
def annotate_highlight(web, req, tmpl): |
6987
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
42 |
mt = ''.join(tmpl('mimetype', encoding=web.encoding)) |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
43 |
if 'html' in mt: |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
44 |
fctx = webutil.filectx(web.repo, req) |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
45 |
style = web.config('web', 'pygments_style', 'colorful') |
d09e813b21e3
highlight: only pygmentize for HTML mimetypes
Rocco Rutte <pdmef@gmx.net>
parents:
6938
diff
changeset
|
46 |
highlight.pygmentize('annotateline', fctx, style, tmpl) |
6938
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
47 |
return web_annotate(web, req, tmpl) |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
48 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
49 |
def generate_css(web, req, tmpl): |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
50 |
pg_style = web.config('web', 'pygments_style', 'colorful') |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
51 |
fmter = highlight.HtmlFormatter(style = pg_style) |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
52 |
req.respond(common.HTTP_OK, 'text/css') |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
53 |
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
|
54 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
55 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
56 |
# monkeypatch in the new version |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
57 |
|
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
58 |
webcommands._filerevision = filerevision_highlight |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
59 |
webcommands.annotate = annotate_highlight |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
60 |
webcommands.highlightcss = generate_css |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
diff
changeset
|
61 |
webcommands.__all__.append('highlightcss') |