~ubuntu-branches/ubuntu/lucid/mercurial/lucid

« back to all changes in this revision

Viewing changes to hgext/highlight.py

  • Committer: Bazaar Package Importer
  • Author(s): Vernon Tang
  • Date: 2009-01-18 10:39:58 UTC
  • mfrom: (8.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090118103958-4ep2fqb5nl2pyc7y
Tags: 1.1.2-2
* debian/mercurial.postinst: symlink /usr/share/doc/mercurial if dpkg didn't
  do it when upgrading (closes: #512155)
* debian/control: mercurial-common replaces all earlier versions of
  mercurial

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
This is Mercurial extension for syntax highlighting in the file
3
 
revision view of hgweb.
4
 
 
5
 
It depends on the pygments syntax highlighting library:
6
 
http://pygments.org/
7
 
 
8
 
To enable the extension add this to hgrc:
9
 
 
10
 
[extensions]
11
 
hgext.highlight =
12
 
 
13
 
There is a single configuration option:
14
 
 
15
 
[web]
16
 
pygments_style = <style>
17
 
 
18
 
The default is 'colorful'.  If this is changed the corresponding CSS
19
 
file should be re-generated by running
20
 
 
21
 
# pygmentize -f html -S <newstyle>
22
 
 
23
 
 
24
 
 
25
 
 
26
 
"""
27
 
 
28
 
from mercurial import demandimport
29
 
demandimport.ignore.extend(['pkgutil',
30
 
                            'pkg_resources',
31
 
                            '__main__',])
32
 
 
33
 
from mercurial.hgweb.hgweb_mod import hgweb
34
 
from mercurial import util
35
 
from mercurial.templatefilters import filters
36
 
 
37
 
from pygments import highlight
38
 
from pygments.util import ClassNotFound
39
 
from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
40
 
from pygments.formatters import HtmlFormatter
41
 
 
42
 
SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
43
 
              'type="text/css" />')
44
 
 
45
 
def pygmentize(self, tmpl, fctx, field):
46
 
    # append a <link ...> to the syntax highlighting css
47
 
    old_header = ''.join(tmpl('header'))
48
 
    if SYNTAX_CSS not in old_header:
49
 
        new_header =  old_header + SYNTAX_CSS
50
 
        tmpl.cache['header'] = new_header
51
 
 
52
 
    text = fctx.data()
53
 
    if util.binary(text):
54
 
        return
55
 
 
56
 
    style = self.config("web", "pygments_style", "colorful")
57
 
    # To get multi-line strings right, we can't format line-by-line
58
 
    try:
59
 
        lexer = guess_lexer_for_filename(fctx.path(), text,
60
 
                                         encoding=util._encoding)
61
 
    except (ClassNotFound, ValueError):
62
 
        try:
63
 
            lexer = guess_lexer(text, encoding=util._encoding)
64
 
        except (ClassNotFound, ValueError):
65
 
            lexer = TextLexer(encoding=util._encoding)
66
 
 
67
 
    formatter = HtmlFormatter(style=style, encoding=util._encoding)
68
 
 
69
 
    colorized = highlight(text, lexer, formatter)
70
 
    # strip wrapping div
71
 
    colorized = colorized[:colorized.find('\n</pre>')]
72
 
    colorized = colorized[colorized.find('<pre>')+5:]
73
 
    coloriter = iter(colorized.splitlines())
74
 
 
75
 
    filters['colorize'] = lambda x: coloriter.next()
76
 
 
77
 
    oldl = tmpl.cache[field]
78
 
    newl = oldl.replace('line|escape', 'line|colorize')
79
 
    tmpl.cache[field] = newl
80
 
 
81
 
def filerevision_highlight(self, tmpl, fctx):
82
 
    pygmentize(self, tmpl, fctx, 'fileline')
83
 
 
84
 
    return realrevision(self, tmpl, fctx)
85
 
 
86
 
def fileannotate_highlight(self, tmpl, fctx):
87
 
    pygmentize(self, tmpl, fctx, 'annotateline')
88
 
 
89
 
    return realannotate(self, tmpl, fctx)
90
 
 
91
 
# monkeypatch in the new version
92
 
# should be safer than overriding the method in a derived class
93
 
# and then patching the class
94
 
realrevision = hgweb.filerevision
95
 
hgweb.filerevision = filerevision_highlight
96
 
realannotate = hgweb.fileannotate
97
 
hgweb.fileannotate = fileannotate_highlight