##// END OF EJS Templates
localrepo: keep the UTF-8 version of branchcache around
localrepo: keep the UTF-8 version of branchcache around

File last commit:

r5991:31726c27 default
r6119:b8919d40 default
Show More
highlight.py
139 lines | 4.1 KiB | text/x-python | PythonLexer
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 """
This is Mercurial extension for syntax highlighting in the file
revision view of hgweb.
It depends on the pygments syntax highlighting library:
http://pygments.org/
To enable the extension add this to hgrc:
[extensions]
hgext.highlight =
There is a single configuration option:
[web]
pygments_style = <style>
The default is 'colorful'. If this is changed the corresponding CSS
file should be re-generated by running
# pygmentize -f html -S <newstyle>
-- Adam Hupp <adam@hupp.org>
"""
from mercurial import demandimport
demandimport.ignore.extend(['pkgutil',
'pkg_resources',
'__main__',])
import mimetypes
from mercurial.hgweb import hgweb_mod
from mercurial.hgweb.hgweb_mod import hgweb
from mercurial import util
from mercurial.hgweb.common import paritygen
from mercurial.node import hex
from pygments import highlight
from pygments.util import ClassNotFound
from pygments.lexers import guess_lexer_for_filename, TextLexer
from pygments.formatters import HtmlFormatter
Bryan O'Sullivan
highlight: clean up coding style a little
r5533 SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
'type="text/css" />')
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
class StripedHtmlFormatter(HtmlFormatter):
def __init__(self, stripecount, *args, **kwargs):
super(StripedHtmlFormatter, self).__init__(*args, **kwargs)
self.stripecount = stripecount
Bryan O'Sullivan
highlight: clean up coding style a little
r5533
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 def wrap(self, source, outfile):
yield 0, "<div class='highlight'>"
yield 0, "<pre>"
parity = paritygen(self.stripecount)
for n, i in source:
if n == 1:
Bryan O'Sullivan
highlight: clean up coding style a little
r5533 i = "<div class='parity%s'>%s</div>" % (parity.next(), i)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 yield n, i
yield 0, "</pre>"
yield 0, "</div>"
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 def pygments_format(filename, text, forcetext, stripecount, style):
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 if not forcetext:
try:
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 lexer = guess_lexer_for_filename(filename, text,
Christian Ebert
highlight: pass encoding to lexers and formatter...
r5655 encoding=util._encoding)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 except ClassNotFound:
Christian Ebert
highlight: pass encoding to lexers and formatter...
r5655 lexer = TextLexer(encoding=util._encoding)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 else:
Christian Ebert
highlight: pass encoding to lexers and formatter...
r5655 lexer = TextLexer(encoding=util._encoding)
Bryan O'Sullivan
highlight: clean up coding style a little
r5533
formatter = StripedHtmlFormatter(stripecount, style=style,
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 linenos='inline', encoding=util._encoding)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 return highlight(text, lexer, formatter)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 def filerevision_pygments(self, tmpl, fctx):
Bryan O'Sullivan
highlight: clean up coding style a little
r5533 """Reimplement hgweb.filerevision to use syntax highlighting"""
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 f = fctx.path()
Christian Ebert
highlight: update according to latest hgweb_mod changes...
r5991 text = fctx.data()
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 fl = fctx.filelog()
n = fctx.filenode()
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 if util.binary(text):
Christian Ebert
highlight: update according to latest hgweb_mod changes...
r5991 mt = mimetypes.guess_type(f)[0] or 'application/octet-stream'
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 text = "(binary:%s)" % mt
# don't parse (binary:...) as anything
forcetext = True
else:
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 # encode to hgweb.encoding for lexers and formatter
Christian Ebert
highlight: update according to latest hgweb_mod changes...
r5991 util._encoding = self.encoding
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 text = util.tolocal(text)
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 forcetext = False
def lines(text):
for line in text.splitlines(True):
yield {"line": line}
style = self.config("web", "pygments_style", "colorful")
Christian Ebert
highlight: fix more tracebacks by forcing util._encoding to hgweb.encoding...
r5748 text_formatted = lines(pygments_format(f, text, forcetext,
Christian Ebert
highlight: mandatory arguments where possible
r5654 self.stripecount, style))
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
Bryan O'Sullivan
highlight: clean up coding style a little
r5533 # override per-line template
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 tmpl.cache['fileline'] = '#line#'
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
# append a <link ...> to the syntax highlighting css
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 old_header = ''.join(tmpl('header'))
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 if SYNTAX_CSS not in old_header:
new_header = old_header + SYNTAX_CSS
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 tmpl.cache['header'] = new_header
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
Christian Ebert
highlight: update according to latest hgweb_mod changes...
r5991 return tmpl("filerevision",
Christian Ebert
highlight: adapt to hgweb_mode refactoring
r5616 file=f,
path=hgweb_mod._up(f), # fixme: make public
text=text_formatted,
rev=fctx.rev(),
node=hex(fctx.node()),
author=fctx.user(),
date=fctx.date(),
desc=fctx.description(),
parent=self.siblings(fctx.parents()),
child=self.siblings(fctx.children()),
rename=self.renamelink(fl, n),
permissions=fctx.manifest().flags(f))
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532
Bryan O'Sullivan
highlight: clean up coding style a little
r5533
Adam Hupp
extension for synax highlighting in the hgweb file revision view...
r5532 # monkeypatch in the new version
# should be safer than overriding the method in a derived class
# and then patching the class
hgweb.filerevision = filerevision_pygments