import color, mode from lex import Grammar, PatternRule, RegionRule import mode.xml import mode.html import mode.javascript import mode.css from mode.perl import StrictStringGrammar, StringGrammar, PerlGrammar class PerlDirectiveGrammar(Grammar): rules = [PatternRule('tt.perl', '(?:PERL|RAWPERL|END)(?![A-Z_])')] class DirectiveGrammar(Grammar): rules = [ PatternRule('tt.keyword', r'(?:BLOCK|CALL|CASE|CATCH|CLEAR|DEBUG|DEFAULT|FINAL|FILTER|FOREACH|ELSIF|ELSE|END|GET|IF|INCLUDE|INSERT|IN|LAST|MACRO|META|NEXT|PERL|PROCESS|RAWPERL|RETURN|SET|STOP|SWITCH|TAGS|THROW|TRY|UNLESS|USE|WHILE|WRAPPER)(?![A-Z_])'), PatternRule('attribute', '[a-zA-Z_][a-zA-Z0-9_]*(?= *=)'), RegionRule('string', '"', StringGrammar, '"'), RegionRule('string', "'", StrictStringGrammar, "'"), ] crule = RegionRule('comment', r'\[\%#', Grammar, r'%%\]') drule = RegionRule('tt.directive', r'\[\%', DirectiveGrammar, r'%%\]') prule = RegionRule('tt.directive', r'\[\%(?= *PERL)', PerlDirectiveGrammar, r'%%\]', PerlGrammar, r'\[%%', PerlDirectiveGrammar, '%%\]') class TagGrammar(Grammar): rules = [crule, prule, drule] + mode.html.TagGrammar.rules class JavascriptGrammar(Grammar): rules = [crule, prule, drule] + mode.javascript.JavascriptGrammar.rules class CSSGrammar(Grammar): rules = [crule, prule, drule] + mode.css.CSSGrammar.rules class CommentGrammar(Grammar): rules = [crule, prule, drule] + mode.html.CommentGrammar.rules class CDataGrammar(Grammar): rules = [crule, prule, drule] + mode.html.CDataGrammar.rules class MetadataGrammar(Grammar): rules = [crule, prule, drule] + mode.html.MetadataGrammar.rules class DoctypeGrammar(Grammar): rules = [crule, prule, drule] + mode.html.DoctypeGrammar.rules class TemplateGrammar(Grammar): rules = [crule, prule, drule] + [ PatternRule('escaped', '&[^;]+;'), PatternRule('html.data', r'[^\[&<]+'), PatternRule('html.data', '[^&<]+'), RegionRule('html.tag', '<(?=script[^a-zA-Z0-9_])', TagGrammar, '>', JavascriptGrammar, ')', TagGrammar, '>'), RegionRule('html.tag', '<(?=style[^a-zA-Z0-9_])', TagGrammar, '>', CSSGrammar, ')', TagGrammar, '>'), RegionRule('comment', ''), RegionRule('html.tag', r'<(?![\?!])', TagGrammar, '/?>'), RegionRule('html.metadata', r'<\?', MetadataGrammar, r'\?>'), RegionRule('html.doctype', ''), PatternRule('html.entity', '&[a-z]+;'), RegionRule('html.cdata', r''), ] class Template(mode.Fundamental): name = 'Template' extensions = ['.tt'] grammar = TemplateGrammar colors = { 'tt.directive.start': ('magenta', 'default', 'bold'), 'tt.directive.comment': ('red', 'default', 'bold'), 'tt.directive.keyword': ('yellow', 'default', 'bold'), 'tt.directive.attribute': ('magenta', 'default', 'bold'), 'tt.directive.string.start': ('green', 'default', 'bold'), 'tt.directive.string.escaped': ('magenta', 'default', 'bold'), 'tt.directive.string.octal': ('magenta', 'default', 'bold'), 'tt.directive.string.null': ('green', 'default', 'bold'), 'tt.directive.string.end': ('green', 'default', 'bold'), 'tt.directive.null': ('magenta', 'default', 'bold'), 'tt.directive.middle0': ('magenta', 'default', 'bold'), 'tt.directive.middle1': ('magenta', 'default', 'bold'), 'tt.directive.end': ('magenta', 'default', 'bold'), 'tt.tag.start': ('default', 'default'), 'tt.tag.namespace': ('magenta', 'default', 'bold'), 'tt.tag.name': ('blue', 'default', 'bold'), 'tt.tag.attrname': ('cyan', 'default', 'bold'), 'tt.tag.string.start': ('green', 'default', 'bold'), 'tt.tag.string.null': ('green', 'default', 'bold'), 'tt.tag.string.end': ('green', 'default', 'bold'), 'tt.tag.end': ('default', 'default'), 'tt.perl': ('cyan', 'default', 'bold'), } _bindings = { 'close-paren': (')',), 'close-brace': ('}',), 'close-bracket': (']',), } install = Template.install