From 4eebbc47a21f7500f4df6121e218df9404f3f54d Mon Sep 17 00:00:00 2001 From: Herwin Bozet Date: Tue, 4 Mar 2025 20:43:02 +0100 Subject: [PATCH] Replaced 'minify-html' with 'Flask-Minify', the old minifier was terrible Update app.py and requirements.txt --- app.py | 51 ++++++++++++++++++++++++++++-------------------- requirements.txt | 3 ++- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/app.py b/app.py index 652c057..0d1d5df 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import mimetypes import os +import re from html import escape from typing import Optional @@ -30,27 +31,6 @@ except ImportError: pass -if os.environ.get('NP_HTML_POST_PROCESS', "NONE") == "MINIFY": - print("Using 'minify' as HTML post-processor") - - from minify_html import minify - - def post_process_html(html: str) -> str: - return minify(html).replace("> <", "><") -elif os.environ.get('NP_HTML_POST_PROCESS', "NONE") == "BS4": - print("Using 'BeautifulSoup4' as HTML post-processor") - - from bs4 import BeautifulSoup - - def post_process_html(html: str) -> str: - return BeautifulSoup(html, features="html.parser").prettify() -else: - print("Using no HTML post-processor") - - def post_process_html(html: str) -> str: - return html - - app = Flask( import_name=__name__, static_folder='static', @@ -64,6 +44,35 @@ app.jinja_env.strip_trailing_newlines = True mimetypes.add_type('application/javascript', '.mjs') +if os.environ.get('NP_HTML_POST_PROCESS', "NONE") == "MINIFY": + print("Using 'Flask-Minify' as HTML minifying post-processor") + + from flask_minify import Minify + Minify(app=app, html=True, js=True, cssless=True) + + def post_process_html(html_content: str) -> str: + return re.sub(r'\s+', ' ', html_content.replace('\n', '')) + + # This fucking library breaks so much shit it's unbelievable. + # And it takes FOREVER to compile because "MuH rUsT iS sUpErIoR"... + # print("Using 'minify' as HTML post-processor") + # from minify_html import minify + # def post_process_html(html_content: str) -> str: + # return minify(html_content).replace("> <", "><") +elif os.environ.get('NP_HTML_POST_PROCESS', "NONE") == "BS4": + print("Using 'BeautifulSoup4' as HTML non-minifying post-processor") + + from bs4 import BeautifulSoup + + def post_process_html(html_content: str) -> str: + return BeautifulSoup(html_content, features="html.parser").prettify() +else: + print("Using no HTML post-processor") + + def post_process_html(html_content: str) -> str: + return html_content + + @app.after_request def add_common_headers(response): # print(response.headers) diff --git a/requirements.txt b/requirements.txt index 503a550..abe4b00 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ Flask~=3.0.3 Jinja2 MarkupSafe -minify-html +#minify-html +Flask-Minify PyYAML~=6.0.2 beautifulsoup4