In [3]: from pygments.formatters.terminal import TerminalFormatter
In [4]: code = "def _format_output(code, args):\n if not args['color']:\n return code\n lexer = None\n # try to find a lexer using the StackOverflow tags\n # or the query arguments\n for ...: keyword in args['query'].split() + args['tags']:\n try:\n lexer = get_lexer_by_name(keyword)\n break\n except ClassNotFound:\n pass\n\n # no lexer fo ...: und above, use the guesser\n if not lexer:\n try:\n lexer = guess_lexer(code)\n except ClassNotFound:\n return code\n return highlight(code,\n ...: lexer,\n TerminalFormatter(bg='dark'))"
In [5]: print(highlight(code, PythonLexer(), TerminalFormatter())) def_format_output(code, args): ifnot args['color']: return code lexer = None # try to find a lexer using the StackOverflow tags # or the query arguments for keyword in args['query'].split() + args['tags']: try: lexer = get_lexer_by_name(keyword) break except ClassNotFound: pass
# no lexer found above, use the guesser ifnot lexer: try: lexer = guess_lexer(code) except ClassNotFound: return code return highlight(code, lexer, TerminalFormatter(bg='dark'))