From 66656cd648bd2baa7aa83fb1ecedb940cb389c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 23 Jul 2024 15:10:30 +0200 Subject: [PATCH] Add warning for ambigous macro names --- .../templates/jinja2/widgets/test/navbar.html | 30 +++++++++++++++++++ .../jinja2/widgets/test/page_header.html | 30 +++++++++++++++++++ jssg/management/commands/list-widgets.py | 16 +++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 content/templates/jinja2/widgets/test/navbar.html create mode 100644 content/templates/jinja2/widgets/test/page_header.html diff --git a/content/templates/jinja2/widgets/test/navbar.html b/content/templates/jinja2/widgets/test/navbar.html new file mode 100644 index 0000000..dada97a --- /dev/null +++ b/content/templates/jinja2/widgets/test/navbar.html @@ -0,0 +1,30 @@ +{% macro navbar( + CTA_LABEL="", + CTA_TARGET="", + CTA_URL="", + CHANGE_LANG_URL="", + CHANGE_LANG_FLAG_URL="", + CHANGE_LANG_ALT="" +) +%} + + + + +{% endmacro %} + diff --git a/content/templates/jinja2/widgets/test/page_header.html b/content/templates/jinja2/widgets/test/page_header.html new file mode 100644 index 0000000..b608435 --- /dev/null +++ b/content/templates/jinja2/widgets/test/page_header.html @@ -0,0 +1,30 @@ +{% macro page_header( + PRIMARY_TITLE="", + SECONDARY_TITLE="", + THIRD_TITLE="" +) +%} + + +
+
+
+
+
+ +

+ {{ PRIMARY_TITLE|safe }} +

+ {% if SECONDARY_TITLE %} +

{{ SECONDARY_TITLE|safe }}

+ {% endif %} + {% if THIRD_TITLE %} + + {% endif %} +
+
+
+
+
+ +{% endmacro %} \ No newline at end of file diff --git a/jssg/management/commands/list-widgets.py b/jssg/management/commands/list-widgets.py index 6ad906f..50ca1f4 100644 --- a/jssg/management/commands/list-widgets.py +++ b/jssg/management/commands/list-widgets.py @@ -46,13 +46,16 @@ def handle(self, *args, **options) : elif options["jinja2"] : nb_file_found = 0 nb_macro_found = 0 + visited = [] for template_dir in settings.JFME_TEMPLATES_DIRS : for widget in (template_dir / "jinja2" / "widgets").rglob("*") : + rel_widget_path = widget.relative_to(template_dir.parent.parent) if widget.is_file() : with open(widget, "r") as w : - self.stdout.write("%s" % str(widget.relative_to(template_dir.parent.parent))) + self.stdout.write("%s" % str(rel_widget_path)) nb_file_found += 1 for macro in Environment().parse(w.read()).find_all(Macro) : + visited.append((widget.stem, macro.name, rel_widget_path)) self.stdout.write("\t%s()" % macro.name) nb_macro_found += 1 @@ -65,6 +68,17 @@ def handle(self, *args, **options) : "macro" if nb_file_found <= 1 else "macros" ) )) + couple_visited = [(x[0], x[1]) for x in visited] + duplicates = set([t for t in couple_visited if couple_visited.count(t) > 1]) # get the (widget.stem, macro.name) doublons + for duplicate in duplicates : + self.stdout.write(self.style.WARNING( + "Warning : macro '%s' in a file named '%s' found more than once, could be ambiguous ; found in :" % (duplicate[0], duplicate[1]) + )) + paths = [x[2] for x in filter(lambda t : (t[0], t[1]) == duplicate, visited)] # get the paths corresponding to (widget.stem, macro.name) + for p in paths : + self.stdout.write(self.style.WARNING( + "\t- %s" % p + )) else : call_command("list-widgets", "-django")