Trying agin without using packages that require Rust.
This commit is contained in:
parent
5f79d32fbb
commit
9695449e79
2 changed files with 20 additions and 9 deletions
26
minify.py
26
minify.py
|
|
@ -1,25 +1,35 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Minify CSS, JS, and HTML files in the deploy directory in-place."""
|
"""Minify CSS, JS, and HTML files in the deploy directory in-place."""
|
||||||
|
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import minify_html
|
import rcssmin
|
||||||
|
import rjsmin
|
||||||
|
|
||||||
DEPLOY_DIR = Path("./deploy")
|
DEPLOY_DIR = Path("./deploy")
|
||||||
|
|
||||||
|
|
||||||
|
def minify_html_content(content):
|
||||||
|
content = re.sub(r"<!--.*?-->", "", content, flags=re.DOTALL) # remove comments
|
||||||
|
content = re.sub(r">\s+<", "><", content) # collapse whitespace between tags
|
||||||
|
content = re.sub(r"\s{2,}", " ", content) # collapse remaining whitespace
|
||||||
|
return content.strip()
|
||||||
|
|
||||||
|
|
||||||
def minify_files():
|
def minify_files():
|
||||||
|
handlers = {
|
||||||
|
".css": lambda c: rcssmin.cssmin(c),
|
||||||
|
".js": lambda c: rjsmin.jsmin(c, keep_bang_comments=False),
|
||||||
|
".html": minify_html_content,
|
||||||
|
}
|
||||||
for file in DEPLOY_DIR.rglob("*"):
|
for file in DEPLOY_DIR.rglob("*"):
|
||||||
if file.suffix not in {".html", ".css", ".js"}:
|
handler = handlers.get(file.suffix)
|
||||||
|
if not handler:
|
||||||
continue
|
continue
|
||||||
content = file.read_text(encoding="utf-8")
|
content = file.read_text(encoding="utf-8")
|
||||||
minified = minify_html.minify(
|
file.write_text(handler(content), encoding="utf-8")
|
||||||
content,
|
|
||||||
minify_js=True,
|
|
||||||
minify_css=True,
|
|
||||||
)
|
|
||||||
file.write_text(minified, encoding="utf-8")
|
|
||||||
print(f"{file.suffix.upper().rjust(5)}: {file}")
|
print(f"{file.suffix.upper().rjust(5)}: {file}")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ Jinja2==3.1.6
|
||||||
Markdown==3.10.2
|
Markdown==3.10.2
|
||||||
MarkupSafe==3.0.3
|
MarkupSafe==3.0.3
|
||||||
mergedeep==1.3.4
|
mergedeep==1.3.4
|
||||||
minify-html==0.15.0
|
|
||||||
mkdocs==1.6.1
|
mkdocs==1.6.1
|
||||||
mkdocs-autorefs==1.4.4
|
mkdocs-autorefs==1.4.4
|
||||||
mkdocs-get-deps==0.2.2
|
mkdocs-get-deps==0.2.2
|
||||||
|
|
@ -23,6 +22,8 @@ pymdown-extensions==10.21
|
||||||
python-dateutil==2.9.0.post0
|
python-dateutil==2.9.0.post0
|
||||||
PyYAML==6.0.3
|
PyYAML==6.0.3
|
||||||
pyyaml_env_tag==1.1
|
pyyaml_env_tag==1.1
|
||||||
|
rcssmin==1.2.2
|
||||||
|
rjsmin==1.2.4
|
||||||
six==1.17.0
|
six==1.17.0
|
||||||
watchdog==6.0.0
|
watchdog==6.0.0
|
||||||
zensical==0.0.27
|
zensical==0.0.27
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue