~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/mako/ext/pygmentplugin.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import re
 
2
try:
 
3
    set
 
4
except NameError:
 
5
    from sets import Set as set
 
6
 
 
7
from pygments.lexers.web import \
 
8
     HtmlLexer, XmlLexer, JavascriptLexer, CssLexer
 
9
from pygments.lexers.agile import PythonLexer
 
10
from pygments.lexer import Lexer, DelegatingLexer, RegexLexer, bygroups, \
 
11
     include, using, this
 
12
from pygments.token import Error, Punctuation, \
 
13
     Text, Comment, Operator, Keyword, Name, String, Number, Other, Literal
 
14
from pygments.util import html_doctype_matches, looks_like_xml
 
15
 
 
16
class MakoLexer(RegexLexer):
 
17
    name = 'Mako'
 
18
    aliases = ['mako']
 
19
    filenames = ['*.mao']
 
20
 
 
21
    tokens = {
 
22
        'root': [
 
23
            (r'(\s*)(\%)(\s*end(?:\w+))(\n|\Z)',
 
24
             bygroups(Text, Comment.Preproc, Keyword, Other)),
 
25
            (r'(\s*)(\%)([^\n]*)(\n|\Z)',
 
26
             bygroups(Text, Comment.Preproc, using(PythonLexer), Other)),
 
27
             (r'(\s*)(##[^\n]*)(\n|\Z)',
 
28
              bygroups(Text, Comment.Preproc, Other)),
 
29
              (r'''(?s)<%doc>.*?</%doc>''', Comment.Preproc),
 
30
            (r'(<%)(def|call|namespace|text)', bygroups(Comment.Preproc, Name.Builtin), 'tag'),
 
31
            (r'(</%)(def|call|namespace|text)(>)', bygroups(Comment.Preproc, Name.Builtin, Comment.Preproc)),
 
32
            (r'<%(?=(include|inherit|namespace|page))', Comment.Preproc, 'ondeftags'),
 
33
            (r'(<%(?:!?))(.*?)(%>)(?s)', bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
 
34
            (r'(\$\{)(.*?)(\})',
 
35
             bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
 
36
            (r'''(?sx)
 
37
                (.+?)               # anything, followed by:
 
38
                (?:
 
39
                 (?<=\n)(?=%|\#\#) |  # an eval or comment line
 
40
                 (?=\#\*) |          # multiline comment
 
41
                 (?=</?%) |         # a python block
 
42
                                    # call start or end
 
43
                 (?=\$\{) |         # a substitution
 
44
                 (?<=\n)(?=\s*%) |
 
45
                                    # - don't consume
 
46
                 (\\\n) |           # an escaped newline
 
47
                 \Z                 # end of string
 
48
                )
 
49
            ''', bygroups(Other, Operator)),
 
50
            (r'\s+', Text),
 
51
        ],
 
52
        'ondeftags': [
 
53
            (r'<%', Comment.Preproc),
 
54
            (r'(?<=<%)(include|inherit|namespace|page)', Name.Builtin),
 
55
            include('tag'),
 
56
        ],
 
57
        'tag': [
 
58
            (r'((?:\w+)\s*=)\s*(".*?")',
 
59
             bygroups(Name.Attribute, String)),
 
60
            (r'/?\s*>', Comment.Preproc, '#pop'),
 
61
            (r'\s+', Text),
 
62
        ],
 
63
        'attr': [
 
64
            ('".*?"', String, '#pop'),
 
65
            ("'.*?'", String, '#pop'),
 
66
            (r'[^\s>]+', String, '#pop'),
 
67
        ],
 
68
    }
 
69
 
 
70
 
 
71
class MakoHtmlLexer(DelegatingLexer):
 
72
    name = 'HTML+Mako'
 
73
    aliases = ['html+mako']
 
74
 
 
75
    def __init__(self, **options):
 
76
        super(MakoHtmlLexer, self).__init__(HtmlLexer, MakoLexer,
 
77
                                              **options)
 
78
 
 
79
class MakoXmlLexer(DelegatingLexer):
 
80
    name = 'XML+Mako'
 
81
    aliases = ['xml+mako']
 
82
 
 
83
    def __init__(self, **options):
 
84
        super(MakoXmlLexer, self).__init__(XmlLexer, MakoLexer,
 
85
                                             **options)
 
86
 
 
87
class MakoJavascriptLexer(DelegatingLexer):
 
88
    name = 'JavaScript+Mako'
 
89
    aliases = ['js+mako', 'javascript+mako']
 
90
 
 
91
    def __init__(self, **options):
 
92
        super(MakoJavascriptLexer, self).__init__(JavascriptLexer,
 
93
                                                    MakoLexer, **options)
 
94
 
 
95
class MakoCssLexer(DelegatingLexer):
 
96
    name = 'CSS+Mako'
 
97
    aliases = ['css+mako']
 
98
 
 
99
    def __init__(self, **options):
 
100
        super(MakoCssLexer, self).__init__(CssLexer, MakoLexer,
 
101
                                             **options)