"""\ MySilverCity - A customized version of the SilverCity HTML generators Features: - whitespace and linebreaks are optionally preserved - generated css classes can be modified Usage: To modify the generated css classes: 1. take a look at the ScintillaConstants.py file in your SilverCity distribution (typically in the site-packages directory). Locate the names of the lexical states you want to keep and/or modify. 2. Create at dictionary with the selected names as keys. The keys must be in lowercase and without the SCE_ prefix. As value, set to True if you only want to keep the css class unmodified, or set it to a new name. Example: mypython_css = { 'p_commentline': True, # keep unaltered 'p_number': True, # " 'p_string': True, # " 'p_character':'p_string', # rename to 'p_string' 'p_triple':True, 'p_tripledouble' : 'p_triple', # combine p_triple and p_tripledouble 'p_word': True, } All other classes like p_operator and p_default are ignored 3. Use the customization dictionary as a parameter to the modified HTML generator classes Author: Kjell Magne Fauske """ from SilverCity import Python, XML, CSS def cleanCssClasses(s1, s2): """Return only the css classes we are interested in Input: s1 - original dictionary with token id -> css name mappings s2 - a dictionary where the keys are the css names we are interested in and the values are either True or a new css name Output: Returns a dictionary with new token id -> css name mappings """ stmp = {} for (key, value) in s1.items(): s2val = s2.get(value,False) if s2val: if type(s2val) == str: stmp[key] = s2val else: stmp[key] = value return stmp class HTMLGeneratorMixIn: """Adds extra functionality to the SilverCity.HTMLGenerator class The mixin class modifies the behaviour of the SilverCity.HTMLGenerator class in three ways: - It is possible to specify which css classes that are generated - A css class name can be renamed and multiple css classes can share the same name - No markup is inserted for whitespaces and linefeeds """ def __init__(self, htmlgenerator, mycss_classes = None, pre = True): """Initilize HTML generator Input: htmlgenerator - An inststance of the original html generator mycss_classes - A dictionary used to modify the css classes generated by the original html generators. Default to None. pre - If set to True, whitespace and linebreaks are unaltered. Useful for usage in the
..
HTML environment. If set to false, whitespace is replaced by   and linefeeds with
tags. Default set to True. """ if mycss_classes: self.css_classes=cleanCssClasses(self.css_classes,mycss_classes) self.pre = pre self.htmlgenerator = htmlgenerator def preformat(self, text): """Override the SilverCity.HTMLGenerator.preformat method""" if self.pre: text = self.escape(text.expandtabs()) return text else: return self.htmlgenerator.preformat(self,text) # Modification of the default python styles mypython_css = { 'p_commentline': True, 'p_number': True, 'p_string': True, 'p_character':'p_string', 'p_triple':True, 'p_tripledouble' : 'p_triple', 'p_word': True, } # Modification of the default xml/xhtml styles myhtml_css = { 'h_comment': True, 'h_tag': True, 'h_attribute': True, 'h_doublestring': True, 'h_singlestring': 'h_doublestring', } class MyPythonHTMLGenerator(HTMLGeneratorMixIn, Python.PythonHTMLGenerator): def __init__(self, mycss_classes = None, pre = True): Python.PythonHTMLGenerator.__init__(self) HTMLGeneratorMixIn.__init__(self, Python.PythonHTMLGenerator, mycss_classes, pre) class MyXMLHTMLGenerator(HTMLGeneratorMixIn, XML.XMLHTMLGenerator): def __init__(self, mycss_classes = None, pre = True): XML.XMLHTMLGenerator.__init__(self) HTMLGeneratorMixIn.__init__(self, XML.XMLHTMLGenerator, mycss_classes, pre) # Modification of the default CSS styles mycss_css = { 'css_tag': True, 'css_class': 'css_tag', 'css_id': 'css_tag', 'css_comment' : True, 'css_identifier' : True, } class MyCSSHTMLGenerator(HTMLGeneratorMixIn, CSS.CSSHTMLGenerator): def __init__(self, mycss_classes = None, pre = True): CSS.CSSHTMLGenerator.__init__(self) HTMLGeneratorMixIn.__init__(self, CSS.CSSHTMLGenerator, mycss_classes, pre)