comparison hgext/highlight/highlight.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
9 # file to defer pygments loading and speedup extension setup. 9 # file to defer pygments loading and speedup extension setup.
10 10
11 from __future__ import absolute_import 11 from __future__ import absolute_import
12 12
13 from mercurial import demandimport 13 from mercurial import demandimport
14
14 demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__']) 15 demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__'])
15 16
16 from mercurial import ( 17 from mercurial import (
17 encoding, 18 encoding,
18 pycompat, 19 pycompat,
19 ) 20 )
20 21
21 from mercurial.utils import ( 22 from mercurial.utils import stringutil
22 stringutil,
23 )
24 23
25 with demandimport.deactivated(): 24 with demandimport.deactivated():
26 import pygments 25 import pygments
27 import pygments.formatters 26 import pygments.formatters
28 import pygments.lexers 27 import pygments.lexers
37 guess_lexer = pygments.lexers.guess_lexer 36 guess_lexer = pygments.lexers.guess_lexer
38 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename 37 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
39 TextLexer = pygments.lexers.TextLexer 38 TextLexer = pygments.lexers.TextLexer
40 HtmlFormatter = pygments.formatters.HtmlFormatter 39 HtmlFormatter = pygments.formatters.HtmlFormatter
41 40
42 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' 41 SYNTAX_CSS = (
43 'type="text/css" />') 42 '\n<link rel="stylesheet" href="{url}highlightcss" ' 'type="text/css" />'
43 )
44
44 45
45 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False): 46 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False):
46 47
47 # append a <link ...> to the syntax highlighting css 48 # append a <link ...> to the syntax highlighting css
48 tmpl.load('header') 49 tmpl.load('header')
65 text = text.decode(pycompat.sysstr(encoding.encoding), 'replace') 66 text = text.decode(pycompat.sysstr(encoding.encoding), 'replace')
66 67
67 # To get multi-line strings right, we can't format line-by-line 68 # To get multi-line strings right, we can't format line-by-line
68 try: 69 try:
69 path = pycompat.sysstr(fctx.path()) 70 path = pycompat.sysstr(fctx.path())
70 lexer = guess_lexer_for_filename(path, text[:1024], 71 lexer = guess_lexer_for_filename(path, text[:1024], stripnl=False)
71 stripnl=False)
72 except (ClassNotFound, ValueError): 72 except (ClassNotFound, ValueError):
73 # guess_lexer will return a lexer if *any* lexer matches. There is 73 # guess_lexer will return a lexer if *any* lexer matches. There is
74 # no way to specify a minimum match score. This can give a high rate of 74 # no way to specify a minimum match score. This can give a high rate of
75 # false positives on files with an unknown filename pattern. 75 # false positives on files with an unknown filename pattern.
76 if guessfilenameonly: 76 if guessfilenameonly:
87 return 87 return
88 88
89 formatter = HtmlFormatter(nowrap=True, style=pycompat.sysstr(style)) 89 formatter = HtmlFormatter(nowrap=True, style=pycompat.sysstr(style))
90 90
91 colorized = highlight(text, lexer, formatter) 91 colorized = highlight(text, lexer, formatter)
92 coloriter = (s.encode(pycompat.sysstr(encoding.encoding), 'replace') 92 coloriter = (
93 for s in colorized.splitlines()) 93 s.encode(pycompat.sysstr(encoding.encoding), 'replace')
94 for s in colorized.splitlines()
95 )
94 96
95 tmpl._filters['colorize'] = lambda x: next(coloriter) 97 tmpl._filters['colorize'] = lambda x: next(coloriter)
96 98
97 oldl = tmpl.cache[field] 99 oldl = tmpl.cache[field]
98 newl = oldl.replace('line|escape', 'line|colorize') 100 newl = oldl.replace('line|escape', 'line|colorize')