style.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.style
  4. ~~~~~~~~~~~~~~
  5. Basic style object.
  6. :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.token import Token, STANDARD_TYPES
  10. class StyleMeta(type):
  11. def __new__(mcs, name, bases, dct):
  12. obj = type.__new__(mcs, name, bases, dct)
  13. for token in STANDARD_TYPES:
  14. if token not in obj.styles:
  15. obj.styles[token] = ''
  16. def colorformat(text):
  17. if text[0:1] == '#':
  18. col = text[1:]
  19. if len(col) == 6:
  20. return col
  21. elif len(col) == 3:
  22. return col[0]+'0'+col[1]+'0'+col[2]+'0'
  23. elif text == '':
  24. return ''
  25. assert False, "wrong color format %r" % text
  26. _styles = obj._styles = {}
  27. for ttype in obj.styles:
  28. for token in ttype.split():
  29. if token in _styles:
  30. continue
  31. ndef = _styles.get(token.parent, None)
  32. styledefs = obj.styles.get(token, '').split()
  33. if not ndef or token is None:
  34. ndef = ['', 0, 0, 0, '', '', 0, 0, 0]
  35. elif 'noinherit' in styledefs and token is not Token:
  36. ndef = _styles[Token][:]
  37. else:
  38. ndef = ndef[:]
  39. _styles[token] = ndef
  40. for styledef in obj.styles.get(token, '').split():
  41. if styledef == 'noinherit':
  42. pass
  43. elif styledef == 'bold':
  44. ndef[1] = 1
  45. elif styledef == 'nobold':
  46. ndef[1] = 0
  47. elif styledef == 'italic':
  48. ndef[2] = 1
  49. elif styledef == 'noitalic':
  50. ndef[2] = 0
  51. elif styledef == 'underline':
  52. ndef[3] = 1
  53. elif styledef == 'nounderline':
  54. ndef[3] = 0
  55. elif styledef[:3] == 'bg:':
  56. ndef[4] = colorformat(styledef[3:])
  57. elif styledef[:7] == 'border:':
  58. ndef[5] = colorformat(styledef[7:])
  59. elif styledef == 'roman':
  60. ndef[6] = 1
  61. elif styledef == 'sans':
  62. ndef[7] = 1
  63. elif styledef == 'mono':
  64. ndef[8] = 1
  65. else:
  66. ndef[0] = colorformat(styledef)
  67. return obj
  68. def style_for_token(cls, token):
  69. t = cls._styles[token]
  70. return {
  71. 'color': t[0] or None,
  72. 'bold': bool(t[1]),
  73. 'italic': bool(t[2]),
  74. 'underline': bool(t[3]),
  75. 'bgcolor': t[4] or None,
  76. 'border': t[5] or None,
  77. 'roman': bool(t[6]) or None,
  78. 'sans': bool(t[7]) or None,
  79. 'mono': bool(t[8]) or None,
  80. }
  81. def list_styles(cls):
  82. return list(cls)
  83. def styles_token(cls, ttype):
  84. return ttype in cls._styles
  85. def __iter__(cls):
  86. for token in cls._styles:
  87. yield token, cls.style_for_token(token)
  88. def __len__(cls):
  89. return len(cls._styles)
  90. class Style(object):
  91. __metaclass__ = StyleMeta
  92. #: overall background color (``None`` means transparent)
  93. background_color = '#ffffff'
  94. #: highlight background color
  95. highlight_color = '#ffffcc'
  96. #: Style definitions for individual token types.
  97. styles = {}