Skip to content

Commit

Permalink
Add warning for ambigous macro names
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 23, 2024
1 parent ab1daca commit 66656cd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
30 changes: 30 additions & 0 deletions content/templates/jinja2/widgets/test/navbar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% macro navbar(
CTA_LABEL="",
CTA_TARGET="",
CTA_URL="",
CHANGE_LANG_URL="",
CHANGE_LANG_FLAG_URL="",
CHANGE_LANG_ALT=""
)
%}

<!-- Navigation-->
<nav class="navbar navbar-light bg-light static-top">
<div class="container">
<a class="navbar-brand" href="#">
<img class="navbar__logo__image" src="{{ static('assets/logo-galae.svg')}}"
alt="galae"/>
<div class="navbar__logo__label">le service e-mail qui vous veut du bien</div>
</a>
<div class="navbar__container__right">
<a class="btn btn-sm btn-primary" href="{{ CTA_URL }}"
target="{{ CTA_TARGET }}">{{ CTA_LABEL }}</a>
<a href="{{ url_for_slug_path(CHANGE_LANG_URL) }}">
<img class="languageFlag" src="{{ static(CHANGE_LANG_FLAG_URL)}}" alt="{{ CHANGE_LANG_ALT }}"/>
</a>
</div>
</div>
</nav>

{% endmacro %}

30 changes: 30 additions & 0 deletions content/templates/jinja2/widgets/test/page_header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% macro page_header(
PRIMARY_TITLE="",
SECONDARY_TITLE="",
THIRD_TITLE=""
)
%}

<!-- Masthead-->
<header class="masthead">
<div class="container position-relative">
<div class="row justify-content-center">
<div class="col-xl-9">
<div class="text-center text-white">
<!-- Page heading-->
<h1 class="mb-5">
{{ PRIMARY_TITLE|safe }}
</h1>
{% if SECONDARY_TITLE %}
<h2>{{ SECONDARY_TITLE|safe }}</h2>
{% endif %}
{% if THIRD_TITLE %}
<h3 class="navbar__logo__label"><i>{{ THIRD_TITLE|safe }}</i></h3>
{% endif %}
</div>
</div>
</div>
</div>
</header>

{% endmacro %}
16 changes: 15 additions & 1 deletion jssg/management/commands/list-widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down

0 comments on commit 66656cd

Please sign in to comment.