codekingpro/portable-devtools
115k
1"""2 pygments.style3 ~~~~~~~~~~~~~~4 5 Basic style object.6 7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11from pygments.token import Token, STANDARD_TYPES12 13# Default mapping of ansixxx to RGB colors.14_ansimap = {15 # dark16 'ansiblack': '000000',17 'ansired': '7f0000',18 'ansigreen': '007f00',19 'ansiyellow': '7f7fe0',20 'ansiblue': '00007f',21 'ansimagenta': '7f007f',22 'ansicyan': '007f7f',23 'ansigray': 'e5e5e5',24 # normal25 '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 names35_deprecated_ansicolors = {36 # dark37 '#ansiblack': 'ansiblack',38 '#ansidarkred': 'ansired',39 '#ansidarkgreen': 'ansigreen',40 '#ansibrown': 'ansiyellow',41 '#ansidarkblue': 'ansiblue',42 '#ansipurple': 'ansimagenta',43 '#ansiteal': 'ansicyan',44 '#ansilightgray': 'ansigray',45 # normal46 '#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)56 57 58class 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 text69 if text[0:1] == '#':70 col = text[1:]71 if len(col) == 6:72 return col73 elif len(col) == 3:74 return col[0] * 2 + col[1] * 2 + col[2] * 275 elif text == '':76 return ''77 elif text.startswith('var') or text.startswith('calc'):78 return text79 assert False, f"wrong color format {text!r}"80 81 _styles = obj._styles = {}82 83 for ttype in obj.styles:84 for token in ttype.split():85 if token in _styles:86 continue87 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] = ndef96 for styledef in obj.styles.get(token, '').split():97 if styledef == 'noinherit':98 pass99 elif styledef == 'bold':100 ndef[1] = 1101 elif styledef == 'nobold':102 ndef[1] = 0103 elif styledef == 'italic':104 ndef[2] = 1105 elif styledef == 'noitalic':106 ndef[2] = 0107 elif styledef == 'underline':108 ndef[3] = 1109 elif styledef == 'nounderline':110 ndef[3] = 0111 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] = 1117 elif styledef == 'sans':118 ndef[7] = 1119 elif styledef == 'mono':120 ndef[8] = 1121 else:122 ndef[0] = colorformat(styledef)123 124 return obj125 126 def style_for_token(cls, token):127 t = cls._styles[token]128 ansicolor = bgansicolor = None129 color = t[0]130 if color in _deprecated_ansicolors:131 color = _deprecated_ansicolors[color]132 if color in ansicolors:133 ansicolor = color134 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 = bgcolor140 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._styles161 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 170class Style(metaclass=StyleMeta):171 172 #: overall background color (``None`` means transparent)173 background_color = '#ffffff'174 175 #: highlight background color176 highlight_color = '#ffffcc'177 178 #: line number font color179 line_number_color = 'inherit'180 181 #: line number background color182 line_number_background_color = 'transparent'183 184 #: special line number font color185 line_number_special_color = '#000000'186 187 #: special line number background color188 line_number_special_background_color = '#ffffc0'189 190 #: Style definitions for individual token types.191 styles = {}192 193 #: user-friendly style name (used when selecting the style, so this194 # should be all-lowercase, no spaces, hyphens)195 name = 'unnamed'196 197 aliases = []198 199 # Attribute for lexers defined within Pygments. If set200 # to True, the style is not shown in the style gallery201 # on the website. This is intended for language-specific202 # styles.203 web_style_gallery_exclude = False204 