forked from pygments/pygments-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_formatter.py
41 lines (36 loc) · 1.39 KB
/
example_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""An example plugin formatter for Pygments."""
from pygments.formatter import Formatter
class ExampleFormatter(Formatter):
# This should be the human-readable name of the format.
name = "Pygments Plugin Example Format"
# This is a list of names that can be used to select the formatter.
# In this example, we can call the pygmentize command as
#
# pygmentize -f example-format
#
# Also, doing
#
# pygments.formatters.find_formatter_class("example-format")
#
# in Python will find the formatter class.
aliases = ["example-format"]
# This is a list of file patterns that the formatter will
# typically be used to produce. In this example, calling
#
# pygmentize -o out.exmplfmt
#
# will automatically select this formatter based on the output
# file name. Similarly,
#
# pygments.formatters.get_formatter_for_filename("out.exmplfmt")
#
# will return an instance of this formatter class.
filenames = ["*.exmplfmt"]
def format_unencoded(self, tokensource, out):
# This formatter writes each token as [<color>]<string> .
for ttype, value in tokensource:
while not self.style.styles_token(ttype):
ttype = ttype.parent
color = self.style.style_for_token(ttype)['color']
out.write("[" + (color or "black") + "]")
out.write(value)