mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-16 00:20:13 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -24,7 +24,10 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from io import StringIO, BytesIO
|
||||
from __future__ import annotations
|
||||
|
||||
from io import BytesIO
|
||||
from io import StringIO
|
||||
|
||||
__version__ = '2.11.2'
|
||||
__docformat__ = 'restructuredtext'
|
||||
|
|
@ -39,11 +42,17 @@ def lex(code, lexer):
|
|||
try:
|
||||
return lexer.get_tokens(code)
|
||||
except TypeError as err:
|
||||
if (isinstance(err.args[0], str) and
|
||||
('unbound method get_tokens' in err.args[0] or
|
||||
'missing 1 required positional argument' in err.args[0])):
|
||||
raise TypeError('lex() argument must be a lexer instance, '
|
||||
'not a class')
|
||||
if (
|
||||
isinstance(err.args[0], str) and
|
||||
(
|
||||
'unbound method get_tokens' in err.args[0] or
|
||||
'missing 1 required positional argument' in err.args[0]
|
||||
)
|
||||
):
|
||||
raise TypeError(
|
||||
'lex() argument must be a lexer instance, '
|
||||
'not a class',
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
|
|
@ -63,11 +72,17 @@ def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builti
|
|||
else:
|
||||
formatter.format(tokens, outfile)
|
||||
except TypeError as err:
|
||||
if (isinstance(err.args[0], str) and
|
||||
('unbound method format' in err.args[0] or
|
||||
'missing 1 required positional argument' in err.args[0])):
|
||||
raise TypeError('format() argument must be a formatter instance, '
|
||||
'not a class')
|
||||
if (
|
||||
isinstance(err.args[0], str) and
|
||||
(
|
||||
'unbound method format' in err.args[0] or
|
||||
'missing 1 required positional argument' in err.args[0]
|
||||
)
|
||||
):
|
||||
raise TypeError(
|
||||
'format() argument must be a formatter instance, '
|
||||
'not a class',
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
|
|
@ -80,4 +95,3 @@ def highlight(code, lexer, formatter, outfile=None):
|
|||
it is returned as a string.
|
||||
"""
|
||||
return format(lex(code, lexer), formatter, outfile)
|
||||
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
from pip._vendor.pygments.cmdline import main
|
||||
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -7,27 +7,43 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from textwrap import dedent
|
||||
|
||||
from pip._vendor.pygments import __version__, highlight
|
||||
from pip._vendor.pygments.util import ClassNotFound, OptionError, docstring_headline, \
|
||||
guess_decode, guess_decode_from_terminal, terminal_encoding, \
|
||||
UnclosingTextIOWrapper
|
||||
from pip._vendor.pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer, \
|
||||
load_lexer_from_file, get_lexer_for_filename, find_lexer_class_for_filename
|
||||
from pip._vendor.pygments.lexers.special import TextLexer
|
||||
from pip._vendor.pygments.formatters.latex import LatexEmbeddedLexer, LatexFormatter
|
||||
from pip._vendor.pygments.formatters import get_all_formatters, get_formatter_by_name, \
|
||||
load_formatter_from_file, get_formatter_for_filename, find_formatter_class
|
||||
from pip._vendor.pygments import __version__
|
||||
from pip._vendor.pygments import highlight
|
||||
from pip._vendor.pygments.filters import find_filter_class
|
||||
from pip._vendor.pygments.filters import get_all_filters
|
||||
from pip._vendor.pygments.formatters import find_formatter_class
|
||||
from pip._vendor.pygments.formatters import get_all_formatters
|
||||
from pip._vendor.pygments.formatters import get_formatter_by_name
|
||||
from pip._vendor.pygments.formatters import get_formatter_for_filename
|
||||
from pip._vendor.pygments.formatters import load_formatter_from_file
|
||||
from pip._vendor.pygments.formatters.latex import LatexEmbeddedLexer
|
||||
from pip._vendor.pygments.formatters.latex import LatexFormatter
|
||||
from pip._vendor.pygments.formatters.terminal import TerminalFormatter
|
||||
from pip._vendor.pygments.formatters.terminal256 import Terminal256Formatter
|
||||
from pip._vendor.pygments.filters import get_all_filters, find_filter_class
|
||||
from pip._vendor.pygments.styles import get_all_styles, get_style_by_name
|
||||
from pip._vendor.pygments.lexers import find_lexer_class_for_filename
|
||||
from pip._vendor.pygments.lexers import get_all_lexers
|
||||
from pip._vendor.pygments.lexers import get_lexer_by_name
|
||||
from pip._vendor.pygments.lexers import get_lexer_for_filename
|
||||
from pip._vendor.pygments.lexers import guess_lexer
|
||||
from pip._vendor.pygments.lexers import load_lexer_from_file
|
||||
from pip._vendor.pygments.lexers.special import TextLexer
|
||||
from pip._vendor.pygments.styles import get_all_styles
|
||||
from pip._vendor.pygments.styles import get_style_by_name
|
||||
from pip._vendor.pygments.util import ClassNotFound
|
||||
from pip._vendor.pygments.util import docstring_headline
|
||||
from pip._vendor.pygments.util import guess_decode
|
||||
from pip._vendor.pygments.util import guess_decode_from_terminal
|
||||
from pip._vendor.pygments.util import OptionError
|
||||
from pip._vendor.pygments.util import terminal_encoding
|
||||
from pip._vendor.pygments.util import UnclosingTextIOWrapper
|
||||
|
||||
|
||||
def _parse_options(o_strs):
|
||||
|
|
@ -68,32 +84,34 @@ def _print_help(what, name):
|
|||
try:
|
||||
if what == 'lexer':
|
||||
cls = get_lexer_by_name(name)
|
||||
print("Help on the %s lexer:" % cls.name)
|
||||
print('Help on the %s lexer:' % cls.name)
|
||||
print(dedent(cls.__doc__))
|
||||
elif what == 'formatter':
|
||||
cls = find_formatter_class(name)
|
||||
print("Help on the %s formatter:" % cls.name)
|
||||
print('Help on the %s formatter:' % cls.name)
|
||||
print(dedent(cls.__doc__))
|
||||
elif what == 'filter':
|
||||
cls = find_filter_class(name)
|
||||
print("Help on the %s filter:" % name)
|
||||
print('Help on the %s filter:' % name)
|
||||
print(dedent(cls.__doc__))
|
||||
return 0
|
||||
except (AttributeError, ValueError):
|
||||
print("%s not found!" % what, file=sys.stderr)
|
||||
print('%s not found!' % what, file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
def _print_list(what):
|
||||
if what == 'lexer':
|
||||
print()
|
||||
print("Lexers:")
|
||||
print("~~~~~~~")
|
||||
print('Lexers:')
|
||||
print('~~~~~~~')
|
||||
|
||||
info = []
|
||||
for fullname, names, exts, _ in get_all_lexers():
|
||||
tup = (', '.join(names)+':', fullname,
|
||||
exts and '(filenames ' + ', '.join(exts) + ')' or '')
|
||||
tup = (
|
||||
', '.join(names) + ':', fullname,
|
||||
exts and '(filenames ' + ', '.join(exts) + ')' or '',
|
||||
)
|
||||
info.append(tup)
|
||||
info.sort()
|
||||
for i in info:
|
||||
|
|
@ -101,14 +119,16 @@ def _print_list(what):
|
|||
|
||||
elif what == 'formatter':
|
||||
print()
|
||||
print("Formatters:")
|
||||
print("~~~~~~~~~~~")
|
||||
print('Formatters:')
|
||||
print('~~~~~~~~~~~')
|
||||
|
||||
info = []
|
||||
for cls in get_all_formatters():
|
||||
doc = docstring_headline(cls)
|
||||
tup = (', '.join(cls.aliases) + ':', doc, cls.filenames and
|
||||
'(filenames ' + ', '.join(cls.filenames) + ')' or '')
|
||||
tup = (
|
||||
', '.join(cls.aliases) + ':', doc, cls.filenames and
|
||||
'(filenames ' + ', '.join(cls.filenames) + ')' or '',
|
||||
)
|
||||
info.append(tup)
|
||||
info.sort()
|
||||
for i in info:
|
||||
|
|
@ -116,23 +136,23 @@ def _print_list(what):
|
|||
|
||||
elif what == 'filter':
|
||||
print()
|
||||
print("Filters:")
|
||||
print("~~~~~~~~")
|
||||
print('Filters:')
|
||||
print('~~~~~~~~')
|
||||
|
||||
for name in get_all_filters():
|
||||
cls = find_filter_class(name)
|
||||
print("* " + name + ':')
|
||||
print(" %s" % docstring_headline(cls))
|
||||
print('* ' + name + ':')
|
||||
print(' %s' % docstring_headline(cls))
|
||||
|
||||
elif what == 'style':
|
||||
print()
|
||||
print("Styles:")
|
||||
print("~~~~~~~")
|
||||
print('Styles:')
|
||||
print('~~~~~~~')
|
||||
|
||||
for name in get_all_styles():
|
||||
cls = get_style_by_name(name)
|
||||
print("* " + name + ':')
|
||||
print(" %s" % docstring_headline(cls))
|
||||
print('* ' + name + ':')
|
||||
print(' %s' % docstring_headline(cls))
|
||||
|
||||
|
||||
def _print_list_as_json(requested_items):
|
||||
|
|
@ -144,7 +164,7 @@ def _print_list_as_json(requested_items):
|
|||
info[fullname] = {
|
||||
'aliases': names,
|
||||
'filenames': filenames,
|
||||
'mimetypes': mimetypes
|
||||
'mimetypes': mimetypes,
|
||||
}
|
||||
result['lexers'] = info
|
||||
|
||||
|
|
@ -155,7 +175,7 @@ def _print_list_as_json(requested_items):
|
|||
info[cls.name] = {
|
||||
'aliases': cls.aliases,
|
||||
'filenames': cls.filenames,
|
||||
'doc': doc
|
||||
'doc': doc,
|
||||
}
|
||||
result['formatters'] = info
|
||||
|
||||
|
|
@ -164,7 +184,7 @@ def _print_list_as_json(requested_items):
|
|||
for name in get_all_filters():
|
||||
cls = find_filter_class(name)
|
||||
info[name] = {
|
||||
'doc': docstring_headline(cls)
|
||||
'doc': docstring_headline(cls),
|
||||
}
|
||||
result['filters'] = info
|
||||
|
||||
|
|
@ -173,20 +193,23 @@ def _print_list_as_json(requested_items):
|
|||
for name in get_all_styles():
|
||||
cls = get_style_by_name(name)
|
||||
info[name] = {
|
||||
'doc': docstring_headline(cls)
|
||||
'doc': docstring_headline(cls),
|
||||
}
|
||||
result['styles'] = info
|
||||
|
||||
json.dump(result, sys.stdout)
|
||||
|
||||
|
||||
def main_inner(parser, argns):
|
||||
if argns.help:
|
||||
parser.print_help()
|
||||
return 0
|
||||
|
||||
if argns.V:
|
||||
print('Pygments version %s, (c) 2006-2021 by Georg Brandl, Matthäus '
|
||||
'Chajdas and contributors.' % __version__)
|
||||
print(
|
||||
'Pygments version %s, (c) 2006-2021 by Georg Brandl, Matthäus '
|
||||
'Chajdas and contributors.' % __version__,
|
||||
)
|
||||
return 0
|
||||
|
||||
def is_only_option(opt):
|
||||
|
|
@ -323,8 +346,10 @@ def main_inner(parser, argns):
|
|||
name = None
|
||||
|
||||
if filename and name:
|
||||
lexer = load_lexer_from_file(filename, name,
|
||||
**parsed_opts)
|
||||
lexer = load_lexer_from_file(
|
||||
filename, name,
|
||||
**parsed_opts,
|
||||
)
|
||||
else:
|
||||
lexer = load_lexer_from_file(lexername, **parsed_opts)
|
||||
except ClassNotFound as err:
|
||||
|
|
@ -342,8 +367,10 @@ def main_inner(parser, argns):
|
|||
|
||||
if argns.INPUTFILE:
|
||||
if argns.s:
|
||||
print('Error: -s option not usable when input file specified',
|
||||
file=sys.stderr)
|
||||
print(
|
||||
'Error: -s option not usable when input file specified',
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 2
|
||||
|
||||
infn = argns.INPUTFILE
|
||||
|
|
@ -388,8 +415,10 @@ def main_inner(parser, argns):
|
|||
|
||||
else: # -s option needs a lexer with -l
|
||||
if not lexer:
|
||||
print('Error: when using -s a lexer has to be selected with -l',
|
||||
file=sys.stderr)
|
||||
print(
|
||||
'Error: when using -s a lexer has to be selected with -l',
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 2
|
||||
|
||||
# process filters
|
||||
|
|
@ -417,8 +446,10 @@ def main_inner(parser, argns):
|
|||
name = None
|
||||
|
||||
if filename and name:
|
||||
fmter = load_formatter_from_file(filename, name,
|
||||
**parsed_opts)
|
||||
fmter = load_formatter_from_file(
|
||||
filename, name,
|
||||
**parsed_opts,
|
||||
)
|
||||
else:
|
||||
fmter = load_formatter_from_file(fmter, **parsed_opts)
|
||||
except ClassNotFound as err:
|
||||
|
|
@ -472,7 +503,8 @@ def main_inner(parser, argns):
|
|||
pass
|
||||
else:
|
||||
outfile = colorama_initialise.wrap_stream(
|
||||
outfile, convert=None, strip=None, autoreset=False, wrap=True)
|
||||
outfile, convert=None, strip=None, autoreset=False, wrap=True,
|
||||
)
|
||||
|
||||
# When using the LaTeX formatter and the option `escapeinside` is
|
||||
# specified, we need a special lexer which collects escaped text
|
||||
|
|
@ -519,68 +551,82 @@ class HelpFormatter(argparse.HelpFormatter):
|
|||
width = shutil.get_terminal_size().columns - 2
|
||||
except Exception:
|
||||
pass
|
||||
argparse.HelpFormatter.__init__(self, prog, indent_increment,
|
||||
max_help_position, width)
|
||||
argparse.HelpFormatter.__init__(
|
||||
self, prog, indent_increment,
|
||||
max_help_position, width,
|
||||
)
|
||||
|
||||
|
||||
def main(args=sys.argv):
|
||||
"""
|
||||
Main command line entry point.
|
||||
"""
|
||||
desc = "Highlight an input file and write the result to an output file."
|
||||
parser = argparse.ArgumentParser(description=desc, add_help=False,
|
||||
formatter_class=HelpFormatter)
|
||||
desc = 'Highlight an input file and write the result to an output file.'
|
||||
parser = argparse.ArgumentParser(
|
||||
description=desc, add_help=False,
|
||||
formatter_class=HelpFormatter,
|
||||
)
|
||||
|
||||
operation = parser.add_argument_group('Main operation')
|
||||
lexersel = operation.add_mutually_exclusive_group()
|
||||
lexersel.add_argument(
|
||||
'-l', metavar='LEXER',
|
||||
help='Specify the lexer to use. (Query names with -L.) If not '
|
||||
'given and -g is not present, the lexer is guessed from the filename.')
|
||||
'given and -g is not present, the lexer is guessed from the filename.',
|
||||
)
|
||||
lexersel.add_argument(
|
||||
'-g', action='store_true',
|
||||
help='Guess the lexer from the file contents, or pass through '
|
||||
'as plain text if nothing can be guessed.')
|
||||
'as plain text if nothing can be guessed.',
|
||||
)
|
||||
operation.add_argument(
|
||||
'-F', metavar='FILTER[:options]', action='append',
|
||||
help='Add a filter to the token stream. (Query names with -L.) '
|
||||
'Filter options are given after a colon if necessary.')
|
||||
'Filter options are given after a colon if necessary.',
|
||||
)
|
||||
operation.add_argument(
|
||||
'-f', metavar='FORMATTER',
|
||||
help='Specify the formatter to use. (Query names with -L.) '
|
||||
'If not given, the formatter is guessed from the output filename, '
|
||||
'and defaults to the terminal formatter if the output is to the '
|
||||
'terminal or an unknown file extension.')
|
||||
'terminal or an unknown file extension.',
|
||||
)
|
||||
operation.add_argument(
|
||||
'-O', metavar='OPTION=value[,OPTION=value,...]', action='append',
|
||||
help='Give options to the lexer and formatter as a comma-separated '
|
||||
'list of key-value pairs. '
|
||||
'Example: `-O bg=light,python=cool`.')
|
||||
'Example: `-O bg=light,python=cool`.',
|
||||
)
|
||||
operation.add_argument(
|
||||
'-P', metavar='OPTION=value', action='append',
|
||||
help='Give a single option to the lexer and formatter - with this '
|
||||
'you can pass options whose value contains commas and equal signs. '
|
||||
'Example: `-P "heading=Pygments, the Python highlighter"`.')
|
||||
'Example: `-P "heading=Pygments, the Python highlighter"`.',
|
||||
)
|
||||
operation.add_argument(
|
||||
'-o', metavar='OUTPUTFILE',
|
||||
help='Where to write the output. Defaults to standard output.')
|
||||
help='Where to write the output. Defaults to standard output.',
|
||||
)
|
||||
|
||||
operation.add_argument(
|
||||
'INPUTFILE', nargs='?',
|
||||
help='Where to read the input. Defaults to standard input.')
|
||||
help='Where to read the input. Defaults to standard input.',
|
||||
)
|
||||
|
||||
flags = parser.add_argument_group('Operation flags')
|
||||
flags.add_argument(
|
||||
'-v', action='store_true',
|
||||
help='Print a detailed traceback on unhandled exceptions, which '
|
||||
'is useful for debugging and bug reports.')
|
||||
'is useful for debugging and bug reports.',
|
||||
)
|
||||
flags.add_argument(
|
||||
'-s', action='store_true',
|
||||
help='Process lines one at a time until EOF, rather than waiting to '
|
||||
'process the entire file. This only works for stdin, only for lexers '
|
||||
'with no line-spanning constructs, and is intended for streaming '
|
||||
'input such as you get from `tail -f`. '
|
||||
'Example usage: `tail -f sql.log | pygmentize -s -l sql`.')
|
||||
'Example usage: `tail -f sql.log | pygmentize -s -l sql`.',
|
||||
)
|
||||
flags.add_argument(
|
||||
'-x', action='store_true',
|
||||
help='Allow custom lexers and formatters to be loaded from a .py file '
|
||||
|
|
@ -589,48 +635,60 @@ def main(args=sys.argv):
|
|||
'with a class named CustomLexer or CustomFormatter; you can also '
|
||||
'specify your own class name with a colon (`-l ./lexer.py:MyLexer`). '
|
||||
'Users should be very careful not to use this option with untrusted '
|
||||
'files, because it will import and run them.')
|
||||
flags.add_argument('--json', help='Output as JSON. This can '
|
||||
'files, because it will import and run them.',
|
||||
)
|
||||
flags.add_argument(
|
||||
'--json', help='Output as JSON. This can '
|
||||
'be only used in conjunction with -L.',
|
||||
default=False,
|
||||
action='store_true')
|
||||
action='store_true',
|
||||
)
|
||||
|
||||
special_modes_group = parser.add_argument_group(
|
||||
'Special modes - do not do any highlighting')
|
||||
'Special modes - do not do any highlighting',
|
||||
)
|
||||
special_modes = special_modes_group.add_mutually_exclusive_group()
|
||||
special_modes.add_argument(
|
||||
'-S', metavar='STYLE -f formatter',
|
||||
help='Print style definitions for STYLE for a formatter '
|
||||
'given with -f. The argument given by -a is formatter '
|
||||
'dependent.')
|
||||
'dependent.',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-L', nargs='*', metavar='WHAT',
|
||||
help='List lexers, formatters, styles or filters -- '
|
||||
'give additional arguments for the thing(s) you want to list '
|
||||
'(e.g. "styles"), or omit them to list everything.')
|
||||
'(e.g. "styles"), or omit them to list everything.',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-N', metavar='FILENAME',
|
||||
help='Guess and print out a lexer name based solely on the given '
|
||||
'filename. Does not take input or highlight anything. If no specific '
|
||||
'lexer can be determined, "text" is printed.')
|
||||
'lexer can be determined, "text" is printed.',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-C', action='store_true',
|
||||
help='Like -N, but print out a lexer name based solely on '
|
||||
'a given content from standard input.')
|
||||
'a given content from standard input.',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-H', action='store', nargs=2, metavar=('NAME', 'TYPE'),
|
||||
help='Print detailed help for the object <name> of type <type>, '
|
||||
'where <type> is one of "lexer", "formatter" or "filter".')
|
||||
'where <type> is one of "lexer", "formatter" or "filter".',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-V', action='store_true',
|
||||
help='Print the package version.')
|
||||
help='Print the package version.',
|
||||
)
|
||||
special_modes.add_argument(
|
||||
'-h', '--help', action='store_true',
|
||||
help='Print this help.')
|
||||
help='Print this help.',
|
||||
)
|
||||
special_modes_group.add_argument(
|
||||
'-a', metavar='ARG',
|
||||
help='Formatter-specific additional argument for the -S (print '
|
||||
'style sheet) mode.')
|
||||
'style sheet) mode.',
|
||||
)
|
||||
|
||||
argns = parser.parse_args(args[1:])
|
||||
|
||||
|
|
@ -640,12 +698,18 @@ def main(args=sys.argv):
|
|||
if argns.v:
|
||||
print(file=sys.stderr)
|
||||
print('*' * 65, file=sys.stderr)
|
||||
print('An unhandled exception occurred while highlighting.',
|
||||
file=sys.stderr)
|
||||
print('Please report the whole traceback to the issue tracker at',
|
||||
file=sys.stderr)
|
||||
print('<https://github.com/pygments/pygments/issues>.',
|
||||
file=sys.stderr)
|
||||
print(
|
||||
'An unhandled exception occurred while highlighting.',
|
||||
file=sys.stderr,
|
||||
)
|
||||
print(
|
||||
'Please report the whole traceback to the issue tracker at',
|
||||
file=sys.stderr,
|
||||
)
|
||||
print(
|
||||
'<https://github.com/pygments/pygments/issues>.',
|
||||
file=sys.stderr,
|
||||
)
|
||||
print('*' * 65, file=sys.stderr)
|
||||
print(file=sys.stderr)
|
||||
raise
|
||||
|
|
@ -658,6 +722,8 @@ def main(args=sys.argv):
|
|||
print(file=sys.stderr)
|
||||
print('*** Error while highlighting:', file=sys.stderr)
|
||||
print(msg, file=sys.stderr)
|
||||
print('*** If this is a bug you want to report, please rerun with -v.',
|
||||
file=sys.stderr)
|
||||
print(
|
||||
'*** If this is a bug you want to report, please rerun with -v.',
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -7,42 +7,47 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
esc = "\x1b["
|
||||
esc = '\x1b['
|
||||
|
||||
codes = {}
|
||||
codes[""] = ""
|
||||
codes["reset"] = esc + "39;49;00m"
|
||||
codes[''] = ''
|
||||
codes['reset'] = esc + '39;49;00m'
|
||||
|
||||
codes["bold"] = esc + "01m"
|
||||
codes["faint"] = esc + "02m"
|
||||
codes["standout"] = esc + "03m"
|
||||
codes["underline"] = esc + "04m"
|
||||
codes["blink"] = esc + "05m"
|
||||
codes["overline"] = esc + "06m"
|
||||
codes['bold'] = esc + '01m'
|
||||
codes['faint'] = esc + '02m'
|
||||
codes['standout'] = esc + '03m'
|
||||
codes['underline'] = esc + '04m'
|
||||
codes['blink'] = esc + '05m'
|
||||
codes['overline'] = esc + '06m'
|
||||
|
||||
dark_colors = ["black", "red", "green", "yellow", "blue",
|
||||
"magenta", "cyan", "gray"]
|
||||
light_colors = ["brightblack", "brightred", "brightgreen", "brightyellow", "brightblue",
|
||||
"brightmagenta", "brightcyan", "white"]
|
||||
dark_colors = [
|
||||
'black', 'red', 'green', 'yellow', 'blue',
|
||||
'magenta', 'cyan', 'gray',
|
||||
]
|
||||
light_colors = [
|
||||
'brightblack', 'brightred', 'brightgreen', 'brightyellow', 'brightblue',
|
||||
'brightmagenta', 'brightcyan', 'white',
|
||||
]
|
||||
|
||||
x = 30
|
||||
for d, l in zip(dark_colors, light_colors):
|
||||
codes[d] = esc + "%im" % x
|
||||
codes[l] = esc + "%im" % (60 + x)
|
||||
codes[d] = esc + '%im' % x
|
||||
codes[l] = esc + '%im' % (60 + x)
|
||||
x += 1
|
||||
|
||||
del d, l, x
|
||||
|
||||
codes["white"] = codes["bold"]
|
||||
codes['white'] = codes['bold']
|
||||
|
||||
|
||||
def reset_color():
|
||||
return codes["reset"]
|
||||
return codes['reset']
|
||||
|
||||
|
||||
def colorize(color_key, text):
|
||||
return codes[color_key] + text + codes["reset"]
|
||||
return codes[color_key] + text + codes['reset']
|
||||
|
||||
|
||||
def ansiformat(attr, text):
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def apply_filters(stream, filters, lexer=None):
|
||||
|
|
@ -31,11 +32,13 @@ def simplefilter(f):
|
|||
for ttype, value in stream:
|
||||
yield ttype, value.lower()
|
||||
"""
|
||||
return type(f.__name__, (FunctionFilter,), {
|
||||
'__module__': getattr(f, '__module__'),
|
||||
'__doc__': f.__doc__,
|
||||
'function': f,
|
||||
})
|
||||
return type(
|
||||
f.__name__, (FunctionFilter,), {
|
||||
'__module__': getattr(f, '__module__'),
|
||||
'__doc__': f.__doc__,
|
||||
'function': f,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class Filter:
|
||||
|
|
@ -62,8 +65,10 @@ class FunctionFilter(Filter):
|
|||
|
||||
def __init__(self, **options):
|
||||
if not hasattr(self, 'function'):
|
||||
raise TypeError('%r used without bound function' %
|
||||
self.__class__.__name__)
|
||||
raise TypeError(
|
||||
'%r used without bound function' %
|
||||
self.__class__.__name__,
|
||||
)
|
||||
Filter.__init__(self, **options)
|
||||
|
||||
def filter(self, lexer, stream):
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -7,11 +7,12 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import codecs
|
||||
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.styles import get_style_by_name
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
|
||||
__all__ = ['Formatter']
|
||||
|
||||
|
|
|
|||
|
|
@ -7,19 +7,22 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import fnmatch
|
||||
import re
|
||||
import sys
|
||||
import types
|
||||
import fnmatch
|
||||
from os.path import basename
|
||||
|
||||
from pip._vendor.pygments.formatters._mapping import FORMATTERS
|
||||
from pip._vendor.pygments.plugin import find_plugin_formatters
|
||||
from pip._vendor.pygments.util import ClassNotFound
|
||||
|
||||
__all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
|
||||
'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS)
|
||||
__all__ = [
|
||||
'get_formatter_by_name', 'get_formatter_for_filename',
|
||||
'get_all_formatters', 'load_formatter_from_file',
|
||||
] + list(FORMATTERS)
|
||||
|
||||
_formatter_cache = {} # classes by name
|
||||
_pattern_cache = {}
|
||||
|
|
@ -74,12 +77,14 @@ def get_formatter_by_name(_alias, **options):
|
|||
"""
|
||||
cls = find_formatter_class(_alias)
|
||||
if cls is None:
|
||||
raise ClassNotFound("no formatter found for name %r" % _alias)
|
||||
raise ClassNotFound('no formatter found for name %r' % _alias)
|
||||
return cls(**options)
|
||||
|
||||
|
||||
def load_formatter_from_file(filename, formattername="CustomFormatter",
|
||||
**options):
|
||||
def load_formatter_from_file(
|
||||
filename, formattername='CustomFormatter',
|
||||
**options,
|
||||
):
|
||||
"""Load a formatter from a file.
|
||||
|
||||
This method expects a file located relative to the current working
|
||||
|
|
@ -101,13 +106,15 @@ def load_formatter_from_file(filename, formattername="CustomFormatter",
|
|||
exec(f.read(), custom_namespace)
|
||||
# Retrieve the class `formattername` from that namespace
|
||||
if formattername not in custom_namespace:
|
||||
raise ClassNotFound('no valid %s class found in %s' %
|
||||
(formattername, filename))
|
||||
raise ClassNotFound(
|
||||
'no valid %s class found in %s' %
|
||||
(formattername, filename),
|
||||
)
|
||||
formatter_class = custom_namespace[formattername]
|
||||
# And finally instantiate it with the options
|
||||
return formatter_class(**options)
|
||||
except OSError as err:
|
||||
raise ClassNotFound('cannot read %s: %s' % (filename, err))
|
||||
raise ClassNotFound('cannot read {}: {}'.format(filename, err))
|
||||
except ClassNotFound:
|
||||
raise
|
||||
except Exception as err:
|
||||
|
|
@ -130,7 +137,7 @@ def get_formatter_for_filename(fn, **options):
|
|||
for filename in cls.filenames:
|
||||
if _fn_matches(fn, filename):
|
||||
return cls(**options)
|
||||
raise ClassNotFound("no formatter found for file name %r" % fn)
|
||||
raise ClassNotFound('no formatter found for file name %r' % fn)
|
||||
|
||||
|
||||
class _automodule(types.ModuleType):
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
FORMATTERS = {
|
||||
'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.'),
|
||||
|
|
@ -30,7 +31,7 @@ FORMATTERS = {
|
|||
'Terminal256Formatter': ('pygments.formatters.terminal256', 'Terminal256', ('terminal256', 'console256', '256'), (), 'Format tokens with ANSI color sequences, for output in a 256-color terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly.'),
|
||||
'TerminalFormatter': ('pygments.formatters.terminal', 'Terminal', ('terminal', 'console'), (), 'Format tokens with ANSI color sequences, for output in a text console. Color sequences are terminated at newlines, so that paging the output works correctly.'),
|
||||
'TerminalTrueColorFormatter': ('pygments.formatters.terminal256', 'TerminalTrueColor', ('terminal16m', 'console16m', '16m'), (), 'Format tokens with ANSI color sequences, for output in a true-color terminal or console. Like in `TerminalFormatter` color sequences are terminated at newlines, so that paging the output works correctly.'),
|
||||
'TestcaseFormatter': ('pygments.formatters.other', 'Testcase', ('testcase',), (), 'Format tokens as appropriate for a new testcase.')
|
||||
'TestcaseFormatter': ('pygments.formatters.other', 'Testcase', ('testcase',), (), 'Format tokens as appropriate for a new testcase.'),
|
||||
}
|
||||
|
||||
if __name__ == '__main__': # pragma: no cover
|
||||
|
|
@ -46,19 +47,25 @@ if __name__ == '__main__': # pragma: no cover
|
|||
for root, dirs, files in os.walk('.'):
|
||||
for filename in files:
|
||||
if filename.endswith('.py') and not filename.startswith('_'):
|
||||
module_name = 'pygments.formatters%s.%s' % (
|
||||
root[1:].replace('/', '.'), filename[:-3])
|
||||
module_name = 'pygments.formatters{}.{}'.format(
|
||||
root[1:].replace('/', '.'), filename[:-3],
|
||||
)
|
||||
print(module_name)
|
||||
module = __import__(module_name, None, None, [''])
|
||||
for formatter_name in module.__all__:
|
||||
formatter = getattr(module, formatter_name)
|
||||
found_formatters.append(
|
||||
'%r: %r' % (formatter_name,
|
||||
(module_name,
|
||||
formatter.name,
|
||||
tuple(formatter.aliases),
|
||||
tuple(formatter.filenames),
|
||||
docstring_headline(formatter))))
|
||||
'{!r}: {!r}'.format(
|
||||
formatter_name,
|
||||
(
|
||||
module_name,
|
||||
formatter.name,
|
||||
tuple(formatter.aliases),
|
||||
tuple(formatter.filenames),
|
||||
docstring_headline(formatter),
|
||||
),
|
||||
),
|
||||
)
|
||||
# sort them to make the diff minimal
|
||||
found_formatters.sort()
|
||||
|
||||
|
|
@ -71,7 +78,7 @@ if __name__ == '__main__': # pragma: no cover
|
|||
# repository, for example by using some kind of automatic
|
||||
# management EOL, like `EolExtension
|
||||
# <https://www.mercurial-scm.org/wiki/EolExtension>`.
|
||||
content = content.replace("\r\n", "\n")
|
||||
content = content.replace('\r\n', '\n')
|
||||
header = content[:content.find('FORMATTERS = {')]
|
||||
footer = content[content.find("if __name__ == '__main__':"):]
|
||||
|
||||
|
|
@ -81,4 +88,4 @@ if __name__ == '__main__': # pragma: no cover
|
|||
fp.write('FORMATTERS = {\n %s\n}\n\n' % ',\n '.join(found_formatters))
|
||||
fp.write(footer)
|
||||
|
||||
print ('=== %d formatters processed.' % len(found_formatters))
|
||||
print('=== %d formatters processed.' % len(found_formatters))
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
|
|
|
|||
|
|
@ -7,10 +7,13 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
|
||||
__all__ = ['GroffFormatter']
|
||||
|
||||
|
|
@ -39,7 +42,7 @@ class GroffFormatter(Formatter):
|
|||
"""
|
||||
|
||||
name = 'groff'
|
||||
aliases = ['groff','troff','roff']
|
||||
aliases = ['groff', 'troff', 'roff']
|
||||
filenames = []
|
||||
|
||||
def __init__(self, **options):
|
||||
|
|
@ -54,7 +57,6 @@ class GroffFormatter(Formatter):
|
|||
self.styles = {}
|
||||
self._make_styles()
|
||||
|
||||
|
||||
def _make_styles(self):
|
||||
regular = '\\f[CR]' if self.monospaced else '\\f[R]'
|
||||
bold = '\\f[CB]' if self.monospaced else '\\f[B]'
|
||||
|
|
@ -77,7 +79,6 @@ class GroffFormatter(Formatter):
|
|||
|
||||
self.styles[ttype] = start, end
|
||||
|
||||
|
||||
def _define_colors(self, outfile):
|
||||
colors = set()
|
||||
for _, ndef in self.style:
|
||||
|
|
@ -87,11 +88,9 @@ class GroffFormatter(Formatter):
|
|||
for color in colors:
|
||||
outfile.write('.defcolor ' + color + ' rgb #' + color + '\n')
|
||||
|
||||
|
||||
def _write_lineno(self, outfile):
|
||||
self._lineno += 1
|
||||
outfile.write("%s% 4d " % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
|
||||
outfile.write('%s% 4d ' % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
|
||||
def _wrap_line(self, line):
|
||||
length = len(line.rstrip('\n'))
|
||||
|
|
@ -100,11 +99,11 @@ class GroffFormatter(Formatter):
|
|||
|
||||
if length > self.wrap:
|
||||
for i in range(0, math.floor(length / self.wrap)):
|
||||
chunk = line[i*self.wrap:i*self.wrap+self.wrap]
|
||||
chunk = line[i * self.wrap:i * self.wrap + self.wrap]
|
||||
newline += (chunk + '\n' + space)
|
||||
remainder = length % self.wrap
|
||||
if remainder > 0:
|
||||
newline += line[-remainder-1:]
|
||||
newline += line[-remainder - 1:]
|
||||
self._linelen = remainder
|
||||
elif self._linelen + length > self.wrap:
|
||||
newline = ('\n' + space) + line
|
||||
|
|
@ -115,13 +114,12 @@ class GroffFormatter(Formatter):
|
|||
|
||||
return newline
|
||||
|
||||
|
||||
def _escape_chars(self, text):
|
||||
text = text.replace('\\', '\\[u005C]'). \
|
||||
replace('.', '\\[char46]'). \
|
||||
replace('\'', '\\[u0027]'). \
|
||||
replace('`', '\\[u0060]'). \
|
||||
replace('~', '\\[u007E]')
|
||||
replace('.', '\\[char46]'). \
|
||||
replace('\'', '\\[u0027]'). \
|
||||
replace('`', '\\[u0060]'). \
|
||||
replace('~', '\\[u007E]')
|
||||
copy = text
|
||||
|
||||
for char in copy:
|
||||
|
|
@ -134,7 +132,6 @@ class GroffFormatter(Formatter):
|
|||
|
||||
return text
|
||||
|
||||
|
||||
def format_unencoded(self, tokensource, outfile):
|
||||
self._define_colors(outfile)
|
||||
|
||||
|
|
|
|||
|
|
@ -7,16 +7,20 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import os
|
||||
import sys
|
||||
import os.path
|
||||
import sys
|
||||
from io import StringIO
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Token, Text, STANDARD_TYPES
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt, get_list_opt
|
||||
from pip._vendor.pygments.token import STANDARD_TYPES
|
||||
from pip._vendor.pygments.token import Text
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
from pip._vendor.pygments.util import get_list_opt
|
||||
|
||||
try:
|
||||
import ctags
|
||||
|
|
@ -429,8 +433,10 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
if self.tagsfile:
|
||||
if not ctags:
|
||||
raise RuntimeError('The "ctags" package must to be installed '
|
||||
'to be able to use the "tagsfile" feature.')
|
||||
raise RuntimeError(
|
||||
'The "ctags" package must to be installed '
|
||||
'to be able to use the "tagsfile" feature.',
|
||||
)
|
||||
self._ctags = ctags.CTags(self.tagsfile)
|
||||
|
||||
linenos = options.get('linenos', False)
|
||||
|
|
@ -531,7 +537,7 @@ class HtmlFormatter(Formatter):
|
|||
styles.sort()
|
||||
|
||||
lines = [
|
||||
'%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:])
|
||||
'{} {{ {} }} /* {} */'.format(prefix(cls), style, repr(ttype)[6:])
|
||||
for (level, ttype, cls, style) in styles
|
||||
]
|
||||
|
||||
|
|
@ -549,13 +555,13 @@ class HtmlFormatter(Formatter):
|
|||
if Text in self.ttype2class:
|
||||
text_style = ' ' + self.class2style[self.ttype2class[Text]][0]
|
||||
lines.insert(
|
||||
0, '%s{ background: %s;%s }' % (
|
||||
prefix(''), bg_color, text_style
|
||||
)
|
||||
0, '{}{{ background: {};{} }}'.format(
|
||||
prefix(''), bg_color, text_style,
|
||||
),
|
||||
)
|
||||
if hl_color is not None:
|
||||
lines.insert(
|
||||
0, '%s { background-color: %s }' % (prefix('hll'), hl_color)
|
||||
0, '{} {{ background-color: {} }}'.format(prefix('hll'), hl_color),
|
||||
)
|
||||
|
||||
return lines
|
||||
|
|
@ -573,7 +579,7 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
def get_css_prefix(self, arg):
|
||||
if arg is None:
|
||||
arg = ('cssclass' in self.options and '.'+self.cssclass or '')
|
||||
arg = ('cssclass' in self.options and '.' + self.cssclass or '')
|
||||
if isinstance(arg, str):
|
||||
args = [arg]
|
||||
else:
|
||||
|
|
@ -595,16 +601,16 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
@property
|
||||
def _linenos_style(self):
|
||||
return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % (
|
||||
return 'color: {}; background-color: {}; padding-left: 5px; padding-right: 5px;'.format(
|
||||
self.style.line_number_color,
|
||||
self.style.line_number_background_color
|
||||
self.style.line_number_background_color,
|
||||
)
|
||||
|
||||
@property
|
||||
def _linenos_special_style(self):
|
||||
return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % (
|
||||
return 'color: {}; background-color: {}; padding-left: 5px; padding-right: 5px;'.format(
|
||||
self.style.line_number_special_color,
|
||||
self.style.line_number_special_background_color
|
||||
self.style.line_number_special_background_color,
|
||||
)
|
||||
|
||||
def _decodeifneeded(self, value):
|
||||
|
|
@ -625,32 +631,46 @@ class HtmlFormatter(Formatter):
|
|||
if not filename or filename[0] == '<':
|
||||
# pseudo files, e.g. name == '<fdopen>'
|
||||
raise AttributeError
|
||||
cssfilename = os.path.join(os.path.dirname(filename),
|
||||
self.cssfile)
|
||||
cssfilename = os.path.join(
|
||||
os.path.dirname(filename),
|
||||
self.cssfile,
|
||||
)
|
||||
except AttributeError:
|
||||
print('Note: Cannot determine output file name, '
|
||||
'using current directory as base for the CSS file name',
|
||||
file=sys.stderr)
|
||||
print(
|
||||
'Note: Cannot determine output file name, '
|
||||
'using current directory as base for the CSS file name',
|
||||
file=sys.stderr,
|
||||
)
|
||||
cssfilename = self.cssfile
|
||||
# write CSS file only if noclobber_cssfile isn't given as an option.
|
||||
try:
|
||||
if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
|
||||
with open(cssfilename, "w") as cf:
|
||||
cf.write(CSSFILE_TEMPLATE %
|
||||
{'styledefs': self.get_style_defs('body')})
|
||||
with open(cssfilename, 'w') as cf:
|
||||
cf.write(
|
||||
CSSFILE_TEMPLATE %
|
||||
{'styledefs': self.get_style_defs('body')},
|
||||
)
|
||||
except OSError as err:
|
||||
err.strerror = 'Error writing CSS file: ' + err.strerror
|
||||
raise
|
||||
|
||||
yield 0, (DOC_HEADER_EXTERNALCSS %
|
||||
dict(title=self.title,
|
||||
cssfile=self.cssfile,
|
||||
encoding=self.encoding))
|
||||
yield 0, (
|
||||
DOC_HEADER_EXTERNALCSS %
|
||||
dict(
|
||||
title=self.title,
|
||||
cssfile=self.cssfile,
|
||||
encoding=self.encoding,
|
||||
)
|
||||
)
|
||||
else:
|
||||
yield 0, (DOC_HEADER %
|
||||
dict(title=self.title,
|
||||
styledefs=self.get_style_defs('body'),
|
||||
encoding=self.encoding))
|
||||
yield 0, (
|
||||
DOC_HEADER %
|
||||
dict(
|
||||
title=self.title,
|
||||
styledefs=self.get_style_defs('body'),
|
||||
encoding=self.encoding,
|
||||
)
|
||||
)
|
||||
|
||||
yield from inner
|
||||
yield 0, DOC_FOOTER
|
||||
|
|
@ -673,7 +693,7 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
lines = []
|
||||
|
||||
for i in range(fl, fl+lncount):
|
||||
for i in range(fl, fl + lncount):
|
||||
print_line = i % st == 0
|
||||
special_line = sp and i % sp == 0
|
||||
|
||||
|
|
@ -696,7 +716,7 @@ class HtmlFormatter(Formatter):
|
|||
style = ' class="normal"'
|
||||
|
||||
if style:
|
||||
line = '<span%s>%s</span>' % (style, line)
|
||||
line = '<span{}>{}</span>'.format(style, line)
|
||||
|
||||
lines.append(line)
|
||||
|
||||
|
|
@ -704,12 +724,13 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
# If a filename was specified, we can't put it into the code table as it
|
||||
# would misalign the line numbers. Hence we emit a separate row for it.
|
||||
filename_tr = ""
|
||||
filename_tr = ''
|
||||
if self.filename:
|
||||
filename_tr = (
|
||||
'<tr><th colspan="2" class="filename"><div class="highlight">'
|
||||
'<span class="filename">' + self.filename + '</span></div>'
|
||||
'</th></tr>')
|
||||
'</th></tr>'
|
||||
)
|
||||
|
||||
# in case you wonder about the seemingly redundant <div> here: since the
|
||||
# content in the other cell also is wrapped in a div, some browsers in
|
||||
|
|
@ -754,13 +775,15 @@ class HtmlFormatter(Formatter):
|
|||
style = ' class="linenos"'
|
||||
|
||||
if style:
|
||||
linenos = '<span%s>%s</span>' % (style, line)
|
||||
linenos = '<span{}>{}</span>'.format(style, line)
|
||||
else:
|
||||
linenos = line
|
||||
|
||||
if aln:
|
||||
yield 1, ('<a href="#%s-%d">%s</a>' % (la, num, linenos) +
|
||||
inner_line)
|
||||
yield 1, (
|
||||
'<a href="#%s-%d">%s</a>' % (la, num, linenos) +
|
||||
inner_line
|
||||
)
|
||||
else:
|
||||
yield 1, linenos + inner_line
|
||||
num += 1
|
||||
|
|
@ -772,7 +795,7 @@ class HtmlFormatter(Formatter):
|
|||
for t, line in inner:
|
||||
if t:
|
||||
i += 1
|
||||
href = "" if self.linenos else ' href="#%s-%d"' % (s, i)
|
||||
href = '' if self.linenos else ' href="#%s-%d"' % (s, i)
|
||||
yield 1, '<a id="%s-%d" name="%s-%d"%s></a>' % (s, i, s, i, href) + line
|
||||
else:
|
||||
yield 0, line
|
||||
|
|
@ -789,15 +812,19 @@ class HtmlFormatter(Formatter):
|
|||
|
||||
def _wrap_div(self, inner):
|
||||
style = []
|
||||
if (self.noclasses and not self.nobackground and
|
||||
self.style.background_color is not None):
|
||||
style.append('background: %s' % (self.style.background_color,))
|
||||
if (
|
||||
self.noclasses and not self.nobackground and
|
||||
self.style.background_color is not None
|
||||
):
|
||||
style.append('background: {}'.format(self.style.background_color))
|
||||
if self.cssstyles:
|
||||
style.append(self.cssstyles)
|
||||
style = '; '.join(style)
|
||||
|
||||
yield 0, ('<div' + (self.cssclass and ' class="%s"' % self.cssclass) +
|
||||
(style and (' style="%s"' % style)) + '>')
|
||||
yield 0, (
|
||||
'<div' + (self.cssclass and ' class="%s"' % self.cssclass) +
|
||||
(style and (' style="%s"' % style)) + '>'
|
||||
)
|
||||
yield from inner
|
||||
yield 0, '</div>\n'
|
||||
|
||||
|
|
@ -848,13 +875,13 @@ class HtmlFormatter(Formatter):
|
|||
css_style = self._get_css_inline_styles(ttype)
|
||||
if css_style:
|
||||
css_style = self.class2style[css_style][0]
|
||||
cspan = '<span style="%s"%s>' % (css_style, title)
|
||||
cspan = '<span style="{}"{}>'.format(css_style, title)
|
||||
else:
|
||||
cspan = ''
|
||||
else:
|
||||
css_class = self._get_css_classes(ttype)
|
||||
if css_class:
|
||||
cspan = '<span class="%s"%s>' % (css_class, title)
|
||||
cspan = '<span class="{}"{}>'.format(css_class, title)
|
||||
else:
|
||||
cspan = ''
|
||||
self.span_element_openers[ttype] = cspan
|
||||
|
|
@ -868,18 +895,22 @@ class HtmlFormatter(Formatter):
|
|||
if base:
|
||||
base += '/'
|
||||
filename, extension = os.path.splitext(filename)
|
||||
url = self.tagurlformat % {'path': base, 'fname': filename,
|
||||
'fext': extension}
|
||||
url = self.tagurlformat % {
|
||||
'path': base, 'fname': filename,
|
||||
'fext': extension,
|
||||
}
|
||||
parts[0] = "<a href=\"%s#%s-%d\">%s" % \
|
||||
(url, self.lineanchors, linenumber, parts[0])
|
||||
parts[-1] = parts[-1] + "</a>"
|
||||
parts[-1] = parts[-1] + '</a>'
|
||||
|
||||
# for all but the last line
|
||||
for part in parts[:-1]:
|
||||
if line:
|
||||
if lspan != cspan:
|
||||
line.extend(((lspan and '</span>'), cspan, part,
|
||||
(cspan and '</span>'), lsep))
|
||||
line.extend((
|
||||
(lspan and '</span>'), cspan, part,
|
||||
(cspan and '</span>'), lsep,
|
||||
))
|
||||
else: # both are the same
|
||||
line.extend((part, (lspan and '</span>'), lsep))
|
||||
yield 1, ''.join(line)
|
||||
|
|
@ -925,9 +956,11 @@ class HtmlFormatter(Formatter):
|
|||
if self.noclasses:
|
||||
style = ''
|
||||
if self.style.highlight_color is not None:
|
||||
style = (' style="background-color: %s"' %
|
||||
(self.style.highlight_color,))
|
||||
yield 1, '<span%s>%s</span>' % (style, value)
|
||||
style = (
|
||||
' style="background-color: %s"' %
|
||||
(self.style.highlight_color,)
|
||||
)
|
||||
yield 1, '<span{}>{}</span>'.format(style, value)
|
||||
else:
|
||||
yield 1, '<span class="hll">%s</span>' % value
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -7,15 +7,17 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
|
||||
get_choice_opt
|
||||
|
||||
import subprocess
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_choice_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
from pip._vendor.pygments.util import get_list_opt
|
||||
|
||||
# Import this carefully
|
||||
try:
|
||||
|
|
@ -32,15 +34,17 @@ except ImportError:
|
|||
except ImportError:
|
||||
_winreg = None
|
||||
|
||||
__all__ = ['ImageFormatter', 'GifImageFormatter', 'JpgImageFormatter',
|
||||
'BmpImageFormatter']
|
||||
__all__ = [
|
||||
'ImageFormatter', 'GifImageFormatter', 'JpgImageFormatter',
|
||||
'BmpImageFormatter',
|
||||
]
|
||||
|
||||
|
||||
# For some unknown reason every font calls it something different
|
||||
STYLES = {
|
||||
'NORMAL': ['', 'Roman', 'Book', 'Normal', 'Regular', 'Medium'],
|
||||
'ITALIC': ['Oblique', 'Italic'],
|
||||
'BOLD': ['Bold'],
|
||||
'NORMAL': ['', 'Roman', 'Book', 'Normal', 'Regular', 'Medium'],
|
||||
'ITALIC': ['Oblique', 'Italic'],
|
||||
'BOLD': ['Bold'],
|
||||
'BOLDITALIC': ['Bold Oblique', 'Bold Italic'],
|
||||
}
|
||||
|
||||
|
|
@ -82,8 +86,10 @@ class FontManager:
|
|||
self._create_nix()
|
||||
|
||||
def _get_nix_font_path(self, name, style):
|
||||
proc = subprocess.Popen(['fc-list', "%s:style=%s" % (name, style), 'file'],
|
||||
stdout=subprocess.PIPE, stderr=None)
|
||||
proc = subprocess.Popen(
|
||||
['fc-list', '{}:style={}'.format(name, style), 'file'],
|
||||
stdout=subprocess.PIPE, stderr=None,
|
||||
)
|
||||
stdout, _ = proc.communicate()
|
||||
if proc.returncode == 0:
|
||||
lines = stdout.splitlines()
|
||||
|
|
@ -102,8 +108,10 @@ class FontManager:
|
|||
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
|
||||
break
|
||||
else:
|
||||
raise FontNotFound('No usable fonts named: "%s"' %
|
||||
self.font_name)
|
||||
raise FontNotFound(
|
||||
'No usable fonts named: "%s"' %
|
||||
self.font_name,
|
||||
)
|
||||
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
|
||||
for stylename in STYLES[style]:
|
||||
path = self._get_nix_font_path(self.font_name, stylename)
|
||||
|
|
@ -121,12 +129,15 @@ class FontManager:
|
|||
|
||||
def _create_mac(self):
|
||||
font_map = {}
|
||||
for font_dir in (os.path.join(os.getenv("HOME"), 'Library/Fonts/'),
|
||||
'/Library/Fonts/', '/System/Library/Fonts/'):
|
||||
for font_dir in (
|
||||
os.path.join(os.getenv('HOME'), 'Library/Fonts/'),
|
||||
'/Library/Fonts/', '/System/Library/Fonts/',
|
||||
):
|
||||
font_map.update(
|
||||
(os.path.splitext(f)[0].lower(), os.path.join(font_dir, f))
|
||||
for f in os.listdir(font_dir)
|
||||
if f.lower().endswith(('ttf', 'ttc')))
|
||||
if f.lower().endswith(('ttf', 'ttc'))
|
||||
)
|
||||
|
||||
for name in STYLES['NORMAL']:
|
||||
path = self._get_mac_font_path(font_map, self.font_name, name)
|
||||
|
|
@ -134,8 +145,10 @@ class FontManager:
|
|||
self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
|
||||
break
|
||||
else:
|
||||
raise FontNotFound('No usable fonts named: "%s"' %
|
||||
self.font_name)
|
||||
raise FontNotFound(
|
||||
'No usable fonts named: "%s"' %
|
||||
self.font_name,
|
||||
)
|
||||
for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
|
||||
for stylename in STYLES[style]:
|
||||
path = self._get_mac_font_path(font_map, self.font_name, stylename)
|
||||
|
|
@ -152,23 +165,25 @@ class FontManager:
|
|||
for suffix in ('', ' (TrueType)'):
|
||||
for style in styles:
|
||||
try:
|
||||
valname = '%s%s%s' % (basename, style and ' '+style, suffix)
|
||||
valname = '{}{}{}'.format(basename, style and ' ' + style, suffix)
|
||||
val, _ = _winreg.QueryValueEx(key, valname)
|
||||
return val
|
||||
except OSError:
|
||||
continue
|
||||
else:
|
||||
if fail:
|
||||
raise FontNotFound('Font %s (%s) not found in registry' %
|
||||
(basename, styles[0]))
|
||||
raise FontNotFound(
|
||||
'Font %s (%s) not found in registry' %
|
||||
(basename, styles[0]),
|
||||
)
|
||||
return None
|
||||
|
||||
def _create_win(self):
|
||||
lookuperror = None
|
||||
keynames = [ (_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows\CurrentVersion\Fonts') ]
|
||||
keynames = [(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
|
||||
(_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows\CurrentVersion\Fonts'), ]
|
||||
for keyname in keynames:
|
||||
try:
|
||||
key = _winreg.OpenKey(*keyname)
|
||||
|
|
@ -352,7 +367,8 @@ class ImageFormatter(Formatter):
|
|||
"""
|
||||
if not pil_available:
|
||||
raise PilNotAvailable(
|
||||
'Python Imaging Library is required for this formatter')
|
||||
'Python Imaging Library is required for this formatter',
|
||||
)
|
||||
Formatter.__init__(self, **options)
|
||||
self.encoding = 'latin1' # let pygments.format() do the right thing
|
||||
# Read the style
|
||||
|
|
@ -364,7 +380,8 @@ class ImageFormatter(Formatter):
|
|||
# Image options
|
||||
self.image_format = get_choice_opt(
|
||||
options, 'image_format', ['png', 'jpeg', 'gif', 'bmp'],
|
||||
self.default_image_format, normcase=True)
|
||||
self.default_image_format, normcase=True,
|
||||
)
|
||||
self.image_pad = get_int_opt(options, 'image_pad', 10)
|
||||
self.line_pad = get_int_opt(options, 'line_pad', 2)
|
||||
# The fonts
|
||||
|
|
@ -374,21 +391,31 @@ class ImageFormatter(Formatter):
|
|||
# Line number options
|
||||
self.line_number_fg = options.get('line_number_fg', '#886')
|
||||
self.line_number_bg = options.get('line_number_bg', '#eed')
|
||||
self.line_number_chars = get_int_opt(options,
|
||||
'line_number_chars', 2)
|
||||
self.line_number_bold = get_bool_opt(options,
|
||||
'line_number_bold', False)
|
||||
self.line_number_italic = get_bool_opt(options,
|
||||
'line_number_italic', False)
|
||||
self.line_number_chars = get_int_opt(
|
||||
options,
|
||||
'line_number_chars', 2,
|
||||
)
|
||||
self.line_number_bold = get_bool_opt(
|
||||
options,
|
||||
'line_number_bold', False,
|
||||
)
|
||||
self.line_number_italic = get_bool_opt(
|
||||
options,
|
||||
'line_number_italic', False,
|
||||
)
|
||||
self.line_number_pad = get_int_opt(options, 'line_number_pad', 6)
|
||||
self.line_numbers = get_bool_opt(options, 'line_numbers', True)
|
||||
self.line_number_separator = get_bool_opt(options,
|
||||
'line_number_separator', True)
|
||||
self.line_number_separator = get_bool_opt(
|
||||
options,
|
||||
'line_number_separator', True,
|
||||
)
|
||||
self.line_number_step = get_int_opt(options, 'line_number_step', 1)
|
||||
self.line_number_start = get_int_opt(options, 'line_number_start', 1)
|
||||
if self.line_numbers:
|
||||
self.line_number_width = (self.fontw * self.line_number_chars +
|
||||
self.line_number_pad * 2)
|
||||
self.line_number_width = (
|
||||
self.fontw * self.line_number_chars +
|
||||
self.line_number_pad * 2
|
||||
)
|
||||
else:
|
||||
self.line_number_width = 0
|
||||
self.hl_lines = []
|
||||
|
|
@ -398,13 +425,17 @@ class ImageFormatter(Formatter):
|
|||
self.hl_lines.append(int(line))
|
||||
except ValueError:
|
||||
pass
|
||||
self.hl_color = options.get('hl_color',
|
||||
self.style.highlight_color) or '#f90'
|
||||
self.hl_color = options.get(
|
||||
'hl_color',
|
||||
self.style.highlight_color,
|
||||
) or '#f90'
|
||||
self.drawables = []
|
||||
|
||||
def get_style_defs(self, arg=''):
|
||||
raise NotImplementedError('The -S option is meaningless for the image '
|
||||
'formatter. Use -O style=<stylename> instead.')
|
||||
raise NotImplementedError(
|
||||
'The -S option is meaningless for the image '
|
||||
'formatter. Use -O style=<stylename> instead.',
|
||||
)
|
||||
|
||||
def _get_line_height(self):
|
||||
"""
|
||||
|
|
@ -472,8 +503,10 @@ class ImageFormatter(Formatter):
|
|||
"""
|
||||
Get the required image size.
|
||||
"""
|
||||
return (self._get_char_x(maxlinelength) + self.image_pad,
|
||||
self._get_line_y(maxlineno + 0) + self.image_pad)
|
||||
return (
|
||||
self._get_char_x(maxlinelength) + self.image_pad,
|
||||
self._get_line_y(maxlineno + 0) + self.image_pad,
|
||||
)
|
||||
|
||||
def _draw_linenumber(self, posno, lineno):
|
||||
"""
|
||||
|
|
@ -482,8 +515,10 @@ class ImageFormatter(Formatter):
|
|||
self._draw_text(
|
||||
self._get_linenumber_pos(posno),
|
||||
str(lineno).rjust(self.line_number_chars),
|
||||
font=self.fonts.get_font(self.line_number_bold,
|
||||
self.line_number_italic),
|
||||
font=self.fonts.get_font(
|
||||
self.line_number_bold,
|
||||
self.line_number_italic,
|
||||
),
|
||||
text_fg=self.line_number_fg,
|
||||
text_bg=None,
|
||||
)
|
||||
|
|
@ -516,9 +551,9 @@ class ImageFormatter(Formatter):
|
|||
self._draw_text(
|
||||
self._get_text_pos(linelength, lineno),
|
||||
temp,
|
||||
font = self._get_style_font(style),
|
||||
text_fg = self._get_text_color(style),
|
||||
text_bg = self._get_text_bg_color(style),
|
||||
font=self._get_style_font(style),
|
||||
text_fg=self._get_text_color(style),
|
||||
text_bg=self._get_text_bg_color(style),
|
||||
)
|
||||
temp_width, temp_hight = self.fonts.get_text_size(temp)
|
||||
linelength += temp_width
|
||||
|
|
@ -556,8 +591,10 @@ class ImageFormatter(Formatter):
|
|||
draw = ImageDraw.Draw(im)
|
||||
recth = im.size[-1]
|
||||
rectw = self.image_pad + self.line_number_width - self.line_number_pad
|
||||
draw.rectangle([(0, 0), (rectw, recth)],
|
||||
fill=self.line_number_bg)
|
||||
draw.rectangle(
|
||||
[(0, 0), (rectw, recth)],
|
||||
fill=self.line_number_bg,
|
||||
)
|
||||
if self.line_number_separator:
|
||||
draw.line([(rectw, 0), (rectw, recth)], fill=self.line_number_fg)
|
||||
del draw
|
||||
|
|
@ -575,7 +612,7 @@ class ImageFormatter(Formatter):
|
|||
im = Image.new(
|
||||
'RGB',
|
||||
self._get_image_size(self.maxlinelength, self.maxlineno),
|
||||
self.background_color
|
||||
self.background_color,
|
||||
)
|
||||
self._paint_line_number_bg(im)
|
||||
draw = ImageDraw.Draw(im)
|
||||
|
|
@ -586,8 +623,10 @@ class ImageFormatter(Formatter):
|
|||
rectw = im.size[0] - x
|
||||
for linenumber in self.hl_lines:
|
||||
y = self._get_line_y(linenumber - 1)
|
||||
draw.rectangle([(x, y), (x + rectw, y + recth)],
|
||||
fill=self.hl_color)
|
||||
draw.rectangle(
|
||||
[(x, y), (x + rectw, y + recth)],
|
||||
fill=self.hl_color,
|
||||
)
|
||||
for pos, value, font, text_fg, text_bg in self.drawables:
|
||||
if text_bg:
|
||||
text_size = draw.textsize(text=value, font=font)
|
||||
|
|
|
|||
|
|
@ -7,10 +7,19 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Keyword, Name, Comment, String, Error, \
|
||||
Number, Operator, Generic, Token, Whitespace
|
||||
from pip._vendor.pygments.token import Comment
|
||||
from pip._vendor.pygments.token import Error
|
||||
from pip._vendor.pygments.token import Generic
|
||||
from pip._vendor.pygments.token import Keyword
|
||||
from pip._vendor.pygments.token import Name
|
||||
from pip._vendor.pygments.token import Number
|
||||
from pip._vendor.pygments.token import Operator
|
||||
from pip._vendor.pygments.token import String
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.token import Whitespace
|
||||
from pip._vendor.pygments.util import get_choice_opt
|
||||
|
||||
|
||||
|
|
@ -20,34 +29,34 @@ __all__ = ['IRCFormatter']
|
|||
#: Map token types to a tuple of color values for light and dark
|
||||
#: backgrounds.
|
||||
IRC_COLORS = {
|
||||
Token: ('', ''),
|
||||
Token: ('', ''),
|
||||
|
||||
Whitespace: ('gray', 'brightblack'),
|
||||
Comment: ('gray', 'brightblack'),
|
||||
Comment.Preproc: ('cyan', 'brightcyan'),
|
||||
Keyword: ('blue', 'brightblue'),
|
||||
Keyword.Type: ('cyan', 'brightcyan'),
|
||||
Operator.Word: ('magenta', 'brightcyan'),
|
||||
Name.Builtin: ('cyan', 'brightcyan'),
|
||||
Name.Function: ('green', 'brightgreen'),
|
||||
Name.Namespace: ('_cyan_', '_brightcyan_'),
|
||||
Name.Class: ('_green_', '_brightgreen_'),
|
||||
Name.Exception: ('cyan', 'brightcyan'),
|
||||
Name.Decorator: ('brightblack', 'gray'),
|
||||
Name.Variable: ('red', 'brightred'),
|
||||
Name.Constant: ('red', 'brightred'),
|
||||
Name.Attribute: ('cyan', 'brightcyan'),
|
||||
Name.Tag: ('brightblue', 'brightblue'),
|
||||
String: ('yellow', 'yellow'),
|
||||
Number: ('blue', 'brightblue'),
|
||||
Whitespace: ('gray', 'brightblack'),
|
||||
Comment: ('gray', 'brightblack'),
|
||||
Comment.Preproc: ('cyan', 'brightcyan'),
|
||||
Keyword: ('blue', 'brightblue'),
|
||||
Keyword.Type: ('cyan', 'brightcyan'),
|
||||
Operator.Word: ('magenta', 'brightcyan'),
|
||||
Name.Builtin: ('cyan', 'brightcyan'),
|
||||
Name.Function: ('green', 'brightgreen'),
|
||||
Name.Namespace: ('_cyan_', '_brightcyan_'),
|
||||
Name.Class: ('_green_', '_brightgreen_'),
|
||||
Name.Exception: ('cyan', 'brightcyan'),
|
||||
Name.Decorator: ('brightblack', 'gray'),
|
||||
Name.Variable: ('red', 'brightred'),
|
||||
Name.Constant: ('red', 'brightred'),
|
||||
Name.Attribute: ('cyan', 'brightcyan'),
|
||||
Name.Tag: ('brightblue', 'brightblue'),
|
||||
String: ('yellow', 'yellow'),
|
||||
Number: ('blue', 'brightblue'),
|
||||
|
||||
Generic.Deleted: ('brightred', 'brightred'),
|
||||
Generic.Inserted: ('green', 'brightgreen'),
|
||||
Generic.Heading: ('**', '**'),
|
||||
Generic.Subheading: ('*magenta*', '*brightmagenta*'),
|
||||
Generic.Error: ('brightred', 'brightred'),
|
||||
Generic.Deleted: ('brightred', 'brightred'),
|
||||
Generic.Inserted: ('green', 'brightgreen'),
|
||||
Generic.Heading: ('**', '**'),
|
||||
Generic.Subheading: ('*magenta*', '*brightmagenta*'),
|
||||
Generic.Error: ('brightred', 'brightred'),
|
||||
|
||||
Error: ('_brightred_', '_brightred_'),
|
||||
Error: ('_brightred_', '_brightred_'),
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -60,38 +69,39 @@ IRC_COLOR_MAP = {
|
|||
'yellow': 5,
|
||||
'magenta': 6,
|
||||
'orange': 7,
|
||||
'green': 7, #compat w/ ansi
|
||||
'green': 7, # compat w/ ansi
|
||||
'brightyellow': 8,
|
||||
'lightgreen': 9,
|
||||
'brightcyan': 9, # compat w/ ansi
|
||||
'brightcyan': 9, # compat w/ ansi
|
||||
'cyan': 10,
|
||||
'lightblue': 11,
|
||||
'red': 11, # compat w/ ansi
|
||||
'red': 11, # compat w/ ansi
|
||||
'brightblue': 12,
|
||||
'brightmagenta': 13,
|
||||
'brightblack': 14,
|
||||
'gray': 15,
|
||||
}
|
||||
|
||||
|
||||
def ircformat(color, text):
|
||||
if len(color) < 1:
|
||||
return text
|
||||
add = sub = ''
|
||||
if '_' in color: # italic
|
||||
if '_' in color: # italic
|
||||
add += '\x1D'
|
||||
sub = '\x1D' + sub
|
||||
color = color.strip('_')
|
||||
if '*' in color: # bold
|
||||
if '*' in color: # bold
|
||||
add += '\x02'
|
||||
sub = '\x02' + sub
|
||||
color = color.strip('*')
|
||||
# underline (\x1F) not supported
|
||||
# backgrounds (\x03FF,BB) not supported
|
||||
if len(color) > 0: # actual color - may have issues with ircformat("red", "blah")+"10" type stuff
|
||||
if len(color) > 0: # actual color - may have issues with ircformat("red", "blah")+"10" type stuff
|
||||
add += '\x03' + str(IRC_COLOR_MAP[color]).zfill(2)
|
||||
sub = '\x03' + sub
|
||||
return add + text + sub
|
||||
return '<'+add+'>'+text+'</'+sub+'>'
|
||||
return '<' + add + '>' + text + '</' + sub + '>'
|
||||
|
||||
|
||||
class IRCFormatter(Formatter):
|
||||
|
|
@ -121,21 +131,23 @@ class IRCFormatter(Formatter):
|
|||
|
||||
def __init__(self, **options):
|
||||
Formatter.__init__(self, **options)
|
||||
self.darkbg = get_choice_opt(options, 'bg',
|
||||
['light', 'dark'], 'light') == 'dark'
|
||||
self.darkbg = get_choice_opt(
|
||||
options, 'bg',
|
||||
['light', 'dark'], 'light',
|
||||
) == 'dark'
|
||||
self.colorscheme = options.get('colorscheme', None) or IRC_COLORS
|
||||
self.linenos = options.get('linenos', False)
|
||||
self._lineno = 0
|
||||
|
||||
def _write_lineno(self, outfile):
|
||||
self._lineno += 1
|
||||
outfile.write("\n%04d: " % self._lineno)
|
||||
outfile.write('\n%04d: ' % self._lineno)
|
||||
|
||||
def _format_unencoded_with_lineno(self, tokensource, outfile):
|
||||
self._write_lineno(outfile)
|
||||
|
||||
for ttype, value in tokensource:
|
||||
if value.endswith("\n"):
|
||||
if value.endswith('\n'):
|
||||
self._write_lineno(outfile)
|
||||
value = value[:-1]
|
||||
color = self.colorscheme.get(ttype)
|
||||
|
|
@ -154,7 +166,7 @@ class IRCFormatter(Formatter):
|
|||
else:
|
||||
outfile.write(value)
|
||||
|
||||
outfile.write("\n")
|
||||
outfile.write('\n')
|
||||
|
||||
def format_unencoded(self, tokensource, outfile):
|
||||
if self.linenos:
|
||||
|
|
|
|||
|
|
@ -7,13 +7,17 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from io import StringIO
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.lexer import Lexer, do_insertions
|
||||
from pip._vendor.pygments.token import Token, STANDARD_TYPES
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt
|
||||
from pip._vendor.pygments.lexer import do_insertions
|
||||
from pip._vendor.pygments.lexer import Lexer
|
||||
from pip._vendor.pygments.token import STANDARD_TYPES
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
|
||||
|
||||
__all__ = ['LatexFormatter']
|
||||
|
|
@ -21,23 +25,23 @@ __all__ = ['LatexFormatter']
|
|||
|
||||
def escape_tex(text, commandprefix):
|
||||
return text.replace('\\', '\x00'). \
|
||||
replace('{', '\x01'). \
|
||||
replace('}', '\x02'). \
|
||||
replace('\x00', r'\%sZbs{}' % commandprefix). \
|
||||
replace('\x01', r'\%sZob{}' % commandprefix). \
|
||||
replace('\x02', r'\%sZcb{}' % commandprefix). \
|
||||
replace('^', r'\%sZca{}' % commandprefix). \
|
||||
replace('_', r'\%sZus{}' % commandprefix). \
|
||||
replace('&', r'\%sZam{}' % commandprefix). \
|
||||
replace('<', r'\%sZlt{}' % commandprefix). \
|
||||
replace('>', r'\%sZgt{}' % commandprefix). \
|
||||
replace('#', r'\%sZsh{}' % commandprefix). \
|
||||
replace('%', r'\%sZpc{}' % commandprefix). \
|
||||
replace('$', r'\%sZdl{}' % commandprefix). \
|
||||
replace('-', r'\%sZhy{}' % commandprefix). \
|
||||
replace("'", r'\%sZsq{}' % commandprefix). \
|
||||
replace('"', r'\%sZdq{}' % commandprefix). \
|
||||
replace('~', r'\%sZti{}' % commandprefix)
|
||||
replace('{', '\x01'). \
|
||||
replace('}', '\x02'). \
|
||||
replace('\x00', r'\%sZbs{}' % commandprefix). \
|
||||
replace('\x01', r'\%sZob{}' % commandprefix). \
|
||||
replace('\x02', r'\%sZcb{}' % commandprefix). \
|
||||
replace('^', r'\%sZca{}' % commandprefix). \
|
||||
replace('_', r'\%sZus{}' % commandprefix). \
|
||||
replace('&', r'\%sZam{}' % commandprefix). \
|
||||
replace('<', r'\%sZlt{}' % commandprefix). \
|
||||
replace('>', r'\%sZgt{}' % commandprefix). \
|
||||
replace('#', r'\%sZsh{}' % commandprefix). \
|
||||
replace('%', r'\%sZpc{}' % commandprefix). \
|
||||
replace('$', r'\%sZdl{}' % commandprefix). \
|
||||
replace('-', r'\%sZhy{}' % commandprefix). \
|
||||
replace("'", r'\%sZsq{}' % commandprefix). \
|
||||
replace('"', r'\%sZdq{}' % commandprefix). \
|
||||
replace('~', r'\%sZti{}' % commandprefix)
|
||||
|
||||
|
||||
DOC_TEMPLATE = r'''
|
||||
|
|
@ -57,7 +61,7 @@ DOC_TEMPLATE = r'''
|
|||
\end{document}
|
||||
'''
|
||||
|
||||
## Small explanation of the mess below :)
|
||||
# Small explanation of the mess below :)
|
||||
#
|
||||
# The previous version of the LaTeX formatter just assigned a command to
|
||||
# each token type defined in the current style. That obviously is
|
||||
|
|
@ -275,8 +279,10 @@ class LatexFormatter(Formatter):
|
|||
|
||||
def rgbcolor(col):
|
||||
if col:
|
||||
return ','.join(['%.2f' % (int(col[i] + col[i + 1], 16) / 255.0)
|
||||
for i in (0, 2, 4)])
|
||||
return ','.join([
|
||||
'%.2f' % (int(col[i] + col[i + 1], 16) / 255.0)
|
||||
for i in (0, 2, 4)
|
||||
])
|
||||
else:
|
||||
return '1,1,1'
|
||||
|
||||
|
|
@ -296,17 +302,25 @@ class LatexFormatter(Formatter):
|
|||
if ndef['mono']:
|
||||
cmndef += r'\let\$$@ff=\textsf'
|
||||
if ndef['color']:
|
||||
cmndef += (r'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}' %
|
||||
rgbcolor(ndef['color']))
|
||||
cmndef += (
|
||||
r'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}' %
|
||||
rgbcolor(ndef['color'])
|
||||
)
|
||||
if ndef['border']:
|
||||
cmndef += (r'\def\$$@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}'
|
||||
r'\fcolorbox[rgb]{%s}{%s}{\strut ##1}}}' %
|
||||
(rgbcolor(ndef['border']),
|
||||
rgbcolor(ndef['bgcolor'])))
|
||||
cmndef += (
|
||||
r'\def\$$@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}'
|
||||
r'\fcolorbox[rgb]{%s}{%s}{\strut ##1}}}' %
|
||||
(
|
||||
rgbcolor(ndef['border']),
|
||||
rgbcolor(ndef['bgcolor']),
|
||||
)
|
||||
)
|
||||
elif ndef['bgcolor']:
|
||||
cmndef += (r'\def\$$@bc##1{{\setlength{\fboxsep}{0pt}'
|
||||
r'\colorbox[rgb]{%s}{\strut ##1}}}' %
|
||||
rgbcolor(ndef['bgcolor']))
|
||||
cmndef += (
|
||||
r'\def\$$@bc##1{{\setlength{\fboxsep}{0pt}'
|
||||
r'\colorbox[rgb]{%s}{\strut ##1}}}' %
|
||||
rgbcolor(ndef['bgcolor'])
|
||||
)
|
||||
if cmndef == '':
|
||||
continue
|
||||
cmndef = cmndef.replace('$$', cp)
|
||||
|
|
@ -321,9 +335,11 @@ class LatexFormatter(Formatter):
|
|||
cp = self.commandprefix
|
||||
styles = []
|
||||
for name, definition in self.cmd2def.items():
|
||||
styles.append(r'\@namedef{%s@tok@%s}{%s}' % (cp, name, definition))
|
||||
return STYLE_TEMPLATE % {'cp': self.commandprefix,
|
||||
'styles': '\n'.join(styles)}
|
||||
styles.append(r'\@namedef{{{}@tok@{}}}{{{}}}'.format(cp, name, definition))
|
||||
return STYLE_TEMPLATE % {
|
||||
'cp': self.commandprefix,
|
||||
'styles': '\n'.join(styles),
|
||||
}
|
||||
|
||||
def format_unencoded(self, tokensource, outfile):
|
||||
# TODO: add support for background colors
|
||||
|
|
@ -337,12 +353,16 @@ class LatexFormatter(Formatter):
|
|||
outfile.write('\\begin{' + self.envname + '}[commandchars=\\\\\\{\\}')
|
||||
if self.linenos:
|
||||
start, step = self.linenostart, self.linenostep
|
||||
outfile.write(',numbers=left' +
|
||||
(start and ',firstnumber=%d' % start or '') +
|
||||
(step and ',stepnumber=%d' % step or ''))
|
||||
outfile.write(
|
||||
',numbers=left' +
|
||||
(start and ',firstnumber=%d' % start or '') +
|
||||
(step and ',stepnumber=%d' % step or ''),
|
||||
)
|
||||
if self.mathescape or self.texcomments or self.escapeinside:
|
||||
outfile.write(',codes={\\catcode`\\$=3\\catcode`\\^=7'
|
||||
'\\catcode`\\_=8\\relax}')
|
||||
outfile.write(
|
||||
',codes={\\catcode`\\$=3\\catcode`\\^=7'
|
||||
'\\catcode`\\_=8\\relax}',
|
||||
)
|
||||
if self.verboptions:
|
||||
outfile.write(',' + self.verboptions)
|
||||
outfile.write(']\n')
|
||||
|
|
@ -401,10 +421,10 @@ class LatexFormatter(Formatter):
|
|||
spl = value.split('\n')
|
||||
for line in spl[:-1]:
|
||||
if line:
|
||||
outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
|
||||
outfile.write('\\{}{{{}}}{{{}}}'.format(cp, styleval, line))
|
||||
outfile.write('\n')
|
||||
if spl[-1]:
|
||||
outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
|
||||
outfile.write('\\{}{{{}}}{{{}}}'.format(cp, styleval, spl[-1]))
|
||||
else:
|
||||
outfile.write(value)
|
||||
|
||||
|
|
@ -418,13 +438,17 @@ class LatexFormatter(Formatter):
|
|||
'latin_1': 'latin1',
|
||||
'iso_8859_1': 'latin1',
|
||||
}.get(encoding.replace('-', '_'), encoding)
|
||||
realoutfile.write(DOC_TEMPLATE %
|
||||
dict(docclass = self.docclass,
|
||||
preamble = self.preamble,
|
||||
title = self.title,
|
||||
encoding = encoding,
|
||||
styledefs = self.get_style_defs(),
|
||||
code = outfile.getvalue()))
|
||||
realoutfile.write(
|
||||
DOC_TEMPLATE %
|
||||
dict(
|
||||
docclass=self.docclass,
|
||||
preamble=self.preamble,
|
||||
title=self.title,
|
||||
encoding=encoding,
|
||||
styledefs=self.get_style_defs(),
|
||||
code=outfile.getvalue(),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class LatexEmbeddedLexer(Lexer):
|
||||
|
|
@ -438,6 +462,7 @@ class LatexEmbeddedLexer(Lexer):
|
|||
the Token.Escape type. Finally text that is not escaped is scanned
|
||||
again with the language lexer.
|
||||
"""
|
||||
|
||||
def __init__(self, left, right, lang, **options):
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
|
@ -460,14 +485,16 @@ class LatexEmbeddedLexer(Lexer):
|
|||
insertion_buf.append((i, t, v))
|
||||
if insertion_buf:
|
||||
insertions.append((len(buffered), insertion_buf))
|
||||
return do_insertions(insertions,
|
||||
self.lang.get_tokens_unprocessed(buffered))
|
||||
return do_insertions(
|
||||
insertions,
|
||||
self.lang.get_tokens_unprocessed(buffered),
|
||||
)
|
||||
|
||||
def _find_safe_escape_tokens(self, text):
|
||||
""" find escape tokens that are not in strings or comments """
|
||||
for i, t, v in self._filter_to(
|
||||
self.lang.get_tokens_unprocessed(text),
|
||||
lambda t: t in Token.Comment or t in Token.String
|
||||
lambda t: t in Token.Comment or t in Token.String,
|
||||
):
|
||||
if t is None:
|
||||
for i2, t2, v2 in self._find_escape_tokens(v):
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.util import get_choice_opt
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.console import colorize
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.util import get_choice_opt
|
||||
|
||||
__all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter']
|
||||
|
||||
|
|
@ -65,8 +66,10 @@ class RawTokenFormatter(Formatter):
|
|||
# and formatter if given with -Oencoding on the command line.
|
||||
# The RawTokenFormatter outputs only ASCII. Override here.
|
||||
self.encoding = 'ascii' # let pygments.format() do the right thing
|
||||
self.compress = get_choice_opt(options, 'compress',
|
||||
['', 'none', 'gz', 'bz2'], '')
|
||||
self.compress = get_choice_opt(
|
||||
options, 'compress',
|
||||
['', 'none', 'gz', 'bz2'], '',
|
||||
)
|
||||
self.error_color = options.get('error_color', None)
|
||||
if self.error_color is True:
|
||||
self.error_color = 'red'
|
||||
|
|
@ -74,15 +77,19 @@ class RawTokenFormatter(Formatter):
|
|||
try:
|
||||
colorize(self.error_color, '')
|
||||
except KeyError:
|
||||
raise ValueError("Invalid color %r specified" %
|
||||
self.error_color)
|
||||
raise ValueError(
|
||||
'Invalid color %r specified' %
|
||||
self.error_color,
|
||||
)
|
||||
|
||||
def format(self, tokensource, outfile):
|
||||
try:
|
||||
outfile.write(b'')
|
||||
except TypeError:
|
||||
raise TypeError('The raw tokens formatter needs a binary '
|
||||
'output file')
|
||||
raise TypeError(
|
||||
'The raw tokens formatter needs a binary '
|
||||
'output file',
|
||||
)
|
||||
if self.compress == 'gz':
|
||||
import gzip
|
||||
outfile = gzip.GzipFile('', 'wb', 9, outfile)
|
||||
|
|
@ -105,14 +112,14 @@ class RawTokenFormatter(Formatter):
|
|||
|
||||
if self.error_color:
|
||||
for ttype, value in tokensource:
|
||||
line = b"%r\t%r\n" % (ttype, value)
|
||||
line = b'%r\t%r\n' % (ttype, value)
|
||||
if ttype is Token.Error:
|
||||
write(colorize(self.error_color, line))
|
||||
else:
|
||||
write(line)
|
||||
else:
|
||||
for ttype, value in tokensource:
|
||||
write(b"%r\t%r\n" % (ttype, value))
|
||||
write(b'%r\t%r\n' % (ttype, value))
|
||||
flush()
|
||||
|
||||
|
||||
|
|
@ -139,7 +146,7 @@ class TestcaseFormatter(Formatter):
|
|||
def __init__(self, **options):
|
||||
Formatter.__init__(self, **options)
|
||||
if self.encoding is not None and self.encoding != 'utf-8':
|
||||
raise ValueError("Only None and utf-8 are allowed encodings.")
|
||||
raise ValueError('Only None and utf-8 are allowed encodings.')
|
||||
|
||||
def format(self, tokensource, outfile):
|
||||
indentation = ' ' * 12
|
||||
|
|
@ -147,7 +154,7 @@ class TestcaseFormatter(Formatter):
|
|||
outbuf = []
|
||||
for ttype, value in tokensource:
|
||||
rawbuf.append(value)
|
||||
outbuf.append('%s(%s, %r),\n' % (indentation, ttype, value))
|
||||
outbuf.append('{}({}, {!r}),\n'.format(indentation, ttype, value))
|
||||
|
||||
before = TESTCASE_BEFORE % (''.join(rawbuf),)
|
||||
during = ''.join(outbuf)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
|
||||
|
|
|
|||
|
|
@ -7,9 +7,11 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.util import get_int_opt, surrogatepair
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
from pip._vendor.pygments.util import surrogatepair
|
||||
|
||||
|
||||
__all__ = ['RtfFormatter']
|
||||
|
|
@ -94,11 +96,15 @@ class RtfFormatter(Formatter):
|
|||
|
||||
def format_unencoded(self, tokensource, outfile):
|
||||
# rtf 1.8 header
|
||||
outfile.write('{\\rtf1\\ansi\\uc0\\deff0'
|
||||
'{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}'
|
||||
'{\\colortbl;' % (self.fontface and
|
||||
' ' + self._escape(self.fontface) or
|
||||
''))
|
||||
outfile.write(
|
||||
'{\\rtf1\\ansi\\uc0\\deff0'
|
||||
'{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}'
|
||||
'{\\colortbl;' % (
|
||||
self.fontface and
|
||||
' ' + self._escape(self.fontface) or
|
||||
''
|
||||
),
|
||||
)
|
||||
|
||||
# convert colors and save them in a mapping to access them later.
|
||||
color_mapping = {}
|
||||
|
|
@ -107,11 +113,13 @@ class RtfFormatter(Formatter):
|
|||
for color in style['color'], style['bgcolor'], style['border']:
|
||||
if color and color not in color_mapping:
|
||||
color_mapping[color] = offset
|
||||
outfile.write('\\red%d\\green%d\\blue%d;' % (
|
||||
int(color[0:2], 16),
|
||||
int(color[2:4], 16),
|
||||
int(color[4:6], 16)
|
||||
))
|
||||
outfile.write(
|
||||
'\\red%d\\green%d\\blue%d;' % (
|
||||
int(color[0:2], 16),
|
||||
int(color[2:4], 16),
|
||||
int(color[4:6], 16),
|
||||
),
|
||||
)
|
||||
offset += 1
|
||||
outfile.write('}\\f0 ')
|
||||
if self.fontsize:
|
||||
|
|
@ -134,8 +142,10 @@ class RtfFormatter(Formatter):
|
|||
if style['underline']:
|
||||
buf.append('\\ul')
|
||||
if style['border']:
|
||||
buf.append('\\chbrdr\\chcfpat%d' %
|
||||
color_mapping[style['border']])
|
||||
buf.append(
|
||||
'\\chbrdr\\chcfpat%d' %
|
||||
color_mapping[style['border']],
|
||||
)
|
||||
start = ''.join(buf)
|
||||
if start:
|
||||
outfile.write('{%s ' % start)
|
||||
|
|
|
|||
|
|
@ -7,10 +7,12 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Comment
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
|
||||
__all__ = ['SvgFormatter']
|
||||
|
||||
|
|
@ -18,14 +20,15 @@ __all__ = ['SvgFormatter']
|
|||
def escape_html(text):
|
||||
"""Escape &, <, > as well as single and double quotes for HTML."""
|
||||
return text.replace('&', '&'). \
|
||||
replace('<', '<'). \
|
||||
replace('>', '>'). \
|
||||
replace('"', '"'). \
|
||||
replace("'", ''')
|
||||
replace('<', '<'). \
|
||||
replace('>', '>'). \
|
||||
replace('"', '"'). \
|
||||
replace("'", ''')
|
||||
|
||||
|
||||
class2style = {}
|
||||
|
||||
|
||||
class SvgFormatter(Formatter):
|
||||
"""
|
||||
Format tokens as an SVG graphics file. This formatter is still experimental.
|
||||
|
|
@ -60,11 +63,11 @@ class SvgFormatter(Formatter):
|
|||
|
||||
`linenostep`
|
||||
If set to a number n > 1, only every nth line number is printed.
|
||||
|
||||
|
||||
`linenowidth`
|
||||
Maximum width devoted to line numbers (default: ``3*ystep``, sufficient
|
||||
for up to 4-digit line numbers. Increase width for longer code blocks).
|
||||
|
||||
for up to 4-digit line numbers. Increase width for longer code blocks).
|
||||
|
||||
`xoffset`
|
||||
Starting offset in X direction, defaults to ``0``.
|
||||
|
||||
|
|
@ -97,7 +100,8 @@ class SvgFormatter(Formatter):
|
|||
self.fontsize = options.get('fontsize', '14px')
|
||||
self.xoffset = get_int_opt(options, 'xoffset', 0)
|
||||
fs = self.fontsize.strip()
|
||||
if fs.endswith('px'): fs = fs[:-2].strip()
|
||||
if fs.endswith('px'):
|
||||
fs = fs[:-2].strip()
|
||||
try:
|
||||
int_fs = int(fs)
|
||||
except:
|
||||
|
|
@ -105,10 +109,10 @@ class SvgFormatter(Formatter):
|
|||
self.yoffset = get_int_opt(options, 'yoffset', int_fs)
|
||||
self.ystep = get_int_opt(options, 'ystep', int_fs + 5)
|
||||
self.spacehack = get_bool_opt(options, 'spacehack', True)
|
||||
self.linenos = get_bool_opt(options,'linenos',False)
|
||||
self.linenostart = get_int_opt(options,'linenostart',1)
|
||||
self.linenostep = get_int_opt(options,'linenostep',1)
|
||||
self.linenowidth = get_int_opt(options,'linenowidth', 3*self.ystep)
|
||||
self.linenos = get_bool_opt(options, 'linenos', False)
|
||||
self.linenostart = get_int_opt(options, 'linenostart', 1)
|
||||
self.linenostep = get_int_opt(options, 'linenostep', 1)
|
||||
self.linenowidth = get_int_opt(options, 'linenowidth', 3 * self.ystep)
|
||||
self._stylecache = {}
|
||||
|
||||
def format_unencoded(self, tokensource, outfile):
|
||||
|
|
@ -122,30 +126,38 @@ class SvgFormatter(Formatter):
|
|||
y = self.yoffset
|
||||
if not self.nowrap:
|
||||
if self.encoding:
|
||||
outfile.write('<?xml version="1.0" encoding="%s"?>\n' %
|
||||
self.encoding)
|
||||
outfile.write(
|
||||
'<?xml version="1.0" encoding="%s"?>\n' %
|
||||
self.encoding,
|
||||
)
|
||||
else:
|
||||
outfile.write('<?xml version="1.0"?>\n')
|
||||
outfile.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
|
||||
'"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/'
|
||||
'svg10.dtd">\n')
|
||||
outfile.write(
|
||||
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
|
||||
'"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/'
|
||||
'svg10.dtd">\n',
|
||||
)
|
||||
outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
|
||||
outfile.write('<g font-family="%s" font-size="%s">\n' %
|
||||
(self.fontfamily, self.fontsize))
|
||||
|
||||
counter = self.linenostart
|
||||
outfile.write(
|
||||
'<g font-family="%s" font-size="%s">\n' %
|
||||
(self.fontfamily, self.fontsize),
|
||||
)
|
||||
|
||||
counter = self.linenostart
|
||||
counter_step = self.linenostep
|
||||
counter_style = self._get_style(Comment)
|
||||
line_x = x
|
||||
|
||||
|
||||
if self.linenos:
|
||||
if counter % counter_step == 0:
|
||||
outfile.write('<text x="%s" y="%s" %s text-anchor="end">%s</text>' %
|
||||
(x+self.linenowidth,y,counter_style,counter))
|
||||
outfile.write(
|
||||
'<text x="%s" y="%s" %s text-anchor="end">%s</text>' %
|
||||
(x + self.linenowidth, y, counter_style, counter),
|
||||
)
|
||||
line_x += self.linenowidth + self.ystep
|
||||
counter += 1
|
||||
|
||||
outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (line_x, y))
|
||||
outfile.write('<text x="{}" y="{}" xml:space="preserve">'.format(line_x, y))
|
||||
for ttype, value in tokensource:
|
||||
style = self._get_style(ttype)
|
||||
tspan = style and '<tspan' + style + '>' or ''
|
||||
|
|
@ -159,11 +171,13 @@ class SvgFormatter(Formatter):
|
|||
y += self.ystep
|
||||
outfile.write('</text>\n')
|
||||
if self.linenos and counter % counter_step == 0:
|
||||
outfile.write('<text x="%s" y="%s" text-anchor="end" %s>%s</text>' %
|
||||
(x+self.linenowidth,y,counter_style,counter))
|
||||
|
||||
outfile.write(
|
||||
'<text x="%s" y="%s" text-anchor="end" %s>%s</text>' %
|
||||
(x + self.linenowidth, y, counter_style, counter),
|
||||
)
|
||||
|
||||
counter += 1
|
||||
outfile.write('<text x="%s" y="%s" ' 'xml:space="preserve">' % (line_x,y))
|
||||
outfile.write('<text x="%s" y="%s" ' 'xml:space="preserve">' % (line_x, y))
|
||||
outfile.write(tspan + parts[-1] + tspanend)
|
||||
outfile.write('</text>')
|
||||
|
||||
|
|
|
|||
|
|
@ -7,11 +7,20 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Keyword, Name, Comment, String, Error, \
|
||||
Number, Operator, Generic, Token, Whitespace
|
||||
from pip._vendor.pygments.console import ansiformat
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.token import Comment
|
||||
from pip._vendor.pygments.token import Error
|
||||
from pip._vendor.pygments.token import Generic
|
||||
from pip._vendor.pygments.token import Keyword
|
||||
from pip._vendor.pygments.token import Name
|
||||
from pip._vendor.pygments.token import Number
|
||||
from pip._vendor.pygments.token import Operator
|
||||
from pip._vendor.pygments.token import String
|
||||
from pip._vendor.pygments.token import Token
|
||||
from pip._vendor.pygments.token import Whitespace
|
||||
from pip._vendor.pygments.util import get_choice_opt
|
||||
|
||||
|
||||
|
|
@ -21,35 +30,35 @@ __all__ = ['TerminalFormatter']
|
|||
#: Map token types to a tuple of color values for light and dark
|
||||
#: backgrounds.
|
||||
TERMINAL_COLORS = {
|
||||
Token: ('', ''),
|
||||
Token: ('', ''),
|
||||
|
||||
Whitespace: ('gray', 'brightblack'),
|
||||
Comment: ('gray', 'brightblack'),
|
||||
Comment.Preproc: ('cyan', 'brightcyan'),
|
||||
Keyword: ('blue', 'brightblue'),
|
||||
Keyword.Type: ('cyan', 'brightcyan'),
|
||||
Operator.Word: ('magenta', 'brightmagenta'),
|
||||
Name.Builtin: ('cyan', 'brightcyan'),
|
||||
Name.Function: ('green', 'brightgreen'),
|
||||
Name.Namespace: ('_cyan_', '_brightcyan_'),
|
||||
Name.Class: ('_green_', '_brightgreen_'),
|
||||
Name.Exception: ('cyan', 'brightcyan'),
|
||||
Name.Decorator: ('brightblack', 'gray'),
|
||||
Name.Variable: ('red', 'brightred'),
|
||||
Name.Constant: ('red', 'brightred'),
|
||||
Name.Attribute: ('cyan', 'brightcyan'),
|
||||
Name.Tag: ('brightblue', 'brightblue'),
|
||||
String: ('yellow', 'yellow'),
|
||||
Number: ('blue', 'brightblue'),
|
||||
Whitespace: ('gray', 'brightblack'),
|
||||
Comment: ('gray', 'brightblack'),
|
||||
Comment.Preproc: ('cyan', 'brightcyan'),
|
||||
Keyword: ('blue', 'brightblue'),
|
||||
Keyword.Type: ('cyan', 'brightcyan'),
|
||||
Operator.Word: ('magenta', 'brightmagenta'),
|
||||
Name.Builtin: ('cyan', 'brightcyan'),
|
||||
Name.Function: ('green', 'brightgreen'),
|
||||
Name.Namespace: ('_cyan_', '_brightcyan_'),
|
||||
Name.Class: ('_green_', '_brightgreen_'),
|
||||
Name.Exception: ('cyan', 'brightcyan'),
|
||||
Name.Decorator: ('brightblack', 'gray'),
|
||||
Name.Variable: ('red', 'brightred'),
|
||||
Name.Constant: ('red', 'brightred'),
|
||||
Name.Attribute: ('cyan', 'brightcyan'),
|
||||
Name.Tag: ('brightblue', 'brightblue'),
|
||||
String: ('yellow', 'yellow'),
|
||||
Number: ('blue', 'brightblue'),
|
||||
|
||||
Generic.Deleted: ('brightred', 'brightred'),
|
||||
Generic.Inserted: ('green', 'brightgreen'),
|
||||
Generic.Heading: ('**', '**'),
|
||||
Generic.Subheading: ('*magenta*', '*brightmagenta*'),
|
||||
Generic.Prompt: ('**', '**'),
|
||||
Generic.Error: ('brightred', 'brightred'),
|
||||
Generic.Deleted: ('brightred', 'brightred'),
|
||||
Generic.Inserted: ('green', 'brightgreen'),
|
||||
Generic.Heading: ('**', '**'),
|
||||
Generic.Subheading: ('*magenta*', '*brightmagenta*'),
|
||||
Generic.Prompt: ('**', '**'),
|
||||
Generic.Error: ('brightred', 'brightred'),
|
||||
|
||||
Error: ('_brightred_', '_brightred_'),
|
||||
Error: ('_brightred_', '_brightred_'),
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -82,8 +91,10 @@ class TerminalFormatter(Formatter):
|
|||
|
||||
def __init__(self, **options):
|
||||
Formatter.__init__(self, **options)
|
||||
self.darkbg = get_choice_opt(options, 'bg',
|
||||
['light', 'dark'], 'light') == 'dark'
|
||||
self.darkbg = get_choice_opt(
|
||||
options, 'bg',
|
||||
['light', 'dark'], 'light',
|
||||
) == 'dark'
|
||||
self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS
|
||||
self.linenos = options.get('linenos', False)
|
||||
self._lineno = 0
|
||||
|
|
@ -93,7 +104,7 @@ class TerminalFormatter(Formatter):
|
|||
|
||||
def _write_lineno(self, outfile):
|
||||
self._lineno += 1
|
||||
outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
outfile.write('%s%04d: ' % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
|
||||
def _get_color(self, ttype):
|
||||
# self.colorscheme is a dict containing usually generic types, so we
|
||||
|
|
@ -124,4 +135,4 @@ class TerminalFormatter(Formatter):
|
|||
outfile.write('\n')
|
||||
|
||||
if self.linenos:
|
||||
outfile.write("\n")
|
||||
outfile.write('\n')
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
# TODO:
|
||||
# - Options to map style's bold/underline/italic/border attributes
|
||||
# to some ANSI attrbutes (something like 'italic=underline')
|
||||
|
|
@ -22,9 +21,10 @@
|
|||
# xterm. This means that default colors are white-on-black, not
|
||||
# black-on-while, so colors like "white background" need to be converted
|
||||
# to "white background, black foreground", etc...
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.console import codes
|
||||
from pip._vendor.pygments.formatter import Formatter
|
||||
from pip._vendor.pygments.style import ansicolors
|
||||
|
||||
|
||||
|
|
@ -41,57 +41,57 @@ class EscapeSequence:
|
|||
|
||||
def escape(self, attrs):
|
||||
if len(attrs):
|
||||
return "\x1b[" + ";".join(attrs) + "m"
|
||||
return ""
|
||||
return '\x1b[' + ';'.join(attrs) + 'm'
|
||||
return ''
|
||||
|
||||
def color_string(self):
|
||||
attrs = []
|
||||
if self.fg is not None:
|
||||
if self.fg in ansicolors:
|
||||
esc = codes[self.fg.replace('ansi','')]
|
||||
esc = codes[self.fg.replace('ansi', '')]
|
||||
if ';01m' in esc:
|
||||
self.bold = True
|
||||
# extract fg color code.
|
||||
attrs.append(esc[2:4])
|
||||
else:
|
||||
attrs.extend(("38", "5", "%i" % self.fg))
|
||||
attrs.extend(('38', '5', '%i' % self.fg))
|
||||
if self.bg is not None:
|
||||
if self.bg in ansicolors:
|
||||
esc = codes[self.bg.replace('ansi','')]
|
||||
esc = codes[self.bg.replace('ansi', '')]
|
||||
# extract fg color code, add 10 for bg.
|
||||
attrs.append(str(int(esc[2:4])+10))
|
||||
attrs.append(str(int(esc[2:4]) + 10))
|
||||
else:
|
||||
attrs.extend(("48", "5", "%i" % self.bg))
|
||||
attrs.extend(('48', '5', '%i' % self.bg))
|
||||
if self.bold:
|
||||
attrs.append("01")
|
||||
attrs.append('01')
|
||||
if self.underline:
|
||||
attrs.append("04")
|
||||
attrs.append('04')
|
||||
if self.italic:
|
||||
attrs.append("03")
|
||||
attrs.append('03')
|
||||
return self.escape(attrs)
|
||||
|
||||
def true_color_string(self):
|
||||
attrs = []
|
||||
if self.fg:
|
||||
attrs.extend(("38", "2", str(self.fg[0]), str(self.fg[1]), str(self.fg[2])))
|
||||
attrs.extend(('38', '2', str(self.fg[0]), str(self.fg[1]), str(self.fg[2])))
|
||||
if self.bg:
|
||||
attrs.extend(("48", "2", str(self.bg[0]), str(self.bg[1]), str(self.bg[2])))
|
||||
attrs.extend(('48', '2', str(self.bg[0]), str(self.bg[1]), str(self.bg[2])))
|
||||
if self.bold:
|
||||
attrs.append("01")
|
||||
attrs.append('01')
|
||||
if self.underline:
|
||||
attrs.append("04")
|
||||
attrs.append('04')
|
||||
if self.italic:
|
||||
attrs.append("03")
|
||||
attrs.append('03')
|
||||
return self.escape(attrs)
|
||||
|
||||
def reset_string(self):
|
||||
attrs = []
|
||||
if self.fg is not None:
|
||||
attrs.append("39")
|
||||
attrs.append('39')
|
||||
if self.bg is not None:
|
||||
attrs.append("49")
|
||||
attrs.append('49')
|
||||
if self.bold or self.underline or self.italic:
|
||||
attrs.append("00")
|
||||
attrs.append('00')
|
||||
return self.escape(attrs)
|
||||
|
||||
|
||||
|
|
@ -186,7 +186,7 @@ class Terminal256Formatter(Formatter):
|
|||
self.xterm_colors.append((v, v, v))
|
||||
|
||||
def _closest_color(self, r, g, b):
|
||||
distance = 257*257*3 # "infinity" (>distance from #000000 to #ffffff)
|
||||
distance = 257 * 257 * 3 # "infinity" (>distance from #000000 to #ffffff)
|
||||
match = 0
|
||||
|
||||
for i in range(0, 254):
|
||||
|
|
@ -195,7 +195,7 @@ class Terminal256Formatter(Formatter):
|
|||
rd = r - values[0]
|
||||
gd = g - values[1]
|
||||
bd = b - values[2]
|
||||
d = rd*rd + gd*gd + bd*bd
|
||||
d = rd * rd + gd * gd + bd * bd
|
||||
|
||||
if d < distance:
|
||||
match = i
|
||||
|
|
@ -239,12 +239,14 @@ class Terminal256Formatter(Formatter):
|
|||
escape.underline = True
|
||||
if self.useitalic and ndef['italic']:
|
||||
escape.italic = True
|
||||
self.style_string[str(ttype)] = (escape.color_string(),
|
||||
escape.reset_string())
|
||||
self.style_string[str(ttype)] = (
|
||||
escape.color_string(),
|
||||
escape.reset_string(),
|
||||
)
|
||||
|
||||
def _write_lineno(self, outfile):
|
||||
self._lineno += 1
|
||||
outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
outfile.write('%s%04d: ' % (self._lineno != 1 and '\n' or '', self._lineno))
|
||||
|
||||
def format(self, tokensource, outfile):
|
||||
return Formatter.format(self, tokensource, outfile)
|
||||
|
|
@ -286,8 +288,7 @@ class Terminal256Formatter(Formatter):
|
|||
outfile.write(value)
|
||||
|
||||
if self.linenos:
|
||||
outfile.write("\n")
|
||||
|
||||
outfile.write('\n')
|
||||
|
||||
|
||||
class TerminalTrueColorFormatter(Terminal256Formatter):
|
||||
|
|
@ -334,5 +335,7 @@ class TerminalTrueColorFormatter(Terminal256Formatter):
|
|||
escape.underline = True
|
||||
if self.useitalic and ndef['italic']:
|
||||
escape.italic = True
|
||||
self.style_string[str(ttype)] = (escape.true_color_string(),
|
||||
escape.reset_string())
|
||||
self.style_string[str(ttype)] = (
|
||||
escape.true_color_string(),
|
||||
escape.reset_string(),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,28 +7,41 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
|
||||
from pip._vendor.pygments.filter import apply_filters, Filter
|
||||
from pip._vendor.pygments.filter import apply_filters
|
||||
from pip._vendor.pygments.filter import Filter
|
||||
from pip._vendor.pygments.filters import get_filter_by_name
|
||||
from pip._vendor.pygments.token import Error, Text, Other, _TokenType
|
||||
from pip._vendor.pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
|
||||
make_analysator, Future, guess_decode
|
||||
from pip._vendor.pygments.regexopt import regex_opt
|
||||
from pip._vendor.pygments.token import _TokenType
|
||||
from pip._vendor.pygments.token import Error
|
||||
from pip._vendor.pygments.token import Other
|
||||
from pip._vendor.pygments.token import Text
|
||||
from pip._vendor.pygments.util import Future
|
||||
from pip._vendor.pygments.util import get_bool_opt
|
||||
from pip._vendor.pygments.util import get_int_opt
|
||||
from pip._vendor.pygments.util import get_list_opt
|
||||
from pip._vendor.pygments.util import guess_decode
|
||||
from pip._vendor.pygments.util import make_analysator
|
||||
|
||||
__all__ = ['Lexer', 'RegexLexer', 'ExtendedRegexLexer', 'DelegatingLexer',
|
||||
'LexerContext', 'include', 'inherit', 'bygroups', 'using', 'this',
|
||||
'default', 'words']
|
||||
__all__ = [
|
||||
'Lexer', 'RegexLexer', 'ExtendedRegexLexer', 'DelegatingLexer',
|
||||
'LexerContext', 'include', 'inherit', 'bygroups', 'using', 'this',
|
||||
'default', 'words',
|
||||
]
|
||||
|
||||
|
||||
_encoding_map = [(b'\xef\xbb\xbf', 'utf-8'),
|
||||
(b'\xff\xfe\0\0', 'utf-32'),
|
||||
(b'\0\0\xfe\xff', 'utf-32be'),
|
||||
(b'\xff\xfe', 'utf-16'),
|
||||
(b'\xfe\xff', 'utf-16be')]
|
||||
_encoding_map = [
|
||||
(b'\xef\xbb\xbf', 'utf-8'),
|
||||
(b'\xff\xfe\0\0', 'utf-32'),
|
||||
(b'\0\0\xfe\xff', 'utf-32be'),
|
||||
(b'\xff\xfe', 'utf-16'),
|
||||
(b'\xfe\xff', 'utf-16be'),
|
||||
]
|
||||
|
||||
_default_analyse = staticmethod(lambda x: 0.0)
|
||||
|
||||
|
|
@ -105,8 +118,10 @@ class Lexer(metaclass=LexerMeta):
|
|||
|
||||
def __repr__(self):
|
||||
if self.options:
|
||||
return '<pygments.lexers.%s with %r>' % (self.__class__.__name__,
|
||||
self.options)
|
||||
return '<pygments.lexers.{} with {!r}>'.format(
|
||||
self.__class__.__name__,
|
||||
self.options,
|
||||
)
|
||||
else:
|
||||
return '<pygments.lexers.%s>' % self.__class__.__name__
|
||||
|
||||
|
|
@ -148,9 +163,11 @@ class Lexer(metaclass=LexerMeta):
|
|||
try:
|
||||
from pip._vendor import chardet
|
||||
except ImportError as e:
|
||||
raise ImportError('To enable chardet encoding guessing, '
|
||||
'please install the chardet library '
|
||||
'from http://chardet.feedparser.org/') from e
|
||||
raise ImportError(
|
||||
'To enable chardet encoding guessing, '
|
||||
'please install the chardet library '
|
||||
'from http://chardet.feedparser.org/',
|
||||
) from e
|
||||
# check for BOM first
|
||||
decoded = None
|
||||
for bom, encoding in _encoding_map:
|
||||
|
|
@ -160,8 +177,10 @@ class Lexer(metaclass=LexerMeta):
|
|||
# no BOM found, so use chardet
|
||||
if decoded is None:
|
||||
enc = chardet.detect(text[:1024]) # Guess using first 1KB
|
||||
decoded = text.decode(enc.get('encoding') or 'utf-8',
|
||||
'replace')
|
||||
decoded = text.decode(
|
||||
enc.get('encoding') or 'utf-8',
|
||||
'replace',
|
||||
)
|
||||
text = decoded
|
||||
else:
|
||||
text = text.decode(self.encoding)
|
||||
|
|
@ -232,8 +251,10 @@ class DelegatingLexer(Lexer):
|
|||
lng_buffer.append((i, t, v))
|
||||
if lng_buffer:
|
||||
insertions.append((len(buffered), lng_buffer))
|
||||
return do_insertions(insertions,
|
||||
self.root_lexer.get_tokens_unprocessed(buffered))
|
||||
return do_insertions(
|
||||
insertions,
|
||||
self.root_lexer.get_tokens_unprocessed(buffered),
|
||||
)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
|
@ -252,9 +273,11 @@ class _inherit:
|
|||
"""
|
||||
Indicates the a state should inherit from its superclass.
|
||||
"""
|
||||
|
||||
def __repr__(self):
|
||||
return 'inherit'
|
||||
|
||||
|
||||
inherit = _inherit() # pylint: disable=invalid-name
|
||||
|
||||
|
||||
|
|
@ -315,8 +338,10 @@ def bygroups(*args):
|
|||
if data is not None:
|
||||
if ctx:
|
||||
ctx.pos = match.start(i + 1)
|
||||
for item in action(lexer,
|
||||
_PseudoMatch(match.start(i + 1), data), ctx):
|
||||
for item in action(
|
||||
lexer,
|
||||
_PseudoMatch(match.start(i + 1), data), ctx,
|
||||
):
|
||||
if item:
|
||||
yield item
|
||||
if ctx:
|
||||
|
|
@ -330,6 +355,7 @@ class _This:
|
|||
Used by ``using``.
|
||||
"""
|
||||
|
||||
|
||||
this = _This()
|
||||
|
||||
|
||||
|
|
@ -391,6 +417,7 @@ class default:
|
|||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
def __init__(self, state):
|
||||
self.state = state
|
||||
|
||||
|
|
@ -402,6 +429,7 @@ class words(Future):
|
|||
|
||||
.. versionadded:: 2.0
|
||||
"""
|
||||
|
||||
def __init__(self, words, prefix='', suffix=''):
|
||||
self.words = words
|
||||
self.prefix = prefix
|
||||
|
|
@ -426,7 +454,7 @@ class RegexLexerMeta(LexerMeta):
|
|||
def _process_token(cls, token):
|
||||
"""Preprocess the token component of a token definition."""
|
||||
assert type(token) is _TokenType or callable(token), \
|
||||
'token type must be simple type or callable, not %r' % (token,)
|
||||
'token type must be simple type or callable, not {!r}'.format(token)
|
||||
return token
|
||||
|
||||
def _process_new_state(cls, new_state, unprocessed, processed):
|
||||
|
|
@ -450,15 +478,21 @@ class RegexLexerMeta(LexerMeta):
|
|||
itokens = []
|
||||
for istate in new_state:
|
||||
assert istate != new_state, 'circular state ref %r' % istate
|
||||
itokens.extend(cls._process_state(unprocessed,
|
||||
processed, istate))
|
||||
itokens.extend(
|
||||
cls._process_state(
|
||||
unprocessed,
|
||||
processed, istate,
|
||||
),
|
||||
)
|
||||
processed[tmp_state] = itokens
|
||||
return (tmp_state,)
|
||||
elif isinstance(new_state, tuple):
|
||||
# push more than one state
|
||||
for istate in new_state:
|
||||
assert (istate in unprocessed or
|
||||
istate in ('#pop', '#push')), \
|
||||
assert (
|
||||
istate in unprocessed or
|
||||
istate in ('#pop', '#push')
|
||||
), \
|
||||
'unknown new state ' + istate
|
||||
return new_state
|
||||
else:
|
||||
|
|
@ -466,8 +500,8 @@ class RegexLexerMeta(LexerMeta):
|
|||
|
||||
def _process_state(cls, unprocessed, processed, state):
|
||||
"""Preprocess a single state definition."""
|
||||
assert type(state) is str, "wrong state name %r" % state
|
||||
assert state[0] != '#', "invalid state name %r" % state
|
||||
assert type(state) is str, 'wrong state name %r' % state
|
||||
assert state[0] != '#', 'invalid state name %r' % state
|
||||
if state in processed:
|
||||
return processed[state]
|
||||
tokens = processed[state] = []
|
||||
|
|
@ -475,9 +509,13 @@ class RegexLexerMeta(LexerMeta):
|
|||
for tdef in unprocessed[state]:
|
||||
if isinstance(tdef, include):
|
||||
# it's a state reference
|
||||
assert tdef != state, "circular state reference %r" % state
|
||||
tokens.extend(cls._process_state(unprocessed, processed,
|
||||
str(tdef)))
|
||||
assert tdef != state, 'circular state reference %r' % state
|
||||
tokens.extend(
|
||||
cls._process_state(
|
||||
unprocessed, processed,
|
||||
str(tdef),
|
||||
),
|
||||
)
|
||||
continue
|
||||
if isinstance(tdef, _inherit):
|
||||
# should be processed already, but may not in the case of:
|
||||
|
|
@ -489,21 +527,25 @@ class RegexLexerMeta(LexerMeta):
|
|||
tokens.append((re.compile('').match, None, new_state))
|
||||
continue
|
||||
|
||||
assert type(tdef) is tuple, "wrong rule def %r" % tdef
|
||||
assert type(tdef) is tuple, 'wrong rule def %r' % tdef
|
||||
|
||||
try:
|
||||
rex = cls._process_regex(tdef[0], rflags, state)
|
||||
except Exception as err:
|
||||
raise ValueError("uncompilable regex %r in state %r of %r: %s" %
|
||||
(tdef[0], state, cls, err)) from err
|
||||
raise ValueError(
|
||||
'uncompilable regex %r in state %r of %r: %s' %
|
||||
(tdef[0], state, cls, err),
|
||||
) from err
|
||||
|
||||
token = cls._process_token(tdef[1])
|
||||
|
||||
if len(tdef) == 2:
|
||||
new_state = None
|
||||
else:
|
||||
new_state = cls._process_new_state(tdef[2],
|
||||
unprocessed, processed)
|
||||
new_state = cls._process_new_state(
|
||||
tdef[2],
|
||||
unprocessed, processed,
|
||||
)
|
||||
|
||||
tokens.append((rex, token, new_state))
|
||||
return tokens
|
||||
|
|
@ -553,7 +595,7 @@ class RegexLexerMeta(LexerMeta):
|
|||
continue
|
||||
|
||||
# Replace the "inherit" value with the items
|
||||
curitems[inherit_ndx:inherit_ndx+1] = items
|
||||
curitems[inherit_ndx:inherit_ndx + 1] = items
|
||||
try:
|
||||
# N.b. this is the index in items (that is, the superclass
|
||||
# copy), so offset required when storing below.
|
||||
|
|
@ -656,7 +698,7 @@ class RegexLexer(Lexer, metaclass=RegexLexerMeta):
|
|||
elif new_state == '#push':
|
||||
statestack.append(statestack[-1])
|
||||
else:
|
||||
assert False, "wrong state def: %r" % new_state
|
||||
assert False, 'wrong state def: %r' % new_state
|
||||
statetokens = tokendefs[statestack[-1]]
|
||||
break
|
||||
else:
|
||||
|
|
@ -688,8 +730,9 @@ class LexerContext:
|
|||
self.stack = stack or ['root']
|
||||
|
||||
def __repr__(self):
|
||||
return 'LexerContext(%r, %r, %r)' % (
|
||||
self.text, self.pos, self.stack)
|
||||
return 'LexerContext({!r}, {!r}, {!r})'.format(
|
||||
self.text, self.pos, self.stack,
|
||||
)
|
||||
|
||||
|
||||
class ExtendedRegexLexer(RegexLexer):
|
||||
|
|
@ -744,7 +787,7 @@ class ExtendedRegexLexer(RegexLexer):
|
|||
elif new_state == '#push':
|
||||
ctx.stack.append(ctx.stack[-1])
|
||||
else:
|
||||
assert False, "wrong state def: %r" % new_state
|
||||
assert False, 'wrong state def: %r' % new_state
|
||||
statetokens = tokendefs[ctx.stack[-1]]
|
||||
break
|
||||
else:
|
||||
|
|
@ -833,8 +876,10 @@ class ProfilingRegexLexerMeta(RegexLexerMeta):
|
|||
|
||||
def _process_regex(cls, regex, rflags, state):
|
||||
if isinstance(regex, words):
|
||||
rex = regex_opt(regex.words, prefix=regex.prefix,
|
||||
suffix=regex.suffix)
|
||||
rex = regex_opt(
|
||||
regex.words, prefix=regex.prefix,
|
||||
suffix=regex.suffix,
|
||||
)
|
||||
else:
|
||||
rex = regex
|
||||
compiled = re.compile(rex, rflags)
|
||||
|
|
@ -861,16 +906,22 @@ class ProfilingRegexLexer(RegexLexer, metaclass=ProfilingRegexLexerMeta):
|
|||
self.__class__._prof_data.append({})
|
||||
yield from RegexLexer.get_tokens_unprocessed(self, text, stack)
|
||||
rawdata = self.__class__._prof_data.pop()
|
||||
data = sorted(((s, repr(r).strip('u\'').replace('\\\\', '\\')[:65],
|
||||
n, 1000 * t, 1000 * t / n)
|
||||
for ((s, r), (n, t)) in rawdata.items()),
|
||||
key=lambda x: x[self._prof_sort_index],
|
||||
reverse=True)
|
||||
data = sorted(
|
||||
((
|
||||
s, repr(r).strip('u\'').replace('\\\\', '\\')[:65],
|
||||
n, 1000 * t, 1000 * t / n,
|
||||
)
|
||||
for ((s, r), (n, t)) in rawdata.items()),
|
||||
key=lambda x: x[self._prof_sort_index],
|
||||
reverse=True,
|
||||
)
|
||||
sum_total = sum(x[3] for x in data)
|
||||
|
||||
print()
|
||||
print('Profiling result for %s lexing %d chars in %.3f ms' %
|
||||
(self.__class__.__name__, len(text), sum_total))
|
||||
print(
|
||||
'Profiling result for %s lexing %d chars in %.3f ms' %
|
||||
(self.__class__.__name__, len(text), sum_total),
|
||||
)
|
||||
print('=' * 110)
|
||||
print('%-20s %-64s ncalls tottime percall' % ('state', 'regex'))
|
||||
print('-' * 110)
|
||||
|
|
|
|||
|
|
@ -7,25 +7,29 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import fnmatch
|
||||
import re
|
||||
import sys
|
||||
import types
|
||||
import fnmatch
|
||||
from os.path import basename
|
||||
|
||||
from pip._vendor.pygments.lexers._mapping import LEXERS
|
||||
from pip._vendor.pygments.modeline import get_filetype_from_buffer
|
||||
from pip._vendor.pygments.plugin import find_plugin_lexers
|
||||
from pip._vendor.pygments.util import ClassNotFound, guess_decode
|
||||
from pip._vendor.pygments.util import ClassNotFound
|
||||
from pip._vendor.pygments.util import guess_decode
|
||||
|
||||
COMPAT = {
|
||||
'Python3Lexer': 'PythonLexer',
|
||||
'Python3TracebackLexer': 'PythonTracebackLexer',
|
||||
}
|
||||
|
||||
__all__ = ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
|
||||
'guess_lexer', 'load_lexer_from_file'] + list(LEXERS) + list(COMPAT)
|
||||
__all__ = [
|
||||
'get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
|
||||
'guess_lexer', 'load_lexer_from_file',
|
||||
] + list(LEXERS) + list(COMPAT)
|
||||
|
||||
_lexer_cache = {}
|
||||
_pattern_cache = {}
|
||||
|
|
@ -118,7 +122,7 @@ def get_lexer_by_name(_alias, **options):
|
|||
raise ClassNotFound('no lexer for alias %r found' % _alias)
|
||||
|
||||
|
||||
def load_lexer_from_file(filename, lexername="CustomLexer", **options):
|
||||
def load_lexer_from_file(filename, lexername='CustomLexer', **options):
|
||||
"""Load a lexer from a file.
|
||||
|
||||
This method expects a file located relative to the current working
|
||||
|
|
@ -140,13 +144,15 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options):
|
|||
exec(f.read(), custom_namespace)
|
||||
# Retrieve the class `lexername` from that namespace
|
||||
if lexername not in custom_namespace:
|
||||
raise ClassNotFound('no valid %s class found in %s' %
|
||||
(lexername, filename))
|
||||
raise ClassNotFound(
|
||||
'no valid %s class found in %s' %
|
||||
(lexername, filename),
|
||||
)
|
||||
lexer_class = custom_namespace[lexername]
|
||||
# And finally instantiate it with the options
|
||||
return lexer_class(**options)
|
||||
except OSError as err:
|
||||
raise ClassNotFound('cannot read %s: %s' % (filename, err))
|
||||
raise ClassNotFound('cannot read {}: {}'.format(filename, err))
|
||||
except ClassNotFound:
|
||||
raise
|
||||
except Exception as err:
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
:copyright: Copyright 2006-2014, 2016 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
LEXERS = {
|
||||
'ABAPLexer': ('pip._vendor.pygments.lexers.business', 'ABAP', ('abap',), ('*.abap', '*.ABAP'), ('text/x-abap',)),
|
||||
|
|
@ -542,19 +543,25 @@ if __name__ == '__main__': # pragma: no cover
|
|||
for root, dirs, files in os.walk('.'):
|
||||
for filename in files:
|
||||
if filename.endswith('.py') and not filename.startswith('_'):
|
||||
module_name = 'pygments.lexers%s.%s' % (
|
||||
root[1:].replace('/', '.'), filename[:-3])
|
||||
module_name = 'pygments.lexers{}.{}'.format(
|
||||
root[1:].replace('/', '.'), filename[:-3],
|
||||
)
|
||||
print(module_name)
|
||||
module = __import__(module_name, None, None, [''])
|
||||
for lexer_name in module.__all__:
|
||||
lexer = getattr(module, lexer_name)
|
||||
found_lexers.append(
|
||||
'%r: %r' % (lexer_name,
|
||||
(module_name,
|
||||
lexer.name,
|
||||
tuple(lexer.aliases),
|
||||
tuple(lexer.filenames),
|
||||
tuple(lexer.mimetypes))))
|
||||
'{!r}: {!r}'.format(
|
||||
lexer_name,
|
||||
(
|
||||
module_name,
|
||||
lexer.name,
|
||||
tuple(lexer.aliases),
|
||||
tuple(lexer.filenames),
|
||||
tuple(lexer.mimetypes),
|
||||
),
|
||||
),
|
||||
)
|
||||
# sort them to make the diff minimal
|
||||
found_lexers.sort()
|
||||
|
||||
|
|
@ -567,7 +574,7 @@ if __name__ == '__main__': # pragma: no cover
|
|||
# repository, for example by using some kind of automatic
|
||||
# management EOL, like `EolExtension
|
||||
# <https://www.mercurial-scm.org/wiki/EolExtension>`.
|
||||
content = content.replace("\r\n", "\n")
|
||||
content = content.replace('\r\n', '\n')
|
||||
header = content[:content.find('LEXERS = {')]
|
||||
footer = content[content.find("if __name__ == '__main__':"):]
|
||||
|
||||
|
|
@ -577,4 +584,4 @@ if __name__ == '__main__': # pragma: no cover
|
|||
fp.write('LEXERS = {\n %s,\n}\n\n' % ',\n '.join(found_lexers))
|
||||
fp.write(footer)
|
||||
|
||||
print ('=== %d lexers processed.' % len(found_lexers))
|
||||
print('=== %d lexers processed.' % len(found_lexers))
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -7,16 +7,19 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
__all__ = ['get_filetype_from_buffer']
|
||||
|
||||
|
||||
modeline_re = re.compile(r'''
|
||||
modeline_re = re.compile(
|
||||
r'''
|
||||
(?: vi | vim | ex ) (?: [<=>]? \d* )? :
|
||||
.* (?: ft | filetype | syn | syntax ) = ( [^:\s]+ )
|
||||
''', re.VERBOSE)
|
||||
''', re.VERBOSE,
|
||||
)
|
||||
|
||||
|
||||
def get_filetype_from_line(l):
|
||||
|
|
@ -30,7 +33,7 @@ def get_filetype_from_buffer(buf, max_lines=5):
|
|||
Scan the buffer for modelines and return filetype if one is found.
|
||||
"""
|
||||
lines = buf.splitlines()
|
||||
for l in lines[-1:-max_lines-1:-1]:
|
||||
for l in lines[-1:-max_lines - 1:-1]:
|
||||
ret = get_filetype_from_line(l)
|
||||
if ret:
|
||||
return ret
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
LEXER_ENTRY_POINT = 'pygments.lexers'
|
||||
FORMATTER_ENTRY_POINT = 'pygments.formatters'
|
||||
STYLE_ENTRY_POINT = 'pygments.styles'
|
||||
|
|
|
|||
|
|
@ -8,12 +8,13 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from re import escape
|
||||
from os.path import commonprefix
|
||||
from itertools import groupby
|
||||
from operator import itemgetter
|
||||
from os.path import commonprefix
|
||||
from re import escape
|
||||
|
||||
CS_ESCAPE = re.compile(r'[\[\^\\\-\]]')
|
||||
FIRST_ELEMENT = itemgetter(0)
|
||||
|
|
@ -74,8 +75,10 @@ def regex_opt_inner(strings, open_paren):
|
|||
# recurse on common 1-string prefixes
|
||||
# print '-> last resort'
|
||||
return open_paren + \
|
||||
'|'.join(regex_opt_inner(list(group[1]), '')
|
||||
for group in groupby(strings, lambda s: s[0] == first[0])) \
|
||||
'|'.join(
|
||||
regex_opt_inner(list(group[1]), '')
|
||||
for group in groupby(strings, lambda s: s[0] == first[0])
|
||||
) \
|
||||
+ close_paren
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
|
|
@ -100,5 +102,5 @@ class Scanner:
|
|||
return '<%s %d/%d>' % (
|
||||
self.__class__.__name__,
|
||||
self.pos,
|
||||
self.data_length
|
||||
self.data_length,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -8,12 +8,13 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
from docutils import nodes
|
||||
from docutils.statemachine import ViewList
|
||||
from docutils.parsers.rst import Directive
|
||||
from docutils.statemachine import ViewList
|
||||
from sphinx.util.nodes import nested_parse_with_titles
|
||||
|
||||
|
||||
|
|
@ -94,7 +95,7 @@ class PygmentsDoc(Directive):
|
|||
self.filenames.add(mod.__file__)
|
||||
cls = getattr(mod, classname)
|
||||
if not cls.__doc__:
|
||||
print("Warning: %s does not have a docstring." % classname)
|
||||
print('Warning: %s does not have a docstring.' % classname)
|
||||
docstring = cls.__doc__
|
||||
if isinstance(docstring, bytes):
|
||||
docstring = docstring.decode('utf8')
|
||||
|
|
@ -103,7 +104,8 @@ class PygmentsDoc(Directive):
|
|||
', '.join(data[2]) or 'None',
|
||||
', '.join(data[3]).replace('*', '\\*').replace('_', '\\') or 'None',
|
||||
', '.join(data[4]) or 'None',
|
||||
docstring))
|
||||
docstring,
|
||||
))
|
||||
if module not in moduledocstrings:
|
||||
moddoc = mod.__doc__
|
||||
if isinstance(moddoc, bytes):
|
||||
|
|
@ -112,9 +114,9 @@ class PygmentsDoc(Directive):
|
|||
|
||||
for module, lexers in sorted(modules.items(), key=lambda x: x[0]):
|
||||
if moduledocstrings[module] is None:
|
||||
raise Exception("Missing docstring for %s" % (module,))
|
||||
raise Exception('Missing docstring for {}'.format(module))
|
||||
heading = moduledocstrings[module].splitlines()[4].strip().rstrip('.')
|
||||
out.append(MODULEDOC % (module, heading, '-'*len(heading)))
|
||||
out.append(MODULEDOC % (module, heading, '-' * len(heading)))
|
||||
for data in lexers:
|
||||
out.append(LEXERDOC % data)
|
||||
|
||||
|
|
@ -133,9 +135,13 @@ class PygmentsDoc(Directive):
|
|||
if isinstance(docstring, bytes):
|
||||
docstring = docstring.decode('utf8')
|
||||
heading = cls.__name__
|
||||
out.append(FMTERDOC % (heading, ', '.join(data[2]) or 'None',
|
||||
', '.join(data[3]).replace('*', '\\*') or 'None',
|
||||
docstring))
|
||||
out.append(
|
||||
FMTERDOC % (
|
||||
heading, ', '.join(data[2]) or 'None',
|
||||
', '.join(data[3]).replace('*', '\\*') or 'None',
|
||||
docstring,
|
||||
),
|
||||
)
|
||||
return ''.join(out)
|
||||
|
||||
def document_filters(self):
|
||||
|
|
|
|||
|
|
@ -7,8 +7,10 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.token import Token, STANDARD_TYPES
|
||||
from pip._vendor.pygments.token import STANDARD_TYPES
|
||||
from pip._vendor.pygments.token import Token
|
||||
|
||||
# Default mapping of ansixxx to RGB colors.
|
||||
_ansimap = {
|
||||
|
|
@ -76,7 +78,7 @@ class StyleMeta(type):
|
|||
return ''
|
||||
elif text.startswith('var') or text.startswith('calc'):
|
||||
return text
|
||||
assert False, "wrong color format %r" % text
|
||||
assert False, 'wrong color format %r' % text
|
||||
|
||||
_styles = obj._styles = {}
|
||||
|
||||
|
|
@ -140,17 +142,17 @@ class StyleMeta(type):
|
|||
bgcolor = _ansimap[bgcolor]
|
||||
|
||||
return {
|
||||
'color': color or None,
|
||||
'bold': bool(t[1]),
|
||||
'italic': bool(t[2]),
|
||||
'underline': bool(t[3]),
|
||||
'bgcolor': bgcolor or None,
|
||||
'border': t[5] or None,
|
||||
'roman': bool(t[6]) or None,
|
||||
'sans': bool(t[7]) or None,
|
||||
'mono': bool(t[8]) or None,
|
||||
'ansicolor': ansicolor,
|
||||
'bgansicolor': bgansicolor,
|
||||
'color': color or None,
|
||||
'bold': bool(t[1]),
|
||||
'italic': bool(t[2]),
|
||||
'underline': bool(t[3]),
|
||||
'bgcolor': bgcolor or None,
|
||||
'border': t[5] or None,
|
||||
'roman': bool(t[6]) or None,
|
||||
'sans': bool(t[7]) or None,
|
||||
'mono': bool(t[8]) or None,
|
||||
'ansicolor': ansicolor,
|
||||
'bgansicolor': bgansicolor,
|
||||
}
|
||||
|
||||
def list_styles(cls):
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pip._vendor.pygments.plugin import find_plugin_styles
|
||||
from pip._vendor.pygments.util import ClassNotFound
|
||||
|
|
@ -14,75 +15,77 @@ from pip._vendor.pygments.util import ClassNotFound
|
|||
|
||||
#: Maps style names to 'submodule::classname'.
|
||||
STYLE_MAP = {
|
||||
'default': 'default::DefaultStyle',
|
||||
'emacs': 'emacs::EmacsStyle',
|
||||
'default': 'default::DefaultStyle',
|
||||
'emacs': 'emacs::EmacsStyle',
|
||||
'friendly': 'friendly::FriendlyStyle',
|
||||
'friendly_grayscale': 'friendly_grayscale::FriendlyGrayscaleStyle',
|
||||
'colorful': 'colorful::ColorfulStyle',
|
||||
'autumn': 'autumn::AutumnStyle',
|
||||
'murphy': 'murphy::MurphyStyle',
|
||||
'manni': 'manni::ManniStyle',
|
||||
'autumn': 'autumn::AutumnStyle',
|
||||
'murphy': 'murphy::MurphyStyle',
|
||||
'manni': 'manni::ManniStyle',
|
||||
'material': 'material::MaterialStyle',
|
||||
'monokai': 'monokai::MonokaiStyle',
|
||||
'perldoc': 'perldoc::PerldocStyle',
|
||||
'pastie': 'pastie::PastieStyle',
|
||||
'borland': 'borland::BorlandStyle',
|
||||
'trac': 'trac::TracStyle',
|
||||
'native': 'native::NativeStyle',
|
||||
'fruity': 'fruity::FruityStyle',
|
||||
'bw': 'bw::BlackWhiteStyle',
|
||||
'vim': 'vim::VimStyle',
|
||||
'vs': 'vs::VisualStudioStyle',
|
||||
'tango': 'tango::TangoStyle',
|
||||
'rrt': 'rrt::RrtStyle',
|
||||
'xcode': 'xcode::XcodeStyle',
|
||||
'igor': 'igor::IgorStyle',
|
||||
'monokai': 'monokai::MonokaiStyle',
|
||||
'perldoc': 'perldoc::PerldocStyle',
|
||||
'pastie': 'pastie::PastieStyle',
|
||||
'borland': 'borland::BorlandStyle',
|
||||
'trac': 'trac::TracStyle',
|
||||
'native': 'native::NativeStyle',
|
||||
'fruity': 'fruity::FruityStyle',
|
||||
'bw': 'bw::BlackWhiteStyle',
|
||||
'vim': 'vim::VimStyle',
|
||||
'vs': 'vs::VisualStudioStyle',
|
||||
'tango': 'tango::TangoStyle',
|
||||
'rrt': 'rrt::RrtStyle',
|
||||
'xcode': 'xcode::XcodeStyle',
|
||||
'igor': 'igor::IgorStyle',
|
||||
'paraiso-light': 'paraiso_light::ParaisoLightStyle',
|
||||
'paraiso-dark': 'paraiso_dark::ParaisoDarkStyle',
|
||||
'lovelace': 'lovelace::LovelaceStyle',
|
||||
'algol': 'algol::AlgolStyle',
|
||||
'algol': 'algol::AlgolStyle',
|
||||
'algol_nu': 'algol_nu::Algol_NuStyle',
|
||||
'arduino': 'arduino::ArduinoStyle',
|
||||
'arduino': 'arduino::ArduinoStyle',
|
||||
'rainbow_dash': 'rainbow_dash::RainbowDashStyle',
|
||||
'abap': 'abap::AbapStyle',
|
||||
'abap': 'abap::AbapStyle',
|
||||
'solarized-dark': 'solarized::SolarizedDarkStyle',
|
||||
'solarized-light': 'solarized::SolarizedLightStyle',
|
||||
'sas': 'sas::SasStyle',
|
||||
'stata': 'stata_light::StataLightStyle',
|
||||
'sas': 'sas::SasStyle',
|
||||
'stata': 'stata_light::StataLightStyle',
|
||||
'stata-light': 'stata_light::StataLightStyle',
|
||||
'stata-dark': 'stata_dark::StataDarkStyle',
|
||||
'inkpot': 'inkpot::InkPotStyle',
|
||||
'stata-dark': 'stata_dark::StataDarkStyle',
|
||||
'inkpot': 'inkpot::InkPotStyle',
|
||||
'zenburn': 'zenburn::ZenburnStyle',
|
||||
'gruvbox-dark': 'gruvbox::GruvboxDarkStyle',
|
||||
'gruvbox-light': 'gruvbox::GruvboxLightStyle',
|
||||
'dracula': 'dracula::DraculaStyle',
|
||||
'one-dark': 'onedark::OneDarkStyle',
|
||||
'lilypond' : 'lilypond::LilyPondStyle',
|
||||
'lilypond': 'lilypond::LilyPondStyle',
|
||||
}
|
||||
|
||||
|
||||
def get_style_by_name(name):
|
||||
if name in STYLE_MAP:
|
||||
mod, cls = STYLE_MAP[name].split('::')
|
||||
builtin = "yes"
|
||||
builtin = 'yes'
|
||||
else:
|
||||
for found_name, style in find_plugin_styles():
|
||||
if name == found_name:
|
||||
return style
|
||||
# perhaps it got dropped into our styles package
|
||||
builtin = ""
|
||||
builtin = ''
|
||||
mod = name
|
||||
cls = name.title() + "Style"
|
||||
cls = name.title() + 'Style'
|
||||
|
||||
try:
|
||||
mod = __import__('pygments.styles.' + mod, None, None, [cls])
|
||||
except ImportError:
|
||||
raise ClassNotFound("Could not find style module %r" % mod +
|
||||
(builtin and ", though it should be builtin") + ".")
|
||||
raise ClassNotFound(
|
||||
'Could not find style module %r' % mod +
|
||||
(builtin and ', though it should be builtin') + '.',
|
||||
)
|
||||
try:
|
||||
return getattr(mod, cls)
|
||||
except AttributeError:
|
||||
raise ClassNotFound("Could not find style class %r in style module." % cls)
|
||||
raise ClassNotFound('Could not find style class %r in style module.' % cls)
|
||||
|
||||
|
||||
def get_all_styles():
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class _TokenType(tuple):
|
||||
|
|
@ -121,92 +122,92 @@ def string_to_tokentype(s):
|
|||
# If you add a new item, please be sure to run this file to perform
|
||||
# a consistency check for duplicate values.
|
||||
STANDARD_TYPES = {
|
||||
Token: '',
|
||||
Token: '',
|
||||
|
||||
Text: '',
|
||||
Whitespace: 'w',
|
||||
Escape: 'esc',
|
||||
Error: 'err',
|
||||
Other: 'x',
|
||||
Text: '',
|
||||
Whitespace: 'w',
|
||||
Escape: 'esc',
|
||||
Error: 'err',
|
||||
Other: 'x',
|
||||
|
||||
Keyword: 'k',
|
||||
Keyword.Constant: 'kc',
|
||||
Keyword.Declaration: 'kd',
|
||||
Keyword.Namespace: 'kn',
|
||||
Keyword.Pseudo: 'kp',
|
||||
Keyword.Reserved: 'kr',
|
||||
Keyword.Type: 'kt',
|
||||
Keyword: 'k',
|
||||
Keyword.Constant: 'kc',
|
||||
Keyword.Declaration: 'kd',
|
||||
Keyword.Namespace: 'kn',
|
||||
Keyword.Pseudo: 'kp',
|
||||
Keyword.Reserved: 'kr',
|
||||
Keyword.Type: 'kt',
|
||||
|
||||
Name: 'n',
|
||||
Name.Attribute: 'na',
|
||||
Name.Builtin: 'nb',
|
||||
Name.Builtin.Pseudo: 'bp',
|
||||
Name.Class: 'nc',
|
||||
Name.Constant: 'no',
|
||||
Name.Decorator: 'nd',
|
||||
Name.Entity: 'ni',
|
||||
Name.Exception: 'ne',
|
||||
Name.Function: 'nf',
|
||||
Name.Function.Magic: 'fm',
|
||||
Name.Property: 'py',
|
||||
Name.Label: 'nl',
|
||||
Name.Namespace: 'nn',
|
||||
Name.Other: 'nx',
|
||||
Name.Tag: 'nt',
|
||||
Name.Variable: 'nv',
|
||||
Name.Variable.Class: 'vc',
|
||||
Name.Variable.Global: 'vg',
|
||||
Name.Variable.Instance: 'vi',
|
||||
Name.Variable.Magic: 'vm',
|
||||
Name: 'n',
|
||||
Name.Attribute: 'na',
|
||||
Name.Builtin: 'nb',
|
||||
Name.Builtin.Pseudo: 'bp',
|
||||
Name.Class: 'nc',
|
||||
Name.Constant: 'no',
|
||||
Name.Decorator: 'nd',
|
||||
Name.Entity: 'ni',
|
||||
Name.Exception: 'ne',
|
||||
Name.Function: 'nf',
|
||||
Name.Function.Magic: 'fm',
|
||||
Name.Property: 'py',
|
||||
Name.Label: 'nl',
|
||||
Name.Namespace: 'nn',
|
||||
Name.Other: 'nx',
|
||||
Name.Tag: 'nt',
|
||||
Name.Variable: 'nv',
|
||||
Name.Variable.Class: 'vc',
|
||||
Name.Variable.Global: 'vg',
|
||||
Name.Variable.Instance: 'vi',
|
||||
Name.Variable.Magic: 'vm',
|
||||
|
||||
Literal: 'l',
|
||||
Literal.Date: 'ld',
|
||||
Literal: 'l',
|
||||
Literal.Date: 'ld',
|
||||
|
||||
String: 's',
|
||||
String.Affix: 'sa',
|
||||
String.Backtick: 'sb',
|
||||
String.Char: 'sc',
|
||||
String.Delimiter: 'dl',
|
||||
String.Doc: 'sd',
|
||||
String.Double: 's2',
|
||||
String.Escape: 'se',
|
||||
String.Heredoc: 'sh',
|
||||
String.Interpol: 'si',
|
||||
String.Other: 'sx',
|
||||
String.Regex: 'sr',
|
||||
String.Single: 's1',
|
||||
String.Symbol: 'ss',
|
||||
String: 's',
|
||||
String.Affix: 'sa',
|
||||
String.Backtick: 'sb',
|
||||
String.Char: 'sc',
|
||||
String.Delimiter: 'dl',
|
||||
String.Doc: 'sd',
|
||||
String.Double: 's2',
|
||||
String.Escape: 'se',
|
||||
String.Heredoc: 'sh',
|
||||
String.Interpol: 'si',
|
||||
String.Other: 'sx',
|
||||
String.Regex: 'sr',
|
||||
String.Single: 's1',
|
||||
String.Symbol: 'ss',
|
||||
|
||||
Number: 'm',
|
||||
Number.Bin: 'mb',
|
||||
Number.Float: 'mf',
|
||||
Number.Hex: 'mh',
|
||||
Number.Integer: 'mi',
|
||||
Number.Integer.Long: 'il',
|
||||
Number.Oct: 'mo',
|
||||
Number: 'm',
|
||||
Number.Bin: 'mb',
|
||||
Number.Float: 'mf',
|
||||
Number.Hex: 'mh',
|
||||
Number.Integer: 'mi',
|
||||
Number.Integer.Long: 'il',
|
||||
Number.Oct: 'mo',
|
||||
|
||||
Operator: 'o',
|
||||
Operator.Word: 'ow',
|
||||
Operator: 'o',
|
||||
Operator.Word: 'ow',
|
||||
|
||||
Punctuation: 'p',
|
||||
Punctuation: 'p',
|
||||
|
||||
Comment: 'c',
|
||||
Comment.Hashbang: 'ch',
|
||||
Comment.Multiline: 'cm',
|
||||
Comment.Preproc: 'cp',
|
||||
Comment.PreprocFile: 'cpf',
|
||||
Comment.Single: 'c1',
|
||||
Comment.Special: 'cs',
|
||||
Comment: 'c',
|
||||
Comment.Hashbang: 'ch',
|
||||
Comment.Multiline: 'cm',
|
||||
Comment.Preproc: 'cp',
|
||||
Comment.PreprocFile: 'cpf',
|
||||
Comment.Single: 'c1',
|
||||
Comment.Special: 'cs',
|
||||
|
||||
Generic: 'g',
|
||||
Generic.Deleted: 'gd',
|
||||
Generic.Emph: 'ge',
|
||||
Generic.Error: 'gr',
|
||||
Generic.Heading: 'gh',
|
||||
Generic.Inserted: 'gi',
|
||||
Generic.Output: 'go',
|
||||
Generic.Prompt: 'gp',
|
||||
Generic.Strong: 'gs',
|
||||
Generic.Subheading: 'gu',
|
||||
Generic.Traceback: 'gt',
|
||||
Generic: 'g',
|
||||
Generic.Deleted: 'gd',
|
||||
Generic.Emph: 'ge',
|
||||
Generic.Error: 'gr',
|
||||
Generic.Heading: 'gh',
|
||||
Generic.Inserted: 'gi',
|
||||
Generic.Output: 'go',
|
||||
Generic.Prompt: 'gp',
|
||||
Generic.Strong: 'gs',
|
||||
Generic.Subheading: 'gu',
|
||||
Generic.Traceback: 'gt',
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
Cc = '\x00-\x1f\x7f-\x9f'
|
||||
|
||||
|
|
@ -79,6 +80,7 @@ cats = ['Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc', 'Me',
|
|||
|
||||
# Generated from unidata 11.0.0
|
||||
|
||||
|
||||
def combine(*args):
|
||||
return ''.join(globals()[cat] for cat in args)
|
||||
|
||||
|
|
@ -94,7 +96,7 @@ def _handle_runs(char_list): # pragma: no cover
|
|||
buf = []
|
||||
for c in char_list:
|
||||
if len(c) == 1:
|
||||
if buf and buf[-1][1] == chr(ord(c)-1):
|
||||
if buf and buf[-1][1] == chr(ord(c) - 1):
|
||||
buf[-1] = (buf[-1][0], c)
|
||||
else:
|
||||
buf.append((c, c))
|
||||
|
|
@ -104,7 +106,7 @@ def _handle_runs(char_list): # pragma: no cover
|
|||
if a == b:
|
||||
yield a
|
||||
else:
|
||||
yield '%s-%s' % (a, b)
|
||||
yield '{}-{}'.format(a, b)
|
||||
|
||||
|
||||
if __name__ == '__main__': # pragma: no cover
|
||||
|
|
@ -116,7 +118,7 @@ if __name__ == '__main__': # pragma: no cover
|
|||
content = fp.read()
|
||||
|
||||
header = content[:content.find('Cc =')]
|
||||
footer = content[content.find("def combine("):]
|
||||
footer = content[content.find('def combine('):]
|
||||
|
||||
for code in range(0x110000):
|
||||
c = chr(code)
|
||||
|
|
@ -141,13 +143,13 @@ if __name__ == '__main__': # pragma: no cover
|
|||
|
||||
for cat in sorted(categories):
|
||||
val = ''.join(_handle_runs(categories[cat]))
|
||||
fp.write('%s = %a\n\n' % (cat, val))
|
||||
fp.write('{} = {!a}\n\n'.format(cat, val))
|
||||
|
||||
cats = sorted(categories)
|
||||
cats.remove('xid_start')
|
||||
cats.remove('xid_continue')
|
||||
fp.write('cats = %r\n\n' % cats)
|
||||
|
||||
fp.write('# Generated from unidata %s\n\n' % (unicodedata.unidata_version,))
|
||||
fp.write('# Generated from unidata {}\n\n'.format(unicodedata.unidata_version))
|
||||
|
||||
fp.write(footer)
|
||||
|
|
|
|||
|
|
@ -7,13 +7,15 @@
|
|||
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from io import TextIOWrapper
|
||||
|
||||
|
||||
split_path_re = re.compile(r'[/\\ ]')
|
||||
doctype_lookup_re = re.compile(r'''
|
||||
doctype_lookup_re = re.compile(
|
||||
r'''
|
||||
<!DOCTYPE\s+(
|
||||
[a-zA-Z_][a-zA-Z0-9]*
|
||||
(?: \s+ # optional in HTML5
|
||||
|
|
@ -21,9 +23,12 @@ doctype_lookup_re = re.compile(r'''
|
|||
"[^"]*")?
|
||||
)
|
||||
[^>]*>
|
||||
''', re.DOTALL | re.MULTILINE | re.VERBOSE)
|
||||
tag_re = re.compile(r'<(.+?)(\s.*?)?>.*?</.+?>',
|
||||
re.UNICODE | re.IGNORECASE | re.DOTALL | re.MULTILINE)
|
||||
''', re.DOTALL | re.MULTILINE | re.VERBOSE,
|
||||
)
|
||||
tag_re = re.compile(
|
||||
r'<(.+?)(\s.*?)?>.*?</.+?>',
|
||||
re.UNICODE | re.IGNORECASE | re.DOTALL | re.MULTILINE,
|
||||
)
|
||||
xml_decl_re = re.compile(r'\s*<\?xml[^>]*\?>', re.I)
|
||||
|
||||
|
||||
|
|
@ -40,8 +45,10 @@ def get_choice_opt(options, optname, allowed, default=None, normcase=False):
|
|||
if normcase:
|
||||
string = string.lower()
|
||||
if string not in allowed:
|
||||
raise OptionError('Value for option %s must be one of %s' %
|
||||
(optname, ', '.join(map(str, allowed))))
|
||||
raise OptionError(
|
||||
'Value for option %s must be one of %s' %
|
||||
(optname, ', '.join(map(str, allowed))),
|
||||
)
|
||||
return string
|
||||
|
||||
|
||||
|
|
@ -52,17 +59,23 @@ def get_bool_opt(options, optname, default=None):
|
|||
elif isinstance(string, int):
|
||||
return bool(string)
|
||||
elif not isinstance(string, str):
|
||||
raise OptionError('Invalid type %r for option %s; use '
|
||||
'1/0, yes/no, true/false, on/off' % (
|
||||
string, optname))
|
||||
raise OptionError(
|
||||
'Invalid type %r for option %s; use '
|
||||
'1/0, yes/no, true/false, on/off' % (
|
||||
string, optname,
|
||||
),
|
||||
)
|
||||
elif string.lower() in ('1', 'yes', 'true', 'on'):
|
||||
return True
|
||||
elif string.lower() in ('0', 'no', 'false', 'off'):
|
||||
return False
|
||||
else:
|
||||
raise OptionError('Invalid value %r for option %s; use '
|
||||
'1/0, yes/no, true/false, on/off' % (
|
||||
string, optname))
|
||||
raise OptionError(
|
||||
'Invalid value %r for option %s; use '
|
||||
'1/0, yes/no, true/false, on/off' % (
|
||||
string, optname,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def get_int_opt(options, optname, default=None):
|
||||
|
|
@ -70,13 +83,19 @@ def get_int_opt(options, optname, default=None):
|
|||
try:
|
||||
return int(string)
|
||||
except TypeError:
|
||||
raise OptionError('Invalid type %r for option %s; you '
|
||||
'must give an integer value' % (
|
||||
string, optname))
|
||||
raise OptionError(
|
||||
'Invalid type %r for option %s; you '
|
||||
'must give an integer value' % (
|
||||
string, optname,
|
||||
),
|
||||
)
|
||||
except ValueError:
|
||||
raise OptionError('Invalid value %r for option %s; you '
|
||||
'must give an integer value' % (
|
||||
string, optname))
|
||||
raise OptionError(
|
||||
'Invalid value %r for option %s; you '
|
||||
'must give an integer value' % (
|
||||
string, optname,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def get_list_opt(options, optname, default=None):
|
||||
|
|
@ -86,9 +105,12 @@ def get_list_opt(options, optname, default=None):
|
|||
elif isinstance(val, (list, tuple)):
|
||||
return list(val)
|
||||
else:
|
||||
raise OptionError('Invalid type %r for option %s; you '
|
||||
'must give a list value' % (
|
||||
val, optname))
|
||||
raise OptionError(
|
||||
'Invalid type %r for option %s; you '
|
||||
'must give a list value' % (
|
||||
val, optname,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def docstring_headline(obj):
|
||||
|
|
@ -97,7 +119,7 @@ def docstring_headline(obj):
|
|||
res = []
|
||||
for line in obj.__doc__.strip().splitlines():
|
||||
if line.strip():
|
||||
res.append(" " + line.strip())
|
||||
res.append(' ' + line.strip())
|
||||
else:
|
||||
break
|
||||
return ''.join(res).lstrip()
|
||||
|
|
@ -155,8 +177,10 @@ def shebang_matches(text, regex):
|
|||
first_line = text.lower()
|
||||
if first_line.startswith('#!'):
|
||||
try:
|
||||
found = [x for x in split_path_re.split(first_line[2:].strip())
|
||||
if x and not x.startswith('-')][-1]
|
||||
found = [
|
||||
x for x in split_path_re.split(first_line[2:].strip())
|
||||
if x and not x.startswith('-')
|
||||
][-1]
|
||||
except IndexError:
|
||||
return False
|
||||
regex = re.compile(r'^%s(\.(exe|cmd|bat|bin))?$' % regex, re.IGNORECASE)
|
||||
|
|
@ -252,6 +276,7 @@ class Future:
|
|||
Handled specially in RegexLexerMeta, to support regex string construction at
|
||||
first use.
|
||||
"""
|
||||
|
||||
def get(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue