#!/usr/bin/env python3 import codecs import os from distutils.dir_util import copy_tree import sys from string import Template from ruamel.yaml import YAML __location__ = os.path.realpath(os.path.dirname(__file__)) static_folder = os.path.join(__location__, 'static') content_folder = os.path.join(__location__, 'content') output_folder = os.path.join(__location__, 'dist') config_file = sys.argv[1] output_file = sys.argv[2] yaml = YAML(typ='safe') with codecs.open(config_file, 'r', encoding='utf-8') as stream: try: config = yaml.load(stream) except yaml.YAMLError as exc: print(exc) exit(1) template_item_text = codecs.open(os.path.join(content_folder, 'item.tmpl.html'), 'r', encoding='utf-8').read() template_item = Template(template_item_text) navigation_item_text = codecs.open(os.path.join(content_folder, 'navigation.tmpl.html'), 'r', encoding='utf-8').read() navigation_item = Template(navigation_item_text) items = '' navigation = '' for config_item_1 in config['items']: items += template_item.substitute(config_item_1) for config_item_2 in config['navigation']: navigation += navigation_item.substitute(config_item_2) template_page_text = codecs.open(os.path.join(content_folder, 'index.tmpl.html'), 'r', encoding='utf-8').read() template_page = Template(template_page_text) outcome = template_page.substitute(items=items, nav=navigation) os.makedirs(output_folder, exist_ok=True) outfile_index = os.path.join(output_folder, output_file) copy_tree(static_folder, output_folder) html = codecs.open(outfile_index, 'w', encoding='utf-8').write(outcome)