summaryrefslogtreecommitdiff
path: root/mako/ext/pygmentplugin.py
blob: 9acbf4cd67a4356527ad8fb9f48dec89365c0cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# ext/pygmentplugin.py
# Copyright 2006-2022 the Mako authors and contributors <see AUTHORS file>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexer import bygroups
from pygments.lexer import DelegatingLexer
from pygments.lexer import include
from pygments.lexer import RegexLexer
from pygments.lexer import using
from pygments.lexers.agile import Python3Lexer
from pygments.lexers.agile import PythonLexer
from pygments.lexers.web import CssLexer
from pygments.lexers.web import HtmlLexer
from pygments.lexers.web import JavascriptLexer
from pygments.lexers.web import XmlLexer
from pygments.token import Comment
from pygments.token import Keyword
from pygments.token import Name
from pygments.token import Operator
from pygments.token import Other
from pygments.token import String
from pygments.token import Text


class MakoLexer(RegexLexer):
    name = "Mako"
    aliases = ["mako"]
    filenames = ["*.mao"]

    tokens = {
        "root": [
            (
                r"(\s*)(\%)(\s*end(?:\w+))(\n|\Z)",
                bygroups(Text, Comment.Preproc, Keyword, Other),
            ),
            (
                r"(\s*)(\%(?!%))([^\n]*)(\n|\Z)",
                bygroups(Text, Comment.Preproc, using(PythonLexer), Other),
            ),
            (
                r"(\s*)(##[^\n]*)(\n|\Z)",
                bygroups(Text, Comment.Preproc, Other),
            ),
            (r"""(?s)<%doc>.*?</%doc>""", Comment.Preproc),
            (
                r"(<%)([\w\.\:]+)",
                bygroups(Comment.Preproc, Name.Builtin),
                "tag",
            ),
            (
                r"(</%)([\w\.\:]+)(>)",
                bygroups(Comment.Preproc, Name.Builtin, Comment.Preproc),
            ),
            (r"<%(?=([\w\.\:]+))", Comment.Preproc, "ondeftags"),
            (
                r"(?s)(<%(?:!?))(.*?)(%>)",
                bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc),
            ),
            (
                r"(\$\{)(.*?)(\})",
                bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc),
            ),
            (
                r"""(?sx)
                (.+?)               # anything, followed by:
                (?:
                 (?<=\n)(?=%(?!%)|\#\#) |  # an eval or comment line
                 (?=\#\*) |          # multiline comment
                 (?=</?%) |         # a python block
                                    # call start or end
                 (?=\$\{) |         # a substitution
                 (?<=\n)(?=\s*%) |
                                    # - don't consume
                 (\\\n) |           # an escaped newline
                 \Z                 # end of string
                )
            """,
                bygroups(Other, Operator),
            ),
            (r"\s+", Text),
        ],
        "ondeftags": [
            (r"<%", Comment.Preproc),
            (r"(?<=<%)(include|inherit|namespace|page)", Name.Builtin),
            include("tag"),
        ],
        "tag": [
            (r'((?:\w+)\s*=)\s*(".*?")', bygroups(Name.Attribute, String)),
            (r"/?\s*>", Comment.Preproc, "#pop"),
            (r"\s+", Text),
        ],
        "attr": [
            ('".*?"', String, "#pop"),
            ("'.*?'", String, "#pop"),
            (r"[^\s>]+", String, "#pop"),
        ],
    }


class MakoHtmlLexer(DelegatingLexer):
    name = "HTML+Mako"
    aliases = ["html+mako"]

    def __init__(self, **options):
        super().__init__(HtmlLexer, MakoLexer, **options)


class MakoXmlLexer(DelegatingLexer):
    name = "XML+Mako"
    aliases = ["xml+mako"]

    def __init__(self, **options):
        super().__init__(XmlLexer, MakoLexer, **options)


class MakoJavascriptLexer(DelegatingLexer):
    name = "JavaScript+Mako"
    aliases = ["js+mako", "javascript+mako"]

    def __init__(self, **options):
        super().__init__(JavascriptLexer, MakoLexer, **options)


class MakoCssLexer(DelegatingLexer):
    name = "CSS+Mako"
    aliases = ["css+mako"]

    def __init__(self, **options):
        super().__init__(CssLexer, MakoLexer, **options)


pygments_html_formatter = HtmlFormatter(
    cssclass="syntax-highlighted", linenos=True
)


def syntax_highlight(filename="", language=None):
    mako_lexer = MakoLexer()
    python_lexer = Python3Lexer()
    if filename.startswith("memory:") or language == "mako":
        return lambda string: highlight(
            string, mako_lexer, pygments_html_formatter
        )
    return lambda string: highlight(
        string, python_lexer, pygments_html_formatter
    )