django-components 0.119__tar.gz → 0.121__tar.gz

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.
Files changed (53) hide show
  1. {django_components-0.119/src/django_components.egg-info → django_components-0.121}/PKG-INFO +1 -1
  2. {django_components-0.119 → django_components-0.121}/pyproject.toml +1 -1
  3. {django_components-0.119 → django_components-0.121}/src/django_components/templatetags/component_tags.py +5 -4
  4. {django_components-0.119 → django_components-0.121}/src/django_components/util/tag_parser.py +120 -62
  5. {django_components-0.119 → django_components-0.121/src/django_components.egg-info}/PKG-INFO +1 -1
  6. {django_components-0.119 → django_components-0.121}/LICENSE +0 -0
  7. {django_components-0.119 → django_components-0.121}/MANIFEST.in +0 -0
  8. {django_components-0.119 → django_components-0.121}/README.md +0 -0
  9. {django_components-0.119 → django_components-0.121}/setup.cfg +0 -0
  10. {django_components-0.119 → django_components-0.121}/src/django_components/__init__.py +0 -0
  11. {django_components-0.119 → django_components-0.121}/src/django_components/app_settings.py +0 -0
  12. {django_components-0.119 → django_components-0.121}/src/django_components/apps.py +0 -0
  13. {django_components-0.119 → django_components-0.121}/src/django_components/attributes.py +0 -0
  14. {django_components-0.119 → django_components-0.121}/src/django_components/autodiscovery.py +0 -0
  15. {django_components-0.119 → django_components-0.121}/src/django_components/component.py +0 -0
  16. {django_components-0.119 → django_components-0.121}/src/django_components/component_media.py +0 -0
  17. {django_components-0.119 → django_components-0.121}/src/django_components/component_registry.py +0 -0
  18. {django_components-0.119 → django_components-0.121}/src/django_components/components/__init__.py +0 -0
  19. {django_components-0.119 → django_components-0.121}/src/django_components/components/dynamic.py +0 -0
  20. {django_components-0.119 → django_components-0.121}/src/django_components/context.py +0 -0
  21. {django_components-0.119 → django_components-0.121}/src/django_components/dependencies.py +0 -0
  22. {django_components-0.119 → django_components-0.121}/src/django_components/expression.py +0 -0
  23. {django_components-0.119 → django_components-0.121}/src/django_components/finders.py +0 -0
  24. {django_components-0.119 → django_components-0.121}/src/django_components/library.py +0 -0
  25. {django_components-0.119 → django_components-0.121}/src/django_components/management/__init__.py +0 -0
  26. {django_components-0.119 → django_components-0.121}/src/django_components/management/commands/__init__.py +0 -0
  27. {django_components-0.119 → django_components-0.121}/src/django_components/management/commands/startcomponent.py +0 -0
  28. {django_components-0.119 → django_components-0.121}/src/django_components/management/commands/upgradecomponent.py +0 -0
  29. {django_components-0.119 → django_components-0.121}/src/django_components/middleware.py +0 -0
  30. {django_components-0.119 → django_components-0.121}/src/django_components/node.py +0 -0
  31. {django_components-0.119 → django_components-0.121}/src/django_components/provide.py +0 -0
  32. {django_components-0.119 → django_components-0.121}/src/django_components/py.typed +0 -0
  33. {django_components-0.119 → django_components-0.121}/src/django_components/slots.py +0 -0
  34. {django_components-0.119 → django_components-0.121}/src/django_components/static/django_components/django_components.min.js +0 -0
  35. {django_components-0.119 → django_components-0.121}/src/django_components/tag_formatter.py +0 -0
  36. {django_components-0.119 → django_components-0.121}/src/django_components/template.py +0 -0
  37. {django_components-0.119 → django_components-0.121}/src/django_components/template_loader.py +0 -0
  38. {django_components-0.119 → django_components-0.121}/src/django_components/templatetags/__init__.py +0 -0
  39. {django_components-0.119 → django_components-0.121}/src/django_components/types.py +0 -0
  40. {django_components-0.119 → django_components-0.121}/src/django_components/urls.py +0 -0
  41. {django_components-0.119 → django_components-0.121}/src/django_components/util/__init__.py +0 -0
  42. {django_components-0.119 → django_components-0.121}/src/django_components/util/cache.py +0 -0
  43. {django_components-0.119 → django_components-0.121}/src/django_components/util/html.py +0 -0
  44. {django_components-0.119 → django_components-0.121}/src/django_components/util/loader.py +0 -0
  45. {django_components-0.119 → django_components-0.121}/src/django_components/util/logger.py +0 -0
  46. {django_components-0.119 → django_components-0.121}/src/django_components/util/misc.py +0 -0
  47. {django_components-0.119 → django_components-0.121}/src/django_components/util/nanoid.py +0 -0
  48. {django_components-0.119 → django_components-0.121}/src/django_components/util/types.py +0 -0
  49. {django_components-0.119 → django_components-0.121}/src/django_components/util/validation.py +0 -0
  50. {django_components-0.119 → django_components-0.121}/src/django_components.egg-info/SOURCES.txt +0 -0
  51. {django_components-0.119 → django_components-0.121}/src/django_components.egg-info/dependency_links.txt +0 -0
  52. {django_components-0.119 → django_components-0.121}/src/django_components.egg-info/requires.txt +0 -0
  53. {django_components-0.119 → django_components-0.121}/src/django_components.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django_components
