Skip to content

Commit c7884f1

Browse files
committed
First pass at renderers
1 parent b7396ad commit c7884f1

File tree

2 files changed

+150
-93
lines changed

2 files changed

+150
-93
lines changed

pattern_library/utils.py

Lines changed: 144 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from pattern_library.exceptions import TemplateIsNotPattern
2323

2424

25+
26+
from django.utils.html import escape
27+
2528
def path_to_section():
2629
section_config = get_sections()
2730
sections = {}
@@ -79,75 +82,6 @@ def get_template_dirs():
7982
return template_dirs
8083

8184

82-
def get_pattern_templates():
83-
templates = base_dict()
84-
template_dirs = get_template_dirs()
85-
86-
for lookup_dir in template_dirs:
87-
for root, dirs, files in os.walk(lookup_dir, topdown=True):
88-
# Ignore folders without files
89-
if not files:
90-
continue
91-
92-
base_path = os.path.relpath(root, lookup_dir)
93-
section, path = section_for(base_path)
94-
95-
# It has no section, ignore it
96-
if not section:
97-
continue
98-
99-
found_templates = []
100-
for current_file in files:
101-
pattern_path = os.path.join(root, current_file)
102-
pattern_path = os.path.relpath(pattern_path, lookup_dir)
103-
104-
if is_pattern(pattern_path):
105-
template = get_template(pattern_path)
106-
pattern_config = get_pattern_config(pattern_path)
107-
pattern_name = pattern_config.get("name")
108-
pattern_filename = os.path.relpath(
109-
template.origin.template_name,
110-
base_path,
111-
)
112-
if pattern_name:
113-
template.pattern_name = pattern_name
114-
else:
115-
template.pattern_name = pattern_filename
116-
117-
template.pattern_filename = pattern_filename
118-
119-
found_templates.append(template)
120-
121-
if found_templates:
122-
lookup_dir_relpath = os.path.relpath(root, lookup_dir)
123-
sub_folders = os.path.relpath(lookup_dir_relpath, path)
124-
templates_to_store = templates
125-
for folder in [section, *sub_folders.split(os.sep)]:
126-
try:
127-
templates_to_store = templates_to_store["template_groups"][
128-
folder
129-
]
130-
except KeyError:
131-
templates_to_store["template_groups"][folder] = base_dict()
132-
templates_to_store = templates_to_store["template_groups"][
133-
folder
134-
]
135-
136-
templates_to_store["templates_stored"].extend(found_templates)
137-
138-
# Order the templates alphabetically
139-
for templates_objs in templates["template_groups"].values():
140-
templates_objs["template_groups"] = order_dict(
141-
templates_objs["template_groups"]
142-
)
143-
144-
# Order the top level by the sections
145-
section_order = [section for section, _ in get_sections()]
146-
templates["template_groups"] = order_dict(
147-
templates["template_groups"], key_sort=lambda key: section_order.index(key)
148-
)
149-
150-
return templates
15185

15286

