diff --git a/pattern_library/loader_tags.py b/pattern_library/loader_tags.py index 4cc6a9b..80dd111 100644 --- a/pattern_library/loader_tags.py +++ b/pattern_library/loader_tags.py @@ -151,9 +151,10 @@ def do_include(parser, token): isolated_context=isolated_context, ) + def visit_extends(self, node, frame): """This method overrides the jinja extends tag - Is called as part of the compiler CodeGenerator + Is called as part of the compiler CodeGenerator and adds a line to use the template_new_context as part of the runtime render to pull in the dpl context Handles visiting extends @@ -196,4 +197,4 @@ def template_new_context( return new_context( self.environment, self.name, self.blocks, vars, shared, self.globals, locals - ) \ No newline at end of file + ) diff --git a/pattern_library/monkey_utils.py b/pattern_library/monkey_utils.py index c3f3bcb..2da37f4 100644 --- a/pattern_library/monkey_utils.py +++ b/pattern_library/monkey_utils.py @@ -118,14 +118,16 @@ def node_render(context): return tag_func + # have to export the original jinja visit Extends # in the case jinja tags are being overriden jinja_visit_Extends = None + def override_jinja_tags(): """ Overrides jinja extends and include tags for use in your pattern library. - Call it in your settings to override tags + Call it in your settings to override tags """ global jinja_visit_Extends try: @@ -133,8 +135,9 @@ def override_jinja_tags(): from jinja2.environment import Template as JinjaTemplate except ModuleNotFoundError: ModuleNotFoundError("install jinja2 to override jinja tags") - + from .loader_tags import template_new_context, visit_extends + jinja_visit_Extends = JinjaCodeGenerator.visit_Extends JinjaTemplate.new_context = template_new_context - JinjaCodeGenerator.visit_Extends = visit_extends \ No newline at end of file + JinjaCodeGenerator.visit_Extends = visit_extends diff --git a/pattern_library/utils.py b/pattern_library/utils.py index 3edc5dd..1edf1f3 100644 --- a/pattern_library/utils.py +++ b/pattern_library/utils.py @@ -22,9 +22,9 @@ from pattern_library.exceptions import TemplateIsNotPattern - from django.utils.html import escape + def path_to_section(): section_config = get_sections() sections = {} @@ -243,7 +243,9 @@ def get_pattern_source(cls, template): @classmethod def get_template_ancestors(cls, template_name, context=None): template = get_template(template_name) - return cls._get_engine(template).get_template_ancestors(template_name, context=context) + return cls._get_engine(template).get_template_ancestors( + template_name, context=context + ) @classmethod def _get_engine(cls, template): @@ -251,6 +253,7 @@ def _get_engine(cls, template): return JinjaTemplateRenderer return DTLTemplateRenderer + class DTLTemplateRenderer: @staticmethod def get_pattern_source(template): @@ -287,7 +290,7 @@ class JinjaTemplateRenderer: @staticmethod def get_pattern_source(template): with open(template.template.filename) as f: - source = escape(f.read()) + source = escape(f.read()) return source @classmethod @@ -306,12 +309,14 @@ def get_template_ancestors(cls, template_name, context=None, ancestors=None): context = Context() pattern_template = get_template(template_name) - #todo - make sure envrionment has context passed in + # todo - make sure envrionment has context passed in environment = pattern_template.template.environment nodelist = environment.parse(pattern_template.template.name) parent_template_name = nodelist.find(Extends) if parent_template_name: ancestors.append(parent_template_name) - cls.get_template_ancestors(parent_template_name, context=context, ancestors=ancestors) + cls.get_template_ancestors( + parent_template_name, context=context, ancestors=ancestors + ) return ancestors diff --git a/tests/tests/test_context_modifiers.py b/tests/tests/test_context_modifiers.py index 12ddd9f..1fcb745 100644 --- a/tests/tests/test_context_modifiers.py +++ b/tests/tests/test_context_modifiers.py @@ -33,7 +33,6 @@ def modifier_3(context, request): class ContextModifierTestCase(SimpleTestCase): - maxDiff = None def setUp(self): diff --git a/tests/tests/test_utils.py b/tests/tests/test_utils.py index 7c05163..682f9ae 100644 --- a/tests/tests/test_utils.py +++ b/tests/tests/test_utils.py @@ -13,9 +13,12 @@ class TestGetTemplateAncestors(SimpleTestCase): def setUp(self): self.renderer = get_renderer() + def test_page(self): self.assertEqual( - self.renderer.get_template_ancestors("patterns/pages/test_page/test_page.html"), + self.renderer.get_template_ancestors( + "patterns/pages/test_page/test_page.html" + ), [ "patterns/pages/test_page/test_page.html", "patterns/base_page.html", @@ -25,7 +28,9 @@ def test_page(self): def test_fragment(self): self.assertEqual( - self.renderer.get_template_ancestors("patterns/atoms/test_atom/test_atom.html"), + self.renderer.get_template_ancestors( + "patterns/atoms/test_atom/test_atom.html" + ), [ "patterns/atoms/test_atom/test_atom.html", ],