Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/style.py: 77%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2 pygments.style
3 ~~~~~~~~~~~~~~
5 Basic style object.
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.token import Token, STANDARD_TYPES
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}
55ansicolors = set(_ansimap)
58class StyleMeta(type):
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] = ''
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 == 'transparent':
78 return text
79 elif text.startswith('var') or text.startswith('calc'):
80 return text
81 assert False, f"wrong color format {text!r}"
83 _styles = obj._styles = {}
85 for ttype in obj.styles:
86 for token in ttype.split():
87 if token in _styles:
88 continue
89 ndef = _styles.get(token.parent, None)
90 styledefs = obj.styles.get(token, '').split()
91 if not ndef or token is None:
92 ndef = ['', 0, 0, 0, '', '', 0, 0, 0]
93 elif 'noinherit' in styledefs and token is not Token:
94 ndef = _styles[Token][:]
95 else:
96 ndef = ndef[:]
97 _styles[token] = ndef
98 for styledef in obj.styles.get(token, '').split():
99 if styledef == 'noinherit':
100 pass
101 elif styledef == 'bold':
102 ndef[1] = 1
103 elif styledef == 'nobold':
104 ndef[1] = 0
105 elif styledef == 'italic':
106 ndef[2] = 1
107 elif styledef == 'noitalic':
108 ndef[2] = 0
109 elif styledef == 'underline':
110 ndef[3] = 1
111 elif styledef == 'nounderline':
112 ndef[3] = 0
113 elif styledef[:3] == 'bg:':
114 ndef[4] = colorformat(styledef[3:])
115 elif styledef[:7] == 'border:':
116 ndef[5] = colorformat(styledef[7:])
117 elif styledef == 'roman':
118 ndef[6] = 1
119 elif styledef == 'sans':
120 ndef[7] = 1
121 elif styledef == 'mono':
122 ndef[8] = 1
123 else:
124 ndef[0] = colorformat(styledef)
126 return obj
128 def style_for_token(cls, token):
129 t = cls._styles[token]
130 ansicolor = bgansicolor = None
131 color = t[0]
132 if color in _deprecated_ansicolors:
133 color = _deprecated_ansicolors[color]
134 if color in ansicolors:
135 ansicolor = color
136 color = _ansimap[color]
137 bgcolor = t[4]
138 if bgcolor in _deprecated_ansicolors:
139 bgcolor = _deprecated_ansicolors[bgcolor]
140 if bgcolor in ansicolors:
141 bgansicolor = bgcolor
142 bgcolor = _ansimap[bgcolor]
144 return {
145 'color': color or None,
146 'bold': bool(t[1]),
147 'italic': bool(t[2]),
148 'underline': bool(t[3]),
149 'bgcolor': bgcolor or None,
150 'border': t[5] or None,
151 'roman': bool(t[6]) or None,
152 'sans': bool(t[7]) or None,
153 'mono': bool(t[8]) or None,
154 'ansicolor': ansicolor,
155 'bgansicolor': bgansicolor,
156 }
158 def list_styles(cls):
159 return list(cls)
161 def styles_token(cls, ttype):
162 return ttype in cls._styles
164 def __iter__(cls):
165 for token in cls._styles:
166 yield token, cls.style_for_token(token)
168 def __len__(cls):
169 return len(cls._styles)
172class Style(metaclass=StyleMeta):
174 #: overall background color (``None`` means transparent)
175 background_color = '#ffffff'
177 #: highlight background color
178 highlight_color = '#ffffcc'
180 #: line number font color
181 line_number_color = 'inherit'
183 #: line number background color
184 line_number_background_color = 'transparent'
186 #: special line number font color
187 line_number_special_color = '#000000'
189 #: special line number background color
190 line_number_special_background_color = '#ffffc0'
192 #: Style definitions for individual token types.
193 styles = {}
195 #: user-friendly style name (used when selecting the style, so this
196 # should be all-lowercase, no spaces, hyphens)
197 name = 'unnamed'
199 aliases = []
201 # Attribute for lexers defined within Pygments. If set
202 # to True, the style is not shown in the style gallery
203 # on the website. This is intended for language-specific
204 # styles.
205 web_style_gallery_exclude = False