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