Attachment 'css.py'

Download

   1 '''
   2 css - syntax coloring css syles
   3 
   4 Using simple 4-5 colors scheme is usually good, but some people would
   5 like finer grain coloring. Therefore the styles are defined here in
   6 classes and subclasses. A simple colorizer will use only the main
   7 classes. A more complemx colorizer would use the sub classes.
   8 
   9 Example:
  10 
  11     <span class="string comment"># This is an example</span>
  12     <span class="string docstring">""" This is a docstring """</span>
  13     <span class="string literal">"This is a literal"</span>
  14 
  15 With this css rule, all these would render the same which can be fine
  16 for most users:
  17 
  18     span.string {color: red}
  19 
  20 But with this rules, each one will render differnt:
  21 
  22     span.string.comment {color: green}
  23     span.string.docstring {color: red}
  24     span.string.literal {color: orage}
  25 
  26 
  27 Usage:
  28 
  29     >>>from css import classes
  30     >>>"<span class="%(keyword)s">class</span>" % classes
  31     <span class="statement keyword">class</span>
  32 
  33 classes dict is immutable, (unless we add methods to edit it).
  34 
  35 '''
  36 
  37 
  38 class _Classes(object):
  39     """ Managed dictionary holding css classes """
  40     __slots__ = ['_dict']
  41 
  42     # This dict is temporray interal structure - dont try to access it.
  43 
  44     _dict  = {
  45         # class: [sub class, ...]
  46         'comment': [
  47             'docstring',
  48             'ignore',
  49             'todo',
  50             ],
  51         'constant': [
  52             'string',
  53             'character',
  54             'number',
  55             'boolean',
  56             'float',
  57             ],
  58         'identifier': [
  59             'function',
  60             'function-name',
  61             'variable',
  62             ],
  63         'statement': [
  64             'conditional',
  65             'repeat',
  66             'label',
  67             'operator',
  68             'keyword',
  69             'exception',
  70             'builtin',
  71             ],
  72         'preprocessor': [
  73             'include',
  74             'define',
  75             'macro',
  76             'precondition',
  77             ],
  78         'type': [
  79             'storageclass',
  80             'structure',
  81             'typedef',
  82             ],
  83         'special': [
  84             'specialchar',
  85             'tag',
  86             'delimeter',
  87             'special-comment',
  88             'debug',
  89             ],
  90         'underlined': [
  91             'reference',
  92             ],
  93         'error': [
  94             'warning',
  95             ],
  96         }
  97 
  98     def __init__(self):
  99         self.optimize()
 100         
 101     def optimize(self):
 102         """ Optimize css dict for fast access
 103 
 104         Move sub classes from their parent to the dict, so we can
 105         access them with single lookup.
 106         """
 107         dict = self._dict
 108         for cls in dict.keys():
 109             if isinstance(dict[cls], list):
 110                 for subclass in dict[cls]:
 111                     if subclass in dict:
 112                         raise RuntimeError(
 113                             '%s: multiple subclass definition' % subclass)
 114                     dict[subclass] = cls
 115                 dict[cls] = None
 116 
 117     def __getitem__(self, item):
 118         """ Return 'parent child' pair or raise KeyError
 119 
 120         Will raise standard KeyError for missnig keys.
 121         """
 122         parent = self._dict[item]
 123         if parent:
 124             return '%s %s' % (parent, item)
 125         return item
 126         
 127     def __getattr__(self, attr):
 128         """ Convinience method to accees keys as attributes """
 129         try:
 130             return self.__getitem__(attr)
 131         except KeyError, why:
 132             raise AttributeError(why)
 133 
 134 
 135 classes = _Classes()
 136 
 137 
 138 if __name__ == '__main__':
 139     c = classes
 140     # testing item access
 141     assert c['comment'] == 'comment'
 142     assert c['docstring'] == 'comment docstring'
 143     
 144     # testing attributes access
 145     assert c.comment == 'comment'
 146     assert c.docstring == 'comment docstring'
 147 
 148     # testing missing key
 149     try:
 150         c.nir
 151     except AttributeError:
 152         pass # :-)
 153 
 154     try:
 155         c["nir"]
 156     except KeyError:
 157         pass
 158     

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-07-26 23:35:27, 5.4 KB) [[attachment:color-test.html]]
  • [get | view] (2004-07-24 22:32:38, 3.8 KB) [[attachment:css.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.