diff --git a/build.py b/build.py index ba94f1d..df8309e 100644 --- a/build.py +++ b/build.py @@ -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)