python-liquid 2.3.6__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- liquid/__about__.py +1 -0
- liquid/__init__.py +161 -187
- liquid/_base_tag.py +66 -0
- liquid/{utils/chain_map.py → _chain_map.py} +18 -19
- liquid/_context.py +465 -0
- liquid/_drop.py +108 -0
- liquid/_filter.py +174 -0
- liquid/_io.py +18 -0
- liquid/_lax_parser_mixin.py +100 -0
- liquid/_lexer.py +93 -0
- liquid/_lexer_extended.py +623 -0
- liquid/_lexer_legacy.py +536 -0
- liquid/_loader.py +84 -0
- liquid/{utils/lru_cache.py → _lru_cache.py} +28 -36
- liquid/_markup.py +253 -0
- liquid/_messages.py +292 -0
- liquid/_missing.py +3 -0
- liquid/_nothing.py +28 -0
- liquid/_parser.py +460 -0
- liquid/_parser_extended.py +293 -0
- liquid/_parser_legacy.py +819 -0
- liquid/_resource_limits.py +50 -0
- liquid/_slice.py +41 -0
- liquid/_static_analysis.py +503 -0
- liquid/_strftime.py +186 -0
- liquid/_template.py +536 -0
- liquid/drops/__init__.py +20 -0
- liquid/drops/forloop.py +78 -0
- liquid/drops/iterable.py +25 -0
- liquid/drops/range.py +76 -0
- liquid/drops/self_.py +45 -0
- liquid/drops/tablerowloop.py +142 -0
- liquid/drops/undefined.py +228 -0
- liquid/environment.py +717 -551
- liquid/exceptions.py +67 -214
- liquid/expressions.py +1184 -0
- liquid/filters/__init__.py +134 -0
- liquid/filters/abs.py +7 -0
- liquid/filters/append.py +5 -0
- liquid/filters/at_least.py +9 -0
- liquid/filters/at_most.py +9 -0
- liquid/filters/babel.py +560 -0
- liquid/filters/base64.py +26 -0
- liquid/filters/capitalize.py +5 -0
- liquid/filters/ceil.py +8 -0
- liquid/filters/compact.py +25 -0
- liquid/filters/concat.py +11 -0
- liquid/filters/date.py +27 -0
- liquid/filters/default.py +25 -0
- liquid/filters/divided_by.py +21 -0
- liquid/filters/downcase.py +5 -0
- liquid/filters/escape.py +56 -0
- liquid/filters/find.py +56 -0
- liquid/filters/first.py +19 -0
- liquid/filters/floor.py +8 -0
- liquid/filters/html.py +23 -0
- liquid/filters/index.py +12 -0
- liquid/filters/join.py +13 -0
- liquid/filters/json_.py +30 -0
- liquid/filters/last.py +17 -0
- liquid/filters/map.py +5 -0
- liquid/filters/minus.py +13 -0
- liquid/filters/modulo.py +18 -0
- liquid/filters/newline_to_br.py +16 -0
- liquid/filters/plus.py +13 -0
- liquid/filters/prepend.py +5 -0
- liquid/filters/reject.py +19 -0
- liquid/filters/remove.py +36 -0
- liquid/filters/replace.py +32 -0
- liquid/filters/reverse.py +5 -0
- liquid/filters/round.py +35 -0
- liquid/filters/size.py +25 -0
- liquid/filters/slice.py +39 -0
- liquid/filters/sort.py +148 -0
- liquid/filters/split.py +34 -0
- liquid/filters/squish.py +9 -0
- liquid/filters/strip.py +13 -0
- liquid/filters/strip_html.py +19 -0
- liquid/filters/strip_newlines.py +17 -0
- liquid/filters/sum.py +21 -0
- liquid/filters/times.py +13 -0
- liquid/filters/translate.py +380 -0
- liquid/filters/truncate.py +39 -0
- liquid/filters/uniq.py +11 -0
- liquid/filters/upcase.py +5 -0
- liquid/filters/url.py +18 -0
- liquid/filters/where.py +18 -0
- liquid/loaders/__init__.py +17 -0
- liquid/loaders/caching_file_system_loader.py +64 -0
- liquid/loaders/choice_loader.py +93 -0
- liquid/loaders/dict_loader.py +56 -0
- liquid/{builtin/loaders → loaders}/file_system_loader.py +51 -55
- liquid/{builtin/loaders → loaders}/mixins.py +39 -51
- liquid/loaders/package_loader.py +110 -0
- liquid/tags/__init__.py +61 -0
- liquid/tags/assign.py +68 -0
- liquid/tags/capture.py +83 -0
- liquid/tags/case.py +199 -0
- liquid/tags/comment.py +56 -0
- liquid/tags/cycle.py +131 -0
- liquid/tags/decrement.py +55 -0
- liquid/tags/doc.py +56 -0
- liquid/tags/echo.py +65 -0
- liquid/tags/else_.py +53 -0
- liquid/tags/error.py +51 -0
- liquid/tags/extends.py +448 -0
- liquid/tags/for_.py +421 -0
- liquid/tags/if_.py +156 -0
- liquid/tags/ifchanged.py +69 -0
- liquid/tags/include.py +223 -0
- liquid/tags/increment.py +55 -0
- liquid/tags/inline_comment.py +70 -0
- liquid/tags/liquid.py +56 -0
- liquid/tags/macro.py +281 -0
- liquid/tags/output.py +58 -0
- liquid/tags/raw.py +60 -0
- liquid/tags/render.py +245 -0
- liquid/tags/tablerow.py +254 -0
- liquid/tags/template_comment.py +56 -0
- liquid/tags/translate.py +316 -0
- liquid/tags/unless.py +121 -0
- liquid/tags/with_.py +83 -0
- liquid/tokens.py +370 -0
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/METADATA +32 -39
- python_liquid-3.0.0.dist-info/RECORD +128 -0
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/WHEEL +1 -1
- {python_liquid-2.3.6.dist-info → python_liquid-3.0.0.dist-info}/licenses/LICENSE +1 -1
- liquid/analyze_tags.py +0 -214
- liquid/ast.py +0 -252
- liquid/builtin/__init__.py +0 -203
- liquid/builtin/content.py +0 -41
- liquid/builtin/drops/__init__.py +0 -3
- liquid/builtin/drops/drops.py +0 -13
- liquid/builtin/expressions/__init__.py +0 -47
- liquid/builtin/expressions/_tokenize.py +0 -186
- liquid/builtin/expressions/arguments.py +0 -207
- liquid/builtin/expressions/filtered.py +0 -387
- liquid/builtin/expressions/logical.py +0 -672
- liquid/builtin/expressions/loop.py +0 -275
- liquid/builtin/expressions/path.py +0 -163
- liquid/builtin/expressions/primitive.py +0 -417
- liquid/builtin/filters/__init__.py +0 -0
- liquid/builtin/filters/array.py +0 -329
- liquid/builtin/filters/extra.py +0 -83
- liquid/builtin/filters/math.py +0 -129
- liquid/builtin/filters/misc.py +0 -137
- liquid/builtin/filters/string.py +0 -416
- liquid/builtin/illegal.py +0 -28
- liquid/builtin/loaders/__init__.py +0 -0
- liquid/builtin/loaders/caching_file_system_loader.py +0 -61
- liquid/builtin/loaders/choice_loader.py +0 -94
- liquid/builtin/loaders/dict_loader.py +0 -64
- liquid/builtin/loaders/package_loader.py +0 -132
- liquid/builtin/output.py +0 -74
- liquid/builtin/tags/__init__.py +0 -0
- liquid/builtin/tags/assign_tag.py +0 -75
- liquid/builtin/tags/capture_tag.py +0 -98
- liquid/builtin/tags/case_tag.py +0 -287
- liquid/builtin/tags/comment_tag.py +0 -77
- liquid/builtin/tags/cycle_tag.py +0 -132
- liquid/builtin/tags/decrement_tag.py +0 -59
- liquid/builtin/tags/doc_tag.py +0 -68
- liquid/builtin/tags/echo_tag.py +0 -45
- liquid/builtin/tags/for_tag.py +0 -337
- liquid/builtin/tags/if_tag.py +0 -209
- liquid/builtin/tags/ifchanged_tag.py +0 -94
- liquid/builtin/tags/include_tag.py +0 -251
- liquid/builtin/tags/increment_tag.py +0 -60
- liquid/builtin/tags/inline_comment_tag.py +0 -47
- liquid/builtin/tags/liquid_tag.py +0 -183
- liquid/builtin/tags/render_tag.py +0 -371
- liquid/builtin/tags/tablerow_tag.py +0 -320
- liquid/builtin/tags/unless_tag.py +0 -218
- liquid/context.py +0 -615
- liquid/expression.py +0 -41
- liquid/extra/__init__.py +0 -98
- liquid/extra/filters/__init__.py +0 -33
- liquid/extra/filters/_json.py +0 -33
- liquid/extra/filters/array.py +0 -60
- liquid/extra/filters/babel.py +0 -552
- liquid/extra/filters/html.py +0 -32
- liquid/extra/filters/translate.py +0 -448
- liquid/extra/tags/__init__.py +0 -17
- liquid/extra/tags/_with.py +0 -80
- liquid/extra/tags/extends_tag.py +0 -576
- liquid/extra/tags/macro_tag.py +0 -270
- liquid/extra/tags/snippet_tag.py +0 -107
- liquid/extra/tags/translate_tag.py +0 -388
- liquid/filter.py +0 -230
- liquid/future/__init__.py +0 -3
- liquid/future/environment.py +0 -30
- liquid/future/filters/__init__.py +0 -1
- liquid/future/tags/__init__.py +0 -1
- liquid/lex.py +0 -260
- liquid/limits.py +0 -66
- liquid/loader.py +0 -141
- liquid/messages.py +0 -357
- liquid/mode.py +0 -12
- liquid/output.py +0 -35
- liquid/parser.py +0 -119
- liquid/span.py +0 -35
- liquid/static_analysis.py +0 -443
- liquid/stream.py +0 -148
- liquid/stringify.py +0 -31
- liquid/tag.py +0 -47
- liquid/template.py +0 -658
- liquid/token.py +0 -145
- liquid/undefined.py +0 -218
- liquid/utils/__init__.py +0 -15
- liquid/utils/html.py +0 -57
- liquid/utils/text.py +0 -28
- python_liquid-2.3.6.dist-info/RECORD +0 -96
liquid/tags/include.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from .._markup import SCOPE_SHARED, Partial
|
|
6
|
+
from .._nothing import NOTHING
|
|
7
|
+
from ..exceptions import NoSuchTemplateError, TemplateNotFoundError
|
|
8
|
+
from ..expressions import Name, StringLiteral
|
|
9
|
+
from ..tokens import TOKEN_COMMA, TOKEN_TAG_END
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from .._context import RenderContext
|
|
13
|
+
from .._io import StringBuffer
|
|
14
|
+
from .._markup import Expression, Markup
|
|
15
|
+
from .._parser import Parser
|
|
16
|
+
from ..expressions import KeywordArgument
|
|
17
|
+
from ..tokens import Token
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class IncludeTag:
|
|
21
|
+
blank = False
|
|
22
|
+
tag = "include"
|
|
23
|
+
|
|
24
|
+
__slots__ = ("alias", "args", "name", "token", "variable")
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
token: Token,
|
|
29
|
+
name: Expression,
|
|
30
|
+
variable: Expression | None,
|
|
31
|
+
alias: Name | None,
|
|
32
|
+
args: list[KeywordArgument],
|
|
33
|
+
) -> None:
|
|
34
|
+
self.token = token
|
|
35
|
+
self.name = name
|
|
36
|
+
self.variable = variable
|
|
37
|
+
self.alias = alias
|
|
38
|
+
self.args = args
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
42
|
+
name_expr = parser.parse_expression()
|
|
43
|
+
|
|
44
|
+
variable: Expression | None = None
|
|
45
|
+
alias: Name | None = None
|
|
46
|
+
|
|
47
|
+
# `for`, `with` and `as` don't have their own tokens kinds.
|
|
48
|
+
ident = parser.value()
|
|
49
|
+
if ident in ("for", "with"):
|
|
50
|
+
parser.next()
|
|
51
|
+
variable = parser.parse_expression()
|
|
52
|
+
|
|
53
|
+
if parser.value() == "as":
|
|
54
|
+
parser.next()
|
|
55
|
+
alias = parser.parse_identifier()
|
|
56
|
+
|
|
57
|
+
if parser.kind() == TOKEN_COMMA:
|
|
58
|
+
parser.next()
|
|
59
|
+
|
|
60
|
+
args = parser.parse_keyword_arguments(require_commas=False)
|
|
61
|
+
parser.carry_whitespace_control()
|
|
62
|
+
parser.eat(TOKEN_TAG_END)
|
|
63
|
+
return IncludeTag(token, name_expr, variable, alias, args)
|
|
64
|
+
|
|
65
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
66
|
+
template_name = context.env.to_str(
|
|
67
|
+
self.name.evaluate(context), context, self.name.span
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
template = context.env.get_template(
|
|
72
|
+
template_name, context=context, tag="include"
|
|
73
|
+
)
|
|
74
|
+
except TemplateNotFoundError as err:
|
|
75
|
+
# Promote TemplateNotFoundError to NoSuchTemplateError
|
|
76
|
+
raise NoSuchTemplateError(
|
|
77
|
+
str(err),
|
|
78
|
+
token=self.name.span,
|
|
79
|
+
source=context.template.source,
|
|
80
|
+
name=context.template.name,
|
|
81
|
+
) from None
|
|
82
|
+
|
|
83
|
+
bind_key = self.alias.value if self.alias else template.name.split(".", 1)[0]
|
|
84
|
+
bind_value: object
|
|
85
|
+
|
|
86
|
+
if self.variable:
|
|
87
|
+
bind_value = self.variable.evaluate(context)
|
|
88
|
+
else:
|
|
89
|
+
bind_value = context.resolve(template_name)
|
|
90
|
+
if bind_value == NOTHING:
|
|
91
|
+
bind_value = None
|
|
92
|
+
|
|
93
|
+
scope: dict[str, object] = {}
|
|
94
|
+
|
|
95
|
+
with context.extend(scope, template=template):
|
|
96
|
+
for arg in self.args:
|
|
97
|
+
scope[arg.name.value] = arg.expr.evaluate(context)
|
|
98
|
+
|
|
99
|
+
# TODO: support __liquid_iter__ too?
|
|
100
|
+
if isinstance(bind_value, list):
|
|
101
|
+
context.raise_for_loop_limit(len(bind_value)) # pyright: ignore[reportUnknownArgumentType]
|
|
102
|
+
|
|
103
|
+
for item in bind_value: # pyright: ignore[reportUnknownVariableType]
|
|
104
|
+
scope[bind_key] = item
|
|
105
|
+
template.render_with_context(context, buffer)
|
|
106
|
+
else:
|
|
107
|
+
scope[bind_key] = bind_value
|
|
108
|
+
template.render_with_context(context, buffer)
|
|
109
|
+
|
|
110
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
111
|
+
template_name = context.env.to_str(
|
|
112
|
+
await self.name.evaluate_async(context), context, self.name.span
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
template = await context.env.get_template_async(
|
|
117
|
+
template_name, context=context, tag="include"
|
|
118
|
+
)
|
|
119
|
+
except TemplateNotFoundError as err:
|
|
120
|
+
# Promote TemplateNotFoundError to NoSuchTemplateError
|
|
121
|
+
raise NoSuchTemplateError(
|
|
122
|
+
str(err),
|
|
123
|
+
token=self.name.span,
|
|
124
|
+
source=context.template.source,
|
|
125
|
+
name=context.template.name,
|
|
126
|
+
) from None
|
|
127
|
+
|
|
128
|
+
bind_key = self.alias.value if self.alias else template.name.split(".", 1)[0]
|
|
129
|
+
bind_value: object
|
|
130
|
+
|
|
131
|
+
if self.variable:
|
|
132
|
+
bind_value = await self.variable.evaluate_async(context)
|
|
133
|
+
else:
|
|
134
|
+
bind_value = context.resolve(template_name)
|
|
135
|
+
if bind_value == NOTHING:
|
|
136
|
+
bind_value = None
|
|
137
|
+
|
|
138
|
+
scope: dict[str, object] = {}
|
|
139
|
+
|
|
140
|
+
with context.extend(scope, template=template):
|
|
141
|
+
for arg in self.args:
|
|
142
|
+
scope[arg.name.value] = await arg.expr.evaluate_async(context)
|
|
143
|
+
|
|
144
|
+
# TODO: support __liquid_iter__ too?
|
|
145
|
+
if isinstance(bind_value, list):
|
|
146
|
+
context.raise_for_loop_limit(len(bind_value)) # pyright: ignore[reportUnknownArgumentType]
|
|
147
|
+
|
|
148
|
+
for item in bind_value: # pyright: ignore[reportUnknownVariableType]
|
|
149
|
+
scope[bind_key] = item
|
|
150
|
+
await template.render_with_context_async(context, buffer)
|
|
151
|
+
else:
|
|
152
|
+
scope[bind_key] = bind_value
|
|
153
|
+
await template.render_with_context_async(context, buffer)
|
|
154
|
+
|
|
155
|
+
def children(self) -> list[Markup]:
|
|
156
|
+
return []
|
|
157
|
+
|
|
158
|
+
def expressions(self) -> list[Expression]:
|
|
159
|
+
result: list[Expression] = [self.name]
|
|
160
|
+
|
|
161
|
+
if self.variable:
|
|
162
|
+
result.append(self.variable)
|
|
163
|
+
|
|
164
|
+
result.extend(arg.expr for arg in self.args)
|
|
165
|
+
return result
|
|
166
|
+
|
|
167
|
+
def block_scope(self) -> list[Name]:
|
|
168
|
+
return []
|
|
169
|
+
|
|
170
|
+
def template_scope(self) -> list[Name]:
|
|
171
|
+
return []
|
|
172
|
+
|
|
173
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
174
|
+
name = self.name.evaluate(context)
|
|
175
|
+
|
|
176
|
+
template = context.env.get_template(
|
|
177
|
+
context.env.to_str(name, context, self.name.span),
|
|
178
|
+
context=context,
|
|
179
|
+
tag="include",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
scope: list[Name] = [arg.name for arg in self.args]
|
|
183
|
+
|
|
184
|
+
if self.variable:
|
|
185
|
+
if self.alias:
|
|
186
|
+
scope.append(self.alias)
|
|
187
|
+
elif isinstance(self.name, StringLiteral):
|
|
188
|
+
scope.append(Name(self.name.token, self.name.value))
|
|
189
|
+
|
|
190
|
+
return [
|
|
191
|
+
Partial(
|
|
192
|
+
template=template,
|
|
193
|
+
scope=SCOPE_SHARED,
|
|
194
|
+
in_scope=scope,
|
|
195
|
+
key=hash((str(self.name), ":".join(n.value for n in scope))),
|
|
196
|
+
)
|
|
197
|
+
]
|
|
198
|
+
|
|
199
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
200
|
+
name = await self.name.evaluate_async(context)
|
|
201
|
+
|
|
202
|
+
template = await context.env.get_template_async(
|
|
203
|
+
context.env.to_str(name, context, self.name.span),
|
|
204
|
+
context=context,
|
|
205
|
+
tag="include",
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
scope: list[Name] = [arg.name for arg in self.args]
|
|
209
|
+
|
|
210
|
+
if self.variable:
|
|
211
|
+
if self.alias:
|
|
212
|
+
scope.append(self.alias)
|
|
213
|
+
elif isinstance(self.name, StringLiteral):
|
|
214
|
+
scope.append(Name(self.name.token, self.name.value))
|
|
215
|
+
|
|
216
|
+
return [
|
|
217
|
+
Partial(
|
|
218
|
+
template=template,
|
|
219
|
+
scope=SCOPE_SHARED,
|
|
220
|
+
in_scope=scope,
|
|
221
|
+
key=hash((str(self.name), ":".join(n.value for n in scope))),
|
|
222
|
+
)
|
|
223
|
+
]
|
liquid/tags/increment.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from ..tokens import TOKEN_TAG_END
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from .._context import RenderContext
|
|
9
|
+
from .._io import StringBuffer
|
|
10
|
+
from .._markup import Expression, Markup, Partial
|
|
11
|
+
from .._parser import Parser
|
|
12
|
+
from ..expressions import Name
|
|
13
|
+
from ..tokens import Token
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class IncrementTag:
|
|
17
|
+
blank = False
|
|
18
|
+
tag = "increment"
|
|
19
|
+
|
|
20
|
+
__slots__ = ("name", "token")
|
|
21
|
+
|
|
22
|
+
def __init__(self, token: Token, name: Name) -> None:
|
|
23
|
+
self.token = token
|
|
24
|
+
self.name = name
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
28
|
+
name = parser.parse_identifier()
|
|
29
|
+
parser.carry_whitespace_control()
|
|
30
|
+
parser.eat(TOKEN_TAG_END)
|
|
31
|
+
return IncrementTag(token, name)
|
|
32
|
+
|
|
33
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
34
|
+
buffer.write(str(context.increment(self.name.value)))
|
|
35
|
+
|
|
36
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
37
|
+
buffer.write(str(context.increment(self.name.value)))
|
|
38
|
+
|
|
39
|
+
def children(self) -> list[Markup]:
|
|
40
|
+
return []
|
|
41
|
+
|
|
42
|
+
def expressions(self) -> list[Expression]:
|
|
43
|
+
return []
|
|
44
|
+
|
|
45
|
+
def block_scope(self) -> list[Name]:
|
|
46
|
+
return []
|
|
47
|
+
|
|
48
|
+
def template_scope(self) -> list[Name]:
|
|
49
|
+
return [self.name]
|
|
50
|
+
|
|
51
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
52
|
+
return []
|
|
53
|
+
|
|
54
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
55
|
+
return []
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from ..exceptions import LiquidSyntaxError
|
|
7
|
+
from ..tokens import TOKEN_COMMENT, TOKEN_TAG_END, token_value
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from .._context import RenderContext
|
|
11
|
+
from .._io import StringBuffer
|
|
12
|
+
from .._markup import Expression, Markup, Partial
|
|
13
|
+
from .._parser import Parser
|
|
14
|
+
from ..expressions import Name
|
|
15
|
+
from ..tokens import Token
|
|
16
|
+
|
|
17
|
+
RE_INVALID_INLINE_COMMENT = re.compile(r"\n\s*[^#\s]")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class InlineCommentTag:
|
|
21
|
+
blank = True
|
|
22
|
+
tag = "#"
|
|
23
|
+
|
|
24
|
+
__slots__ = ("text", "token")
|
|
25
|
+
|
|
26
|
+
def __init__(self, token: Token, text: str) -> None:
|
|
27
|
+
self.token = token
|
|
28
|
+
self.text = text
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
32
|
+
comment_token = parser.eat(TOKEN_COMMENT)
|
|
33
|
+
parser.carry_whitespace_control()
|
|
34
|
+
parser.eat(TOKEN_TAG_END)
|
|
35
|
+
|
|
36
|
+
comment_text = token_value(comment_token, parser.source)
|
|
37
|
+
|
|
38
|
+
if RE_INVALID_INLINE_COMMENT.search(comment_text):
|
|
39
|
+
raise LiquidSyntaxError(
|
|
40
|
+
"every line of an inline comment must start with a '#' character",
|
|
41
|
+
token=comment_token,
|
|
42
|
+
source=parser.source,
|
|
43
|
+
name=parser.template_name,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
return InlineCommentTag(token, comment_text)
|
|
47
|
+
|
|
48
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
def children(self) -> list[Markup]:
|
|
55
|
+
return []
|
|
56
|
+
|
|
57
|
+
def expressions(self) -> list[Expression]:
|
|
58
|
+
return []
|
|
59
|
+
|
|
60
|
+
def block_scope(self) -> list[Name]:
|
|
61
|
+
return []
|
|
62
|
+
|
|
63
|
+
def template_scope(self) -> list[Name]:
|
|
64
|
+
return []
|
|
65
|
+
|
|
66
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
67
|
+
return []
|
|
68
|
+
|
|
69
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
70
|
+
return []
|
liquid/tags/liquid.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from .._markup import is_blank_block, render_block, render_block_async
|
|
6
|
+
from ..tokens import TOKEN_TAG_END
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from .._context import RenderContext
|
|
10
|
+
from .._io import StringBuffer
|
|
11
|
+
from .._markup import Block, Expression, Markup, Partial
|
|
12
|
+
from .._parser import Parser
|
|
13
|
+
from ..expressions import Name
|
|
14
|
+
from ..tokens import Token
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class LiquidTag:
|
|
18
|
+
tag = "liquid"
|
|
19
|
+
|
|
20
|
+
__slots__ = ("blank", "block", "token")
|
|
21
|
+
|
|
22
|
+
def __init__(self, token: Token, block: Block) -> None:
|
|
23
|
+
self.token = token
|
|
24
|
+
self.block = block
|
|
25
|
+
self.blank = is_blank_block(block)
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
29
|
+
block = parser.parse_line_statements()
|
|
30
|
+
parser.carry_whitespace_control()
|
|
31
|
+
parser.eat(TOKEN_TAG_END)
|
|
32
|
+
return LiquidTag(token, block)
|
|
33
|
+
|
|
34
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
35
|
+
render_block(self.block, context, buffer)
|
|
36
|
+
|
|
37
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
38
|
+
await render_block_async(self.block, context, buffer)
|
|
39
|
+
|
|
40
|
+
def children(self) -> list[Markup]:
|
|
41
|
+
return [node for node in self.block if not isinstance(node, str)]
|
|
42
|
+
|
|
43
|
+
def expressions(self) -> list[Expression]:
|
|
44
|
+
return []
|
|
45
|
+
|
|
46
|
+
def block_scope(self) -> list[Name]:
|
|
47
|
+
return []
|
|
48
|
+
|
|
49
|
+
def template_scope(self) -> list[Name]:
|
|
50
|
+
return []
|
|
51
|
+
|
|
52
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
53
|
+
return []
|
|
54
|
+
|
|
55
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
56
|
+
return []
|
liquid/tags/macro.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from itertools import zip_longest
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
|
|
7
|
+
from .._markup import (
|
|
8
|
+
SCOPE_ISOLATED,
|
|
9
|
+
Partial,
|
|
10
|
+
render_block,
|
|
11
|
+
render_block_async,
|
|
12
|
+
)
|
|
13
|
+
from ..expressions import KeywordArgument, Name, Parameter
|
|
14
|
+
from ..tokens import TOKEN_COMMA, TOKEN_TAG_END, span
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from .._context import RenderContext
|
|
18
|
+
from .._io import StringBuffer
|
|
19
|
+
from .._markup import Block, Expression, Markup
|
|
20
|
+
from .._parser import Parser
|
|
21
|
+
from ..tokens import Token
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class Macro:
|
|
26
|
+
params: list[Parameter]
|
|
27
|
+
block: Block
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class MacroTag:
|
|
31
|
+
tag = "macro"
|
|
32
|
+
blank = True
|
|
33
|
+
|
|
34
|
+
__slots__ = ("block", "name", "params", "token")
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self, token: Token, name: Name, params: list[Parameter], block: Block
|
|
38
|
+
) -> None:
|
|
39
|
+
self.token = token
|
|
40
|
+
self.name = name
|
|
41
|
+
self.params = params
|
|
42
|
+
self.block = block
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
46
|
+
name = parser.parse_name()
|
|
47
|
+
|
|
48
|
+
if parser.kind() == TOKEN_COMMA:
|
|
49
|
+
# Leading commas are OK.
|
|
50
|
+
parser.next()
|
|
51
|
+
|
|
52
|
+
args = parser.parse_parameters(require_commas=True)
|
|
53
|
+
|
|
54
|
+
if parser.kind() == TOKEN_COMMA:
|
|
55
|
+
# Trailing commas are OK.
|
|
56
|
+
parser.next()
|
|
57
|
+
|
|
58
|
+
parser.carry_whitespace_control()
|
|
59
|
+
parser.eat(TOKEN_TAG_END)
|
|
60
|
+
|
|
61
|
+
block = parser.parse_block(("endmacro",))
|
|
62
|
+
parser.eat_empty_tag("endmacro")
|
|
63
|
+
return MacroTag(token, name, args, block)
|
|
64
|
+
|
|
65
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
66
|
+
macros: dict[str, Macro] = context.get_register("macros", register_callback)
|
|
67
|
+
macros[self.name.value] = Macro(self.params, self.block)
|
|
68
|
+
|
|
69
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
70
|
+
macros: dict[str, Macro] = context.get_register("macros", register_callback)
|
|
71
|
+
macros[self.name.value] = Macro(self.params, self.block)
|
|
72
|
+
|
|
73
|
+
def children(self) -> list[Markup]:
|
|
74
|
+
# We're treating macro blocks as partial templates so as to hook into the
|
|
75
|
+
# isolated scope behavior found in rendered partial templates.
|
|
76
|
+
return []
|
|
77
|
+
|
|
78
|
+
def expressions(self) -> list[Expression]:
|
|
79
|
+
return [param.expr for param in self.params if param.expr]
|
|
80
|
+
|
|
81
|
+
def block_scope(self) -> list[Name]:
|
|
82
|
+
return []
|
|
83
|
+
|
|
84
|
+
def template_scope(self) -> list[Name]:
|
|
85
|
+
return []
|
|
86
|
+
|
|
87
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
88
|
+
# Pretend this is a partial template so we get an isolated scope.
|
|
89
|
+
return [
|
|
90
|
+
Partial(
|
|
91
|
+
template=context.template,
|
|
92
|
+
scope=SCOPE_ISOLATED,
|
|
93
|
+
in_scope=[param.name for param in self.params],
|
|
94
|
+
key=hash(self.name.value),
|
|
95
|
+
)
|
|
96
|
+
]
|
|
97
|
+
|
|
98
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
99
|
+
return self.partials(context)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class CallTag:
|
|
103
|
+
tag = "call"
|
|
104
|
+
blank = False
|
|
105
|
+
|
|
106
|
+
__slots__ = ("args", "name", "token")
|
|
107
|
+
|
|
108
|
+
def __init__(
|
|
109
|
+
self, token: Token, name: Name, args: list[Expression | KeywordArgument]
|
|
110
|
+
) -> None:
|
|
111
|
+
self.token = token
|
|
112
|
+
self.name = name
|
|
113
|
+
self.args = args
|
|
114
|
+
|
|
115
|
+
@staticmethod
|
|
116
|
+
def parse(token: Token, parser: Parser) -> Markup:
|
|
117
|
+
name = parser.parse_name()
|
|
118
|
+
|
|
119
|
+
if parser.kind() == TOKEN_COMMA:
|
|
120
|
+
# Leading commas are OK.
|
|
121
|
+
parser.next()
|
|
122
|
+
|
|
123
|
+
args = parser.parse_arguments(require_commas=True)
|
|
124
|
+
|
|
125
|
+
if parser.kind() == TOKEN_COMMA:
|
|
126
|
+
# Trailing commas are OK.
|
|
127
|
+
parser.next()
|
|
128
|
+
|
|
129
|
+
parser.carry_whitespace_control()
|
|
130
|
+
parser.eat(TOKEN_TAG_END)
|
|
131
|
+
|
|
132
|
+
return CallTag(token, name, args)
|
|
133
|
+
|
|
134
|
+
def render(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
135
|
+
macros: dict[str, Macro] = context.get_register("macros", register_callback)
|
|
136
|
+
macro = macros.get(self.name.value)
|
|
137
|
+
|
|
138
|
+
if not macro:
|
|
139
|
+
buffer.write(
|
|
140
|
+
context.env.serialize(
|
|
141
|
+
context.env.undefined(
|
|
142
|
+
self.name.value,
|
|
143
|
+
self.name.span,
|
|
144
|
+
context.template.source,
|
|
145
|
+
context.template.name,
|
|
146
|
+
),
|
|
147
|
+
context,
|
|
148
|
+
self.name.span,
|
|
149
|
+
)
|
|
150
|
+
)
|
|
151
|
+
return None
|
|
152
|
+
|
|
153
|
+
args, excess_args, excess_kwargs = self._args(macro)
|
|
154
|
+
scope: dict[str, Any] = {"args": [], "kwargs": {}}
|
|
155
|
+
|
|
156
|
+
for arg in excess_args:
|
|
157
|
+
scope["args"].append(arg.evaluate(context))
|
|
158
|
+
|
|
159
|
+
for k, v in excess_kwargs.items():
|
|
160
|
+
scope["kwargs"][k] = v.evaluate(context)
|
|
161
|
+
|
|
162
|
+
for name, expr in args.items():
|
|
163
|
+
if expr:
|
|
164
|
+
scope[name] = expr.evaluate(context)
|
|
165
|
+
else:
|
|
166
|
+
scope[name] = context.env.undefined(
|
|
167
|
+
name,
|
|
168
|
+
span=span(self.token, self.args[-1].span)
|
|
169
|
+
if self.args
|
|
170
|
+
else self.token,
|
|
171
|
+
source=context.template.source,
|
|
172
|
+
template_name=context.template.name,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
ctx = context.copy(scope, disabled_tags=("include", "block"))
|
|
176
|
+
return render_block(macro.block, ctx, buffer)
|
|
177
|
+
|
|
178
|
+
async def render_async(self, context: RenderContext, buffer: StringBuffer) -> None:
|
|
179
|
+
macros: dict[str, Macro] = context.get_register("macros", register_callback)
|
|
180
|
+
macro = macros.get(self.name.value)
|
|
181
|
+
|
|
182
|
+
if not macro:
|
|
183
|
+
buffer.write(
|
|
184
|
+
context.env.serialize(
|
|
185
|
+
context.env.undefined(
|
|
186
|
+
self.name.value,
|
|
187
|
+
self.name.span,
|
|
188
|
+
context.template.source,
|
|
189
|
+
context.template.name,
|
|
190
|
+
),
|
|
191
|
+
context,
|
|
192
|
+
self.name.span,
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return None
|
|
196
|
+
|
|
197
|
+
args, excess_args, excess_kwargs = self._args(macro)
|
|
198
|
+
scope: dict[str, Any] = {"args": [], "kwargs": {}}
|
|
199
|
+
|
|
200
|
+
for arg in excess_args:
|
|
201
|
+
scope["args"].append(await arg.evaluate_async(context))
|
|
202
|
+
|
|
203
|
+
for k, v in excess_kwargs.items():
|
|
204
|
+
scope["kwargs"][k] = await v.evaluate_async(context)
|
|
205
|
+
|
|
206
|
+
for name, expr in args.items():
|
|
207
|
+
if expr:
|
|
208
|
+
scope[name] = await expr.evaluate_async(context)
|
|
209
|
+
else:
|
|
210
|
+
scope[name] = context.env.undefined(
|
|
211
|
+
name,
|
|
212
|
+
span=span(self.token, self.args[-1].span)
|
|
213
|
+
if self.args
|
|
214
|
+
else self.token,
|
|
215
|
+
source=context.template.source,
|
|
216
|
+
template_name=context.template.name,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
ctx = context.copy(scope, disabled_tags=("include", "block"))
|
|
220
|
+
return await render_block_async(macro.block, ctx, buffer)
|
|
221
|
+
|
|
222
|
+
def _args(
|
|
223
|
+
self, macro: Macro
|
|
224
|
+
) -> tuple[dict[str, Expression | None], list[Expression], dict[str, Expression]]:
|
|
225
|
+
args: dict[str, Expression | None] = {
|
|
226
|
+
p.name.value: p.expr for p in macro.params
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
excess_args: list[Expression] = []
|
|
230
|
+
excess_kwargs: dict[str, Expression] = {}
|
|
231
|
+
|
|
232
|
+
positional_args = (
|
|
233
|
+
arg for arg in self.args if not isinstance(arg, KeywordArgument)
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
for param, expr in zip_longest(macro.params, positional_args, fillvalue=None):
|
|
237
|
+
if param is None:
|
|
238
|
+
assert expr is not None # noqa: S101
|
|
239
|
+
excess_args.append(expr)
|
|
240
|
+
continue
|
|
241
|
+
|
|
242
|
+
if expr is None:
|
|
243
|
+
break
|
|
244
|
+
|
|
245
|
+
args[param.name.value] = expr
|
|
246
|
+
|
|
247
|
+
for arg in self.args:
|
|
248
|
+
if not isinstance(arg, KeywordArgument):
|
|
249
|
+
continue
|
|
250
|
+
|
|
251
|
+
if arg.name.value in args:
|
|
252
|
+
# Potentially override a positional argument.
|
|
253
|
+
args[arg.name.value] = arg.expr
|
|
254
|
+
else:
|
|
255
|
+
excess_kwargs[arg.name.value] = arg.expr
|
|
256
|
+
|
|
257
|
+
return args, excess_args, excess_kwargs
|
|
258
|
+
|
|
259
|
+
def children(self) -> list[Markup]:
|
|
260
|
+
return []
|
|
261
|
+
|
|
262
|
+
def expressions(self) -> list[Expression]:
|
|
263
|
+
return [
|
|
264
|
+
arg.expr if isinstance(arg, KeywordArgument) else arg for arg in self.args
|
|
265
|
+
]
|
|
266
|
+
|
|
267
|
+
def block_scope(self) -> list[Name]:
|
|
268
|
+
return []
|
|
269
|
+
|
|
270
|
+
def template_scope(self) -> list[Name]:
|
|
271
|
+
return []
|
|
272
|
+
|
|
273
|
+
def partials(self, context: RenderContext) -> list[Partial]:
|
|
274
|
+
return []
|
|
275
|
+
|
|
276
|
+
async def partials_async(self, context: RenderContext) -> list[Partial]:
|
|
277
|
+
return self.partials(context)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def register_callback() -> dict[str, Macro]:
|
|
281
|
+
return {}
|