Back to index

moin  1.9.0~rc2
style.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.style
00004     ~~~~~~~~~~~~~~
00005 
00006     Basic style object.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 from pygments.token import Token, STANDARD_TYPES
00013 
00014 
00015 class StyleMeta(type):
00016 
00017     def __new__(mcs, name, bases, dct):
00018         obj = type.__new__(mcs, name, bases, dct)
00019         for token in STANDARD_TYPES:
00020             if token not in obj.styles:
00021                 obj.styles[token] = ''
00022 
00023         def colorformat(text):
00024             if text[0:1] == '#':
00025                 col = text[1:]
00026                 if len(col) == 6:
00027                     return col
00028                 elif len(col) == 3:
00029                     return col[0]+'0'+col[1]+'0'+col[2]+'0'
00030             elif text == '':
00031                 return ''
00032             assert False, "wrong color format %r" % text
00033 
00034         _styles = obj._styles = {}
00035 
00036         for ttype in obj.styles:
00037             for token in ttype.split():
00038                 if token in _styles:
00039                     continue
00040                 ndef = _styles.get(token.parent, None)
00041                 styledefs = obj.styles.get(token, '').split()
00042                 if  not ndef or token is None:
00043                     ndef = ['', 0, 0, 0, '', '', 0, 0, 0]
00044                 elif 'noinherit' in styledefs and token is not Token:
00045                     ndef = _styles[Token][:]
00046                 else:
00047                     ndef = ndef[:]
00048                 _styles[token] = ndef
00049                 for styledef in obj.styles.get(token, '').split():
00050                     if styledef == 'noinherit':
00051                         pass
00052                     elif styledef == 'bold':
00053                         ndef[1] = 1
00054                     elif styledef == 'nobold':
00055                         ndef[1] = 0
00056                     elif styledef == 'italic':
00057                         ndef[2] = 1
00058                     elif styledef == 'noitalic':
00059                         ndef[2] = 0
00060                     elif styledef == 'underline':
00061                         ndef[3] = 1
00062                     elif styledef == 'nounderline':
00063                         ndef[3] = 0
00064                     elif styledef[:3] == 'bg:':
00065                         ndef[4] = colorformat(styledef[3:])
00066                     elif styledef[:7] == 'border:':
00067                         ndef[5] = colorformat(styledef[7:])
00068                     elif styledef == 'roman':
00069                         ndef[6] = 1
00070                     elif styledef == 'sans':
00071                         ndef[7] = 1
00072                     elif styledef == 'mono':
00073                         ndef[8] = 1
00074                     else:
00075                         ndef[0] = colorformat(styledef)
00076 
00077         return obj
00078 
00079     def style_for_token(cls, token):
00080         t = cls._styles[token]
00081         return {
00082             'color':        t[0] or None,
00083             'bold':         bool(t[1]),
00084             'italic':       bool(t[2]),
00085             'underline':    bool(t[3]),
00086             'bgcolor':      t[4] or None,
00087             'border':       t[5] or None,
00088             'roman':        bool(t[6]) or None,
00089             'sans':         bool(t[7]) or None,
00090             'mono':         bool(t[8]) or None,
00091         }
00092 
00093     def list_styles(cls):
00094         return list(cls)
00095 
00096     def styles_token(cls, ttype):
00097         return ttype in cls._styles
00098 
00099     def __iter__(cls):
00100         for token in cls._styles:
00101             yield token, cls.style_for_token(token)
00102 
00103     def __len__(cls):
00104         return len(cls._styles)
00105 
00106 
00107 class Style(object):
00108     __metaclass__ = StyleMeta
00109 
00110     #: overall background color (``None`` means transparent)
00111     background_color = '#ffffff'
00112 
00113     #: highlight background color
00114     highlight_color = '#ffffcc'
00115 
00116     #: Style definitions for individual token types.
00117     styles = {}