From 2c30a3bb30dce07ecf4c84389225c4823b35f2d6 Mon Sep 17 00:00:00 2001 From: Digital Studium Date: Fri, 28 Jul 2023 20:35:34 +0300 Subject: [PATCH] Add minifiers --- common | 2 +- franca.py | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/common b/common index faba107..f551c17 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit faba107ed11c0ffb1965710e602cab9a1342baf0 +Subproject commit f551c17705ea4be7369423f5d829264911588523 diff --git a/franca.py b/franca.py index 5e209dd..ad89357 100644 --- a/franca.py +++ b/franca.py @@ -10,7 +10,7 @@ import yaml import frontmatter import markdown2 import fire -from PIL import Image +import minify_html from common.functions import * @@ -38,13 +38,13 @@ running = False # нужно для проверки class Develop(FileSystemEventHandler): def on_modified(self, event): print(f'event type: {event.event_type} path : {event.src_path}') - crater() + franca() def on_created(self, event): print(f'event type: {event.event_type} path : {event.src_path}') - crater() + franca() def on_deleted(self, event): print(f'event type: {event.event_type} path : {event.src_path}') - crater() + franca() # Функция для запуска разработки @@ -66,7 +66,7 @@ def develop(prod): pass # Функция для генерации сайта -def crater(prod=False): +def franca(prod=False): # if prod is False, then redefine config.base_url if prod is False: config['base_url'] = "http://127.0.0.1:8000" @@ -124,16 +124,12 @@ def crater(prod=False): if image: os.makedirs(os.path.dirname(f'public{image}'), exist_ok=True) filename = image.split('/')[-1].split('.')[0] - extension = image.split('/')[-1].split('.')[1] - image = Image.open(f'assets{image}') - image.thumbnail((600, 600)) - image.save(f'public/images/{filename}_600.{extension}', optimize=True) - posts[language][section][url]['image'] = f'/images/{filename}_600.{extension}' + create_thumbnail(f'assets{image}', f'public/images/{filename}_600.jpg', 600) + posts[language][section][url]['image'] = f'/images/{filename}_600.jpg' - image.thumbnail((400, 400)) - image.save(f'public/images/{filename}_400.{extension}', optimize=True) - posts[language][section][url]['thumbnail'] = f'/images/{filename}_400.{extension}' + create_thumbnail(f'assets{image}', f'public/images/{filename}_400.jpg', 400) + posts[language][section][url]['thumbnail'] = f'/images/{filename}_400.jpg' for section, urls in posts[language].items(): @@ -141,25 +137,27 @@ def crater(prod=False): html = base.render(config=config, section=section, language=language, posts=posts) - write_file(f"public/{language}/{section}/index.html", html) + write_file(f"public/{language}/{section}/index.html", minify_html.minify(html, minify_js=True)) for url, post in urls.items(): html = base.render(config=config, post=post, language=language, url=url, posts=posts) - write_file(f"public/{language}{url}/index.html", html) + write_file(f"public/{language}{url}/index.html", minify_html.minify(html, minify_js=True)) html = base.render(config=config, posts=posts, language=language, home=True) - write_file(f"public/{language}/index.html", html) + write_file(f"public/{language}/index.html", minify_html.minify(html, minify_js=True)) # copy images/css/js from theme shutil.copytree(f"themes/{config['theme']}/static/images", 'public/images', dirs_exist_ok=True) - copy_file(f"themes/{config['theme']}/static/css/style.css", 'public/css/') + css = read_file(f"themes/{config['theme']}/static/css/style.css") + minify_css('public/css/style.css', css) if 'css_includes' in config: for include in config['css_includes']: - copy_file(f"themes/{config['theme']}/static/css/{include}", 'public/css/') + css = read_file(f"themes/{config['theme']}/static/css/{include}") + minify_css(f'public/css/{include}', css) if 'js_includes' in config: for include in config['js_includes']: copy_file(f"themes/{config['theme']}/static/js/{include}", 'public/js/') @@ -173,15 +171,18 @@ def crater(prod=False): # Write main index.html html = index.render(config=config) - write_file('public/index.html', html) + write_file('public/index.html', minify_html.minify(html, minify_js=True)) # Write robots.txt - robots_content = "User-agent: *\nDisallow: /" - if config.get('search_engines', None) == "allow": - robots_content = robots_content.rstrip("/") + robots_content = "User-agent: *" + if not config.get('search_engines', None) == "allow": + robots_content += "\nDisallow: /" write_file('public/robots.txt', robots_content) + if 'pagefind' in config: + os.system("npx pagefind --source public") # build search index + develop(prod) if __name__ == '__main__': - fire.Fire(crater) + fire.Fire(franca)