]>
Commit | Line | Data |
---|---|---|
e0df8241 JR |
1 | """ |
2 | pygments.style | |
3 | ~~~~~~~~~~~~~~ | |
4 | ||
5 | Basic style object. | |
6 | ||
7 | :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. | |
8 | :license: BSD, see LICENSE for details. | |
9 | """ | |
10 | ||
11 | from pip._vendor.pygments.token import Token, STANDARD_TYPES | |
12 | ||
13 | # Default mapping of ansixxx to RGB colors. | |
14 | _ansimap = { | |
15 | # dark | |
16 | 'ansiblack': '000000', | |
17 | 'ansired': '7f0000', | |
18 | 'ansigreen': '007f00', | |
19 | 'ansiyellow': '7f7fe0', | |
20 | 'ansiblue': '00007f', | |
21 | 'ansimagenta': '7f007f', | |
22 | 'ansicyan': '007f7f', | |
23 | 'ansigray': 'e5e5e5', | |
24 | # normal | |
25 | 'ansibrightblack': '555555', | |
26 | 'ansibrightred': 'ff0000', | |
27 | 'ansibrightgreen': '00ff00', | |
28 | 'ansibrightyellow': 'ffff00', | |
29 | 'ansibrightblue': '0000ff', | |
30 | 'ansibrightmagenta': 'ff00ff', | |
31 | 'ansibrightcyan': '00ffff', | |
32 | 'ansiwhite': 'ffffff', | |
33 | } | |
34 | # mapping of deprecated #ansixxx colors to new color names | |
35 | _deprecated_ansicolors = { | |
36 | # dark | |
37 | '#ansiblack': 'ansiblack', | |
38 | '#ansidarkred': 'ansired', | |
39 | '#ansidarkgreen': 'ansigreen', | |
40 | '#ansibrown': 'ansiyellow', | |
41 | '#ansidarkblue': 'ansiblue', | |
42 | '#ansipurple': 'ansimagenta', | |
43 | '#ansiteal': 'ansicyan', | |
44 | '#ansilightgray': 'ansigray', | |
45 | # normal | |
46 | '#ansidarkgray': 'ansibrightblack', | |
47 | '#ansired': 'ansibrightred', | |
48 | '#ansigreen': 'ansibrightgreen', | |
49 | '#ansiyellow': 'ansibrightyellow', | |
50 | '#ansiblue': 'ansibrightblue', | |
51 | '#ansifuchsia': 'ansibrightmagenta', | |
52 | '#ansiturquoise': 'ansibrightcyan', | |
53 | '#ansiwhite': 'ansiwhite', | |
54 | } | |
55 | ansicolors = set(_ansimap) | |
56 | ||
57 | ||
58 | class StyleMeta(type): | |
59 | ||
60 | def __new__(mcs, name, bases, dct): | |
61 | obj = type.__new__(mcs, name, bases, dct) | |
62 | for token in STANDARD_TYPES: | |
63 | if token not in obj.styles: | |
64 | obj.styles[token] = '' | |
65 | ||
66 | def colorformat(text): | |
67 | if text in ansicolors: | |
68 | return text | |
69 | if text[0:1] == '#': | |
70 | col = text[1:] | |
71 | if len(col) == 6: | |
72 | return col | |
73 | elif len(col) == 3: | |
74 | return col[0] * 2 + col[1] * 2 + col[2] * 2 | |
75 | elif text == '': | |
76 | return '' | |
77 | elif text.startswith('var') or text.startswith('calc'): | |
78 | return text | |
79 | assert False, "wrong color format %r" % text | |
80 | ||
81 | _styles = obj._styles = {} | |
82 | ||
83 | for ttype in obj.styles: | |
84 | for token in ttype.split(): | |
85 | if token in _styles: | |
86 | continue | |
87 | ndef = _styles.get(token.parent, None) | |
88 | styledefs = obj.styles.get(token, '').split() | |
89 | if not ndef or token is None: | |
90 | ndef = ['', 0, 0, 0, '', '', 0, 0, 0] | |
91 | elif 'noinherit' in styledefs and token is not Token: | |
92 | ndef = _styles[Token][:] | |
93 | else: | |
94 | ndef = ndef[:] | |
95 | _styles[token] = ndef | |
96 | for styledef in obj.styles.get(token, '').split(): | |
97 | if styledef == 'noinherit': | |
98 | pass | |
99 | elif styledef == 'bold': | |
100 | ndef[1] = 1 | |
101 | elif styledef == 'nobold': | |
102 | ndef[1] = 0 | |
103 | elif styledef == 'italic': | |
104 | ndef[2] = 1 | |
105 | elif styledef == 'noitalic': | |
106 | ndef[2] = 0 | |
107 | elif styledef == 'underline': | |
108 | ndef[3] = 1 | |
109 | elif styledef == 'nounderline': | |
110 | ndef[3] = 0 | |
111 | elif styledef[:3] == 'bg:': | |
112 | ndef[4] = colorformat(styledef[3:]) | |
113 | elif styledef[:7] == 'border:': | |
114 | ndef[5] = colorformat(styledef[7:]) | |
115 | elif styledef == 'roman': | |
116 | ndef[6] = 1 | |
117 | elif styledef == 'sans': | |
118 | ndef[7] = 1 | |
119 | elif styledef == 'mono': | |
120 | ndef[8] = 1 | |
121 | else: | |
122 | ndef[0] = colorformat(styledef) | |
123 | ||
124 | return obj | |
125 | ||
126 | def style_for_token(cls, token): | |
127 | t = cls._styles[token] | |
128 | ansicolor = bgansicolor = None | |
129 | color = t[0] | |
130 | if color in _deprecated_ansicolors: | |
131 | color = _deprecated_ansicolors[color] | |
132 | if color in ansicolors: | |
133 | ansicolor = color | |
134 | color = _ansimap[color] | |
135 | bgcolor = t[4] | |
136 | if bgcolor in _deprecated_ansicolors: | |
137 | bgcolor = _deprecated_ansicolors[bgcolor] | |
138 | if bgcolor in ansicolors: | |
139 | bgansicolor = bgcolor | |
140 | bgcolor = _ansimap[bgcolor] | |
141 | ||
142 | return { | |
143 | 'color': color or None, | |
144 | 'bold': bool(t[1]), | |
145 | 'italic': bool(t[2]), | |
146 | 'underline': bool(t[3]), | |
147 | 'bgcolor': bgcolor or None, | |
148 | 'border': t[5] or None, | |
149 | 'roman': bool(t[6]) or None, | |
150 | 'sans': bool(t[7]) or None, | |
151 | 'mono': bool(t[8]) or None, | |
152 | 'ansicolor': ansicolor, | |
153 | 'bgansicolor': bgansicolor, | |
154 | } | |
155 | ||
156 | def list_styles(cls): | |
157 | return list(cls) | |
158 | ||
159 | def styles_token(cls, ttype): | |
160 | return ttype in cls._styles | |
161 | ||
162 | def __iter__(cls): | |
163 | for token in cls._styles: | |
164 | yield token, cls.style_for_token(token) | |
165 | ||
166 | def __len__(cls): | |
167 | return len(cls._styles) | |
168 | ||
169 | ||
170 | class Style(metaclass=StyleMeta): | |
171 | ||
172 | #: overall background color (``None`` means transparent) | |
173 | background_color = '#ffffff' | |
174 | ||
175 | #: highlight background color | |
176 | highlight_color = '#ffffcc' | |
177 | ||
178 | #: line number font color | |
179 | line_number_color = 'inherit' | |
180 | ||
181 | #: line number background color | |
182 | line_number_background_color = 'transparent' | |
183 | ||
184 | #: special line number font color | |
185 | line_number_special_color = '#000000' | |
186 | ||
187 | #: special line number background color | |
188 | line_number_special_background_color = '#ffffc0' | |
189 | ||
190 | #: Style definitions for individual token types. | |
191 | styles = {} | |
192 | ||
193 | # Attribute for lexers defined within Pygments. If set | |
194 | # to True, the style is not shown in the style gallery | |
195 | # on the website. This is intended for language-specific | |
196 | # styles. | |
197 | web_style_gallery_exclude = False |