diff --git a/hgext/highlight/__init__.py b/hgext/highlight/__init__.py --- a/hgext/highlight/__init__.py +++ b/hgext/highlight/__init__.py @@ -28,7 +28,6 @@ from __future__ import absolute_import -from . import highlight from mercurial.hgweb import ( common, webcommands, @@ -47,6 +46,21 @@ # leave the attribute unspecified. testedwith = 'ships-with-hg-core' +# Hide highlight ImportError at import time, so this module does not have +# unwanted side-effect if pygments is not installed. This affects `hg help` +# which loads every extension. +highlight = None + +def _loadhighlight(): + global highlight + if highlight is None: + from . import highlight + +try: + _loadhighlight() +except ImportError: + pass + def pygmentize(web, field, fctx, tmpl): style = web.config('web', 'pygments_style', 'colorful') expr = web.config('web', 'highlightfiles', "size('<5M')") @@ -56,6 +70,7 @@ tree = fileset.parse(expr) mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) if fctx.path() in fileset.getset(mctx, tree): + _loadhighlight() highlight.pygmentize(field, fctx, style, tmpl, guessfilenameonly=filenameonly) @@ -83,6 +98,7 @@ def generate_css(web, req, tmpl): pg_style = web.config('web', 'pygments_style', 'colorful') + _loadhighlight() fmter = highlight.HtmlFormatter(style=pg_style) req.respond(common.HTTP_OK, 'text/css') return ['/* pygments_style = %s */\n\n' % pg_style,