This is subclassed from the custom formatter for bpython. Its format() method receives the tokensource and outfile params passed to it from the Pygments highlight() method and slops them into the appropriate format string as defined above, then writes to the outfile object the final
| 36 | |
| 37 | |
| 38 | class BPythonFormatter(Formatter): |
| 39 | """This is subclassed from the custom formatter for bpython. Its format() |
| 40 | method receives the tokensource and outfile params passed to it from the |
| 41 | Pygments highlight() method and slops them into the appropriate format |
| 42 | string as defined above, then writes to the outfile object the final |
| 43 | formatted string. This does not write real strings. It writes format string |
| 44 | (FmtStr) objects. |
| 45 | |
| 46 | See the Pygments source for more info; it's pretty |
| 47 | straightforward.""" |
| 48 | |
| 49 | def __init__( |
| 50 | self, |
| 51 | color_scheme: dict[_TokenType, str], |
| 52 | **options: str | bool | None, |
| 53 | ) -> None: |
| 54 | self.f_strings = {k: f"\x01{v}" for k, v in color_scheme.items()} |
| 55 | # FIXME: mypy currently fails to handle this properly |
| 56 | super().__init__(**options) # type: ignore |
| 57 | |
| 58 | def format(self, tokensource, outfile): |
| 59 | o = "" |
| 60 | |
| 61 | for token, text in tokensource: |
| 62 | while token not in self.f_strings: |
| 63 | token = token.parent |
| 64 | o += f"{self.f_strings[token]}\x03{text}\x04" |
| 65 | outfile.write(parse(o.rstrip())) |
| 66 | |
| 67 | |
| 68 | class Interp(ReplInterpreter): |