Debian Codegraph @

docs/utils/pygments_ansi_color.py

raw
1 # -*- coding: utf-8 -*-
2 """Pygments lexer for text containing ANSI color codes."""
3 from __future__ import (
4 absolute_import,
5 unicode_literals,
6 )
7
8 import itertools
9 import re
10
11 import pygments.lexer
12 import pygments.token
13
14 Color = pygments.token.Token.Color
15
16 _ansi_code_to_color = {
17 0: 'Black',
18 1: 'Red',
19 2: 'Green',
20 3: 'Yellow',
21 4: 'Blue',
22 5: 'Magenta',
23 6: 'Cyan',
24 7: 'White',
25 }
26
27
28 def _token_from_lexer_state(bold, fg_color, bg_color):
29 """Construct a token given the current lexer state.
30
31 We can only emit one token even though we have a multiple-tuple state.
32 To do work around this, we construct tokens like "BoldRed".
33 """
34 token_name = ''
35
36 if bold:
37 token_name += 'Bold'
38
39 if fg_color:
40 token_name += fg_color
41
42 if bg_color:
43 token_name += 'BG' + bg_color
44
45 if token_name == '':
46 return pygments.token.Text
47 else:
48 return getattr(Color, token_name)
49
50
51 def color_tokens(fg_colors, bg_colors):
52 """Return color tokens for a given set of colors.
53
54 Pygments doesn't have a generic "color" token; instead everything is
55 contextual (e.g. "comment" or "variable"). That doesn't make sense for us,
56 where the colors actually *are* what we care about.
57
58 This function will register combinations of tokens (things like "Red" or
59 "BoldRedBGGreen") based on the colors passed in.
60
61 You can also define the tokens yourself, but note that the token names are
62 *not* currently guaranteed to be stable between releases as I'm not really
63 happy with this approach.
64
65 Usage:
66
67 fg_colors = bg_colors = {
68 'Black': '#000000',
69 'Red': '#EF2929',
70 'Green': '#8AE234',
71 'Yellow': '#FCE94F',
72 'Blue': '#3465A4',
73 'Magenta': '#c509c5',
74 'Cyan': '#34E2E2',
75 'White': '#ffffff',
76 }
77 class MyStyle(pygments.styles.SomeStyle):
78 styles = dict(pygments.styles.SomeStyle.styles)
79 styles.update(color_tokens(fg_colors, bg_colors))
80 """
81 styles = {}
82
83 for bold, fg_color, bg_color in itertools.product(
84 (False, True),
85 {None} | set(fg_colors),
86 {None} | set(bg_colors),
87 ):
88 token = _token_from_lexer_state(bold, fg_color, bg_color)
89 if token is not pygments.token.Text:
90 value = []
91 if bold:
92 value.append('bold')
93 if fg_color:
94 value.append(fg_colors[fg_color])
95 if bg_color:
96 value.append('bg:' + bg_colors[bg_color])
97 styles[token] = ' '.join(value)
98
99 return styles
100
101
102 class AnsiColorLexer(pygments.lexer.RegexLexer):
103 name = 'ANSI Color'
104 aliases = ('ansi-color', 'ansi', 'ansi-terminal')
105 flags = re.DOTALL | re.MULTILINE
106
107 def __init__(self, *args, **kwargs):
108 super(AnsiColorLexer, self).__init__(*args, **kwargs)
109 self.reset_state()
110
111 def reset_state(self):
112 self.bold = False
113 self.fg_color = None
114 self.bg_color = None
115
116 @property
117 def current_token(self):
118 return _token_from_lexer_state(
119 self.bold, self.fg_color, self.bg_color,
120 )
121
122 def process(self, match):
123 """Produce the next token and bit of text.
124
125 Interprets the ANSI code (which may be a color code or some other
126 code), changing the lexer state and producing a new token. If it's not
127 a color code, we just strip it out and move on.
128
129 Some useful reference for ANSI codes:
130 * http://ascii-table.com/ansi-escape-sequences.php
131 """
132 # "after_escape" contains everything after the start of the escape
133 # sequence, up to the next escape sequence. We still need to separate
134 # the content from the end of the escape sequence.
135 after_escape = match.group(1)
136
137 # TODO: this doesn't handle the case where the values are non-numeric.
138 # This is rare but can happen for keyboard remapping, e.g.
139 # '\x1b[0;59;"A"p'
140 parsed = re.match(
141 r'([0-9;=]*?)?([a-zA-Z])(.*)$',
142 after_escape,
143 re.DOTALL | re.MULTILINE,
144 )
145 if parsed is None:
146 # This shouldn't ever happen if we're given valid text + ANSI, but
147 # people can provide us with utter junk, and we should tolerate it.
148 text = after_escape
149 else:
150 value, code, text = parsed.groups()
151
152 if code == 'm': # "m" is "Set Graphics Mode"
153 # Special case \x1b[m is a reset code
154 if value == '':
155 self.reset_state()
156 else:
157 values = value.split(';')
158 for value in values:
159 try:
160 value = int(value)
161 except ValueError:
162 # Shouldn't ever happen, but could with invalid
163 # ANSI.
164 continue
165 else:
166 fg_color = _ansi_code_to_color.get(value - 30)
167 bg_color = _ansi_code_to_color.get(value - 40)
168 if fg_color:
169 self.fg_color = fg_color
170 elif bg_color:
171 self.bg_color = bg_color
172 elif value == 1:
173 self.bold = True
174 elif value == 22:
175 self.bold = False
176 elif value == 39:
177 self.fg_color = None
178 elif value == 49:
179 self.bg_color = None
180 elif value == 0:
181 self.reset_state()
182
183 yield match.start(), self.current_token, text
184
185 tokens = {
186 # states have to be native strings
187 str('root'): [
188 (r'\x1b\[([^\x1b]*)', process),
189 (r'[^\x1b]+', pygments.token.Text),
190 ],
191 }