import color, mode from lex import Grammar, PatternRule, RegionRule from mode.xml import TagGrammar from mode.javascript import JavascriptGrammar from mode.css import CSSGrammar class HTMLGrammar(Grammar): rules = [ # TODO: how does cdata work again? RegionRule(r'comment', r''), # BUG: not all scripts are javascript and not all styles are CSS # but, dynamically choosing a grammar based on the 'type' attribute # (which may be on a different line) is impractical. RegionRule(r'script', r'<(?=script[^a-zA-Z0-9_])', TagGrammar, r'>', JavascriptGrammar, r')', TagGrammar, r'>'), RegionRule(r'style', r'<(?=style[^a-zA-Z0-9_])', TagGrammar, r'>', CSSGrammar, r')', TagGrammar, r'>'), RegionRule(r'tag', r''), ] class HTML(mode.Fundamental): modename = 'HTML' extensions = ['.html', '.htm', '.shtml', '.shtm', '.xhtml'] grammar = HTMLGrammar colors = {} def __init__(self, w): mode.Fundamental.__init__(self, w) self.add_bindings('close-paren', (')',)) self.add_bindings('close-brace', ('}',)) self.add_bindings('close-bracket', (']',)) _colorbase = { 'start': ('default', 'default'), 'namespace': ('magenta', 'default'), 'name': ('blue', 'default'), 'attrname': ('cyan', 'default'), 'string.start': ('green', 'default'), 'string.null': ('green', 'default'), 'string.end': ('green', 'default'), 'end': ('default', 'default'), } for _name in _colorbase: HTML.colors['script.%s' % _name] = _colorbase[_name] HTML.colors['style.%s' % _name] = _colorbase[_name] HTML.colors['tag.%s' % _name] = _colorbase[_name] install = HTML.install