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