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
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
"""The built-in _render_ tag."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import sys
|
|
6
|
-
from typing import TYPE_CHECKING
|
|
7
|
-
from typing import Iterable
|
|
8
|
-
from typing import Optional
|
|
9
|
-
from typing import TextIO
|
|
10
|
-
from typing import Union
|
|
11
|
-
|
|
12
|
-
from liquid.ast import Node
|
|
13
|
-
from liquid.ast import Partial
|
|
14
|
-
from liquid.ast import PartialScope
|
|
15
|
-
from liquid.builtin.drops import IterableDrop
|
|
16
|
-
from liquid.builtin.expressions import Identifier
|
|
17
|
-
from liquid.builtin.expressions import KeywordArgument
|
|
18
|
-
from liquid.builtin.expressions import Literal
|
|
19
|
-
from liquid.builtin.expressions import Path
|
|
20
|
-
from liquid.builtin.expressions import StringLiteral
|
|
21
|
-
from liquid.builtin.expressions import parse_identifier
|
|
22
|
-
from liquid.builtin.expressions import parse_primitive
|
|
23
|
-
from liquid.builtin.tags.for_tag import ForLoop
|
|
24
|
-
from liquid.builtin.tags.include_tag import TAG_INCLUDE
|
|
25
|
-
from liquid.exceptions import LiquidSyntaxError
|
|
26
|
-
from liquid.exceptions import TemplateNotFoundError
|
|
27
|
-
from liquid.tag import Tag
|
|
28
|
-
from liquid.template import BoundTemplate
|
|
29
|
-
from liquid.token import TOKEN_AS
|
|
30
|
-
from liquid.token import TOKEN_FOR
|
|
31
|
-
from liquid.token import TOKEN_TAG
|
|
32
|
-
from liquid.token import TOKEN_WITH
|
|
33
|
-
from liquid.token import TOKEN_WORD
|
|
34
|
-
from liquid.token import Token
|
|
35
|
-
from liquid.utils import ReadOnlyChainMap
|
|
36
|
-
|
|
37
|
-
if TYPE_CHECKING:
|
|
38
|
-
from liquid.context import RenderContext
|
|
39
|
-
from liquid.expression import Expression
|
|
40
|
-
from liquid.stream import TokenStream
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
TAG_RENDER = sys.intern("render")
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
class RenderNode(Node):
|
|
47
|
-
"""The built-in _render_ tag."""
|
|
48
|
-
|
|
49
|
-
__slots__ = ("name", "var", "loop", "alias", "args")
|
|
50
|
-
tag = TAG_RENDER
|
|
51
|
-
|
|
52
|
-
def __init__(
|
|
53
|
-
self,
|
|
54
|
-
token: Token,
|
|
55
|
-
name: Union[StringLiteral, Identifier],
|
|
56
|
-
*,
|
|
57
|
-
var: Optional[Expression] = None,
|
|
58
|
-
loop: bool = False,
|
|
59
|
-
alias: Optional[Identifier] = None,
|
|
60
|
-
args: Optional[list[KeywordArgument]] = None,
|
|
61
|
-
):
|
|
62
|
-
super().__init__(token)
|
|
63
|
-
self.name = name
|
|
64
|
-
self.var = var
|
|
65
|
-
self.loop = loop
|
|
66
|
-
self.alias = alias
|
|
67
|
-
self.args = args or []
|
|
68
|
-
self.blank = False
|
|
69
|
-
|
|
70
|
-
def __str__(self) -> str:
|
|
71
|
-
var = ""
|
|
72
|
-
if self.var:
|
|
73
|
-
var = f" for {self.var}" if self.loop else f" with {self.var}"
|
|
74
|
-
if self.alias:
|
|
75
|
-
var += f" as {self.alias}"
|
|
76
|
-
if self.args:
|
|
77
|
-
var += ","
|
|
78
|
-
args = " " + ", ".join(str(arg) for arg in self.args) if self.args else ""
|
|
79
|
-
return f"{{% render {self.name}{var}{args} %}}"
|
|
80
|
-
|
|
81
|
-
def render_to_output(self, context: RenderContext, buffer: TextIO) -> int:
|
|
82
|
-
"""Render the node to the output buffer."""
|
|
83
|
-
if isinstance(self.name, Identifier):
|
|
84
|
-
# We're expecting an inline snippet.
|
|
85
|
-
template: Optional[BoundTemplate] = context.resolve(
|
|
86
|
-
self.name, token=self.token, default=None
|
|
87
|
-
)
|
|
88
|
-
if not isinstance(template, BoundTemplate):
|
|
89
|
-
raise TemplateNotFoundError(
|
|
90
|
-
self.name,
|
|
91
|
-
filename=context.template.full_name(),
|
|
92
|
-
token=self.name.token,
|
|
93
|
-
)
|
|
94
|
-
else:
|
|
95
|
-
try:
|
|
96
|
-
template = context.env.get_template(
|
|
97
|
-
self.name.value, context=context, tag=self.tag
|
|
98
|
-
)
|
|
99
|
-
except TemplateNotFoundError as err:
|
|
100
|
-
err.token = self.name.token
|
|
101
|
-
err.template_name = context.template.full_name()
|
|
102
|
-
raise
|
|
103
|
-
|
|
104
|
-
# Evaluate keyword arguments once. Unlike 'include', 'render' can not
|
|
105
|
-
# mutate variables in the outer scope, so there's no need to re-evaluate
|
|
106
|
-
# arguments for each loop (if any).
|
|
107
|
-
args = {arg.name: arg.value.evaluate(context) for arg in self.args}
|
|
108
|
-
|
|
109
|
-
# We're using a chain map here in case we need to push a forloop drop into
|
|
110
|
-
# it. As drops are read only, the built-in collections.ChainMap will not do.
|
|
111
|
-
namespace = ReadOnlyChainMap(args)
|
|
112
|
-
|
|
113
|
-
# New context with globals and filters from the parent, plus the read only
|
|
114
|
-
# namespace containing render arguments and bound variable.
|
|
115
|
-
ctx = context.copy(
|
|
116
|
-
namespace,
|
|
117
|
-
disabled_tags=[TAG_INCLUDE],
|
|
118
|
-
carry_loop_iterations=True,
|
|
119
|
-
template=template,
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
# Optionally bind a variable to the render namespace.
|
|
123
|
-
if self.var is not None:
|
|
124
|
-
val = self.var.evaluate(context)
|
|
125
|
-
key = self.alias or template.name.split(".")[0]
|
|
126
|
-
|
|
127
|
-
# If the variable is array-like, render the template once for each item.
|
|
128
|
-
# `self.loop` being True indicates the render expression used "for" not
|
|
129
|
-
# "with". This distinction is not made when using the 'include' tag.
|
|
130
|
-
if self.loop and isinstance(val, (tuple, list, IterableDrop)):
|
|
131
|
-
ctx.raise_for_loop_limit(len(val))
|
|
132
|
-
forloop = ForLoop(
|
|
133
|
-
name=key,
|
|
134
|
-
it=iter(val),
|
|
135
|
-
length=len(val),
|
|
136
|
-
parentloop=context.env.undefined("parentloop", token=self.token),
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
args["forloop"] = forloop
|
|
140
|
-
args[key] = None
|
|
141
|
-
|
|
142
|
-
for itm in forloop:
|
|
143
|
-
args[key] = itm
|
|
144
|
-
template.render_with_context(
|
|
145
|
-
ctx, buffer, partial=True, block_scope=True
|
|
146
|
-
)
|
|
147
|
-
else:
|
|
148
|
-
# The bound variable is not array-like, shove it into the namespace
|
|
149
|
-
# via args.
|
|
150
|
-
args[key] = val
|
|
151
|
-
template.render_with_context(
|
|
152
|
-
ctx, buffer, partial=True, block_scope=True
|
|
153
|
-
)
|
|
154
|
-
else:
|
|
155
|
-
template.render_with_context(ctx, buffer, partial=True, block_scope=True)
|
|
156
|
-
|
|
157
|
-
return True
|
|
158
|
-
|
|
159
|
-
async def render_to_output_async(
|
|
160
|
-
self, context: RenderContext, buffer: TextIO
|
|
161
|
-
) -> int:
|
|
162
|
-
"""Render the node to the output buffer."""
|
|
163
|
-
if isinstance(self.name, Identifier):
|
|
164
|
-
# We're expecting an inline snippet.
|
|
165
|
-
template: Optional[BoundTemplate] = context.resolve(
|
|
166
|
-
self.name, token=self.token, default=None
|
|
167
|
-
)
|
|
168
|
-
if not isinstance(template, BoundTemplate):
|
|
169
|
-
raise TemplateNotFoundError(
|
|
170
|
-
self.name,
|
|
171
|
-
filename=context.template.full_name(),
|
|
172
|
-
token=self.name.token,
|
|
173
|
-
)
|
|
174
|
-
else:
|
|
175
|
-
try:
|
|
176
|
-
template = await context.env.get_template_async(
|
|
177
|
-
self.name.value, context=context, tag=self.tag
|
|
178
|
-
)
|
|
179
|
-
except TemplateNotFoundError as err:
|
|
180
|
-
err.token = self.name.token
|
|
181
|
-
err.template_name = context.template.full_name()
|
|
182
|
-
raise
|
|
183
|
-
|
|
184
|
-
# Evaluate keyword arguments once. Unlike 'include', 'render' can not
|
|
185
|
-
# mutate variables in the outer scope, so there's no need to re-evaluate
|
|
186
|
-
# arguments for each loop (if any).
|
|
187
|
-
args = {arg.name: await arg.value.evaluate_async(context) for arg in self.args}
|
|
188
|
-
|
|
189
|
-
# We're using a chain map here in case we need to push a forloop drop into
|
|
190
|
-
# it. As drops are read only, the built-in collections.ChainMap will not do.
|
|
191
|
-
namespace = ReadOnlyChainMap(args)
|
|
192
|
-
|
|
193
|
-
# New context with globals and filters from the parent, plus the read only
|
|
194
|
-
# namespace containing render arguments and bound variable.
|
|
195
|
-
ctx = context.copy(
|
|
196
|
-
namespace,
|
|
197
|
-
disabled_tags=[TAG_INCLUDE],
|
|
198
|
-
carry_loop_iterations=True,
|
|
199
|
-
template=template,
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
# Optionally bind a variable to the render namespace.
|
|
203
|
-
if self.var is not None:
|
|
204
|
-
val = await self.var.evaluate_async(context)
|
|
205
|
-
key = self.alias or template.name.split(".")[0]
|
|
206
|
-
|
|
207
|
-
# If the variable is array-like, render the template once for each item.
|
|
208
|
-
# `self.loop` being True indicates the render expression used "for" not
|
|
209
|
-
# "with". This distinction is not made when using the 'include' tag.
|
|
210
|
-
if self.loop and isinstance(val, (tuple, list, IterableDrop)):
|
|
211
|
-
ctx.raise_for_loop_limit(len(val))
|
|
212
|
-
forloop = ForLoop(
|
|
213
|
-
name=key,
|
|
214
|
-
it=iter(val),
|
|
215
|
-
length=len(val),
|
|
216
|
-
parentloop=context.env.undefined("parentloop", token=self.token),
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
args["forloop"] = forloop
|
|
220
|
-
args[key] = None
|
|
221
|
-
|
|
222
|
-
for itm in forloop:
|
|
223
|
-
args[key] = itm
|
|
224
|
-
await template.render_with_context_async(
|
|
225
|
-
ctx, buffer, partial=True, block_scope=True
|
|
226
|
-
)
|
|
227
|
-
else:
|
|
228
|
-
# The bound variable is not array-like, shove it into the namespace
|
|
229
|
-
# via args.
|
|
230
|
-
args[key] = val
|
|
231
|
-
await template.render_with_context_async(
|
|
232
|
-
ctx, buffer, partial=True, block_scope=True
|
|
233
|
-
)
|
|
234
|
-
else:
|
|
235
|
-
await template.render_with_context_async(
|
|
236
|
-
ctx, buffer, partial=True, block_scope=True
|
|
237
|
-
)
|
|
238
|
-
|
|
239
|
-
return True
|
|
240
|
-
|
|
241
|
-
def children(
|
|
242
|
-
self, static_context: RenderContext, *, include_partials: bool = True
|
|
243
|
-
) -> Iterable[Node]:
|
|
244
|
-
"""Return this node's children."""
|
|
245
|
-
if isinstance(self.name, Identifier):
|
|
246
|
-
# We're expecting an inline snippet.
|
|
247
|
-
# Always visit inline snippets, even if include_partials is False.
|
|
248
|
-
template: Optional[BoundTemplate] = static_context.resolve(
|
|
249
|
-
self.name, token=self.token, default=None
|
|
250
|
-
)
|
|
251
|
-
if template:
|
|
252
|
-
yield from template.nodes
|
|
253
|
-
elif include_partials:
|
|
254
|
-
name = self.name.evaluate(static_context)
|
|
255
|
-
try:
|
|
256
|
-
template = static_context.env.get_template(
|
|
257
|
-
str(name), context=static_context, tag=self.tag
|
|
258
|
-
)
|
|
259
|
-
yield from template.nodes
|
|
260
|
-
except TemplateNotFoundError as err:
|
|
261
|
-
err.token = self.name.token
|
|
262
|
-
err.template_name = static_context.template.full_name()
|
|
263
|
-
raise
|
|
264
|
-
|
|
265
|
-
async def children_async(
|
|
266
|
-
self, static_context: RenderContext, *, include_partials: bool = True
|
|
267
|
-
) -> Iterable[Node]:
|
|
268
|
-
"""Return this node's children."""
|
|
269
|
-
if isinstance(self.name, Identifier):
|
|
270
|
-
# We're expecting an inline snippet.
|
|
271
|
-
template: Optional[BoundTemplate] = static_context.resolve(
|
|
272
|
-
self.name, token=self.token, default=None
|
|
273
|
-
)
|
|
274
|
-
if template:
|
|
275
|
-
return template.nodes
|
|
276
|
-
elif include_partials:
|
|
277
|
-
name = await self.name.evaluate_async(static_context)
|
|
278
|
-
try:
|
|
279
|
-
template = await static_context.env.get_template_async(
|
|
280
|
-
str(name), context=static_context, tag=self.tag
|
|
281
|
-
)
|
|
282
|
-
return template.nodes
|
|
283
|
-
except TemplateNotFoundError as err:
|
|
284
|
-
err.token = self.name.token
|
|
285
|
-
err.template_name = static_context.template.full_name()
|
|
286
|
-
raise
|
|
287
|
-
return []
|
|
288
|
-
|
|
289
|
-
def expressions(self) -> Iterable[Expression]:
|
|
290
|
-
"""Return this node's expressions."""
|
|
291
|
-
if self.var:
|
|
292
|
-
yield self.var
|
|
293
|
-
yield from (arg.value for arg in self.args)
|
|
294
|
-
|
|
295
|
-
def partial_scope(self) -> Optional[Partial]:
|
|
296
|
-
"""Return information about a partial template loaded by this node."""
|
|
297
|
-
scope: list[Identifier] = [
|
|
298
|
-
Identifier(arg.name, token=arg.token) for arg in self.args
|
|
299
|
-
]
|
|
300
|
-
|
|
301
|
-
if self.var:
|
|
302
|
-
if self.alias:
|
|
303
|
-
scope.append(self.alias)
|
|
304
|
-
elif isinstance(self.name, Literal):
|
|
305
|
-
scope.append(
|
|
306
|
-
Identifier(
|
|
307
|
-
str(self.name.value).split(".", 1)[0], token=self.name.token
|
|
308
|
-
)
|
|
309
|
-
)
|
|
310
|
-
|
|
311
|
-
partial_name = self.name.value if isinstance(self.name, StringLiteral) else ""
|
|
312
|
-
partial_key = hash((partial_name, *[arg.name for arg in self.args]))
|
|
313
|
-
|
|
314
|
-
# Static analysis will use the parent template name if Partial.name is
|
|
315
|
-
# empty. Which is what we want for inline snippets.
|
|
316
|
-
return Partial(
|
|
317
|
-
name=partial_name,
|
|
318
|
-
scope=PartialScope.ISOLATED,
|
|
319
|
-
in_scope=scope,
|
|
320
|
-
key=partial_key,
|
|
321
|
-
)
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
BIND_TAGS = frozenset((TOKEN_WITH, TOKEN_FOR))
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
class RenderTag(Tag):
|
|
328
|
-
"""The built-in _render_ tag."""
|
|
329
|
-
|
|
330
|
-
name = TAG_RENDER
|
|
331
|
-
block = False
|
|
332
|
-
node_class = RenderNode
|
|
333
|
-
|
|
334
|
-
def parse(self, stream: TokenStream) -> Node:
|
|
335
|
-
"""Parse tokens from _stream_ into an AST node."""
|
|
336
|
-
token = stream.eat(TOKEN_TAG)
|
|
337
|
-
tokens = stream.into_inner(tag=token, eat=False)
|
|
338
|
-
name: Union[Expression, Identifier] = parse_primitive(self.env, tokens)
|
|
339
|
-
|
|
340
|
-
if isinstance(name, Path):
|
|
341
|
-
head = name.head()
|
|
342
|
-
if len(name.path) != 1 or not isinstance(head, str):
|
|
343
|
-
raise LiquidSyntaxError(
|
|
344
|
-
"expected an identifier, found a path",
|
|
345
|
-
token=name.token,
|
|
346
|
-
)
|
|
347
|
-
name = Identifier(head, token=name.token)
|
|
348
|
-
elif not isinstance(name, StringLiteral):
|
|
349
|
-
raise LiquidSyntaxError("expected a string or identifier", token=name.token)
|
|
350
|
-
|
|
351
|
-
alias: Optional[Identifier] = None
|
|
352
|
-
var: Optional[Path] = None
|
|
353
|
-
loop: bool = False
|
|
354
|
-
|
|
355
|
-
# Optionally bind a variable to the included template context
|
|
356
|
-
if tokens.current.kind in BIND_TAGS:
|
|
357
|
-
loop = tokens.current.kind == TOKEN_FOR
|
|
358
|
-
next(tokens) # Eat 'with' or 'for'
|
|
359
|
-
tokens.expect(TOKEN_WORD)
|
|
360
|
-
var = Path.parse(self.env, tokens)
|
|
361
|
-
|
|
362
|
-
# The bound variable will take the name of the template by default,
|
|
363
|
-
# or an alias if an identifier follows the "as" keyword.
|
|
364
|
-
if tokens.current.kind == TOKEN_AS:
|
|
365
|
-
next(tokens) # Eat 'as'
|
|
366
|
-
tokens.expect(TOKEN_WORD)
|
|
367
|
-
alias = parse_identifier(self.env, tokens)
|
|
368
|
-
|
|
369
|
-
# Zero or more keyword arguments
|
|
370
|
-
args = KeywordArgument.parse(self.env, tokens)
|
|
371
|
-
return self.node_class(token, name, var=var, loop=loop, alias=alias, args=args)
|
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
"""The built-in _tablerow_ tag."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import sys
|
|
6
|
-
from typing import TYPE_CHECKING
|
|
7
|
-
from typing import Any
|
|
8
|
-
from typing import Iterable
|
|
9
|
-
from typing import Iterator
|
|
10
|
-
from typing import Mapping
|
|
11
|
-
from typing import TextIO
|
|
12
|
-
|
|
13
|
-
from liquid.ast import BlockNode
|
|
14
|
-
from liquid.ast import Node
|
|
15
|
-
from liquid.builtin.expressions import Identifier
|
|
16
|
-
from liquid.builtin.expressions import LoopExpression
|
|
17
|
-
from liquid.exceptions import BreakLoop
|
|
18
|
-
from liquid.exceptions import ContinueLoop
|
|
19
|
-
from liquid.limits import to_int
|
|
20
|
-
from liquid.parser import get_parser
|
|
21
|
-
from liquid.tag import Tag
|
|
22
|
-
from liquid.token import TOKEN_TAG
|
|
23
|
-
from liquid.token import Token
|
|
24
|
-
|
|
25
|
-
if TYPE_CHECKING:
|
|
26
|
-
from liquid.context import RenderContext
|
|
27
|
-
from liquid.expression import Expression
|
|
28
|
-
from liquid.stream import TokenStream
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
TAG_TABLEROW = sys.intern("tablerow")
|
|
32
|
-
TAG_ENDTABLEROW = sys.intern("endtablerow")
|
|
33
|
-
|
|
34
|
-
END_TAGBLOCK = frozenset((TAG_ENDTABLEROW,))
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class TableRow(Mapping[str, object]):
|
|
38
|
-
"""Table row helper variables."""
|
|
39
|
-
|
|
40
|
-
__slots__ = (
|
|
41
|
-
"name",
|
|
42
|
-
"it",
|
|
43
|
-
"length",
|
|
44
|
-
"ncols",
|
|
45
|
-
"_index",
|
|
46
|
-
"_row",
|
|
47
|
-
"_col",
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
_keys = frozenset(
|
|
51
|
-
[
|
|
52
|
-
"length",
|
|
53
|
-
"index",
|
|
54
|
-
"index0",
|
|
55
|
-
"rindex",
|
|
56
|
-
"rindex0",
|
|
57
|
-
"first",
|
|
58
|
-
"last",
|
|
59
|
-
"col",
|
|
60
|
-
"col0",
|
|
61
|
-
"col_first",
|
|
62
|
-
"col_last",
|
|
63
|
-
"row",
|
|
64
|
-
]
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
def __init__(self, name: str, it: Iterator[Any], length: int, ncols: int) -> None:
|
|
68
|
-
self.name = name
|
|
69
|
-
self.it = it
|
|
70
|
-
self.length = length
|
|
71
|
-
self.ncols = ncols
|
|
72
|
-
self._index = -1
|
|
73
|
-
self._row = 1
|
|
74
|
-
self._col = 0
|
|
75
|
-
|
|
76
|
-
def __repr__(self) -> str: # pragma: no cover
|
|
77
|
-
return f"TableRow(name='{self.name}', length={self.length})"
|
|
78
|
-
|
|
79
|
-
def __getitem__(self, key: str) -> object:
|
|
80
|
-
if key in self._keys:
|
|
81
|
-
return getattr(self, key)
|
|
82
|
-
raise KeyError(key)
|
|
83
|
-
|
|
84
|
-
def __len__(self) -> int:
|
|
85
|
-
return len(self._keys)
|
|
86
|
-
|
|
87
|
-
def __iter__(self) -> Iterator[Any]:
|
|
88
|
-
return self
|
|
89
|
-
|
|
90
|
-
def __next__(self) -> object:
|
|
91
|
-
self.step()
|
|
92
|
-
return next(self.it)
|
|
93
|
-
|
|
94
|
-
@property
|
|
95
|
-
def index(self) -> int:
|
|
96
|
-
"""The 1-based index of the current loop iteration."""
|
|
97
|
-
return self._index + 1
|
|
98
|
-
|
|
99
|
-
@property
|
|
100
|
-
def index0(self) -> int:
|
|
101
|
-
"""The 0-based index of the current loop iteration."""
|
|
102
|
-
return self._index
|
|
103
|
-
|
|
104
|
-
@property
|
|
105
|
-
def rindex(self) -> int:
|
|
106
|
-
"""The 1-based index, counting from the right, of the current loop iteration."""
|
|
107
|
-
return self.length - self._index
|
|
108
|
-
|
|
109
|
-
@property
|
|
110
|
-
def rindex0(self) -> int:
|
|
111
|
-
"""The 0-based index, counting from the right, of the current loop iteration."""
|
|
112
|
-
return self.length - self._index - 1
|
|
113
|
-
|
|
114
|
-
@property
|
|
115
|
-
def first(self) -> bool:
|
|
116
|
-
"""True if this is the first iteration, false otherwise."""
|
|
117
|
-
return self._index == 0
|
|
118
|
-
|
|
119
|
-
@property
|
|
120
|
-
def last(self) -> bool:
|
|
121
|
-
"""True if this is the last iteration, false otherwise."""
|
|
122
|
-
return self._index == self.length - 1
|
|
123
|
-
|
|
124
|
-
@property
|
|
125
|
-
def col(self) -> int:
|
|
126
|
-
"""The 1-based index of the current column."""
|
|
127
|
-
return self._col
|
|
128
|
-
|
|
129
|
-
@property
|
|
130
|
-
def col0(self) -> int:
|
|
131
|
-
"""The 0-based index of the current column."""
|
|
132
|
-
return self._col - 1
|
|
133
|
-
|
|
134
|
-
@property
|
|
135
|
-
def col_first(self) -> bool:
|
|
136
|
-
"""True if this is the first column. False otherwise."""
|
|
137
|
-
return self._col == 1
|
|
138
|
-
|
|
139
|
-
@property
|
|
140
|
-
def col_last(self) -> bool:
|
|
141
|
-
"""True if this is the last iteration, false otherwise."""
|
|
142
|
-
return self._col == self.ncols
|
|
143
|
-
|
|
144
|
-
@property
|
|
145
|
-
def row(self) -> int:
|
|
146
|
-
"""The current row number."""
|
|
147
|
-
return self._row
|
|
148
|
-
|
|
149
|
-
def step(self) -> None:
|
|
150
|
-
"""Step the forloop forward."""
|
|
151
|
-
self._index += 1
|
|
152
|
-
if self._col == self.ncols:
|
|
153
|
-
self._col = 1
|
|
154
|
-
self._row += 1
|
|
155
|
-
else:
|
|
156
|
-
self._col += 1
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
class TablerowNode(Node):
|
|
160
|
-
"""The built-in _tablerow_ tag."""
|
|
161
|
-
|
|
162
|
-
interrupts = True
|
|
163
|
-
"""If _true_, handle `break` and `continue` interrupts inside a tablerow loop."""
|
|
164
|
-
|
|
165
|
-
__slots__ = ("expression", "block")
|
|
166
|
-
|
|
167
|
-
def __init__(
|
|
168
|
-
self,
|
|
169
|
-
token: Token,
|
|
170
|
-
expression: LoopExpression,
|
|
171
|
-
block: BlockNode,
|
|
172
|
-
):
|
|
173
|
-
super().__init__(token)
|
|
174
|
-
self.expression = expression
|
|
175
|
-
self.block = block
|
|
176
|
-
self.blank = False
|
|
177
|
-
|
|
178
|
-
def __str__(self) -> str:
|
|
179
|
-
return f"tablerow({self.expression}) {{ {self.block} }}"
|
|
180
|
-
|
|
181
|
-
def _int_or_zero(self, arg: object) -> int:
|
|
182
|
-
try:
|
|
183
|
-
return to_int(arg)
|
|
184
|
-
except ValueError:
|
|
185
|
-
return 0
|
|
186
|
-
|
|
187
|
-
def render_to_output(self, context: RenderContext, buffer: TextIO) -> int:
|
|
188
|
-
"""Return this node's children."""
|
|
189
|
-
name = self.expression.identifier
|
|
190
|
-
loop_iter, length = self.expression.evaluate(context)
|
|
191
|
-
|
|
192
|
-
if not length:
|
|
193
|
-
# An empty iterable renders no rows.
|
|
194
|
-
return True
|
|
195
|
-
|
|
196
|
-
if self.expression.cols:
|
|
197
|
-
cols = self._int_or_zero(self.expression.cols.evaluate(context))
|
|
198
|
-
else:
|
|
199
|
-
cols = length
|
|
200
|
-
|
|
201
|
-
context.raise_for_loop_limit(length)
|
|
202
|
-
tablerow = TableRow(name, loop_iter, length, cols)
|
|
203
|
-
|
|
204
|
-
namespace: dict[str, object] = {
|
|
205
|
-
"tablerowloop": tablerow,
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
buffer.write('<tr class="row1">\n')
|
|
209
|
-
_break = False
|
|
210
|
-
|
|
211
|
-
with context.loop(namespace, tablerow):
|
|
212
|
-
for item in tablerow:
|
|
213
|
-
namespace[name] = item
|
|
214
|
-
buffer.write(f'<td class="col{tablerow.col}">')
|
|
215
|
-
|
|
216
|
-
try:
|
|
217
|
-
self.block.render(context=context, buffer=buffer)
|
|
218
|
-
except BreakLoop:
|
|
219
|
-
if self.interrupts:
|
|
220
|
-
_break = True
|
|
221
|
-
else:
|
|
222
|
-
raise
|
|
223
|
-
except ContinueLoop:
|
|
224
|
-
if not self.interrupts:
|
|
225
|
-
raise
|
|
226
|
-
|
|
227
|
-
buffer.write("</td>")
|
|
228
|
-
|
|
229
|
-
if tablerow.col_last and not tablerow.last:
|
|
230
|
-
buffer.write(f'</tr>\n<tr class="row{tablerow.row + 1}">')
|
|
231
|
-
|
|
232
|
-
if _break:
|
|
233
|
-
break
|
|
234
|
-
|
|
235
|
-
buffer.write("</tr>\n")
|
|
236
|
-
return True
|
|
237
|
-
|
|
238
|
-
async def render_to_output_async(
|
|
239
|
-
self, context: RenderContext, buffer: TextIO
|
|
240
|
-
) -> int:
|
|
241
|
-
"""Return this node's children."""
|
|
242
|
-
name = self.expression.identifier
|
|
243
|
-
loop_iter, length = await self.expression.evaluate_async(context)
|
|
244
|
-
|
|
245
|
-
if self.expression.cols:
|
|
246
|
-
cols = self._int_or_zero(await self.expression.cols.evaluate_async(context))
|
|
247
|
-
else:
|
|
248
|
-
cols = length
|
|
249
|
-
|
|
250
|
-
context.raise_for_loop_limit(length)
|
|
251
|
-
tablerow = TableRow(name, loop_iter, length, cols)
|
|
252
|
-
|
|
253
|
-
namespace: dict[str, object] = {
|
|
254
|
-
"tablerowloop": tablerow,
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
buffer.write('<tr class="row1">\n')
|
|
258
|
-
_break = False
|
|
259
|
-
|
|
260
|
-
with context.extend(namespace):
|
|
261
|
-
for item in tablerow:
|
|
262
|
-
namespace[name] = item
|
|
263
|
-
buffer.write(f'<td class="col{tablerow.col}">')
|
|
264
|
-
|
|
265
|
-
try:
|
|
266
|
-
await self.block.render_async(context=context, buffer=buffer)
|
|
267
|
-
except BreakLoop:
|
|
268
|
-
if self.interrupts:
|
|
269
|
-
_break = True
|
|
270
|
-
else:
|
|
271
|
-
raise
|
|
272
|
-
except ContinueLoop:
|
|
273
|
-
if not self.interrupts:
|
|
274
|
-
raise
|
|
275
|
-
|
|
276
|
-
buffer.write("</td>")
|
|
277
|
-
|
|
278
|
-
if tablerow.col_last and not tablerow.last:
|
|
279
|
-
buffer.write(f'</tr>\n<tr class="row{tablerow.row + 1}">')
|
|
280
|
-
|
|
281
|
-
if _break:
|
|
282
|
-
break
|
|
283
|
-
|
|
284
|
-
buffer.write("</tr>\n")
|
|
285
|
-
return True
|
|
286
|
-
|
|
287
|
-
def children(
|
|
288
|
-
self,
|
|
289
|
-
static_context: RenderContext, # noqa: ARG002
|
|
290
|
-
*,
|
|
291
|
-
include_partials: bool = True, # noqa: ARG002
|
|
292
|
-
) -> Iterable[Node]:
|
|
293
|
-
"""Return this node's children."""
|
|
294
|
-
yield self.block
|
|
295
|
-
|
|
296
|
-
def expressions(self) -> Iterable[Expression]:
|
|
297
|
-
"""Return this node's expressions."""
|
|
298
|
-
yield self.expression
|
|
299
|
-
|
|
300
|
-
def block_scope(self) -> Iterable[Identifier]:
|
|
301
|
-
"""Return variables this node adds to the node's block scope."""
|
|
302
|
-
yield Identifier(self.expression.identifier, token=self.expression.token)
|
|
303
|
-
yield Identifier("tablerowloop", token=self.token)
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
class TablerowTag(Tag):
|
|
307
|
-
"""The built-in _tablerow_ tag."""
|
|
308
|
-
|
|
309
|
-
name = TAG_TABLEROW
|
|
310
|
-
end = TAG_ENDTABLEROW
|
|
311
|
-
node_class = TablerowNode
|
|
312
|
-
|
|
313
|
-
def parse(self, stream: TokenStream) -> TablerowNode:
|
|
314
|
-
"""Parse tokens from _stream_ into an AST node."""
|
|
315
|
-
token = stream.eat(TOKEN_TAG)
|
|
316
|
-
tokens = stream.into_inner(tag=token)
|
|
317
|
-
expr = LoopExpression.parse(self.env, tokens)
|
|
318
|
-
block = get_parser(self.env).parse_block(stream, END_TAGBLOCK)
|
|
319
|
-
stream.expect(TOKEN_TAG, value=TAG_ENDTABLEROW)
|
|
320
|
-
return self.node_class(token, expression=expr, block=block)
|