vite/make.py (view raw)
1#!/usr/bin/env python3
2
3import os
4import sys
5import jinja2
6import time
7import argparse
8import http.server
9import socketserver
10
11from markdown2 import markdown_path
12from huepy import *
13from distutils.dir_util import copy_tree
14
15# import config file
16try:
17 sys.path.append(os.getcwd())
18 import config
19except ModuleNotFoundError:
20 print(bad('Error: config.py not found.'))
21 print(que('Are you sure you\'re in a project directory?'))
22 sys.exit(1)
23
24
25# constants
26PAGES_PATH = 'pages/'
27BUILD_PATH = 'build/'
28TEMPL_PATH = 'templates/'
29TEMPL_FILE = TEMPL_PATH + config.template
30PORT = 1911
31
32
33# argument parsing
34def parser():
35 desc = green('A simple and minimal static site generator.')
36 usage = lightblue('vite') + ' [build | serve]'
37 serve_help = 'Serve pages from the ' + italic('build') + ' directory.'
38 build_help = 'Build pages from the ' + italic('pages') + ' directory.'
39 parser = argparse.ArgumentParser(description=desc, usage=usage)
40 parser.add_argument('serve', nargs='*', help=serve_help)
41 parser.add_argument('build', nargs='*', help=build_help)
42 return parser
43
44
45# jinja2
46def jinja_render(html_text, TEMPL_FILE):
47 template_loader = jinja2.FileSystemLoader('./')
48 env = jinja2.Environment(loader=template_loader)
49 template = env.get_template(TEMPL_FILE)
50 output = template.render(title=config.title,
51 author=config.author,
52 header=config.header,
53 footer=config.footer,
54 body=html_text)
55 return output
56
57
58def markdown_render(filename):
59 html_text = markdown_path(PAGES_PATH + filename)
60 return html_text
61
62
63def html_gen():
64 for page in os.listdir(PAGES_PATH):
65 html_text = markdown_render(page)
66 html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0]
67 if not os.path.exists(html_file):
68 os.mkdir(html_file)
69 output = jinja_render(html_text, TEMPL_FILE)
70 with open(os.path.join(html_file, 'index.html'), 'w') as f:
71 f.write(output)
72 print(run('Rendered %s.' % (page)))
73
74
75def server():
76 handler = http.server.SimpleHTTPRequestHandler
77 os.chdir(BUILD_PATH)
78 try:
79 with socketserver.TCPServer(('', PORT), handler) as httpd:
80 print(run(f'Serving the {italic("build")} directory at http://localhost:{PORT}'))
81 print(white('Ctrl+C') + ' to stop.')
82 httpd.serve_forever()
83 except KeyboardInterrupt:
84 print(info('Stopping server.'))
85 httpd.socket.close()
86 sys.exit(1)
87
88
89def main():
90 args = parser().parse_args()
91
92 if args.serve:
93 server()
94 elif args.build:
95 start = time.process_time()
96 TEMPL_FILE = TEMPL_PATH + config.template
97 if not os.listdir(PAGES_PATH):
98 print(info(italic('pages') + ' directory is empty. Nothing to build.'))
99 sys.exit(1)
100 else:
101 try:
102 html_gen()
103 if not os.path.exists(os.path.join(BUILD_PATH, 'static')):
104 os.mkdir(os.path.join(BUILD_PATH, 'static'))
105 copy_tree('static', os.path.join(BUILD_PATH, 'static'))
106 print(good('Done in %0.5fs.' % (time.process_time() - start)))
107 except jinja2.exceptions.TemplateNotFound:
108 print(bad('Error: specified template not found: %s' % TEMPL_FILE))
109 else:
110 parser().print_help()
111 sys.exit(1)
112
113
114if __name__ == "__main__":
115 main()