all repos — py-vite @ f45e3de024c6abb079ba4a537e0d03f86070d329

the original vite, written in python

vite/make.py (view raw)

 1#!/usr/bin/env python3
 2
 3from markdown2 import markdown_path
 4import os
 5import sys
 6import jinja2
 7import time
 8
 9# import config file
10try:
11    sys.path.append(os.getcwd())
12    import config
13except ModuleNotFoundError:
14    print('Error: config.py not found')
15
16# constants
17PAGES_PATH = 'pages/'
18BUILD_PATH = 'build/'
19TEMPL_PATH = 'templates'
20
21
22# jinja2
23def jinja_render(html_text, template_file):
24    template_loader = jinja2.FileSystemLoader('./')
25    env = jinja2.Environment(loader=template_loader)
26    template = env.get_template(template_file)
27    output = template.render(title=config.title,
28                             author=config.author,
29                             header=config.header,
30                             footer=config.footer,
31                             body=html_text)
32    return output
33
34
35def markdown_render(filename):
36    html_text = markdown_path(PAGES_PATH + filename)
37    return html_text
38
39
40def main():
41    start = time.process_time()
42    template_file = TEMPL_PATH + '/index.html'
43    for page in os.listdir(PAGES_PATH):
44        html_text = markdown_render(page)
45        html_path = os.path.splitext(os.path.join(BUILD_PATH, page))[0]
46        if not os.path.exists(html_path):
47            os.mkdir(html_path)
48        output = jinja_render(html_text, template_file)
49        with open(os.path.join(html_path, 'index.html'), 'w') as f:
50            f.write(output)
51            print('Rendered %s' % (page))
52    print('Done in %0.5fs' % (time.process_time() - start))
53        
54
55if __name__ == "__main__":
56    main()