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