From aefb8507e7fc441ff180495359ee0c033b6a8e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 10 Jul 2024 11:47:54 +0200 Subject: [PATCH] get only macros in widgets and remove allwidgets for Django (no macros in Django templates) --- README.md | 5 +- content/templates/jinja2/allwidgets.html | 2 - jssg/management/commands/distill-local.py | 3 +- .../{listwidgets.py => list-widgets.py} | 6 +- jssg/management/commands/make-widgets.py | 39 +++++++++++++ jssg/management/commands/makewidgets.py | 56 ------------------- jssg/management/commands/runserver.py | 3 +- 7 files changed, 47 insertions(+), 67 deletions(-) rename jssg/management/commands/{listwidgets.py => list-widgets.py} (89%) create mode 100644 jssg/management/commands/make-widgets.py delete mode 100644 jssg/management/commands/makewidgets.py diff --git a/README.md b/README.md index 17fee4b..8d5d3a3 100644 --- a/README.md +++ b/README.md @@ -109,9 +109,10 @@ For each command, the option `-h` give u some help. `./manage.py distill-local` to make the static website, see [Prod](#prod) for usage - `./manage.py listwidgets` to list all widgets found in content directories + `./manage.py list-widgets` to list all widgets found in content directories - `./manage.py makewidgets` to make a file that groups all widgets 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 called by `runserver` and `distill-local` commands. \ + See an example in `EXAMPLE.md` ## Others diff --git a/content/templates/jinja2/allwidgets.html b/content/templates/jinja2/allwidgets.html index e5870e8..dcc18fc 100644 --- a/content/templates/jinja2/allwidgets.html +++ b/content/templates/jinja2/allwidgets.html @@ -27,8 +27,6 @@ {% endmacro %} - - {% macro page_block_h2_with_ul_content_and_image_left( IMAGE_URL = '', TITLE = '', diff --git a/jssg/management/commands/distill-local.py b/jssg/management/commands/distill-local.py index a4e2e2f..8b84da6 100644 --- a/jssg/management/commands/distill-local.py +++ b/jssg/management/commands/distill-local.py @@ -4,6 +4,5 @@ class Command(DistilllocalCommand): def handle(self, *args, **options): - call_command('makewidgets', 'jinja2') - call_command('makewidgets', 'django') + call_command('make-widgets') super(Command, self).handle(*args, **options) \ No newline at end of file diff --git a/jssg/management/commands/listwidgets.py b/jssg/management/commands/list-widgets.py similarity index 89% rename from jssg/management/commands/listwidgets.py rename to jssg/management/commands/list-widgets.py index 766ca4b..74627f4 100644 --- a/jssg/management/commands/listwidgets.py +++ b/jssg/management/commands/list-widgets.py @@ -16,12 +16,12 @@ def add_arguments(self, parser): def handle(self, *args, **options) : if options["engine"] != "jinja2" and options["engine"] != "django" and options["engine"] != "" : - call_command("listwidgets", "--help") + call_command("list-widgets", "--help") return if options["engine"] == "" : - call_command("listwidgets", "jinja2") - call_command("listwidgets", "django") + call_command("list-widgets", "jinja2") + call_command("list-widgets", "django") return n = 0 diff --git a/jssg/management/commands/make-widgets.py b/jssg/management/commands/make-widgets.py new file mode 100644 index 0000000..2c225ba --- /dev/null +++ b/jssg/management/commands/make-widgets.py @@ -0,0 +1,39 @@ +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 = "Make a file which contains all widgets macros for easier import in templates and pages." + + def handle(self, *args, **options): + + path = settings.BASE_DIR / "content" / "templates" / "jinja2" / "allwidgets.html" + buf = "" + n = 0 + for template_dir in settings.JFME_TEMPLATES_DIRS : + for widget in (template_dir / "jinja2" / "widgets").rglob("*") : + if widget.is_file() : + with open(widget, "r") as w : + for macro in findall(r"{%[\s]*macro[\s\S]*%}[\s\S]*{%[\s]*endmacro[\s]*%}", w.read()) : + buf += macro + "\n" + n += 1 + + if n == 0 : + self.stdout.write( + "0 widget macro written" + ) + else : + path.parent.mkdir(exist_ok=True, parents=True) + with open(path, "w+") as f : + f.write(buf) + if n == 1 : + self.stdout.write( + "1 widget macro written in %s" % + (path.relative_to(settings.BASE_DIR)) + ) + else : + self.stdout.write( + "%d widget macros written in %s" % + (n, path.relative_to(settings.BASE_DIR)) + ) \ No newline at end of file diff --git a/jssg/management/commands/makewidgets.py b/jssg/management/commands/makewidgets.py deleted file mode 100644 index ee39af6..0000000 --- a/jssg/management/commands/makewidgets.py +++ /dev/null @@ -1,56 +0,0 @@ -from django.core.management.base import BaseCommand -from django.core.management import call_command -from django.conf import settings - -class Command(BaseCommand): - help = "Make a file which contains all widgets for easier import in templates and pages." - - def add_arguments(self, parser): - parser.add_argument( - "engine", - nargs = '?', - type=str, - default="", - help="The template engine or . If not given, make for both engines." - ) - - def handle(self, *args, **options): - if options["engine"] != "jinja2" and options["engine"] != "django" and options["engine"] != "" : - call_command("makewidgets", "--help") - return - - if options["engine"] == "" : - call_command("makewidgets", "jinja2") - call_command("makewidgets", "django") - return - - path = settings.BASE_DIR / "content" / "templates" / options["engine"] / ("allwidgets.html") - buf = "" - n = 0 - for template_dir in settings.JFME_TEMPLATES_DIRS : - for widget in (template_dir / options["engine"] / "widgets").rglob("*") : - if widget.is_file() : - w = open(widget, "r") - buf += w.read() + "\n" - w.close() - n += 1 - - if n == 0 : - self.stdout.write( - "0 %s widget written" % - options["engine"] - ) - else : - path.parent.mkdir(exist_ok=True, parents=True) - with open(path, "w+") as f : - f.write(buf) - if n == 1 : - self.stdout.write( - "1 %s widget written in %s" % - (options["engine"], path.relative_to(settings.BASE_DIR)) - ) - else : - self.stdout.write( - "%d %s widgets written in %s" % - (n, options["engine"], path.relative_to(settings.BASE_DIR)) - ) \ No newline at end of file diff --git a/jssg/management/commands/runserver.py b/jssg/management/commands/runserver.py index e28355c..cbf5de5 100644 --- a/jssg/management/commands/runserver.py +++ b/jssg/management/commands/runserver.py @@ -3,6 +3,5 @@ class Command(RunserverCommand): def inner_run(self, *args, **options): - call_command('makewidgets', 'jinja2') - call_command('makewidgets', 'django') + call_command('make-widgets') super(Command, self).inner_run(*args, **options) \ No newline at end of file