style.py 5.9 KB

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