|
22 | 22 | from pattern_library.exceptions import TemplateIsNotPattern |
23 | 23 |
|
24 | 24 |
|
| 25 | + |
| 26 | +from django.utils.html import escape |
| 27 | + |
25 | 28 | def path_to_section(): |
26 | 29 | section_config = get_sections() |
27 | 30 | sections = {} |
@@ -79,75 +82,6 @@ def get_template_dirs(): |
79 | 82 | return template_dirs |
80 | 83 |
|
81 | 84 |
|
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 |
151 | 85 |
|
152 | 86 |
|
153 | 87 | def get_pattern_config_str(template_name): |
@@ -227,27 +161,149 @@ def render_pattern(request, template_name, allow_non_patterns=False, config=None |
227 | 161 | return render_to_string(template_name, request=request, context=context) |
228 | 162 |
|
229 | 163 |
|
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] |
238 | 256 |
|
239 | | - if context is None: |
240 | | - context = Context() |
| 257 | + if context is None: |
| 258 | + context = Context() |
241 | 259 |
|
242 | | - pattern_template = get_template(template_name) |
| 260 | + pattern_template = get_template(template_name) |
243 | 261 |
|
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: |
247 | 305 | 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) |
252 | 307 |
|
253 | | - return ancestors |
| 308 | + return ancestors |
| 309 | +>>>>>>> 68fea3c (First pass at renderers) |
0 commit comments