本文整理汇总了Python中pygments.lexers.TextLexer.add_filter方法的典型用法代码示例。如果您正苦于以下问题:Python TextLexer.add_filter方法的具体用法?Python TextLexer.add_filter怎么用?Python TextLexer.add_filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers.TextLexer
的用法示例。
在下文中一共展示了TextLexer.add_filter方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_lines
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
def print_lines(console_printer,
file_dict,
sourcerange):
"""
Prints the lines between the current and the result line. If needed
they will be shortened.
:param console_printer: Object to print messages on the console.
:param file_dict: A dictionary containing all files as values with
filenames as key.
:param sourcerange: The SourceRange object referring to the related
lines to print.
"""
no_color = not console_printer.print_colored
for i in range(sourcerange.start.line, sourcerange.end.line + 1):
# Print affected file's line number in the sidebar.
console_printer.print(format_lines(lines='', line_nr=i, symbol='['),
color=FILE_LINES_COLOR,
end='')
line = file_dict[sourcerange.file][i - 1].rstrip('\n')
try:
lexer = get_lexer_for_filename(sourcerange.file)
except ClassNotFound:
lexer = TextLexer()
lexer.add_filter(VisibleWhitespaceFilter(
spaces=True, tabs=True,
tabsize=SpacingHelper.DEFAULT_TAB_WIDTH))
# highlight() combines lexer and formatter to output a ``str``
# object.
printed_chars = 0
if i == sourcerange.start.line and sourcerange.start.column:
console_printer.print(highlight_text(
no_color, line[:sourcerange.start.column - 1],
BackgroundMessageStyle, lexer), end='')
printed_chars = sourcerange.start.column - 1
if i == sourcerange.end.line and sourcerange.end.column:
console_printer.print(highlight_text(
no_color, line[printed_chars:sourcerange.end.column - 1],
BackgroundSourceRangeStyle, lexer), end='')
console_printer.print(highlight_text(
no_color, line[sourcerange.end.column - 1:],
BackgroundSourceRangeStyle, lexer), end='')
console_printer.print('')
else:
console_printer.print(highlight_text(
no_color, line[printed_chars:], BackgroundMessageStyle, lexer),
end='')
console_printer.print('')
示例2: run
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
def run(self):
self.assert_has_content()
try:
lexer = get_lexer_by_name(self.arguments[0])
except ValueError:
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
lexer.add_filter(ctypes_types_highlighter)
lexer.add_filter(custom_highlighters)
# take an arbitrary option if more than one is given
formatter = self.options and \
VARIANTS[self.options.keys()[0]] or \
DEFAULT
print >>open('pygments.css', 'w'), formatter.get_style_defs('.highlight')
parsed = highlight(u'\n'.join(self.content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]
示例3: main
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
#.........这里部分代码省略.........
if outfn:
if not fmter:
try:
fmter = get_formatter_for_filename(outfn, **parsed_opts)
except (OptionError, ClassNotFound) as err:
print("Error:", err, file=sys.stderr)
return 1
try:
outfile = open(outfn, "wb")
except Exception as err:
print("Error: cannot open outfile:", err, file=sys.stderr)
return 1
else:
if not fmter:
fmter = TerminalFormatter(**parsed_opts)
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
outfile = sys.stdout.buffer
else:
outfile = sys.stdout
# determine output encoding if not explicitly selected
if not outencoding:
if outfn:
# output file? use lexer encoding for now (can still be None)
fmter.encoding = inencoding
else:
# else use terminal encoding
fmter.encoding = terminal_encoding(sys.stdout)
# provide coloring under Windows, if possible
if not outfn and sys.platform in ("win32", "cygwin") and fmter.name in ("Terminal", "Terminal256"):
# unfortunately colorama doesn't support binary streams on Py3
if sys.version_info > (3,):
import io
outfile = io.TextIOWrapper(outfile, encoding=fmter.encoding)
fmter.encoding = None
try:
import colorama.initialise
except ImportError:
pass
else:
outfile = colorama.initialise.wrap_stream(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
# before running the chosen language lexer.
escapeinside = parsed_opts.get("escapeinside", "")
if len(escapeinside) == 2 and isinstance(fmter, LatexFormatter):
left = escapeinside[0]
right = escapeinside[1]
lexer = LatexEmbeddedLexer(left, right, lexer)
# ... and do it!
try:
# process filters
for fname, fopts in F_opts:
lexer.add_filter(fname, **fopts)
if "-s" not in opts:
# process whole input as per normal...
highlight(code, lexer, fmter, outfile)
else:
if not lexer:
print("Error: when using -s a lexer has to be selected with -l", file=sys.stderr)
return 1
# line by line processing of stdin (eg: for 'tail -f')...
try:
while 1:
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
line = sys.stdin.buffer.readline()
else:
line = sys.stdin.readline()
if not line:
break
if not inencoding:
line = guess_decode_from_terminal(line, sys.stdin)[0]
highlight(line, lexer, fmter, outfile)
if hasattr(outfile, "flush"):
outfile.flush()
except KeyboardInterrupt:
return 0
except Exception:
raise
import traceback
info = traceback.format_exception(*sys.exc_info())
msg = info[-1].strip()
if len(info) >= 3:
# extract relevant file and position info
msg += "\n (f%s)" % info[-2].split("\n")[0].strip()[1:]
print(file=sys.stderr)
print("*** Error while highlighting:", file=sys.stderr)
print(msg, file=sys.stderr)
return 1
return 0
示例4: ConsoleInteractionTest
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
class ConsoleInteractionTest(unittest.TestCase):
def setUp(self):
self.log_printer = ListLogPrinter()
self.console_printer = ConsolePrinter(print_colored=False)
self.no_color = not self.console_printer.print_colored
self.file_diff_dict = {}
self.section = Section('t')
self.local_bears = OrderedDict([('default', [SomelocalBear]),
('test', [SomelocalBear])])
self.global_bears = OrderedDict([('default', [SomeglobalBear]),
('test', [SomeglobalBear])])
self.old_open_editor_applicable = OpenEditorAction.is_applicable
OpenEditorAction.is_applicable = staticmethod(
lambda *args: 'OpenEditorAction cannot be applied')
self.old_apply_patch_applicable = ApplyPatchAction.is_applicable
ApplyPatchAction.is_applicable = staticmethod(
lambda *args: 'ApplyPatchAction cannot be applied')
self.lexer = TextLexer()
self.lexer.add_filter(VisibleWhitespaceFilter(
spaces=True,
tabs=True,
tabsize=SpacingHelper.DEFAULT_TAB_WIDTH))
def tearDown(self):
OpenEditorAction.is_applicable = self.old_open_editor_applicable
ApplyPatchAction.is_applicable = self.old_apply_patch_applicable
def test_require_settings(self):
curr_section = Section('')
self.assertRaises(TypeError, acquire_settings,
self.log_printer, 0, curr_section)
with simulate_console_inputs(0, 1, 2) as generator:
self.assertEqual(acquire_settings(self.log_printer,
{'setting': ['help text',
'SomeBear']},
curr_section),
{'setting': 0})
self.assertEqual(acquire_settings(self.log_printer,
{'setting': ['help text',
'SomeBear',
'AnotherBear']},
curr_section),
{'setting': 1})
self.assertEqual(acquire_settings(self.log_printer,
{'setting': ['help text',
'SomeBear',
'AnotherBear',
'YetAnotherBear']},
curr_section),
{'setting': 2})
self.assertEqual(generator.last_input, 2)
def test_print_diffs_info(self):
file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']}
diff_dict = {'a': Diff(file_dict['a']),
'b': Diff(file_dict['b'])}
diff_dict['a'].add_lines(1, ['test\n'])
diff_dict['a'].delete_line(3)
diff_dict['b'].add_lines(0, ['first\n'])
previous_diffs = {'a': Diff(file_dict['a'])}
previous_diffs['a'].change_line(2, 'b\n', 'b_changed\n')
with retrieve_stdout() as stdout:
print_diffs_info(diff_dict, self.console_printer)
self.assertEqual(stdout.getvalue(),
'| | +1 -1 in a\n'
'| | +1 -0 in b\n')
@patch('coalib.output.ConsoleInteraction.acquire_actions_and_apply')
@patch('coalib.output.ConsoleInteraction.ShowPatchAction.'
'apply_from_section')
def test_print_result_interactive_small_patch(self, apply_from_section, _):
file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']}
diff_dict = {'a': Diff(file_dict['a']),
'b': Diff(file_dict['b'])}
diff_dict['a'].add_lines(1, ['test\n'])
diff_dict['a'].delete_line(3)
result = Result('origin', 'msg', diffs=diff_dict)
section = Section('test')
print_result(self.console_printer,
section,
self.file_diff_dict,
result,
file_dict,
True)
apply_from_section.assert_called_once_with(
result, file_dict, self.file_diff_dict, section)
@patch('coalib.output.ConsoleInteraction.acquire_actions_and_apply')
@patch('coalib.output.ConsoleInteraction.print_diffs_info')
def test_print_result_interactive_big_patch(self, diffs_info, _):
file_dict = {'a': ['a\n', 'b\n', 'c\n'], 'b': ['old_first\n']}
#.........这里部分代码省略.........
示例5: getattr
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
# stdin/stdout encoding otherwise.
# (This is a compromise, I'm not too happy with it...)
if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts:
if outfn:
# encoding pass-through
fmter.encoding = 'latin1'
else:
# use terminal encoding
lexer.encoding = getattr(sys.stdin, 'encoding', None) or 'ascii'
fmter.encoding = getattr(sys.stdout, 'encoding', None) or 'ascii'
# ... and do it!
try:
# process filters
for fname, fopts in F_opts:
lexer.add_filter(fname, **fopts)
highlight(code, lexer, fmter, outfile)
except Exception, err:
import traceback
info = traceback.format_exception(*sys.exc_info())
msg = info[-1].strip()
if len(info) >= 3:
# extract relevant file and position info
msg += '\n (f%s)' % info[-2].split('\n')[0].strip()[1:]
print >>sys.stderr
print >>sys.stderr, '*** Error while highlighting:'
print >>sys.stderr, msg
return 1
return 0
示例6: main
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
#.........这里部分代码省略.........
print('Error:', err, file=sys.stderr)
return 1
try:
outfile = open(outfn, 'wb')
except Exception as err:
print('Error: cannot open outfile:', err, file=sys.stderr)
return 1
else:
if not fmter:
fmter = TerminalFormatter(**parsed_opts)
outfile = sys.stdout
# select lexer
lexer = opts.pop('-l', None)
if lexer:
try:
lexer = get_lexer_by_name(lexer, **parsed_opts)
except (OptionError, ClassNotFound) as err:
print('Error:', err, file=sys.stderr)
return 1
if args:
if len(args) > 1:
print(usage, file=sys.stderr)
return 2
infn = args[0]
try:
code = open(infn, 'rb').read()
except Exception as err:
print('Error: cannot read infile:', err, file=sys.stderr)
return 1
if not lexer:
try:
lexer = get_lexer_for_filename(infn, code, **parsed_opts)
except ClassNotFound as err:
if '-g' in opts:
try:
lexer = guess_lexer(code, **parsed_opts)
except ClassNotFound:
lexer = TextLexer(**parsed_opts)
else:
print('Error:', err, file=sys.stderr)
return 1
except OptionError as err:
print('Error:', err, file=sys.stderr)
return 1
else:
if '-g' in opts:
code = sys.stdin.read()
try:
lexer = guess_lexer(code, **parsed_opts)
except ClassNotFound:
lexer = TextLexer(**parsed_opts)
elif not lexer:
print('Error: no lexer name given and reading ' + \
'from stdin (try using -g or -l <lexer>)', file=sys.stderr)
return 2
else:
code = sys.stdin.read()
# No encoding given? Use latin1 if output file given,
# stdin/stdout encoding otherwise.
# (This is a compromise, I'm not too happy with it...)
if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts:
if outfn:
# encoding pass-through
fmter.encoding = 'latin1'
else:
if sys.version_info < (3,):
# use terminal encoding; Python 3's terminals already do that
lexer.encoding = getattr(sys.stdin, 'encoding',
None) or 'ascii'
fmter.encoding = getattr(sys.stdout, 'encoding',
None) or 'ascii'
elif not outfn and sys.version_info > (3,):
# output to terminal with encoding -> use .buffer
outfile = sys.stdout.buffer
# ... and do it!
try:
# process filters
for fname, fopts in F_opts:
lexer.add_filter(fname, **fopts)
highlight(code, lexer, fmter, outfile)
except Exception:
import traceback
info = traceback.format_exception(*sys.exc_info())
msg = info[-1].strip()
if len(info) >= 3:
# extract relevant file and position info
msg += '\n (f%s)' % info[-2].split('\n')[0].strip()[1:]
print(file=sys.stderr)
print('*** Error while highlighting:', file=sys.stderr)
print(msg, file=sys.stderr)
return 1
return 0
示例7: ConsoleInteractionTest
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
class ConsoleInteractionTest(unittest.TestCase):
def setUp(self):
self.log_printer = LogPrinter(ConsolePrinter(print_colored=False))
self.console_printer = ConsolePrinter(print_colored=False)
self.file_diff_dict = {}
self.section = Section("t")
self.local_bears = OrderedDict([("default", [SomelocalBear]),
("test", [SomelocalBear])])
self.global_bears = OrderedDict([("default", [SomeglobalBear]),
("test", [SomeglobalBear])])
self.old_open_editor_applicable = OpenEditorAction.is_applicable
OpenEditorAction.is_applicable = staticmethod(lambda *args: False)
self.old_apply_patch_applicable = ApplyPatchAction.is_applicable
ApplyPatchAction.is_applicable = staticmethod(lambda *args: False)
self.lexer = TextLexer()
self.lexer.add_filter(VisibleWhitespaceFilter(
spaces="•",
tabs=True,
tabsize=SpacingHelper.DEFAULT_TAB_WIDTH))
def tearDown(self):
OpenEditorAction.is_applicable = self.old_open_editor_applicable
ApplyPatchAction.is_applicable = self.old_apply_patch_applicable
def test_require_settings(self):
self.assertRaises(TypeError, acquire_settings, self.log_printer, 0)
with simulate_console_inputs(0, 1, 2) as generator:
self.assertEqual(acquire_settings(self.log_printer,
{"setting": ["help text",
"SomeBear"]}),
{"setting": 0})
self.assertEqual(acquire_settings(self.log_printer,
{"setting": ["help text",
"SomeBear",
"AnotherBear"]}),
{"setting": 1})
self.assertEqual(acquire_settings(self.log_printer,
{"setting": ["help text",
"SomeBear",
"AnotherBear",
"YetAnotherBear"]}),
{"setting": 2})
self.assertEqual(generator.last_input, 2)
def test_print_diffs_info(self):
file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]}
diff_dict = {"a": Diff(file_dict['a']),
"b": Diff(file_dict['b'])}
diff_dict["a"].add_lines(1, ["test\n"])
diff_dict["a"].delete_line(3)
diff_dict["b"].add_lines(0, ["first\n"])
previous_diffs = {"a": Diff(file_dict['a'])}
previous_diffs["a"].change_line(2, "b\n", "b_changed\n")
with retrieve_stdout() as stdout:
print_diffs_info(diff_dict, self.console_printer)
self.assertEqual(stdout.getvalue(),
"| | +1 -1 in a\n"
"| | +1 -0 in b\n")
@patch("coalib.output.ConsoleInteraction.acquire_actions_and_apply")
@patch("coalib.output.ConsoleInteraction.ShowPatchAction."
"apply_from_section")
def test_print_result_interactive_small_patch(self, apply_from_section, _):
file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]}
diff_dict = {"a": Diff(file_dict['a']),
"b": Diff(file_dict['b'])}
diff_dict["a"].add_lines(1, ["test\n"])
diff_dict["a"].delete_line(3)
result = Result("origin", "msg", diffs=diff_dict)
section = Section("test")
print_result(self.console_printer,
self.log_printer,
section,
self.file_diff_dict,
result,
file_dict,
True)
apply_from_section.assert_called_once_with(
result, file_dict, self.file_diff_dict, section)
@patch("coalib.output.ConsoleInteraction.acquire_actions_and_apply")
@patch("coalib.output.ConsoleInteraction.print_diffs_info")
def test_print_result_interactive_big_patch(self, diffs_info, _):
file_dict = {"a": ["a\n", "b\n", "c\n"], "b": ["old_first\n"]}
diff_dict = {"a": Diff(file_dict['a']),
"b": Diff(file_dict['b'])}
diff_dict["a"].add_lines(1, ["test\n", "test1\n", "test2\n"])
diff_dict["a"].delete_line(3)
diff_dict["a"].add_lines(3, ["3test\n"])
result = Result("origin", "msg", diffs=diff_dict)
section = Section("test")
#.........这里部分代码省略.........
示例8: main_inner
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
#.........这里部分代码省略.........
else:
print('Error:', err, file=sys.stderr)
return 1
except OptionError as err:
print('Error:', err, file=sys.stderr)
return 1
elif '-s' not in opts: # treat stdin as full file (-s support is later)
# read code from terminal, always in binary mode since we want to
# decode ourselves and be tolerant with it
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
code = sys.stdin.buffer.read()
else:
code = sys.stdin.read()
if not inencoding:
code, inencoding = guess_decode_from_terminal(code, sys.stdin)
# else the lexer will do the decoding
if not lexer:
try:
lexer = guess_lexer(code, **parsed_opts)
except ClassNotFound:
lexer = TextLexer(**parsed_opts)
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)
return 2
# process filters
for fname, fopts in F_opts:
try:
lexer.add_filter(fname, **fopts)
except ClassNotFound as err:
print('Error:', err, file=sys.stderr)
return 1
# select formatter
outfn = opts.pop('-o', None)
fmter = opts.pop('-f', None)
if fmter:
try:
fmter = get_formatter_by_name(fmter, **parsed_opts)
except (OptionError, ClassNotFound) as err:
print('Error:', err, file=sys.stderr)
return 1
if outfn:
if not fmter:
try:
fmter = get_formatter_for_filename(outfn, **parsed_opts)
except (OptionError, ClassNotFound) as err:
print('Error:', err, file=sys.stderr)
return 1
try:
outfile = open(outfn, 'wb')
except Exception as err:
print('Error: cannot open outfile:', err, file=sys.stderr)
return 1
else:
if not fmter:
fmter = TerminalFormatter(**parsed_opts)
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
outfile = sys.stdout.buffer
示例9: main
# 需要导入模块: from pygments.lexers import TextLexer [as 别名]
# 或者: from pygments.lexers.TextLexer import add_filter [as 别名]
#.........这里部分代码省略.........
print("Error:", err, file=sys.stderr)
return 1
try:
outfile = open(outfn, "wb")
except Exception as err:
print("Error: cannot open outfile:", err, file=sys.stderr)
return 1
else:
if not fmter:
fmter = TerminalFormatter(**parsed_opts)
outfile = sys.stdout
# select lexer
lexer = opts.pop("-l", None)
if lexer:
try:
lexer = get_lexer_by_name(lexer, **parsed_opts)
except (OptionError, ClassNotFound) as err:
print("Error:", err, file=sys.stderr)
return 1
if args:
if len(args) > 1:
print(usage, file=sys.stderr)
return 2
infn = args[0]
try:
code = open(infn, "rb").read()
except Exception as err:
print("Error: cannot read infile:", err, file=sys.stderr)
return 1
if not lexer:
try:
lexer = get_lexer_for_filename(infn, code, **parsed_opts)
except ClassNotFound as err:
if "-g" in opts:
try:
lexer = guess_lexer(code, **parsed_opts)
except ClassNotFound:
lexer = TextLexer(**parsed_opts)
else:
print("Error:", err, file=sys.stderr)
return 1
except OptionError as err:
print("Error:", err, file=sys.stderr)
return 1
else:
if "-g" in opts:
code = sys.stdin.read()
try:
lexer = guess_lexer(code, **parsed_opts)
except ClassNotFound:
lexer = TextLexer(**parsed_opts)
elif not lexer:
print(
"Error: no lexer name given and reading " + "from stdin (try using -g or -l <lexer>)", file=sys.stderr
)
return 2
else:
code = sys.stdin.read()
# No encoding given? Use latin1 if output file given,
# stdin/stdout encoding otherwise.
# (This is a compromise, I'm not too happy with it...)
if "encoding" not in parsed_opts and "outencoding" not in parsed_opts:
if outfn:
# encoding pass-through
fmter.encoding = "latin1"
else:
if sys.version_info < (3,):
# use terminal encoding; Python 3's terminals already do that
lexer.encoding = getattr(sys.stdin, "encoding", None) or "ascii"
fmter.encoding = getattr(sys.stdout, "encoding", None) or "ascii"
elif not outfn and sys.version_info > (3,):
# output to terminal with encoding -> use .buffer
outfile = sys.stdout.buffer
# ... and do it!
try:
# process filters
for fname, fopts in F_opts:
lexer.add_filter(fname, **fopts)
highlight(code, lexer, fmter, outfile)
except Exception as err:
import traceback
info = traceback.format_exception(*sys.exc_info())
msg = info[-1].strip()
if len(info) >= 3:
# extract relevant file and position info
msg += "\n (f%s)" % info[-2].split("\n")[0].strip()[1:]
print(file=sys.stderr)
print("*** Error while highlighting:", file=sys.stderr)
print(msg, file=sys.stderr)
return 1
return 0