13
13
from jinja2 import Environment
14
14
15
15
16
- class FileSystemTemplateComponentsLoader (FileSystemLoader ):
16
+ class ElementsLoader (FileSystemLoader ):
17
17
def get_source (self , environment : "Environment" , template : str ):
18
18
contents , path , uptodate = super ().get_source (environment , template )
19
19
20
- # Clear components cache if it looks like a component changed
21
- # if os.path.splitext(path)[1] == ".html" and "components " in path and "template_components " in self.__dict__:
22
- # del self.__dict__["template_components "]
20
+ # Clear elements cache if it looks like a element changed
21
+ # if os.path.splitext(path)[1] == ".html" and "elements " in path and "template_elements " in self.__dict__:
22
+ # del self.__dict__["template_elements "]
23
23
24
- # If it's html, replace component tags
24
+ # If it's html, replace element tags
25
25
if os .path .splitext (path )[1 ] == ".html" :
26
- self ._template_components_environment = (
27
- environment # Save this so we can use it in template_components
26
+ self ._elements_environment = (
27
+ environment # Save this so we can use it in template_elements
28
28
)
29
- contents = self .replace_template_component_tags (contents )
29
+ contents = self .replace_template_element_tags (contents )
30
30
31
31
return contents , path , uptodate
32
32
33
33
@cached_property
34
- def template_components (self ):
35
- components = []
34
+ def template_elements (self ):
35
+ elements = []
36
36
37
37
for searchpath in self .searchpath :
38
- components_dir = os .path .join (searchpath , "components " )
39
- if os .path .isdir (components_dir ):
40
- for root , dirs , files in os .walk (components_dir ):
38
+ elements_dir = os .path .join (searchpath , "elements " )
39
+ if os .path .isdir (elements_dir ):
40
+ for root , dirs , files in os .walk (elements_dir ):
41
41
for file in files :
42
42
relative_path = os .path .relpath (
43
- os .path .join (root , file ), components_dir
43
+ os .path .join (root , file ), elements_dir
44
44
)
45
45
# Replace slashes with .
46
- component_name = os .path .splitext (relative_path )[0 ].replace (
46
+ element_name = os .path .splitext (relative_path )[0 ].replace (
47
47
os .sep , "."
48
48
)
49
- components .append (
49
+ elements .append (
50
50
{
51
51
"path" : relative_path ,
52
- "html_name" : component_name , # Uses . syntax
53
- "tag_name" : component_name .replace (
52
+ "html_name" : element_name , # Uses . syntax
53
+ "tag_name" : element_name .replace (
54
54
"." , "_"
55
55
), # Uses _ syntax
56
56
}
57
57
)
58
58
59
- for component in components :
60
- component_name = component ["html_name" ]
61
- jinja_tag_name = component ["tag_name" ]
62
- component_relative_path = component ["path" ]
59
+ for element in elements :
60
+ element_name = element ["html_name" ]
61
+ jinja_tag_name = element ["tag_name" ]
62
+ element_relative_path = element ["path" ]
63
63
64
- class ComponentExtension (Extension ):
64
+ class ElemenetExtension (Extension ):
65
65
def parse (self , parser ):
66
66
lineno = next (parser .stream ).lineno
67
67
args = [
@@ -93,27 +93,27 @@ def _render(self, context, **kwargs):
93
93
rendered = template .render ({** context , ** kwargs })
94
94
95
95
if settings .DEBUG :
96
- # Add an HTML comment in dev to help identify components in output
96
+ # Add an HTML comment in dev to help identify elements in output
97
97
return f"<!-- <{ self .html_name } >\n { self .source_ref } -->\n { rendered } \n <!-- </{ self .html_name } > -->"
98
98
else :
99
99
return rendered
100
100
101
101
# Create a new class on the fly
102
- NamedComponentExtension = type (
103
- f"HTMLComponent. { component_name } " ,
104
- (ComponentExtension ,),
102
+ NamedElementExtension = type (
103
+ f"BoltElement. { element_name } " ,
104
+ (ElemenetExtension ,),
105
105
{
106
106
"tags" : {jinja_tag_name , f"end{ jinja_tag_name } " },
107
- "template_name" : f"components/ { component_relative_path } " ,
107
+ "template_name" : f"elements/ { element_relative_path } " ,
108
108
"jinja_tag_name" : jinja_tag_name ,
109
- "html_name" : component_name ,
109
+ "html_name" : element_name ,
110
110
},
111
111
)
112
- self ._template_components_environment .add_extension (NamedComponentExtension )
112
+ self ._elements_environment .add_extension (NamedElementExtension )
113
113
114
- return components
114
+ return elements
115
115
116
- def replace_template_component_tags (self , contents : str ):
116
+ def replace_template_element_tags (self , contents : str ):
117
117
def replace_quoted_braces (s ) -> str :
118
118
"""
119
119
We're converting to tag syntax, but it's very natural to write
@@ -122,19 +122,19 @@ def replace_quoted_braces(s) -> str:
122
122
"""
123
123
return re .sub (r"(?<=\"{{)(.+)(?=}}\")" , r"\1" , s )
124
124
125
- for component in self .template_components :
126
- component_name = component ["html_name" ]
127
- jinja_tag_name = component ["tag_name" ]
125
+ for element in self .template_elements :
126
+ element_name = element ["html_name" ]
127
+ jinja_tag_name = element ["tag_name" ]
128
128
129
129
closing_pattern = re .compile (
130
- rf"<{ component_name } (\s+[\s\S]*?)?>([\s\S]*?)</{ component_name } >"
130
+ rf"<{ element_name } (\s+[\s\S]*?)?>([\s\S]*?)</{ element_name } >"
131
131
)
132
- self_closing_pattern = re .compile (rf"<{ component_name } (\s+[\s\S]*?)?/>" )
132
+ self_closing_pattern = re .compile (rf"<{ element_name } (\s+[\s\S]*?)?/>" )
133
133
134
134
def closing_cb (match : re .Match ) -> str :
135
- if f"<{ component_name } " in match .group (2 ):
135
+ if f"<{ element_name } " in match .group (2 ):
136
136
raise ValueError (
137
- f"Component { component_name } cannot be nested in itself"
137
+ f"Element { element_name } cannot be nested in itself"
138
138
)
139
139
140
140
attrs_str = match .group (1 ) or ""
0 commit comments