From 777575619c037c5c231e89e7dc784a66d10079b4 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 28 Sep 2022 01:55:10 +0000 Subject: [PATCH 1/3] Add and use Flask-Minify. --- requirements.txt | 3 ++- undercover/__init__.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 18bfade..39a47aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,9 @@ bcrypt==4.0.0 click==8.1.3 email-validator==1.3.0 Flask==2.2.2 -Flask-WTF==1.0.1 +Flask-Minify=0.39 Flask-SQLAlchemy==2.5.1 +Flask-WTF==1.0.1 gunicorn==20.1.0 itsdangerous==2.1.2 Jinja2==3.1.2 diff --git a/undercover/__init__.py b/undercover/__init__.py index d6ea145..262f658 100644 --- a/undercover/__init__.py +++ b/undercover/__init__.py @@ -4,6 +4,7 @@ import os import sys from flask import Flask +from flask_minify import Minify import undercover.routes from undercover.css import optimize_css_file @@ -13,6 +14,7 @@ def create_app(test_config=None): optimize_css_file() app = Flask(__name__, instance_relative_config=True) + Minify(app=app, html=True, js=True, cssless=True) secret_key = os.environ.get('UNDERCOVER_SECRET_KEY') if not secret_key: sys.stderr.write("WARNING: UNDERCOVER_SECRET_KEY is not defined! Application may be insecure.\n") From 4915afa7509b5718eed4bb6b09c5badd4bbc1dc8 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 28 Sep 2022 01:56:52 +0000 Subject: [PATCH 2/3] Fix requirements syntax --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 39a47aa..4dc78e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ bcrypt==4.0.0 click==8.1.3 email-validator==1.3.0 Flask==2.2.2 -Flask-Minify=0.39 +Flask-Minify==0.39 Flask-SQLAlchemy==2.5.1 Flask-WTF==1.0.1 gunicorn==20.1.0 From 82faabd34a1c846ba7c574bc12f7d385d4cc501b Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 28 Sep 2022 02:00:34 +0000 Subject: [PATCH 3/3] Remove previous css minification --- tests/test_css.py | 23 ----------------------- undercover/__init__.py | 3 --- undercover/css.py | 19 ------------------- undercover/templates/base.jinja2 | 2 +- 4 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 tests/test_css.py delete mode 100644 undercover/css.py diff --git a/tests/test_css.py b/tests/test_css.py deleted file mode 100644 index af9dc40..0000000 --- a/tests/test_css.py +++ /dev/null @@ -1,23 +0,0 @@ -from undercover import css - - -def test_empty_string(): - assert css.optimize_css("") == "" - - -def test_simple_definition(): - simple_definition = """ - div { - width: 100%; - } - """ - assert css.optimize_css(simple_definition) == "div{width:100%;}" - - -def test_multi_part_property(): - simple_definition = """ - div { - padding: 1em 2em 3em 4em; - } - """ - assert css.optimize_css(simple_definition) == "div{padding:1em 2em 3em 4em;}" diff --git a/undercover/__init__.py b/undercover/__init__.py index 262f658..c69fbf9 100644 --- a/undercover/__init__.py +++ b/undercover/__init__.py @@ -7,12 +7,9 @@ from flask import Flask from flask_minify import Minify import undercover.routes -from undercover.css import optimize_css_file def create_app(test_config=None): - optimize_css_file() - app = Flask(__name__, instance_relative_config=True) Minify(app=app, html=True, js=True, cssless=True) secret_key = os.environ.get('UNDERCOVER_SECRET_KEY') diff --git a/undercover/css.py b/undercover/css.py deleted file mode 100644 index b2b328a..0000000 --- a/undercover/css.py +++ /dev/null @@ -1,19 +0,0 @@ -import os - - -def optimize_css(css: str) -> str: - import re - minified_with_comments = "".join(list(map( - lambda line: re.sub(r'\s*([:{])\s*', r'\1', re.sub(r'^\s*', '', line)), - css.split('\n') - ))) - return re.sub(r'/\*[\s\S]*?\*/', "", minified_with_comments) - - -def optimize_css_file(): - root = os.path.dirname(os.getcwd()) - static_dir = root + '/undercover/undercover/static/' - css = open(static_dir + 'styles.css', 'r').read() - minified = optimize_css(css) - with open(static_dir + 'styles_min.css', 'w') as minified_file: - minified_file.write(minified) diff --git a/undercover/templates/base.jinja2 b/undercover/templates/base.jinja2 index 89bee77..04a760e 100644 --- a/undercover/templates/base.jinja2 +++ b/undercover/templates/base.jinja2 @@ -3,7 +3,7 @@ {% block title %}UnderCover{% endblock title %} - + {% block head %}{{ head }}{% endblock head %}