Skip to content

Commit

Permalink
get only macros in widgets and remove allwidgets for Django (no macro…
Browse files Browse the repository at this point in the history
…s in Django templates)
  • Loading branch information
Clément committed Jul 10, 2024
1 parent 3e8c153 commit aefb850
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 67 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions content/templates/jinja2/allwidgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
</nav>

{% endmacro %}


{% macro page_block_h2_with_ul_content_and_image_left(
IMAGE_URL = '',
TITLE = '',
Expand Down
3 changes: 1 addition & 2 deletions jssg/management/commands/distill-local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions jssg/management/commands/make-widgets.py
Original file line number Diff line number Diff line change
@@ -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))
)
56 changes: 0 additions & 56 deletions jssg/management/commands/makewidgets.py

This file was deleted.

3 changes: 1 addition & 2 deletions jssg/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit aefb850

Please sign in to comment.