Add minifiers

This commit is contained in:
Digital Studium 2023-07-28 20:35:34 +03:00
parent 024ba1f56a
commit 2c30a3bb30
2 changed files with 25 additions and 24 deletions

2
common

@ -1 +1 @@
Subproject commit faba107ed11c0ffb1965710e602cab9a1342baf0 Subproject commit f551c17705ea4be7369423f5d829264911588523

View File

@ -10,7 +10,7 @@ import yaml
import frontmatter import frontmatter
import markdown2 import markdown2
import fire import fire
from PIL import Image import minify_html
from common.functions import * from common.functions import *
@ -38,13 +38,13 @@ running = False # нужно для проверки
class Develop(FileSystemEventHandler): class Develop(FileSystemEventHandler):
def on_modified(self, event): def on_modified(self, event):
print(f'event type: {event.event_type} path : {event.src_path}') print(f'event type: {event.event_type} path : {event.src_path}')
crater() franca()
def on_created(self, event): def on_created(self, event):
print(f'event type: {event.event_type} path : {event.src_path}') print(f'event type: {event.event_type} path : {event.src_path}')
crater() franca()
def on_deleted(self, event): def on_deleted(self, event):
print(f'event type: {event.event_type} path : {event.src_path}') print(f'event type: {event.event_type} path : {event.src_path}')
crater() franca()
# Функция для запуска разработки # Функция для запуска разработки
@ -66,7 +66,7 @@ def develop(prod):
pass pass
# Функция для генерации сайта # Функция для генерации сайта
def crater(prod=False): def franca(prod=False):
# if prod is False, then redefine config.base_url # if prod is False, then redefine config.base_url
if prod is False: if prod is False:
config['base_url'] = "http://127.0.0.1:8000" config['base_url'] = "http://127.0.0.1:8000"
@ -124,16 +124,12 @@ def crater(prod=False):
if image: if image:
os.makedirs(os.path.dirname(f'public{image}'), exist_ok=True) os.makedirs(os.path.dirname(f'public{image}'), exist_ok=True)
filename = image.split('/')[-1].split('.')[0] filename = image.split('/')[-1].split('.')[0]
extension = image.split('/')[-1].split('.')[1]
image = Image.open(f'assets{image}')
image.thumbnail((600, 600)) create_thumbnail(f'assets{image}', f'public/images/{filename}_600.jpg', 600)
image.save(f'public/images/{filename}_600.{extension}', optimize=True) posts[language][section][url]['image'] = f'/images/{filename}_600.jpg'
posts[language][section][url]['image'] = f'/images/{filename}_600.{extension}'
image.thumbnail((400, 400)) create_thumbnail(f'assets{image}', f'public/images/{filename}_400.jpg', 400)
image.save(f'public/images/{filename}_400.{extension}', optimize=True) posts[language][section][url]['thumbnail'] = f'/images/{filename}_400.jpg'
posts[language][section][url]['thumbnail'] = f'/images/{filename}_400.{extension}'
for section, urls in posts[language].items(): for section, urls in posts[language].items():
@ -141,25 +137,27 @@ def crater(prod=False):
html = base.render(config=config, section=section, html = base.render(config=config, section=section,
language=language, posts=posts) 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(): for url, post in urls.items():
html = base.render(config=config, post=post, html = base.render(config=config, post=post,
language=language, url=url, posts=posts) 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, html = base.render(config=config, posts=posts,
language=language, home=True) 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 # copy images/css/js from theme
shutil.copytree(f"themes/{config['theme']}/static/images", 'public/images', dirs_exist_ok=True) 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: if 'css_includes' in config:
for include in config['css_includes']: 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: if 'js_includes' in config:
for include in config['js_includes']: for include in config['js_includes']:
copy_file(f"themes/{config['theme']}/static/js/{include}", 'public/js/') copy_file(f"themes/{config['theme']}/static/js/{include}", 'public/js/')
@ -173,15 +171,18 @@ def crater(prod=False):
# Write main index.html # Write main index.html
html = index.render(config=config) 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 # Write robots.txt
robots_content = "User-agent: *\nDisallow: /" robots_content = "User-agent: *"
if config.get('search_engines', None) == "allow": if not config.get('search_engines', None) == "allow":
robots_content = robots_content.rstrip("/") robots_content += "\nDisallow: /"
write_file('public/robots.txt', robots_content) write_file('public/robots.txt', robots_content)
if 'pagefind' in config:
os.system("npx pagefind --source public") # build search index
develop(prod) develop(prod)
if __name__ == '__main__': if __name__ == '__main__':
fire.Fire(crater) fire.Fire(franca)