comparison hgext/highlight/__init__.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents a7abc6081bc5
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should 43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
44 # be specifying the version(s) of Mercurial they are tested with, or 44 # be specifying the version(s) of Mercurial they are tested with, or
45 # leave the attribute unspecified. 45 # leave the attribute unspecified.
46 testedwith = 'ships-with-hg-core' 46 testedwith = 'ships-with-hg-core'
47 47
48
48 def pygmentize(web, field, fctx, tmpl): 49 def pygmentize(web, field, fctx, tmpl):
49 style = web.config('web', 'pygments_style', 'colorful') 50 style = web.config('web', 'pygments_style', 'colorful')
50 expr = web.config('web', 'highlightfiles', "size('<5M')") 51 expr = web.config('web', 'highlightfiles', "size('<5M')")
51 filenameonly = web.configbool('web', 'highlightonlymatchfilename', False) 52 filenameonly = web.configbool('web', 'highlightonlymatchfilename', False)
52 53
53 ctx = fctx.changectx() 54 ctx = fctx.changectx()
54 m = ctx.matchfileset(expr) 55 m = ctx.matchfileset(expr)
55 if m(fctx.path()): 56 if m(fctx.path()):
56 highlight.pygmentize(field, fctx, style, tmpl, 57 highlight.pygmentize(
57 guessfilenameonly=filenameonly) 58 field, fctx, style, tmpl, guessfilenameonly=filenameonly
59 )
60
58 61
59 def filerevision_highlight(orig, web, fctx): 62 def filerevision_highlight(orig, web, fctx):
60 mt = web.res.headers['Content-Type'] 63 mt = web.res.headers['Content-Type']
61 # only pygmentize for mimetype containing 'html' so we both match 64 # only pygmentize for mimetype containing 'html' so we both match
62 # 'text/html' and possibly 'application/xhtml+xml' in the future 65 # 'text/html' and possibly 'application/xhtml+xml' in the future
68 if 'html' in mt: 71 if 'html' in mt:
69 pygmentize(web, 'fileline', fctx, web.tmpl) 72 pygmentize(web, 'fileline', fctx, web.tmpl)
70 73
71 return orig(web, fctx) 74 return orig(web, fctx)
72 75
76
73 def annotate_highlight(orig, web): 77 def annotate_highlight(orig, web):
74 mt = web.res.headers['Content-Type'] 78 mt = web.res.headers['Content-Type']
75 if 'html' in mt: 79 if 'html' in mt:
76 fctx = webutil.filectx(web.repo, web.req) 80 fctx = webutil.filectx(web.repo, web.req)
77 pygmentize(web, 'annotateline', fctx, web.tmpl) 81 pygmentize(web, 'annotateline', fctx, web.tmpl)
78 82
79 return orig(web) 83 return orig(web)
80 84
85
81 def generate_css(web): 86 def generate_css(web):
82 pg_style = web.config('web', 'pygments_style', 'colorful') 87 pg_style = web.config('web', 'pygments_style', 'colorful')
83 fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style)) 88 fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style))
84 web.res.headers['Content-Type'] = 'text/css' 89 web.res.headers['Content-Type'] = 'text/css'
85 style_defs = fmter.get_style_defs(pycompat.sysstr('')) 90 style_defs = fmter.get_style_defs(pycompat.sysstr(''))
86 web.res.setbodybytes(''.join([ 91 web.res.setbodybytes(
87 '/* pygments_style = %s */\n\n' % pg_style, 92 ''.join(
88 pycompat.bytestr(style_defs), 93 [
89 ])) 94 '/* pygments_style = %s */\n\n' % pg_style,
95 pycompat.bytestr(style_defs),
96 ]
97 )
98 )
90 return web.res.sendresponse() 99 return web.res.sendresponse()
100
91 101
92 def extsetup(ui): 102 def extsetup(ui):
93 # monkeypatch in the new version 103 # monkeypatch in the new version
94 extensions.wrapfunction(webcommands, '_filerevision', 104 extensions.wrapfunction(
95 filerevision_highlight) 105 webcommands, '_filerevision', filerevision_highlight
106 )
96 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight) 107 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
97 webcommands.highlightcss = generate_css 108 webcommands.highlightcss = generate_css
98 webcommands.__all__.append('highlightcss') 109 webcommands.__all__.append('highlightcss')