15387
def get_pattern_config_str(template_name):
@@ -227,27 +161,149 @@ def render_pattern(request, template_name, allow_non_patterns=False, config=None
227161
return render_to_string(template_name, request=request, context=context)
228162

229163

230-
def get_template_ancestors(template_name, context=None, ancestors=None):
231-
"""
232-
Returns a list of template names, starting with provided name
233-
and followed by the names of any templates that extends until
234-
the most extended template is reached.
235-
"""
236-
if ancestors is None:
237-
ancestors = [template_name]
164+
def get_renderer():
165+
return JinjaTemplateRenderer
166+
167+
168+
class TemplateRenderer:
169+
170+
@classmethod
171+
def get_pattern_templates(cls,):
172+
templates = base_dict()
173+
template_dirs = get_template_dirs()
174+
175+
for lookup_dir in template_dirs:
176+
for root, dirs, files in os.walk(lookup_dir, topdown=True):
177+
# Ignore folders without files
178+
if not files:
179+
continue
180+
181+
base_path = os.path.relpath(root, lookup_dir)
182+
section, path = section_for(base_path)
183+
184+
# It has no section, ignore it
185+
if not section:
186+
continue
187+
188+
found_templates = []
189+
for current_file in files:
190+
pattern_path = os.path.join(root, current_file)
191+
pattern_path = os.path.relpath(pattern_path, lookup_dir)
192+
193+
if is_pattern(pattern_path):
194+
template = get_template(pattern_path)
195+
pattern_config = get_pattern_config(pattern_path)
196+
pattern_name = pattern_config.get("name")
197+
pattern_filename = os.path.relpath(
198+
template.origin.template_name,
199+
base_path,
200+
)
201+
if pattern_name:
202+
template.pattern_name = pattern_name
203+
else:
204+
template.pattern_name = pattern_filename
205+
206+
template.pattern_filename = pattern_filename
207+
208+
found_templates.append(template)
209+
210+
if found_templates:
211+
lookup_dir_relpath = os.path.relpath(root, lookup_dir)
212+
sub_folders = os.path.relpath(lookup_dir_relpath, path)
213+
templates_to_store = templates
214+
for folder in [section, *sub_folders.split(os.sep)]:
215+
try:
216+
templates_to_store = templates_to_store["template_groups"][
217+
folder
218+
]
219+
except KeyError:
220+
templates_to_store["template_groups"][folder] = base_dict()
221+
222+
templates_to_store = templates_to_store["template_groups"][
223+
folder
224+
]
225+
226+
templates_to_store["templates_stored"].extend(found_templates)
227+
228+
# Order the templates alphabetically
229+
for templates_objs in templates["template_groups"].values():
230+
templates_objs["template_groups"] = order_dict(
231+
templates_objs["template_groups"]
232+
)
233+
234+
# Order the top level by the sections
235+
section_order = [section for section, _ in get_sections()]
236+
templates["template_groups"] = order_dict(
237+
templates["template_groups"], key_sort=lambda key: section_order.index(key)
238+
)
239+
240+
return templates
241+
242+
class DTLTemplateRenderer(TemplateRenderer):
243+
@classmethod
244+
def get_pattern_source(cls, template):
245+
return escape(template.template.source)
246+
247+
@classmethod
248+
def get_template_ancestors(cls, template_name, context=None, ancestors=None):
249+
"""
250+
Returns a list of template names, starting with provided name
251+
and followed by the names of any templates that extends until
252+
the most extended template is reached.
253+
"""
254+
if ancestors is None:
255+
ancestors = [template_name]
238256

239-
if context is None:
240-
context = Context()
257+
if context is None:
258+
context = Context()
241259

242-
pattern_template = get_template(template_name)
260+
pattern_template = get_template(template_name)
243261

244-
for node in pattern_template.template.nodelist:
245-
if isinstance(node, ExtendsNode):
246-
parent_template_name = node.parent_name.resolve(context)
262+
for node in pattern_template.template.nodelist:
263+
if isinstance(node, ExtendsNode):
264+
parent_template_name = node.parent_name.resolve(context)
265+
ancestors.append(parent_template_name)
266+
cls.get_template_ancestors(
267+
parent_template_name, context=context, ancestors=ancestors
268+
)
269+
break
270+
271+
return ancestors
272+
273+
274+
275+
class JinjaTemplateRenderer(TemplateRenderer):
276+
277+
@classmethod
278+
def get_pattern_source(cls, template):
279+
with open(template.template.filename) as f:
280+
source = escape(f.read())
281+
return source
282+
283+
284+
@classmethod
285+
def get_template_ancestors(cls, template_name, context=None, ancestors=None):
286+
"""
287+
Returns a list of template names, starting with provided name
288+
and followed by the names of any templates that extends until
289+
the most extended template is reached.
290+
"""
291+
from jinja2.nodes import Extends
292+
293+
if ancestors is None:
294+
ancestors = [template_name]
295+
296+
if context is None:
297+
context = Context()
298+
299+
pattern_template = get_template(template_name)
300+
#todo - make sure envrionment has context passed in
301+
environment = pattern_template.template.environment
302+
nodelist = environment.parse(pattern_template.name)
303+
parent_template_name = nodelist.find(Extends)
304+
if parent_template_name:
247305
ancestors.append(parent_template_name)
248-
get_template_ancestors(
249-
parent_template_name, context=context, ancestors=ancestors
250-
)
251-
break
306+
cls.get_template_ancestors(parent_template_name, context=context, ancestors=ancestors)
252307

253-
return ancestors
308+
return ancestors
309+
>>>>>>> 68fea3c (First pass at renderers)

pattern_library/views.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
get_pattern_config_str,
1616
get_pattern_context,
1717
get_pattern_markdown,
18-
get_pattern_templates,
1918
get_sections,
20-
get_template_ancestors,
2119
is_pattern,
2220
render_pattern,
21+
get_renderer,
2322
)
2423

2524

@@ -52,7 +51,8 @@ def get_first_template(self, templates):
5251

5352
def get(self, request, pattern_template_name=None):
5453
# Get all pattern templates
55-
templates = get_pattern_templates()
54+
renderer = get_renderer()
55+
templates = renderer.get_pattern_templates()
5656

5757
if pattern_template_name is None:
5858
# Just display the first pattern if a specific one isn't requested
@@ -67,7 +67,7 @@ def get(self, request, pattern_template_name=None):
6767
context = self.get_context_data()
6868
context["pattern_templates"] = templates
6969
context["pattern_template_name"] = pattern_template_name
70-
context["pattern_source"] = escape(template.template.source)
70+
context["pattern_source"] = renderer.get_pattern_source(template)
7171
context["pattern_config"] = escape(
7272
get_pattern_config_str(pattern_template_name)
7373
)
@@ -83,7 +83,8 @@ class RenderPatternView(TemplateView):
8383

8484
@method_decorator(xframe_options_sameorigin)
8585
def get(self, request, pattern_template_name=None):
86-
pattern_template_ancestors = get_template_ancestors(
86+
renderer = get_renderer()
87+
pattern_template_ancestors = renderer.get_template_ancestors(
8788
pattern_template_name,
8889
context=get_pattern_context(self.kwargs["pattern_template_name"]),
8990
)

0 commit comments

Comments
 (0)