3
- Version: 0.119
3
+ Version: 0.121
4
4
  Summary: A way to create simple reusable template components in Django.
5
5
  Author-email: Emil Stenström <emil@emilstenstrom.se>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "django_components"
7
- version = "0.119"
7
+ version = "0.121"
8
8
  requires-python = ">=3.8, <4.0"
9
9
  description = "A way to create simple reusable template components in Django."
10
10
  keywords = ["django", "components", "css", "js", "html"]
@@ -860,7 +860,7 @@ def _parse_tag_preprocess(
860
860
 
861
861
  # First token is tag name, e.g. `slot` in `{% slot <name> ... %}`
862
862
  tag_name_attr = attrs.pop(0)
863
- tag_name = tag_name_attr.value
863
+ tag_name = tag_name_attr.formatted_value()
864
864
 
865
865
  # Sanity check
866
866
  if tag_name != tag_spec.tag:
@@ -872,7 +872,7 @@ def _parse_tag_preprocess(
872
872
  # Otherwise, depending on the tag spec, the tag may be:
873
873
  # 2. Block tag - With corresponding end tag, e.g. `{% endslot %}`
874
874
  # 3. Inlined tag - Without the end tag.
875
- last_token = attrs[-1].value if len(attrs) else None
875
+ last_token = attrs[-1].formatted_value() if len(attrs) else None
876
876
  if last_token == "/":
877
877
  attrs.pop()
878
878
  is_inline = True
@@ -1168,7 +1168,8 @@ def _fix_nested_tags(parser: Parser, block_token: Token) -> None:
1168
1168
  return
1169
1169
 
1170
1170
  last_attr = attrs[-1]
1171
- last_token = last_attr.value
1171
+ last_attr_part = last_attr.parts[-1]
1172
+ last_token = last_attr_part.value
1172
1173
 
1173
1174
  # User probably forgot to wrap the nested tag in quotes, or this is the end of the input.
1174
1175
  # `{% component ... key={% nested %} %}`
@@ -1204,7 +1205,7 @@ def _fix_nested_tags(parser: Parser, block_token: Token) -> None:
1204
1205
 
1205
1206
  # There is 3 double quotes, but if the contents get split at the first `%}`
1206
1207
  # then there will be a single unclosed double quote in the last bit.
1207
- has_unclosed_quote = not last_attr.quoted and last_token and last_token[0] in ('"', "'")
1208
+ has_unclosed_quote = not last_attr_part.quoted and last_token and last_token[0] in ('"', "'")
1208
1209
 
1209
1210
  needs_fixing = has_unclosed_tag and has_unclosed_quote
1210
1211
 
@@ -1,39 +1,79 @@
1
1
  from dataclasses import dataclass
2
2
  from typing import List, Optional, Sequence, Tuple, Union
3
3
 
4
- TAG_WHITESPACE = (" ", "\t", "\n", "\r", "\f")
4
+ TAG_WHITESPACES = (" ", "\t", "\n", "\r", "\f")
5
+ TAG_QUOTES = ("'", '"', '_("', "_('")
6
+ TAG_FILTER_JOINERS = ("|", ":")
7
+ TAG_SPREAD = "..."
5
8
 
6
9
 
7
10
  @dataclass
8
- class TagAttr:
9
- key: Optional[str]
11
+ class TagAttrPart:
12
+ """
13
+ Django tag attributes may consist of multiple parts, being separated by filter pipes (`|`)
14
+ or filter arguments (`:`). This class represents a single part of the attribute value.
15
+
16
+ E.g. in the following tag:
17
+ ```django
18
+ {% component "my_comp" key="my val's" key2=val2|filter1:"one" %}
19
+ ```
20
+
21
+ The `key2` attribute has three parts: `val2`, `filter1` and `"one"`.
22
+ """
23
+
10
24
  value: str
11
- start_index: int
25
+ """The actual value of the part, e.g. `val2` in `key2=val2` or `my string` in `_("my string")`."""
26
+ prefix: Optional[str]
12
27
  """
13
- Start index of the attribute (include both key and value),
14
- relative to the start of the owner Tag.
28
+ If this part is filter or filter arguent, `prefix` is the string that connects it to the previous part.
29
+ E.g. the `|` and `:` in `key2=val2|filter1:"one"`.
15
30
  """
16
31
  quoted: Optional[str]
17
32
  """Whether the value is quoted, and the character that's used for the quotation"""
18
- spread: bool
19
- """Whether the value is a spread syntax, e.g. `...my_var`"""
20
33
  translation: bool
21
34
  """Whether the value is a translation string, e.g. `_("my string")`"""
22
35
 
23
36
  def __post_init__(self) -> None:
24
37
  if self.translation and not self.quoted:
25
38
  raise ValueError("Translation value must be quoted")
26
- if self.translation and self.spread:
27
- raise ValueError("Cannot combine translation and spread syntax")
28
39
 
29
- def formatted_value(self) -> str:
40
+ def formatted(self) -> str:
41
+ """
42
+ Format the part as a string that can be used in a Django template tag.
43
+ E.g. `val2`, `|filter1:"one"`, `_("my string")`.
44
+ """
30
45
  value = f"{self.quoted}{self.value}{self.quoted}" if self.quoted else self.value
31
46
  if self.translation:
32
47
  value = f"_({value})"
33
- elif self.spread:
34
- value = f"...{value}"
48
+ if self.prefix:
49
+ value = f"{self.prefix}{value}"
50
+ return value
51
+
52
+
53
+ @dataclass
54
+ class TagAttr:
55
+ key: Optional[str]
56
+ parts: List[TagAttrPart]
57
+ start_index: int
58
+ """
59
+ Start index of the attribute (include both key and value),
60
+ relative to the start of the owner Tag.
61
+ """
62
+ spread: bool
63
+ """Whether the value is a spread syntax, e.g. `...my_var`"""
64
+
65
+ def formatted_value(self) -> str:
66
+ value = "".join(part.formatted() for part in self.parts)
67
+ if self.spread:
68
+ value = f"{TAG_SPREAD}{value}"
35
69
  return value
36
70
 
71
+ def formatted(self) -> str:
72
+ s = self.formatted_value()
73
+ if self.key:
74
+ return f"{self.key}={s}"
75
+ return s
76
+
37
77
 
38
78
  # Parse the content of a Django template tag like this:
39
79
  #
@@ -132,91 +172,109 @@ def parse_tag_attrs(text: str) -> Tuple[str, List[TagAttr]]:
132
172
 
133
173
  return result
134
174
 
135
- # Parse
175
+ def parse_attr_parts() -> List[TagAttrPart]:
176
+ parts: List[TagAttrPart] = []
177
+
178
+ while index < len(text) and not is_next_token("=", *TAG_WHITESPACES):
179
+ is_translation = False
180
+ value: str = ""
181
+ quoted: Optional[str] = None
182
+ prefix: Optional[str] = None
183
+
184
+ if is_next_token(*TAG_FILTER_JOINERS):
185
+ prefix = taken_n(1) # | or :
186
+
187
+ # E.g. `height="20"` or `height=_("my_text")` or `height="my_text"|fil1:"one"`
188
+ if is_next_token(*TAG_QUOTES):
189
+ # NOTE: Strings may be wrapped in `_()` to allow for translation.
190
+ # See https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#string-literals-passed-to-tags-and-filters # noqa: E501
191
+ if is_next_token("_("):
192
+ taken_n(2) # _(
193
+ is_translation = True
194
+
195
+ # NOTE: We assume no space between the translation syntax and the quote.
196
+ quote_char = taken_n(1) # " or '
197
+
198
+ # NOTE: Handle escaped quotes like \" or \', and continue until we reach the closing quote.
199
+ value = take_until([quote_char], ignore=["\\" + quote_char])
200
+ # Handle the case when there is a trailing quote, e.g. when a text value is not closed.
201
+ # `{% component 'my_comp' text="organis %}`
202
+
203
+ if is_next_token(quote_char):
204
+ add_token(quote_char)
205
+ if is_translation:
206
+ taken_n(1) # )
207
+ quoted = quote_char
208
+ else:
209
+ quoted = None
210
+ value = quote_char + value
211
+ # E.g. `height=20` or `height=my_var` or or `height=my_var|fil1:"one"`
212
+ else:
213
+ value = take_until(["=", *TAG_WHITESPACES, *TAG_FILTER_JOINERS])
214
+ quoted = None
215
+
216
+ parts.append(
217
+ TagAttrPart(
218
+ value=value,
219
+ prefix=prefix,
220
+ quoted=quoted,
221
+ translation=is_translation,
222
+ )
223
+ )
224
+
225
+ return parts
226
+
227
+ # Parse attributes
136
228
  attrs: List[TagAttr] = []
137
229
  while index < len(text):
138
230
  # Skip whitespace
139
- take_while(TAG_WHITESPACE)
231
+ take_while(TAG_WHITESPACES)
140
232
 
141
233
  start_index = len(normalized)
234
+ key = None
142
235
 
143
236
  # If token starts with a quote, we assume it's a value without key part.
144
237
  # e.g. `component 'my_comp'`
145
238
  # Otherwise, parse the key.
146
- if is_next_token("'", '"', '_("', "_('", "..."):
239
+ if is_next_token(*TAG_QUOTES, TAG_SPREAD):
147
240
  key = None
148
241
  else:
149
- key = take_until(["=", *TAG_WHITESPACE])
242
+ parts = parse_attr_parts()
150
243
 
151
244
  # We've reached the end of the text
152
- if not key:
245
+ if not parts:
153
246
  break
154
247
 
155
248
  # Has value
156
249
  if is_next_token("="):
157
250
  add_token("=")
251
+ key = "".join(part.formatted() for part in parts)
158
252
  else:
159
253
  # Actually was a value without key part
254
+ key = None
160
255
  attrs.append(
161
256
  TagAttr(
162
- key=None,
163
- value=key,
257
+ key=key,
258
+ parts=parts,
164
259
  start_index=start_index,
165
- quoted=None,
166
- translation=False,
167
260
  spread=False,
168
261
  )
169
262
  )
170
263
  continue
171
264
 
172
- # Move the spread synxtax out of the way, so that we properly handle what's next.
173
- is_spread = is_next_token("...")
265
+ # Move the spread syntax out of the way, so that we properly handle what's next.
266
+ is_spread = is_next_token(TAG_SPREAD)
174
267
  if is_spread:
175
- taken_n(3) # ...
176
-
177
- # Parse the value
178
- #
179
- # E.g. `height="20"`
180
- # NOTE: We don't need to parse the attributes fully. We just need to account
181
- # for the quotes.
182
- if is_next_token("'", '"', '_("', "_('"):
183
- # NOTE: Strings may be wrapped in `_()` to allow for translation.
184
- # See https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#string-literals-passed-to-tags-and-filters # noqa: E501
185
- if is_next_token("_("):
186
- taken_n(2) # _(
187
- is_translation = True
188
- else:
189
- is_translation = False
268
+ taken_n(len(TAG_SPREAD)) # ...
190
269
 
191
- # NOTE: We assume no space between the translation syntax and the quote.
192
- quote_char = taken_n(1) # " or '
193
-
194
- # NOTE: Handle escaped quotes like \" or \', and continue until we reach the closing quote.
195
- value = take_until([quote_char], ignore=["\\" + quote_char])
196
- # Handle the case when there is a trailing quote, e.g. when a text value is not closed.
197
- # `{% component 'my_comp' text="organis %}`
198
-
199
- if is_next_token(quote_char):
200
- add_token(quote_char)
201
- if is_translation:
202
- taken_n(1) # )
203
- quoted = quote_char
204
- else:
205
- quoted = None
206
- value = quote_char + value
207
- # E.g. `height=20`
208
- else:
209
- value = take_until(TAG_WHITESPACE)
210
- quoted = None
270
+ parts = parse_attr_parts()
211
271
 
212
272
  attrs.append(
213
273
  TagAttr(
214
274
  key=key,
215
- value=value,
275
+ parts=parts,
216
276
  start_index=start_index,
217
- quoted=quoted,
218
277
  spread=is_spread,
219
- translation=False,
220
278
  )
221
279
  )
222
280
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django_components
3
- Version: 0.119
3
+ Version: 0.121
4
4
  Summary: A way to create simple reusable template components in Django.
5
5
  Author-email: Emil Stenström <emil@emilstenstrom.se>
6
6
  License: MIT