2
This is Mercurial extension for syntax highlighting in the file
3
revision view of hgweb.
5
It depends on the pygments syntax highlighting library:
8
To enable the extension add this to hgrc:
13
There is a single configuration option:
16
pygments_style = <style>
18
The default is 'colorful'. If this is changed the corresponding CSS
19
file should be re-generated by running
21
# pygmentize -f html -S <newstyle>
28
from mercurial import demandimport
29
demandimport.ignore.extend(['pkgutil',
33
from mercurial.hgweb.hgweb_mod import hgweb
34
from mercurial import util
35
from mercurial.templatefilters import filters
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
42
SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
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
56
style = self.config("web", "pygments_style", "colorful")
57
# To get multi-line strings right, we can't format line-by-line
59
lexer = guess_lexer_for_filename(fctx.path(), text,
60
encoding=util._encoding)
61
except (ClassNotFound, ValueError):
63
lexer = guess_lexer(text, encoding=util._encoding)
64
except (ClassNotFound, ValueError):
65
lexer = TextLexer(encoding=util._encoding)
67
formatter = HtmlFormatter(style=style, encoding=util._encoding)
69
colorized = highlight(text, lexer, formatter)
71
colorized = colorized[:colorized.find('\n</pre>')]
72
colorized = colorized[colorized.find('<pre>')+5:]
73
coloriter = iter(colorized.splitlines())
75
filters['colorize'] = lambda x: coloriter.next()
77
oldl = tmpl.cache[field]
78
newl = oldl.replace('line|escape', 'line|colorize')
79
tmpl.cache[field] = newl
81
def filerevision_highlight(self, tmpl, fctx):
82
pygmentize(self, tmpl, fctx, 'fileline')
84
return realrevision(self, tmpl, fctx)
86
def fileannotate_highlight(self, tmpl, fctx):
87
pygmentize(self, tmpl, fctx, 'annotateline')
89
return realannotate(self, tmpl, fctx)
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