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