Add cdd minifier

This commit is contained in:
Digital Studium 2023-07-28 20:34:51 +03:00
parent faba107ed1
commit f551c17705
1 changed files with 54 additions and 1 deletions

View File

@ -1,9 +1,12 @@
import os import os
import re
import shutil import shutil
from functools import partial from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path from pathlib import Path
from PIL import Image
def read_file(file_path): def read_file(file_path):
with open(file_path, 'r') as f: with open(file_path, 'r') as f:
@ -12,6 +15,7 @@ def read_file(file_path):
def write_file(file_path, content): def write_file(file_path, content):
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as f: with open(file_path, 'w') as f:
f.write(content) f.write(content)
@ -27,3 +31,52 @@ def start_httpd(directory: Path, port: int = 8000, interface: str = 'localhost')
handler = partial(SimpleHTTPRequestHandler, directory=directory) handler = partial(SimpleHTTPRequestHandler, directory=directory)
httpd = HTTPServer((interface, port), handler) httpd = HTTPServer((interface, port), handler)
httpd.serve_forever() httpd.serve_forever()
# Функция для генерации jpeg миниатюры
def create_thumbnail(source_path, dest_path, size):
with Image.open(source_path) as img:
if img.mode != "RGB":
img = img.convert("RGB")
img.thumbnail((size, size))
img.save(dest_path, "JPEG", optimize=True, quality=80)
# This function was writenn in 2014 by some good guy: https://github.com/bmcculley/minify/blob/master/cssmin.py
def minify_css(saveFileName, css):
os.makedirs(os.path.dirname(saveFileName), exist_ok=True)
# remove comments - this will break a lot of hacks :-P
css = re.sub( r'\s*/\*\s*\*/', "$$HACK1$$", css ) # preserve IE<6 comment hack
css = re.sub( r'/\*[\s\S]*?\*/', "", css )
css = css.replace( "$$HACK1$$", '/**/' ) # preserve IE<6 comment hack
# url() doesn't need quotes
css = re.sub( r'url\((["\'])([^)]*)\1\)', r'url(\2)', css )
# spaces may be safely collapsed as generated content will collapse them anyway
css = re.sub( r'\s+', ' ', css )
# shorten collapsable colors: #aabbcc to #abc
css = re.sub( r'#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3(\s|;)', r'#\1\2\3\4', css )
# fragment values can loose zeros
css = re.sub( r':\s*0(\.\d+([cm]m|e[mx]|in|p[ctx]))\s*;', r':\1;', css )
for rule in re.findall( r'([^{]+){([^}]*)}', css ):
# we don't need spaces around operators
selectors = [re.sub( r'(?<=[\[\(>+=])\s+|\s+(?=[=~^$*|>+\]\)])', r'', selector.strip() ) for selector in rule[0].split( ',' )]
# order is important, but we still want to discard repetitions
properties = {}
porder = []
for prop in re.findall( '(.*?):(.*?)(;|$)', rule[1] ):
key = prop[0].strip().lower()
if key not in porder: porder.append( key )
properties[ key ] = prop[1].strip()
# output rule if it contains any declarations
if properties:
f = open(saveFileName,'a')
f.write( "%s{%s}" % ( ','.join( selectors ), ''.join(['%s:%s;' % (key, properties[key]) for key in porder])[:-1] ) )
f.close()