Lexer for ptpython input. If the input starts with an exclamation mark, use a Bash lexer, otherwise, use a Python 3 lexer.
| 12 | |
| 13 | |
| 14 | class PtpythonLexer(Lexer): |
| 15 | """ |
| 16 | Lexer for ptpython input. |
| 17 | |
| 18 | If the input starts with an exclamation mark, use a Bash lexer, otherwise, |
| 19 | use a Python 3 lexer. |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, python_lexer: Lexer | None = None) -> None: |
| 23 | self.python_lexer = python_lexer or PygmentsLexer(PythonLexer) |
| 24 | self.system_lexer = PygmentsLexer(BashLexer) |
| 25 | |
| 26 | def lex_document(self, document: Document) -> Callable[[int], StyleAndTextTuples]: |
| 27 | if document.text.startswith("!"): |
| 28 | return self.system_lexer.lex_document(document) |
| 29 | |
| 30 | return self.python_lexer.lex_document(document) |