all repos — py-vite @ 9b7ca8070dbe98b914544b321bf27edb199d4bd0

the original vite, written in python

refactor(make.py): break main() into html_gen()
Anirudh icyph0x@protonmail.com
Sun, 15 Apr 2018 13:31:37 +0530
commit

9b7ca8070dbe98b914544b321bf27edb199d4bd0

parent

df6983319126438b61c8fadbb9c07e5c86f55d95

1 files changed, 19 insertions(+), 14 deletions(-)

jump to
M make.pymake.py

@@ -21,13 +21,14 @@ # constants

PAGES_PATH = 'pages/' BUILD_PATH = 'build/' TEMPL_PATH = 'templates/' +TEMPL_FILE = TEMPL_PATH + config.template # jinja2 -def jinja_render(html_text, template_file): +def jinja_render(html_text, TEMPL_FILE): template_loader = jinja2.FileSystemLoader('./') env = jinja2.Environment(loader=template_loader) - template = env.get_template(template_file) + template = env.get_template(TEMPL_FILE) output = template.render(title=config.title, author=config.author, header=config.header,

@@ -39,28 +40,32 @@

def markdown_render(filename): html_text = markdown_path(PAGES_PATH + filename) return html_text + + +def html_gen(): + for page in os.listdir(PAGES_PATH): + html_text = markdown_render(page) + html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0] + if not os.path.exists(html_file): + os.mkdir(html_file) + output = jinja_render(html_text, TEMPL_FILE) + with open(os.path.join(html_file, 'index.html'), 'w') as f: + f.write(output) + print(run('Rendered %s.' % (page))) def main(): start = time.process_time() - template_file = TEMPL_PATH + config.template + TEMPL_FILE = TEMPL_PATH + config.template if not os.listdir(PAGES_PATH): print(info(italic('pages') + ' directory is empty. Nothing to build.')) sys.exit(1) else: try: - for page in os.listdir(PAGES_PATH): - html_text = markdown_render(page) - html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0] - if not os.path.exists(html_file): - os.mkdir(html_file) - output = jinja_render(html_text, template_file) - with open(os.path.join(html_file, 'index.html'), 'w') as f: - f.write(output) - print(run('Rendered %s.' % (page))) - print(info('Done in %0.5fs.' % (time.process_time() - start))) + html_gen() + print(info('Done in %0.5fs.' % (time.process_time() - start))) except jinja2.exceptions.TemplateNotFound: - print(bad('Error: specified template not found: %s' % (template_file))) + print(bad('Error: specified template not found: %s' % (TEMPL_FILE))) if __name__ == "__main__":