Commits

Anonymous committed a303173

[svn] added matt_good's genshi lexer (thx :D)

  • Participants
  • Parent commits 523dc49
  • Branches trunk

Comments (0)

Files changed (2)

pygments/lexers/_mapping.py

     'DiffLexer': ('pygments.lexers.text', 'Diff', ('diff',), ('*.diff', '*.patch')),
     'DjangoLexer': ('pygments.lexers.templates', 'django template', ('django',), ()),
     'ErbLexer': ('pygments.lexers.templates', 'ERB', ('erb',), ()),
+    'GenshiLexer': ('pygments.lexers.templates', 'Genshi', ('genshi', 'kid'), ()),
     'HtmlDjangoLexer': ('pygments.lexers.templates', 'HTML+Django', ('html+django',), ()),
+    'HtmlGenshiLexer': ('pygments.lexers.templates', 'HTML+Genshi', ('html+genshi', 'html+kid'), ()),
     'HtmlLexer': ('pygments.lexers.web', 'HTML', ('html',), ('*.html', '*.htm', '*.xhtml')),
     'HtmlPhpLexer': ('pygments.lexers.templates', 'HTML+PHP', ('html+php',), ('*.phtml',)),
     'HtmlSmartyLexer': ('pygments.lexers.templates', 'HTML+Smarty', ('html+smarty',), ()),

pygments/lexers/templates.py

 
     Lexers for various template engines.
 
-    :copyright: 2006 by Armin Ronacher, Georg Brandl.
+    :copyright: 2006 by Armin Ronacher, Georg Brandl, Matt Good.
     :license: GNU LGPL, see LICENSE for more details.
 """
 
 
 from pygments.lexers.web import \
      PhpLexer, HtmlLexer, XmlLexer, JavascriptLexer, CssLexer
+from pygments.lexers.agile import PythonLexer
 from pygments.lexer import \
      Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, include, using
 from pygments.token import \
            'SmartyLexer', 'HtmlSmartyLexer', 'XmlSmartyLexer',
            'CssSmartyLexer', 'JavascriptSmartyLexer', 'DjangoLexer',
            'HtmlDjangoLexer', 'CssDjangoLexer', 'XmlDjangoLexer',
-           'JavascriptDjangoLexer']
+           'JavascriptDjangoLexer', 'GenshiLexer', 'HtmlGenshiLexer']
 
 
 class ErbLexer(Lexer):
         return rv
 
 
+class GenshiMarkupLexer(RegexLexer):
+    flags = re.DOTALL
+
+    tokens = {
+        'root': [
+            (r'[^<\$]+', Other),
+            (r'(<\?python)(.*?)(\?>)',
+             bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
+            (r'<\s*py:[a-zA-Z0-9]+', Name.Tag, 'pytag'),
+            (r'<\s*[a-zA-Z0-9:]+', Name.Tag, 'tag'),
+            include('variable'),
+            (r'[<\$]', Other),
+        ],
+        'pytag': [
+            (r'\s+', Text),
+            (r'[a-zA-Z0-9_:-]+\s*=', Name.Attribute, 'pyattr'),
+            (r'/?\s*>', Name.Tag, '#pop'),
+        ],
+        'pyattr': [
+            ('(")(.*?)(")', bygroups(String, using(PythonLexer), String), '#pop'),
+            ("(')(.*?)(')", bygroups(String, using(PythonLexer), String), '#pop'),
+            (r'[^\s>]+', String, '#pop'),
+        ],
+        'tag': [
+            (r'\s+', Text),
+            (r'py:[a-zA-Z0-9_-]+\s*=', Name.Attribute, 'pyattr'),
+            (r'[a-zA-Z0-9_:-]+\s*=', Name.Attribute, 'attr'),
+            (r'/?\s*>', Name.Tag, '#pop'),
+        ],
+        'attr': [
+            ('"', String, 'attr-dstring'),
+            ("'", String, 'attr-sstring'),
+            (r'[^\s>]*', String, '#pop')
+        ],
+        'attr-dstring': [
+            ('"', String, '#pop'),
+            include('strings'),
+            ("'", String)
+        ],
+        'attr-sstring': [
+            ("'", String, '#pop'),
+            include('strings'),
+            ("'", String)
+        ],
+        'strings': [
+            ('[^"\'$]+', String),
+            include('variable')
+        ],
+        'variable': [
+            (r'(?<!\$)(\$\{)(.+?)(\})',
+             bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
+            (r'(?<!\$)(\$)([a-zA-Z_][a-zA-Z0-9_\.]*)',
+             Name.Variable),
+        ]
+    }
+
+
+class HtmlGenshiLexer(DelegatingLexer):
+    name = 'HTML+Genshi'
+    aliases = ['html+genshi', 'html+kid']
+
+    def __init__(self, **options):
+        super(HtmlGenshiLexer, self).__init__(HtmlLexer, GenshiMarkupLexer,
+                                              **options)
+
+
+class GenshiLexer(DelegatingLexer):
+    name = 'Genshi'
+    aliases = ['genshi', 'kid', 'xml+genshi', 'xml+kid']
+
+    def __init__(self, **options):
+        super(GenshiLexer, self).__init__(HtmlLexer, GenshiMarkupLexer,
+                                          **options)
+
+
 class RhtmlLexer(DelegatingLexer):
     name = 'RHTML'
     aliases = ['rhtml', 'html+erb', 'html+ruby']