This is 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 formatte
| 91 | |
| 92 | |
| 93 | class BPythonFormatter(Formatter): |
| 94 | """This is the custom formatter for bpython. |
| 95 | Its format() method receives the tokensource |
| 96 | and outfile params passed to it from the |
| 97 | Pygments highlight() method and slops |
| 98 | them into the appropriate format string |
| 99 | as defined above, then writes to the outfile |
| 100 | object the final formatted string. |
| 101 | |
| 102 | See the Pygments source for more info; it's pretty |
| 103 | straightforward.""" |
| 104 | |
| 105 | def __init__( |
| 106 | self, color_scheme: MutableMapping[str, str], **options: Any |
| 107 | ) -> None: |
| 108 | self.f_strings = {} |
| 109 | for k, v in theme_map.items(): |
| 110 | self.f_strings[k] = f"\x01{color_scheme[v]}" |
| 111 | if k is Parenthesis: |
| 112 | # FIXME: Find a way to make this the inverse of the current |
| 113 | # background colour |
| 114 | self.f_strings[k] += "I" |
| 115 | super().__init__(**options) |
| 116 | |
| 117 | def format( |
| 118 | self, |
| 119 | tokensource: Iterable[MutableMapping[_TokenType, str]], |
| 120 | outfile: TextIO, |
| 121 | ) -> None: |
| 122 | o: str = "" |
| 123 | for token, text in tokensource: |
| 124 | if text == "\n": |
| 125 | continue |
| 126 | |
| 127 | while token not in self.f_strings: |
| 128 | if token.parent is None: |
| 129 | break |
| 130 | else: |
| 131 | token = token.parent |
| 132 | o += f"{self.f_strings[token]}\x03{text}\x04" |
| 133 | outfile.write(o.rstrip()) |
| 134 | |
| 135 | |
| 136 | # vim: sw=4 ts=4 sts=4 ai et |
nothing calls this directly
no outgoing calls
no test coverage detected