Skip to content

Commit

Permalink
Add format-html command (#42)
Browse files Browse the repository at this point in the history
* Add format-html command

* Add format-html command in doc

---------

Co-authored-by: Clément <[email protected]>
  • Loading branch information
ClmntBcqt and Clément authored Jul 24, 2024
1 parent bd113f9 commit 1042e9d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ For each command, the option `-h` give u some help.
`./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`

`./manage.py format-html <action>` to minify or beautify the html content (`<action>` being `minify` or `beautify`)

## Others

JFM-Engine is a friendly fork of [JSSG](https://github.com/jtremesay/jssg/) made in agreement with the JSSG developer because of different goals. \
Expand Down
2 changes: 1 addition & 1 deletion content/templates/jinja2/allwidgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="navbar__container__right">
<a class="btn btn-sm btn-primary" href="{{ CTA_URL }}"
target="{{ CTA_TARGET }}">{{ CTA_LABEL }}</a>
<a href="{{ url('page', args=[CHANGE_LANG_URL]) }}">
<a href="{{ url_for_slug_path(CHANGE_LANG_URL) }}">
<img class="languageFlag" src="{{ static(CHANGE_LANG_FLAG_URL)}}" alt="{{ CHANGE_LANG_ALT }}"/>
</a>
</div>
Expand Down
33 changes: 33 additions & 0 deletions jssg/management/commands/format-html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.core.management.base import BaseCommand
from pathlib import Path
from bs4 import BeautifulSoup
from django.conf import settings

class Command(BaseCommand):
help = "Format (beautify or minify) the html files in dist content"

def add_arguments(self, parser):
parser.add_argument(
"mode",
choices=["beautify", "minify"],
type=str,
help="Beautify or minify the html files"
),
parser.add_argument(
"distpath",
nargs='?',
type=str,
default=str(settings.DIST_DIR),
help="To specify a particular dist path. Default is: " + str(settings.DIST_DIR)
)

def handle(self, *args, **options) :
for path in Path(options["distpath"]).rglob("*.html") :
with open(path, "r+") as file :
soup = BeautifulSoup(file.read(), 'html.parser')
file.seek(0)
if options["mode"] == "minify" :
file.write(str(soup).replace('\n', ''))
else :
file.write(soup.prettify())
file.truncate()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ django_vite_plugin==3.0.0
markdown2[all]==2.4.13
whitenoise==6.7.0
Jinja2==3.1.4
beautifulsoup4==4.12.3
django-jinja-markdown
12 changes: 6 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"@/*": [
"./*"
],
"@s:jssg/*": [
"./jssg/static/jssg/*"
],
"@t:jssg/*": [
"./jssg/templates/jssg/*"
],
"@s:django_jinja_markdown/*": [
"./env/lib/python3.9/site-packages/django_jinja_markdown/static/django_jinja_markdown/*"
],
Expand All @@ -35,12 +41,6 @@
],
"@t:django_distill/*": [
"./env/lib/python3.9/site-packages/django_distill/templates/django_distill/*"
],
"@s:jssg/*": [
"./jssg/static/jssg/*"
],
"@t:jssg/*": [
"./jssg/templates/jssg/*"
]
}
},
Expand Down

0 comments on commit 1042e9d

Please sign in to comment.