From 4f8bfe846f7a5b00aea4ad0cb6908af34cda1844 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 28 Jul 2021 22:50:30 -0400 Subject: [PATCH] Add basic CSS minification. Added correct lang writing.html --- .gitignore | 2 ++ flaskr/__init__.py | 21 ++++++++++++++++++--- flaskr/templates/writing.html | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 22cabc0..447e2be 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,8 @@ temp/ .coverage htmlcov/ +*_min* + dist/ build/ *.egg-info/ diff --git a/flaskr/__init__.py b/flaskr/__init__.py index abfcf1a..17955fe 100644 --- a/flaskr/__init__.py +++ b/flaskr/__init__.py @@ -1,17 +1,32 @@ # Copyright Sage Vaillancourt 2021 import os +import time +import subprocess from flask import ( Flask, redirect, url_for, render_template, send_from_directory ) -import subprocess -import time - import writing +def optimize_css(): + import re + root = os.path.dirname(os.getcwd()) + static_dir = root + '/undercover/flaskr/static/' + css = open(static_dir + 'styles.css', 'r').read() + minified_with_comments = "".join(list(map( + lambda line: line if 'media' in line else line.replace(' ', ''), + css.split('\n') + ))) + minified = re.sub( r'/\*[\s\S]*?\*/', "", minified_with_comments) + minified_file = open(static_dir + 'styles_min.css', 'w') + minified_file.write(minified) + minified_file.close() + def create_app(test_config=None): + optimize_css() + app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', diff --git a/flaskr/templates/writing.html b/flaskr/templates/writing.html index d315d7a..afb4b8c 100644 --- a/flaskr/templates/writing.html +++ b/flaskr/templates/writing.html @@ -1,8 +1,8 @@ - + + {{ url_for('static', filename='styles_min.css') }}">