From 29a9d253812810448f5fb213115dccf0aab657d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 24 Jul 2022 23:08:38 +0200 Subject: [PATCH] Add auto-wrapping media queries for CSS static resources. --- scripts/blog.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/scripts/blog.py b/scripts/blog.py index 354ef5d..050f0ec 100644 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -42,7 +42,7 @@ ARTICLE_TEMPLATE_NAME = 'article.tmpl.html' STATIC_RESOURCES = ( ('style.css', 'css/style.css'), ('light-syntax.css', 'css/light-syntax.css'), - ('dark-syntax.css', 'css/dark-syntax.css'), + ('dark-syntax.css', 'css/dark-syntax.css', ('@media (prefers-color-scheme: dark) {\n', '\n}')), ) JINJA_ENV = jinja2.Environment( @@ -176,10 +176,22 @@ def regen_all(source_top, dest_top, docs=None): with open(doc_full_path + '.html', 'wt') as f: render_article(doc, front_matter, f) - for src, dest in STATIC_RESOURCES: - target_dest = os.path.join(dest_top, dest) + for static in STATIC_RESOURCES: + src_path = static[0] + dest_path = static[1] + + if len(static) > 2: + before, after = static[2] + else: + before, after = '', '' + target_dest = os.path.join(dest_top, dest_path) os.makedirs(os.path.dirname(target_dest), exist_ok=True) - shutil.copy(os.path.join(STATIC_PATH, src), target_dest) + with open(os.path.join(STATIC_PATH, src_path), 'rt') as src: + data = before + src.read() + after + + with open(target_dest, 'wt') as f: + f.write(data) + return docs @@ -201,10 +213,22 @@ def main(source_top, dest_top): if filepath.startswith(STATIC_PATH): t0 = time.time() update_statics() - for src, dest in STATIC_RESOURCES: - target_dest = os.path.join(dest_top, dest) + for static in STATIC_RESOURCES: + src_path = static[0] + dest_path = static[1] + + if len(static) > 2: + before, after = static[2] + else: + before, after = '', '' + target_dest = os.path.join(dest_top, dest_path) os.makedirs(os.path.dirname(target_dest), exist_ok=True) - shutil.copy(os.path.join(STATIC_PATH, src), target_dest) + with open(os.path.join(STATIC_PATH, src_path), 'rt') as src: + data = before + src.read() + after + + with open(target_dest, 'wt') as f: + f.write(data) + docs = regen_all(source_top, dest_top, docs) logging.info("Updated all in {:.2f}s".format(time.time() - t0))