This commit is contained in:
caandt 2024-10-01 00:08:13 -05:00
parent e8d2f67a40
commit 50365401d3

View file

@ -23,30 +23,32 @@ def glob_all_to(src, dst):
else:
yield page, Path(f'{dst}/{page.relative_to(src)}')
def minify(html):
from bs4 import BeautifulSoup
import minify_html
soup = BeautifulSoup(html, 'html.parser')
for div in soup.find_all('div'):
div.unwrap()
tab = False
for code in soup.css.select('pre code'):
tab_width = min((x for x in (len(x) - len(x.lstrip(' ')) for x in code.get_text().split('\n')) if x > 1), default=4)
new = re.sub(f'^({" " * tab_width})+', lambda x: '\t' * (len(x.group(0)) // tab_width), code.get_text(), flags=re.M)
if new != code.string:
code.string = new
tab = True
code.unwrap()
if tab:
tab = soup.new_tag('style')
tab.string = '*{tab-size:4}'
soup.insert(1, tab)
for tag in soup.find_all(True):
tag.attrs = {k: v for k, v in tag.attrs.items() if k != 'class'}
return minify_html.minify(str(soup))
def path_write(path, text):
path.parent.mkdir(exist_ok=True, parents=True)
if minimal and path.suffix == '.html':
from bs4 import BeautifulSoup
import minify_html
soup = BeautifulSoup(text, 'html.parser')
for div in soup.find_all('div'):
div.unwrap()
tab = False
for code in soup.css.select('pre code'):
tab_width = min((x for x in (len(x) - len(x.lstrip(' ')) for x in code.get_text().split('\n')) if x > 1), default=4)
new = re.sub(f'^({" " * tab_width})+', lambda x: '\t' * (len(x.group(0)) // tab_width), code.get_text(), flags=re.M)
if new != code.string:
code.string = new
tab = True
code.unwrap()
if tab:
tab = soup.new_tag('style')
tab.string = '*{tab-size:4}'
soup.insert(1, tab)
for tag in soup.find_all(True):
tag.attrs = {k: v for k, v in tag.attrs.items() if k != 'class'}
t = minify_html.minify(str(soup))
path.write_text(t)
path.write_text(minify(text))
else:
path.write_text(text)