Commits

thatch  committed 5c53d77

Add ASP.net lexers

  • Participants
  • Parent commits 9fe544c

Comments (0)

Files changed (5)

   * Rebol
   * MXML
   * Cython
+  * ASP.net (VB/C#)
 
 - Add "anchorlinenos" option to HTML formatter.
 

File pygments/lexers/_mapping.py

     'BrainfuckLexer': ('pygments.lexers.other', 'Brainfuck', ('brainfuck', 'bf'), ('*.bf', '*.b'), ('application/x-brainfuck',)),
     'CLexer': ('pygments.lexers.compiled', 'C', ('c',), ('*.c', '*.h'), ('text/x-chdr', 'text/x-csrc')),
     'CObjdumpLexer': ('pygments.lexers.asm', 'c-objdump', ('c-objdump',), ('*.c-objdump',), ('text/x-c-objdump',)),
+    'CSharpAspxLexer': ('pygments.lexers.dotnet', 'aspx-cs', ('aspx-cs',), ('*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd'), ()),
     'CSharpLexer': ('pygments.lexers.dotnet', 'C#', ('csharp', 'c#'), ('*.cs',), ('text/x-csharp',)),
     'CheetahHtmlLexer': ('pygments.lexers.templates', 'HTML+Cheetah', ('html+cheetah', 'html+spitfire'), (), ('text/html+cheetah', 'text/html+spitfire')),
     'CheetahJavascriptLexer': ('pygments.lexers.templates', 'JavaScript+Cheetah', ('js+cheetah', 'javascript+cheetah', 'js+spitfire', 'javascript+spitfire'), (), ('application/x-javascript+cheetah', 'text/x-javascript+cheetah', 'text/javascript+cheetah', 'application/x-javascript+spitfire', 'text/x-javascript+spitfire', 'text/javascript+spitfire')),
     'TcshLexer': ('pygments.lexers.other', 'Tcsh', ('tcsh', 'csh'), ('*.tcsh', '*.csh'), ('application/x-csh',)),
     'TexLexer': ('pygments.lexers.text', 'TeX', ('tex', 'latex'), ('*.tex', '*.aux', '*.toc'), ('text/x-tex', 'text/x-latex')),
     'TextLexer': ('pygments.lexers.special', 'Text only', ('text',), ('*.txt',), ('text/plain',)),
+    'VbNetAspxLexer': ('pygments.lexers.dotnet', 'aspx-vb', ('aspx-vb',), ('*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd'), ()),
     'VbNetLexer': ('pygments.lexers.dotnet', 'VB.net', ('vb.net', 'vbnet'), ('*.vb', '*.bas'), ('text/x-vbnet', 'text/x-vba')),
     'VimLexer': ('pygments.lexers.text', 'VimL', ('vim',), ('*.vim', '.vimrc'), ('text/x-vim',)),
     'XmlDjangoLexer': ('pygments.lexers.templates', 'XML+Django/Jinja', ('xml+django', 'xml+jinja'), (), ('application/xml+django', 'application/xml+jinja')),

File pygments/lexers/dotnet.py

 """
 import re
 
-from pygments.lexer import RegexLexer, bygroups, using, this
+from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, using, this
 from pygments.token import Punctuation, \
-     Text, Comment, Operator, Keyword, Name, String, Number, Literal
+     Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
 from pygments.util import get_choice_opt
 from pygments import unistring as uni
 
-__all__ = ['CSharpLexer', 'BooLexer', 'VbNetLexer']
+from pygments.lexers.web import XmlLexer
+
+__all__ = ['CSharpLexer', 'BooLexer', 'VbNetLexer', 'CSharpAspxLexer',
+           'VbNetAspxLexer']
 
 
 def _escape(st):
             (r'[a-z_][a-z0-9_.]*', Name.Namespace, '#pop')
         ],
     }
+
+class GenericAspxLexer(RegexLexer):
+    """
+    Lexer for ASP.NET pages.
+    """
+
+    name = 'aspx-gen'
+    filenames = []
+    mimetypes = []
+
+    flags = re.DOTALL
+
+    tokens = {
+        'root': [
+            (r'(<%[@=#]?)(.*?)(%>)', bygroups(Name.Tag, Other, Name.Tag)),
+            (r'(<script.*?>)(.*?)(</script>)', bygroups(using(XmlLexer),
+                                                        Other,
+                                                        using(XmlLexer))),
+            (r'(.+?)(?=<)', using(XmlLexer)),
+            (r'.+', using(XmlLexer)),
+        ],
+    }
+
+#TODO support multiple languages within the same source file
+class CSharpAspxLexer(DelegatingLexer):
+    """
+    Lexer for highligting C# within ASP.NET pages.
+    """
+
+    name = 'aspx-cs'
+    aliases = ['aspx-cs']
+    filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
+    mimetypes = []
+
+    def __init__(self, **options):
+        super(CSharpAspxLexer, self).__init__(CSharpLexer,GenericAspxLexer,
+                                              **options)
+
+    def analyse_text(text):
+        if re.search(r'Page\s*Language="C#"', text, re.I) is not None:
+            return 0.2
+        elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None:
+            return 0.15
+        return 0.001 # TODO really only for when filename matched...
+
+class VbNetAspxLexer(DelegatingLexer):
+    """
+    Lexer for highligting Visual Basic.net within ASP.NET pages.
+    """
+
+    name = 'aspx-vb'
+    aliases = ['aspx-vb']
+    filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
+    mimetypes = []
+
+    def __init__(self, **options):
+        super(VbNetAspxLexer, self).__init__(VbNetLexer,GenericAspxLexer,
+                                              **options)
+
+    def analyse_text(text):
+        if re.search(r'Page\s*Language="Vb"', text, re.I) is not None:
+            return 0.2
+        elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None:
+            return 0.15

File tests/examplefiles/example.aspx

+<%@ Page Language="C#" %>
+ 
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+ 
+<script runat="server">
+ 
+    protected void Page_Load(object sender, EventArgs e)
+    {
+        Label1.Text = DateTime.Now.ToLongDateString();
+    }
+ 
+</script>
+ 
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head runat="server">
+    <title>Sample page</title>
+</head>
+<body>
+    <form id="form1" runat="server">
+    <div>
+        The current time is: <asp:Label runat="server" id="Label1" />
+    </div>
+    </form>
+ 
+</body>
+</html>

File tests/examplefiles/example2.aspx

+<%@ Register TagPrefix="Acme" TagName="Message" Src="userctrl2_vb.ascx" %>
+
+<html>
+
+  <script language="VB" runat="server">
+
+      Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
+          MyMessage.MessageText = "Message text changed!"
+          MyMessage.Color = "red"
+      End Sub
+
+  </script>
+
+<body style="font: 10pt verdana">
+
+  <h3>A Simple User Control w/ Properties</h3>
+
+  <form runat="server">
+
+    <Acme:Message id="MyMessage" MessageText="This is a custom message!" Color="blue" runat="server"/>
+
+    <p>
+
+    <asp:button text="Change Properties" OnClick="SubmitBtn_Click" runat=server/>
+
+  </form>
+
+</body>
+</html>