Enable debug mode in config, enhance HTML response handling with minification, and clean up worklog template by removing commented button
This commit is contained in:
parent
fa37497e3a
commit
f17051224d
5 changed files with 35 additions and 29 deletions
|
|
@ -1,18 +1,30 @@
|
|||
from bs4 import BeautifulSoup
|
||||
from flask import current_app as app
|
||||
import re
|
||||
|
||||
from . import main
|
||||
|
||||
@main.after_request
|
||||
def prettify_html_response(response):
|
||||
if app.debug and response.content_type.startswith("text/html"):
|
||||
def prettify_or_minify_html_response(response):
|
||||
print(app.debug, response.content_type)
|
||||
if response.content_type.startswith("text/html"):
|
||||
try:
|
||||
soup = BeautifulSoup(response.get_data(as_text=True), 'html5lib')
|
||||
pretty_html = soup.prettify()
|
||||
html = response.get_data(as_text=True)
|
||||
soup = BeautifulSoup(html, 'html5lib')
|
||||
|
||||
if app.debug:
|
||||
pretty_html = soup.prettify()
|
||||
response.set_data(pretty_html.encode("utf-8")) # type: ignore
|
||||
else:
|
||||
# Minify by stripping extra whitespace between tags and inside text
|
||||
minified_html = re.sub(r">\s+<", "><", str(soup)) # collapse whitespace between tags
|
||||
minified_html = re.sub(r"\s{2,}", " ", minified_html) # collapse multi-spaces to one
|
||||
minified_html = re.sub(r"\n+", "", minified_html) # remove newlines
|
||||
|
||||
response.set_data(minified_html.encode("utf-8")) # type: ignore
|
||||
|
||||
response.set_data(pretty_html.encode("utf-8")) # type: ignore
|
||||
response.headers['Content-Type'] = 'text/html; charset=utf-8'
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Prettifying failed: {e}")
|
||||
print(f"⚠️ Prettifying/Minifying failed: {e}")
|
||||
|
||||
return response
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue