from pprint import pp from pathlib import Path import re src = Path('src') views = src.joinpath('views') template = views.joinpath('template') module = views.joinpath('module') static = Path('src', 'static') dist = Path('dist') dist.mkdir(exist_ok=True) for x in dist.rglob('*'): if x.is_file() or x.is_symlink(): x.unlink() for x in dist.rglob('*/'): x.rmdir() def read(file_path): with file_path.open() as html_file: return (file_path.stem, html_file.read()) templates = dict(map(read, template.glob('*.html'))) modules = dict(map(read, module.glob('*.html'))) def format(item, modules): name, content = item content = content.format(**modules) content = re.sub(r'\s+', ' ', content) content = re.sub(r'>\s<', '><', content) return (name, content) dist_html = dict(map(lambda item: format(item, modules), templates.items())) def mk_dist(dist, item): key, content = item path = dist if key != 'index': path = dist.joinpath(key) path.mkdir(exist_ok=True) with path.joinpath('index.html').open('w') as index: index.write(content) for item in dist_html.items(): mk_dist(dist, item) dist.joinpath('static')\ .symlink_to(static.resolve(strict=True), target_is_directory=True)