Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3 pygments.style 

4 ~~~~~~~~~~~~~~ 

5 

6 Basic style object. 

7 

8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. 

9 :license: BSD, see LICENSE for details. 

10""" 

11 

12from pygments.token import Token, STANDARD_TYPES 

13 

14# Default mapping of ansixxx to RGB colors. 

15_ansimap = { 

16 # dark 

17 'ansiblack': '000000', 

18 'ansired': '7f0000', 

19 'ansigreen': '007f00', 

20 'ansiyellow': '7f7fe0', 

21 'ansiblue': '00007f', 

22 'ansimagenta': '7f007f', 

23 'ansicyan': '007f7f', 

24 'ansigray': 'e5e5e5', 

25 # normal 

26 'ansibrightblack': '555555', 

27 'ansibrightred': 'ff0000', 

28 'ansibrightgreen': '00ff00', 

29 'ansibrightyellow': 'ffff00', 

30 'ansibrightblue': '0000ff', 

31 'ansibrightmagenta': 'ff00ff', 

32 'ansibrightcyan': '00ffff', 

33 'ansiwhite': 'ffffff', 

34} 

35# mapping of deprecated #ansixxx colors to new color names 

36_deprecated_ansicolors = { 

37 # dark 

38 '#ansiblack': 'ansiblack', 

39 '#ansidarkred': 'ansired', 

40 '#ansidarkgreen': 'ansigreen', 

41 '#ansibrown': 'ansiyellow', 

42 '#ansidarkblue': 'ansiblue', 

43 '#ansipurple': 'ansimagenta', 

44 '#ansiteal': 'ansicyan', 

45 '#ansilightgray': 'ansigray', 

46 # normal 

47 '#ansidarkgray': 'ansibrightblack', 

48 '#ansired': 'ansibrightred', 

49 '#ansigreen': 'ansibrightgreen', 

50 '#ansiyellow': 'ansibrightyellow', 

51 '#ansiblue': 'ansibrightblue', 

52 '#ansifuchsia': 'ansibrightmagenta', 

53 '#ansiturquoise': 'ansibrightcyan', 

54 '#ansiwhite': 'ansiwhite', 

55} 

56ansicolors = set(_ansimap) 

57 

58 

59class StyleMeta(type): 

60 

61 def __new__(mcs, name, bases, dct): 

62 obj = type.__new__(mcs, name, bases, dct) 

63 for token in STANDARD_TYPES: 

64 if token not in obj.styles: 

65 obj.styles[token] = '' 

66 

67 def colorformat(text): 

68 if text in ansicolors: 

69 return text 

70 if text[0:1] == '#': 

71 col = text[1:] 

72 if len(col) == 6: 

73 return col 

74 elif len(col) == 3: 

75 return col[0] * 2 + col[1] * 2 + col[2] * 2 

76 elif text == '': 

77 return '' 

78 elif text.startswith('var') or text.startswith('calc'): 

79 return text 

80 assert False, "wrong color format %r" % text 

81 

82 _styles = obj._styles = {} 

83 

84 for ttype in obj.styles: 

85 for token in ttype.split(): 

86 if token in _styles: 

87 continue 

88 ndef = _styles.get(token.parent, None) 

89 styledefs = obj.styles.get(token, '').split() 

90 if not ndef or token is None: 

91 ndef = ['', 0, 0, 0, '', '', 0, 0, 0] 

92 elif 'noinherit' in styledefs and token is not Token: 

93 ndef = _styles[Token][:] 

94 else: 

95 ndef = ndef[:] 

96 _styles[token] = ndef 

97 for styledef in obj.styles.get(token, '').split(): 

98 if styledef == 'noinherit': 

99 pass 

100 elif styledef == 'bold': 

101 ndef[1] = 1 

102 elif styledef == 'nobold': 

103 ndef[1] = 0 

104 elif styledef == 'italic': 

105 ndef[2] = 1 

106 elif styledef == 'noitalic': 

107 ndef[2] = 0 

108 elif styledef == 'underline': 

109 ndef[3] = 1 

110 elif styledef == 'nounderline': 

111 ndef[3] = 0 

112 elif styledef[:3] == 'bg:': 

113 ndef[4] = colorformat(styledef[3:]) 

114 elif styledef[:7] == 'border:': 

115 ndef[5] = colorformat(styledef[7:]) 

116 elif styledef == 'roman': 

117 ndef[6] = 1 

118 elif styledef == 'sans': 

119 ndef[7] = 1 

120 elif styledef == 'mono': 

121 ndef[8] = 1 

122 else: 

123 ndef[0] = colorformat(styledef) 

124 

125 return obj 

126 

127 def style_for_token(cls, token): 

128 t = cls._styles[token] 

129 ansicolor = bgansicolor = None 

130 color = t[0] 

131 if color in _deprecated_ansicolors: 

132 color = _deprecated_ansicolors[color] 

133 if color in ansicolors: 

134 ansicolor = color 

135 color = _ansimap[color] 

136 bgcolor = t[4] 

137 if bgcolor in _deprecated_ansicolors: 

138 bgcolor = _deprecated_ansicolors[color] 

139 if bgcolor in ansicolors: 

140 bgansicolor = bgcolor 

141 bgcolor = _ansimap[bgcolor] 

142 

143 return { 

144 'color': color or None, 

145 'bold': bool(t[1]), 

146 'italic': bool(t[2]), 

147 'underline': bool(t[3]), 

148 'bgcolor': bgcolor or None, 

149 'border': t[5] or None, 

150 'roman': bool(t[6]) or None, 

151 'sans': bool(t[7]) or None, 

152 'mono': bool(t[8]) or None, 

153 'ansicolor': ansicolor, 

154 'bgansicolor': bgansicolor, 

155 } 

156 

157 def list_styles(cls): 

158 return list(cls) 

159 

160 def styles_token(cls, ttype): 

161 return ttype in cls._styles 

162 

163 def __iter__(cls): 

164 for token in cls._styles: 

165 yield token, cls.style_for_token(token) 

166 

167 def __len__(cls): 

168 return len(cls._styles) 

169 

170 

171class Style(metaclass=StyleMeta): 

172 

173 #: overall background color (``None`` means transparent) 

174 background_color = '#ffffff' 

175 

176 #: highlight background color 

177 highlight_color = '#ffffcc' 

178 

179 #: line number font color 

180 line_number_color = '#000000' 

181 

182 #: line number background color 

183 line_number_background_color = '#f0f0f0' 

184 

185 #: special line number font color 

186 line_number_special_color = '#000000' 

187 

188 #: special line number background color 

189 line_number_special_background_color = '#ffffc0' 

190 

191 #: Style definitions for individual token types. 

192 styles = {}