From f2d61d2daa38065e4d6d3c80e2e1b80374992a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 12 Jul 2024 17:10:14 +0200 Subject: [PATCH] list-widgets : count all the macros and not only files --- README.md | 2 +- jssg/management/commands/distill-local.py | 1 + jssg/management/commands/list-widgets.py | 11 ++++++++--- jssg/management/commands/runserver.py | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8d5d3a3..c1317d7 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ For each command, the option `-h` give u some help. `./manage.py list-widgets` to list all widgets found in content directories - `./manage.py make-widgets` to make a file that groups all jinja2 widgets macros for easier includes. It is called by `runserver` and `distill-local` commands. \ + `./manage.py make-widgets` to make a file that groups all jinja2 widgets macros for easier includes. It is automatically called by `runserver` and `distill-local` commands. \ See an example in `EXAMPLE.md` diff --git a/jssg/management/commands/distill-local.py b/jssg/management/commands/distill-local.py index 8b84da6..e53be23 100644 --- a/jssg/management/commands/distill-local.py +++ b/jssg/management/commands/distill-local.py @@ -2,6 +2,7 @@ DistilllocalCommand = importlib.import_module("django_distill.management.commands.distill-local").Command from django.core.management import call_command +#Override the default distill-local command to call the make-widget before it execute class Command(DistilllocalCommand): def handle(self, *args, **options): call_command('make-widgets') diff --git a/jssg/management/commands/list-widgets.py b/jssg/management/commands/list-widgets.py index 74627f4..7d11861 100644 --- a/jssg/management/commands/list-widgets.py +++ b/jssg/management/commands/list-widgets.py @@ -1,6 +1,7 @@ from django.core.management.base import BaseCommand from django.core.management import call_command from django.conf import settings +from re import findall class Command(BaseCommand): help = "List all the widgets found in content templates." @@ -27,9 +28,13 @@ def handle(self, *args, **options) : n = 0 for template_dir in settings.JFME_TEMPLATES_DIRS : for widget in (template_dir / options["engine"] / "widgets").rglob("*") : - if widget.is_file() : - self.stdout.write(str(widget.relative_to(settings.BASE_DIR))) - n += 1 + with open(widget, "r") as w : + self.stdout.write("In %s : " % str(widget)) + i = 0 + for macro in findall(r"{%[\s]*macro[\s]*(.*)\(", w.read()) : + self.stdout.write("\tmacro '%s()'" % macro) + i += 1 + n += i if n > 1 : self.stdout.write( diff --git a/jssg/management/commands/runserver.py b/jssg/management/commands/runserver.py index cbf5de5..5113617 100644 --- a/jssg/management/commands/runserver.py +++ b/jssg/management/commands/runserver.py @@ -1,6 +1,7 @@ from django.core.management.commands.runserver import Command as RunserverCommand from django.core.management import call_command +#Override the default runserver command to call the make-widget before it execute class Command(RunserverCommand): def inner_run(self, *args, **options): call_command('make-widgets')