Improved Docs minify (#17816)

Signed-off-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
pull/17805/head^2
Glenn Jocher 2 days ago committed by GitHub
parent 5b124dc7ca
commit 7c97ed1937
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      .github/workflows/docs.yml
  2. 59
      docs/build_docs.py

@ -48,7 +48,7 @@ jobs:
python-version: "3.x"
- uses: astral-sh/setup-uv@v4
- name: Install Dependencies
run: uv pip install --system ruff black tqdm mkdocs-material "mkdocstrings[python]" mkdocs-jupyter mkdocs-redirects mkdocs-ultralytics-plugin mkdocs-macros-plugin
run: uv pip install --system ruff black tqdm mkdocs-material "mkdocstrings[python]" mkdocs-jupyter mkdocs-redirects mkdocs-ultralytics-plugin mkdocs-macros-plugin csscompressor jsmin
- name: Ruff fixes
continue-on-error: true
run: ruff check --fix --unsafe-fixes --select D --ignore=D100,D104,D203,D205,D212,D213,D401,D406,D407,D413 .

@ -238,32 +238,41 @@ def remove_macros():
print(f"Removed {len(macros_indices)} URLs containing '/macros/' from {sitemap}")
def minify_html_files():
"""Minifies all HTML files in the site directory and prints reduction stats."""
def minify_files(html=True, css=True, js=True):
"""Minifies HTML, CSS, and JS files and prints total reduction stats."""
minify, compress, jsmin = None, None, None
try:
from minify_html import minify # pip install minify-html
except ImportError:
if html:
from minify_html import minify
if css:
from csscompressor import compress
if js:
import jsmin
except ImportError as e:
print(f"Missing required package: {str(e)}")
return
total_original_size = 0
total_minified_size = 0
for html_file in tqdm(SITE.rglob("*.html"), desc="Minifying HTML files"):
with open(html_file, encoding="utf-8") as f:
content = f.read()
original_size = len(content)
minified_content = minify(content, keep_closing_tags=True, minify_css=True, minify_js=True)
minified_size = len(minified_content)
total_original_size += original_size
total_minified_size += minified_size
with open(html_file, "w", encoding="utf-8") as f:
f.write(minified_content)
total_reduction = total_original_size - total_minified_size
total_percent_reduction = (total_reduction / total_original_size) * 100
print(f"Minify HTML reduction: {total_percent_reduction:.2f}% " f"({total_reduction / 1024:.2f} KB saved)")
stats = {}
for ext, minifier in {
"html": (lambda x: minify(x, keep_closing_tags=True, minify_css=True, minify_js=True)) if html else None,
"css": compress if css else None,
"js": jsmin.jsmin if js else None,
}.items():
if not minifier:
continue
stats[ext] = {"original": 0, "minified": 0}
for f in tqdm(SITE.rglob(f"*.{ext}"), desc=f"Minifying {ext.upper()}"):
content = f.read_text(encoding="utf-8")
minified = minifier(content)
stats[ext]["original"] += len(content)
stats[ext]["minified"] += len(minified)
f.write_text(minified, encoding="utf-8")
for ext, data in stats.items():
if data["original"]:
r = data["original"] - data["minified"] # reduction
print(f"Total {ext.upper()} reduction: {(r / data['original']) * 100:.2f}% ({r / 1024:.2f} KB saved)")
def main():
@ -279,8 +288,8 @@ def main():
# Update docs HTML pages
update_docs_html()
# Minify HTML files
minify_html_files()
# Minify files
minify_files(html=False)
# Show command to serve built website
print('Docs built correctly ✅\nServe site at http://localhost:8000 with "python -m http.server --directory site"')

Loading…
Cancel
Save