Skip to content

Commit 2e5a726

Browse files
committed
Upgrade pygments to 2.11.2
1 parent a47fc78 commit 2e5a726

File tree

16 files changed

+315
-37
lines changed

16 files changed

+315
-37
lines changed

news/pygments.vendor.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add pygments 2.10.0 as a vendored dependency.
1+
Upgrade pygments to 2.11.2

src/pip/_vendor/pygments/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
2525
:license: BSD, see LICENSE for details.
2626
"""
27-
import sys
2827
from io import StringIO, BytesIO
2928

30-
__version__ = '2.10.0'
29+
__version__ = '2.11.2'
3130
__docformat__ = 'restructuredtext'
3231

3332
__all__ = ['lex', 'format', 'highlight']

src/pip/_vendor/pygments/cmdline.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,50 @@ def _print_list(what):
135135
print(" %s" % docstring_headline(cls))
136136

137137

138+
def _print_list_as_json(requested_items):
139+
import json
140+
result = {}
141+
if 'lexer' in requested_items:
142+
info = {}
143+
for fullname, names, filenames, mimetypes in get_all_lexers():
144+
info[fullname] = {
145+
'aliases': names,
146+
'filenames': filenames,
147+
'mimetypes': mimetypes
148+
}
149+
result['lexers'] = info
150+
151+
if 'formatter' in requested_items:
152+
info = {}
153+
for cls in get_all_formatters():
154+
doc = docstring_headline(cls)
155+
info[cls.name] = {
156+
'aliases': cls.aliases,
157+
'filenames': cls.filenames,
158+
'doc': doc
159+
}
160+
result['formatters'] = info
161+
162+
if 'filter' in requested_items:
163+
info = {}
164+
for name in get_all_filters():
165+
cls = find_filter_class(name)
166+
info[name] = {
167+
'doc': docstring_headline(cls)
168+
}
169+
result['filters'] = info
170+
171+
if 'style' in requested_items:
172+
info = {}
173+
for name in get_all_styles():
174+
cls = get_style_by_name(name)
175+
info[name] = {
176+
'doc': docstring_headline(cls)
177+
}
178+
result['styles'] = info
179+
180+
json.dump(result, sys.stdout)
181+
138182
def main_inner(parser, argns):
139183
if argns.help:
140184
parser.print_help()
@@ -150,20 +194,33 @@ def is_only_option(opt):
150194

151195
# handle ``pygmentize -L``
152196
if argns.L is not None:
153-
if not is_only_option('L'):
197+
arg_set = set()
198+
for k, v in vars(argns).items():
199+
if v:
200+
arg_set.add(k)
201+
202+
arg_set.discard('L')
203+
arg_set.discard('json')
204+
205+
if arg_set:
154206
parser.print_help(sys.stderr)
155207
return 2
208+
156209
# print version
157-
main(['', '-V'])
210+
if not argns.json:
211+
main(['', '-V'])
158212
allowed_types = {'lexer', 'formatter', 'filter', 'style'}
159213
largs = [arg.rstrip('s') for arg in argns.L]
160214
if any(arg not in allowed_types for arg in largs):
161215
parser.print_help(sys.stderr)
162216
return 0
163217
if not largs:
164218
largs = allowed_types
165-
for arg in largs:
166-
_print_list(arg)
219+
if not argns.json:
220+
for arg in largs:
221+
_print_list(arg)
222+
else:
223+
_print_list_as_json(largs)
167224
return 0
168225

169226
# handle ``pygmentize -H``
@@ -533,6 +590,10 @@ def main(args=sys.argv):
533590
'specify your own class name with a colon (`-l ./lexer.py:MyLexer`). '
534591
'Users should be very careful not to use this option with untrusted '
535592
'files, because it will import and run them.')
593+
flags.add_argument('--json', help='Output as JSON. This can '
594+
'be only used in conjunction with -L.',
595+
default=False,
596+
action='store_true')
536597

537598
special_modes_group = parser.add_argument_group(
538599
'Special modes - do not do any highlighting')

src/pip/_vendor/pygments/formatters/_mapping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'BBCodeFormatter': ('pygments.formatters.bbcode', 'BBCode', ('bbcode', 'bb'), (), 'Format tokens with BBcodes. These formatting codes are used by many bulletin boards, so you can highlight your sourcecode with pygments before posting it there.'),
1717
'BmpImageFormatter': ('pygments.formatters.img', 'img_bmp', ('bmp', 'bitmap'), ('*.bmp',), 'Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
1818
'GifImageFormatter': ('pygments.formatters.img', 'img_gif', ('gif',), ('*.gif',), 'Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
19+
'GroffFormatter': ('pygments.formatters.groff', 'groff', ('groff', 'troff', 'roff'), (), 'Format tokens with groff escapes to change their color and font style.'),
1920
'HtmlFormatter': ('pygments.formatters.html', 'HTML', ('html',), ('*.html', '*.htm'), "Format tokens as HTML 4 ``<span>`` tags within a ``<pre>`` tag, wrapped in a ``<div>`` tag. The ``<div>``'s CSS class can be set by the `cssclass` option."),
2021
'IRCFormatter': ('pygments.formatters.irc', 'IRC', ('irc', 'IRC'), (), 'Format tokens with IRC color sequences'),
2122
'ImageFormatter': ('pygments.formatters.img', 'img', ('img', 'IMG', 'png'), ('*.png',), 'Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
"""
2+
pygments.formatters.groff
3+
~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
Formatter for groff output.
6+
7+
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
8+
:license: BSD, see LICENSE for details.
9+
"""
10+
11+
import math
12+
from pip._vendor.pygments.formatter import Formatter
13+
from pip._vendor.pygments.util import get_bool_opt, get_int_opt
14+
15+
__all__ = ['GroffFormatter']
16+
17+
18+
class GroffFormatter(Formatter):
19+
"""
20+
Format tokens with groff escapes to change their color and font style.
21+
22+
.. versionadded:: 2.11
23+
24+
Additional options accepted:
25+
26+
`style`
27+
The style to use, can be a string or a Style subclass (default:
28+
``'default'``).
29+
30+
`monospaced`
31+
If set to true, monospace font will be used (default: ``true``).
32+
33+
`linenos`
34+
If set to true, print the line numbers (default: ``false``).
35+
36+
`wrap`
37+
Wrap lines to the specified number of characters. Disabled if set to 0
38+
(default: ``0``).
39+
"""
40+
41+
name = 'groff'
42+
aliases = ['groff','troff','roff']
43+
filenames = []
44+
45+
def __init__(self, **options):
46+
Formatter.__init__(self, **options)
47+
48+
self.monospaced = get_bool_opt(options, 'monospaced', True)
49+
self.linenos = get_bool_opt(options, 'linenos', False)
50+
self._lineno = 0
51+
self.wrap = get_int_opt(options, 'wrap', 0)
52+
self._linelen = 0
53+
54+
self.styles = {}
55+
self._make_styles()
56+
57+
58+
def _make_styles(self):
59+
regular = '\\f[CR]' if self.monospaced else '\\f[R]'
60+
bold = '\\f[CB]' if self.monospaced else '\\f[B]'
61+
italic = '\\f[CI]' if self.monospaced else '\\f[I]'
62+
63+
for ttype, ndef in self.style:
64+
start = end = ''
65+
if ndef['color']:
66+
start += '\\m[%s]' % ndef['color']
67+
end = '\\m[]' + end
68+
if ndef['bold']:
69+
start += bold
70+
end = regular + end
71+
if ndef['italic']:
72+
start += italic
73+
end = regular + end
74+
if ndef['bgcolor']:
75+
start += '\\M[%s]' % ndef['bgcolor']
76+
end = '\\M[]' + end
77+
78+
self.styles[ttype] = start, end
79+
80+
81+
def _define_colors(self, outfile):
82+
colors = set()
83+
for _, ndef in self.style:
84+
if ndef['color'] is not None:
85+
colors.add(ndef['color'])
86+
87+
for color in colors:
88+
outfile.write('.defcolor ' + color + ' rgb #' + color + '\n')
89+
90+
91+
def _write_lineno(self, outfile):
92+
self._lineno += 1
93+
outfile.write("%s% 4d " % (self._lineno != 1 and '\n' or '', self._lineno))
94+
95+
96+
def _wrap_line(self, line):
97+
length = len(line.rstrip('\n'))
98+
space = ' ' if self.linenos else ''
99+
newline = ''
100+
101+
if length > self.wrap:
102+
for i in range(0, math.floor(length / self.wrap)):
103+
chunk = line[i*self.wrap:i*self.wrap+self.wrap]
104+
newline += (chunk + '\n' + space)
105+
remainder = length % self.wrap
106+
if remainder > 0:
107+
newline += line[-remainder-1:]
108+
self._linelen = remainder
109+
elif self._linelen + length > self.wrap:
110+
newline = ('\n' + space) + line
111+
self._linelen = length
112+
else:
113+
newline = line
114+
self._linelen += length
115+
116+
return newline
117+
118+
119+
def _escape_chars(self, text):
120+
text = text.replace('\\', '\\[u005C]'). \
121+
replace('.', '\\[char46]'). \
122+
replace('\'', '\\[u0027]'). \
123+
replace('`', '\\[u0060]'). \
124+
replace('~', '\\[u007E]')
125+
copy = text
126+
127+
for char in copy:
128+
if len(char) != len(char.encode()):
129+
uni = char.encode('unicode_escape') \
130+
.decode()[1:] \
131+
.replace('x', 'u00') \
132+
.upper()
133+
text = text.replace(char, '\\[u' + uni[1:] + ']')
134+
135+
return text
136+
137+
138+
def format_unencoded(self, tokensource, outfile):
139+
self._define_colors(outfile)
140+
141+
outfile.write('.nf\n\\f[CR]\n')
142+
143+
if self.linenos:
144+
self._write_lineno(outfile)
145+
146+
for ttype, value in tokensource:
147+
start, end = self.styles[ttype]
148+
149+
for line in value.splitlines(True):
150+
if self.wrap > 0:
151+
line = self._wrap_line(line)
152+
153+
if start and end:
154+
text = self._escape_chars(line.rstrip('\n'))
155+
if text != '':
156+
outfile.write(''.join((start, text, end)))
157+
else:
158+
outfile.write(self._escape_chars(line.rstrip('\n')))
159+
160+
if line.endswith('\n'):
161+
if self.linenos:
162+
self._write_lineno(outfile)
163+
self._linelen = 0
164+
else:
165+
outfile.write('\n')
166+
self._linelen = 0
167+
168+
outfile.write('\n.fi')

src/pip/_vendor/pygments/formatters/html.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ class ``"special"`` (default: ``0``).
349349
350350
.. versionadded:: 2.4
351351
352+
`debug_token_types`
353+
Add ``title`` attributes to all token ``<span>`` tags that show the
354+
name of the token.
355+
356+
.. versionadded:: 2.10
357+
352358
353359
**Subclassing the HTML formatter**
354360
@@ -419,6 +425,7 @@ def __init__(self, **options):
419425
self.filename = self._decodeifneeded(options.get('filename', ''))
420426
self.wrapcode = get_bool_opt(options, 'wrapcode', False)
421427
self.span_element_openers = {}
428+
self.debug_token_types = get_bool_opt(options, 'debug_token_types', False)
422429

423430
if self.tagsfile:
424431
if not ctags:
@@ -765,7 +772,8 @@ def _wrap_lineanchors(self, inner):
765772
for t, line in inner:
766773
if t:
767774
i += 1
768-
yield 1, '<a id="%s-%d" name="%s-%d"></a>' % (s, i, s, i) + line
775+
href = "" if self.linenos else ' href="#%s-%d"' % (s, i)
776+
yield 1, '<a id="%s-%d" name="%s-%d"%s></a>' % (s, i, s, i, href) + line
769777
else:
770778
yield 0, line
771779

@@ -835,12 +843,20 @@ def _format_lines(self, tokensource):
835843
try:
836844
cspan = self.span_element_openers[ttype]
837845
except KeyError:
846+
title = ' title="%s"' % '.'.join(ttype) if self.debug_token_types else ''
838847
if nocls:
839848
css_style = self._get_css_inline_styles(ttype)
840-
cspan = css_style and '<span style="%s">' % self.class2style[css_style][0] or ''
849+
if css_style:
850+
css_style = self.class2style[css_style][0]
851+
cspan = '<span style="%s"%s>' % (css_style, title)
852+
else:
853+
cspan = ''
841854
else:
842855
css_class = self._get_css_classes(ttype)
843-
cspan = css_class and '<span class="%s">' % css_class or ''
856+
if css_class:
857+
cspan = '<span class="%s"%s>' % (css_class, title)
858+
else:
859+
cspan = ''
844860
self.span_element_openers[ttype] = cspan
845861

846862
parts = self._translate_parts(value)

src/pip/_vendor/pygments/formatters/irc.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
:license: BSD, see LICENSE for details.
99
"""
1010

