File: templatablepattern.py

package info (click to toggle)
python-jsbeautifier 1.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 756 kB
  • sloc: python: 13,140; sh: 14; makefile: 7
file content (192 lines) | stat: -rw-r--r-- 7,809 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# The MIT License (MIT)
#
# Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import copy
from ..core.pattern import Pattern

__all__ = ["TemplatablePattern"]


class TemplateNames:
    def __init__(self):
        self.django = False
        self.erb = False
        self.handlebars = False
        self.php = False
        self.smarty = False
        self.angular = False


class TemplatePatterns:
    def __init__(self, input_scanner):
        pattern = Pattern(input_scanner)
        self.handlebars_comment = pattern.starting_with(r"{{!--").until_after(r"--}}")
        self.handlebars_unescaped = pattern.starting_with(r"{{{").until_after(r"}}}")
        self.handlebars = pattern.starting_with(r"{{").until_after(r"}}")
        self.php = pattern.starting_with(r"<\?(?:[= ]|php)").until_after(r"\?>")
        self.erb = pattern.starting_with(r"<%[^%]").until_after(r"[^%]%>")
        # django coflicts with handlebars a bit.
        self.django = pattern.starting_with(r"{%").until_after(r"%}")
        self.django_value = pattern.starting_with(r"{{").until_after(r"}}")
        self.django_comment = pattern.starting_with(r"{#").until_after(r"#}")
        self.smarty_value = pattern.starting_with(r"{(?=[^}{\s\n])").until_after(r"}")
        self.smarty_comment = pattern.starting_with(r"{\*").until_after(r"\*}")
        self.smarty_literal = pattern.starting_with(r"{literal}").until_after(
            r"{/literal}"
        )


class TemplatablePattern(Pattern):
    def __init__(self, input_scanner, parent=None):
        Pattern.__init__(self, input_scanner, parent)
        self.__template_pattern = None
        self._disabled = TemplateNames()
        self._excluded = TemplateNames()

        if parent is not None:
            self.__template_pattern = self._input.get_regexp(parent.__template_pattern)
            self._disabled = copy.copy(parent._disabled)
            self._excluded = copy.copy(parent._excluded)

        self.__patterns = TemplatePatterns(input_scanner)

    def _create(self):
        return TemplatablePattern(self._input, self)

    def _update(self):
        self.__set_templated_pattern()

    def read_options(self, options):
        result = self._create()
        for language in ["django", "erb", "handlebars", "php", "smarty", "angular"]:
            setattr(result._disabled, language, not (language in options.templating))
        result._update()
        return result

    def disable(self, language):
        result = self._create()
        setattr(result._disabled, language, True)
        result._update()
        return result

    def exclude(self, language):
        result = self._create()
        setattr(result._excluded, language, True)
        result._update()
        return result

    def read(self):
        result = ""
        if bool(self._match_pattern):
            result = self._input.read(self._starting_pattern)
        else:
            result = self._input.read(self._starting_pattern, self.__template_pattern)

        next = self._read_template()

        while bool(next):
            if self._match_pattern is not None:
                next += self._input.read(self._match_pattern)
            else:
                next += self._input.readUntil(self.__template_pattern)

            result += next
            next = self._read_template()

        if self._until_after:
            result += self._input.readUntilAfter(self._until_pattern)

        return result

    def __set_templated_pattern(self):
        items = list()

        if not self._disabled.php:
            items.append(self.__patterns.php._starting_pattern.pattern)

        if not self._disabled.handlebars:
            items.append(self.__patterns.handlebars._starting_pattern.pattern)

        if not self._disabled.erb:
            items.append(self.__patterns.erb._starting_pattern.pattern)

        if not self._disabled.django:
            items.append(self.__patterns.django._starting_pattern.pattern)
            # The starting pattern for django is more complex because it has different
            # patterns for value, comment, and other sections
            items.append(self.__patterns.django_value._starting_pattern.pattern)
            items.append(self.__patterns.django_comment._starting_pattern.pattern)

        if not self._disabled.smarty:
            items.append(self.__patterns.smarty._starting_pattern.pattern)

        if self._until_pattern:
            items.append(self._until_pattern.pattern)

        self.__template_pattern = self._input.get_regexp(r"(?:" + "|".join(items) + ")")

    def _read_template(self):
        resulting_string = ""
        c = self._input.peek()
        if c == "<":
            peek1 = self._input.peek(1)
            if not self._disabled.php and not self._excluded.php and peek1 == "?":
                resulting_string = resulting_string or self.__patterns.php.read()

            if not self._disabled.erb and not self._excluded.erb and peek1 == "%":
                resulting_string = resulting_string or self.__patterns.erb.read()
        elif c == "{":
            if not self._disabled.handlebars and not self._excluded.handlebars:
                resulting_string = (
                    resulting_string or self.__patterns.handlebars_comment.read()
                )
                resulting_string = (
                    resulting_string or self.__patterns.handlebars_unescaped.read()
                )
                resulting_string = resulting_string or self.__patterns.handlebars.read()
            if not self._disabled.django:
                # django coflicts with handlebars a bit.
                if not self._excluded.django and not self._excluded.handlebars:
                    resulting_string = (
                        resulting_string or self.__patterns.django_value.read()
                    )
                if not self._excluded.django:
                    resulting_string = (
                        resulting_string or self.__patterns.django_comment.read()
                    )
                    resulting_string = resulting_string or self.__patterns.django.read()
            if not self._disabled.smarty:
                # smarty cannot be enabled with django or handlebars enabled

                if self._disabled.django and self._disabled.handlebars:
                    resulting_string = (
                        resulting_string or self.__patterns.smarty_comment.read()
                    )
                    resulting_string = (
                        resulting_string or self.__patterns.smarty_literal.read()
                    )

                    resulting_string = resulting_string or self.__patterns.smarty.read()

        return resulting_string