ClickHouse/docs/tools/website.py

70 lines
2.0 KiB
Python
Raw Normal View History

import logging
import os
import shutil
import cssmin
import htmlmin
2020-02-18 14:19:44 +00:00
import jinja2
import jsmin
2020-02-18 14:19:44 +00:00
def build_website(args):
logging.info('Building website')
2020-02-18 14:19:44 +00:00
env = jinja2.Environment(
loader=args.output_dir
)
shutil.copytree(
args.website_dir,
args.output_dir,
ignore=shutil.ignore_patterns(
'*.md',
'*.sh',
'build',
'docs',
'public',
'node_modules'
)
)
2020-02-18 14:19:44 +00:00
for root, _, filenames in os.walk(args.output_dir):
for filename in filenames:
path = os.path.join(root, filename)
if not filename.endswith('.html'):
continue
logging.info('Processing %s', path)
with open(path, 'rb') as f:
content = f.read().decode('utf-8')
template = env.from_string(content)
content = template.render(args.__dict__)
with open(path, 'wb') as f:
f.write(content.encode('utf-8'))
def minify_website(args):
if args.minify:
2020-02-18 14:19:44 +00:00
logging.info('Minifying website')
for root, _, filenames in os.walk(args.output_dir):
for filename in filenames:
path = os.path.join(root, filename)
if not (
filename.endswith('.html') or
filename.endswith('.css') or
filename.endswith('.js')
):
continue
2020-02-18 14:19:44 +00:00
logging.info('Minifying %s', path)
with open(path, 'rb') as f:
content = f.read().decode('utf-8')
2020-02-18 14:20:53 +00:00
if filename.endswith('.html'):
content = htmlmin.minify(content, remove_empty_space=False)
elif filename.endswith('.css'):
content = cssmin.cssmin(content)
elif filename.endswith('.js'):
content = jsmin.jsmin(content)
with open(path, 'wb') as f:
f.write(content.encode('utf-8'))