|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import hashlib |
| 4 | +import re |
4 | 5 |
|
5 | 6 | from django.core.cache import cache |
| 7 | +from django.template.base import Node |
6 | 8 | from django.template.base import Origin |
| 9 | +from django.template.base import Template |
| 10 | +from django.template.context import Context |
7 | 11 | from django.template.engine import Engine |
| 12 | +from django.template.loader_tags import ExtendsNode |
| 13 | +from django.template.loader_tags import IncludeNode |
8 | 14 | from django.template.loaders.filesystem import Loader as FileSystemLoader |
9 | 15 |
|
10 | 16 | from ._typing import override |
11 | | -from .compiler import BIRD_PATTERN |
12 | 17 | from .compiler import Compiler |
| 18 | +from .components import components |
| 19 | +from .staticfiles import ComponentAssetRegistry |
| 20 | +from .templatetags.tags.bird import TAG |
| 21 | +from .templatetags.tags.bird import BirdNode |
| 22 | + |
| 23 | +BIRD_TAG_PATTERN = re.compile(rf"{{%\s*{TAG}\s+([^\s%}}]+)") |
13 | 24 |
|
14 | 25 |
|
15 | 26 | class BirdLoader(FileSystemLoader): |
16 | 27 | def __init__(self, engine: Engine): |
17 | 28 | super().__init__(engine) |
18 | 29 | self.compiler = Compiler() |
| 30 | + self.asset_registry = ComponentAssetRegistry() |
19 | 31 |
|
20 | 32 | @override |
21 | 33 | def get_contents(self, origin: Origin) -> str: |
22 | 34 | contents = super().get_contents(origin) |
23 | | - if not BIRD_PATTERN.search(contents): |
| 35 | + |
| 36 | + if not BIRD_TAG_PATTERN.search(contents): |
24 | 37 | return contents |
| 38 | + |
| 39 | + template = Template(contents, origin=origin, engine=self.engine) |
| 40 | + context = Context() |
| 41 | + with context.bind_template(template): |
| 42 | + self._scan_for_components(template, context) |
| 43 | + |
25 | 44 | cache_key = f"bird_component_{hashlib.md5(contents.encode()).hexdigest()}" |
26 | | - compiled = cache.get(cache_key) # pyright: ignore[reportAny] |
| 45 | + compiled = cache.get(cache_key) |
27 | 46 | if compiled is None: |
28 | 47 | compiled = self.compiler.compile(contents) |
29 | 48 | cache.set(cache_key, compiled, timeout=None) |
30 | 49 | return compiled |
| 50 | + |
| 51 | + def _scan_for_components(self, template: Template | Node, context: Context) -> None: |
| 52 | + if not hasattr(template, "nodelist"): |
| 53 | + return |
| 54 | + |
| 55 | + for node in template.nodelist: |
| 56 | + if isinstance(node, BirdNode): |
| 57 | + component = components.get_component(node.name) |
| 58 | + self.asset_registry.register(component) |
| 59 | + |
| 60 | + if isinstance(node, ExtendsNode): |
| 61 | + parent_template = node.get_parent(context) |
| 62 | + self._scan_for_components(parent_template, context) |
| 63 | + |
| 64 | + if isinstance(node, IncludeNode): |
| 65 | + template_name = node.template.token.strip("'\"") |
| 66 | + included_template = self.engine.get_template(template_name) |
| 67 | + self._scan_for_components(included_template, context) |
| 68 | + |
| 69 | + if hasattr(node, "nodelist"): |
| 70 | + self._scan_for_components(node, context) |
0 commit comments