11-
import sys
12-
1311
from pip._vendor.pygments.formatter import Formatter
1412
from pip._vendor.pygments.token import Keyword, Name, Comment, String, Error, \
1513
Number, Operator, Generic, Token, Whitespace
@@ -142,7 +140,7 @@ def _format_unencoded_with_lineno(self, tokensource, outfile):
142140
value = value[:-1]
143141
color = self.colorscheme.get(ttype)
144142
while color is None:
145-
ttype = ttype[:-1]
143+
ttype = ttype.parent
146144
color = self.colorscheme.get(ttype)
147145
if color:
148146
color = color[self.darkbg]

src/pip/_vendor/pygments/formatters/terminal.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
:license: BSD, see LICENSE for details.
99
"""
1010

11-
import sys
12-
1311
from pip._vendor.pygments.formatter import Formatter
1412
from pip._vendor.pygments.token import Keyword, Name, Comment, String, Error, \
1513
Number, Operator, Generic, Token, Whitespace

src/pip/_vendor/pygments/formatters/terminal256.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
# black-on-while, so colors like "white background" need to be converted
2424
# to "white background, black foreground", etc...
2525

26-
import sys
27-
2826
from pip._vendor.pygments.formatter import Formatter
2927
from pip._vendor.pygments.console import codes
3028
from pip._vendor.pygments.style import ansicolors
@@ -281,7 +279,7 @@ def format_unencoded(self, tokensource, outfile):
281279

282280
except KeyError:
283281
# ottype = ttype
284-
ttype = ttype[:-1]
282+
ttype = ttype.parent
285283
# outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' )
286284

287285
if not_found:

0 commit comments

Comments
 (0)