[PATCH 1 of 3] highlight: consolidate duplicate code

Gregory Szorc gregory.szorc at gmail.com
Thu Oct 15 01:22:33 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1444869727 25200
#      Wed Oct 14 17:42:07 2015 -0700
# Node ID 7269a021a45143109239459fbedca6c6bd408367
# Parent  07db7e95c464537aeb2dd7aba39de0813eaffd04
highlight: consolidate duplicate code

I'm adding some logic in a future patch and this will make it so I only
have to add it once.

diff --git a/hgext/highlight/__init__.py b/hgext/highlight/__init__.py
--- a/hgext/highlight/__init__.py
+++ b/hgext/highlight/__init__.py
@@ -34,8 +34,14 @@ def checkfctx(fctx, expr):
     tree = fileset.parse(expr)
     mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None)
     return fctx.path() in fileset.getset(mctx, tree)
 
+def pygmentize(web, field, fctx, tmpl):
+    style = web.config('web', 'pygments_style', 'colorful')
+    expr = web.config('web', 'highlightfiles', "size('<5M')")
+    if checkfctx(fctx, expr):
+        highlight.pygmentize(field, fctx, style, tmpl)
+
 def filerevision_highlight(orig, web, req, tmpl, fctx):
     mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
     # only pygmentize for mimetype containing 'html' so we both match
     # 'text/html' and possibly 'application/xhtml+xml' in the future
@@ -44,22 +50,18 @@ def filerevision_highlight(orig, web, re
     # raw file is sent using rawfile() and doesn't call us, so we
     # can't clash with the file's content-type here in case we
     # pygmentize a html file
     if 'html' in mt:
-        style = web.config('web', 'pygments_style', 'colorful')
-        expr = web.config('web', 'highlightfiles', "size('<5M')")
-        if checkfctx(fctx, expr):
-            highlight.pygmentize('fileline', fctx, style, tmpl)
+        pygmentize(web, 'fileline', fctx, tmpl)
+
     return orig(web, req, tmpl, fctx)
 
 def annotate_highlight(orig, web, req, tmpl):
     mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
     if 'html' in mt:
         fctx = webutil.filectx(web.repo, req)
-        style = web.config('web', 'pygments_style', 'colorful')
-        expr = web.config('web', 'highlightfiles', "size('<5M')")
-        if checkfctx(fctx, expr):
-            highlight.pygmentize('annotateline', fctx, style, tmpl)
+        pygmentize(web, 'annotateline', fctx, tmpl)
+
     return orig(web, req, tmpl)
 
 def generate_css(web, req, tmpl):
     pg_style = web.config('web', 'pygments_style', 'colorful')


More information about the Mercurial-devel mailing list