-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add format-html command * Add format-html command in doc --------- Co-authored-by: Clément <[email protected]>
- Loading branch information
Showing
5 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters