style.py 5.6 KB

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