author | Matt Mackall <mpm@selenic.com> |
Thu, 26 Jun 2008 13:58:22 -0500 | |
changeset 6745 | ed01fa8ceaa6 |
parent 6666 | 53465a7464e2 |
permissions | -rw-r--r-- |
6666
53465a7464e2
convert comments to docstrings in a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6665
diff
changeset
|
1 |
"""a mercurial extension for syntax highlighting in hgweb |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
2 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
3 |
It depends on the pygments syntax highlighting library: |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
4 |
http://pygments.org/ |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
5 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
6 |
To enable the extension add this to hgrc: |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
7 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
8 |
[extensions] |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
9 |
hgext.highlight = |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
10 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
11 |
There is a single configuration option: |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
12 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
13 |
[web] |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
14 |
pygments_style = <style> |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
15 |
|
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
16 |
The default is 'colorful'. |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
17 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
18 |
-- Adam Hupp <adam@hupp.org> |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
19 |
""" |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
20 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
21 |
from mercurial import demandimport |
6394
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
22 |
demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',]) |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
23 |
|
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
24 |
from mercurial.hgweb import webcommands, webutil, common |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
25 |
from mercurial import util |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
26 |
from mercurial.templatefilters import filters |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
27 |
|
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
28 |
from pygments import highlight |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
29 |
from pygments.util import ClassNotFound |
6199
0068809347d7
highlight: forgot import
Brendan Cully <brendan@kublai.com>
parents:
6198
diff
changeset
|
30 |
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
31 |
from pygments.formatters import HtmlFormatter |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
32 |
|
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
33 |
SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' |
5533
6cf7d7fe7d3d
highlight: clean up coding style a little
Bryan O'Sullivan <bos@serpentine.com>
parents:
5532
diff
changeset
|
34 |
'type="text/css" />') |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
35 |
|
6394
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
36 |
def pygmentize(field, fctx, style, tmpl): |
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
37 |
|
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
38 |
# append a <link ...> to the syntax highlighting css |
5616
88ca3e0fb6e5
highlight: adapt to hgweb_mode refactoring
Christian Ebert <blacktrash@gmx.net>
parents:
5533
diff
changeset
|
39 |
old_header = ''.join(tmpl('header')) |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
40 |
if SYNTAX_CSS not in old_header: |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
41 |
new_header = old_header + SYNTAX_CSS |
5616
88ca3e0fb6e5
highlight: adapt to hgweb_mode refactoring
Christian Ebert <blacktrash@gmx.net>
parents:
5533
diff
changeset
|
42 |
tmpl.cache['header'] = new_header |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
43 |
|
6194
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
44 |
text = fctx.data() |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
45 |
if util.binary(text): |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
46 |
return |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
47 |
|
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
48 |
# To get multi-line strings right, we can't format line-by-line |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
49 |
try: |
6662
22c303a514f8
Truncate input to 1K when using pygments guess_lexer.
Brendan Cully <brendan@kublai.com>
parents:
6494
diff
changeset
|
50 |
lexer = guess_lexer_for_filename(fctx.path(), text[:1024], |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
51 |
encoding=util._encoding) |
6494
c30849d4c8ba
highlight: backward compatibility with pygments 0.5.1
Benoit Allard <benoit@aeteurope.nl>
parents:
6212
diff
changeset
|
52 |
except (ClassNotFound, ValueError): |
6198
358cc9cf54db
highlight: guess by text when path name is ambiguous
Brendan Cully <brendan@kublai.com>
parents:
6197
diff
changeset
|
53 |
try: |
6662
22c303a514f8
Truncate input to 1K when using pygments guess_lexer.
Brendan Cully <brendan@kublai.com>
parents:
6494
diff
changeset
|
54 |
lexer = guess_lexer(text[:1024], encoding=util._encoding) |
6494
c30849d4c8ba
highlight: backward compatibility with pygments 0.5.1
Benoit Allard <benoit@aeteurope.nl>
parents:
6212
diff
changeset
|
55 |
except (ClassNotFound, ValueError): |
6198
358cc9cf54db
highlight: guess by text when path name is ambiguous
Brendan Cully <brendan@kublai.com>
parents:
6197
diff
changeset
|
56 |
lexer = TextLexer(encoding=util._encoding) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
57 |
|
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
58 |
formatter = HtmlFormatter(style=style, encoding=util._encoding) |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
59 |
|
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
60 |
colorized = highlight(text, lexer, formatter) |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
61 |
# strip wrapping div |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
62 |
colorized = colorized[:colorized.find('\n</pre>')] |
6197
f6565f7d9489
highlight: make div trimmer work when lexer fails to identify text.
Brendan Cully <brendan@kublai.com>
parents:
6196
diff
changeset
|
63 |
colorized = colorized[colorized.find('<pre>')+5:] |
6196
fc9535ae6fe2
highlight: use iter() instead of generator comprehension
Brendan Cully <brendan@kublai.com>
parents:
6194
diff
changeset
|
64 |
coloriter = iter(colorized.splitlines()) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
65 |
|
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
66 |
filters['colorize'] = lambda x: coloriter.next() |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
67 |
|
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
68 |
oldl = tmpl.cache[field] |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
69 |
newl = oldl.replace('line|escape', 'line|colorize') |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
70 |
tmpl.cache[field] = newl |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
71 |
|
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
72 |
web_filerevision = webcommands._filerevision |
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
73 |
web_annotate = webcommands.annotate |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
74 |
|
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
75 |
def filerevision_highlight(web, tmpl, fctx): |
6394
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
76 |
style = web.config('web', 'pygments_style', 'colorful') |
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
77 |
pygmentize('fileline', fctx, style, tmpl) |
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
78 |
return web_filerevision(web, tmpl, fctx) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
79 |
|
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
80 |
def annotate_highlight(web, req, tmpl): |
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
81 |
fctx = webutil.filectx(web.repo, req) |
6394
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
82 |
style = web.config('web', 'pygments_style', 'colorful') |
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
83 |
pygmentize('annotateline', fctx, style, tmpl) |
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
84 |
return web_annotate(web, req, tmpl) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
85 |
|
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
86 |
def generate_css(web, req, tmpl): |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
87 |
pg_style = web.config('web', 'pygments_style', 'colorful') |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
88 |
fmter = HtmlFormatter(style = pg_style) |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
89 |
req.respond(common.HTTP_OK, 'text/css') |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
90 |
return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')] |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
91 |
|
5533
6cf7d7fe7d3d
highlight: clean up coding style a little
Bryan O'Sullivan <bos@serpentine.com>
parents:
5532
diff
changeset
|
92 |
|
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
93 |
# monkeypatch in the new version |
6393
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
94 |
|
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
95 |
webcommands._filerevision = filerevision_highlight |
894875eae49b
hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6212
diff
changeset
|
96 |
webcommands.annotate = annotate_highlight |
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
97 |
webcommands.highlightcss = generate_css |
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
98 |
webcommands.__all__.append('highlightcss') |