vite/vite.py (view raw)
1#!/usr/bin/env python3
2
3"""
4Vite - A simple and minimal static site generator.
5"""
6
7import sys
8import pathlib
9import os
10import jinja2
11import time
12import http.server
13import socketserver
14
15from markdown2 import markdown_path
16from huepy import *
17from distutils.dir_util import copy_tree
18from vite import vite
19
20
21# constants
22PAGES_PATH = 'pages/'
23BUILD_PATH = 'build/'
24TEMPL_PATH = 'templates/'
25TEMPL_FILE = ''
26PORT = 1911
27
28
29def import_config():
30 try:
31 sys.path.append(os.getcwd())
32 globals()['config'] = __import__('config')
33 global TEMPL_FILE
34 TEMPL_FILE = os.path.join(TEMPL_PATH, config.template)
35 except ImportError:
36 print(bad('Error: config.py not found.'))
37 print(que('Are you sure you\'re in a project directory?'))
38 sys.exit(1)
39
40
41def create_project(path):
42 try:
43 abs_path = pathlib.Path(path).resolve()
44 cur_path = pathlib.Path('.').resolve()
45 os.makedirs(os.path.join(path, 'build'))
46 os.mkdir(os.path.join(path, 'pages'))
47 os.mkdir(os.path.join(path, 'templates'))
48 os.mkdir(os.path.join(path, 'static'))
49 create_config(path)
50 create_template(path)
51 print(good('Created project directory at %s.' % (abs_path)))
52 except FileExistsError:
53 print(bad('Error: specified path exists.'))
54
55
56def create_config(path):
57 with open(os.path.join(path, 'config.py'), 'w') as f:
58 f.write("""# config.py - Vite's configuration script
59
60title = ''
61author = ''
62header = ''
63footer = ''
64template = 'index.html' # default is index.html
65 """)
66
67
68def create_template(path):
69 with open(os.path.join(path, 'templates', 'index.html'), 'w') as f:
70 f.write("""
71<!DOCTYPE html>
72<html>
73<header>
74 {{ header }}
75 <title>
76 {{ title }}
77 </title>
78</header>
79
80<body>
81 {{ body }}
82</body>
83
84<footer>
85 {{ footer }}
86 <p> {{ author }} </p>
87<footer>
88
89 """)
90
91# jinja2
92def jinja_render(html_text, TEMPL_FILE):
93 template_loader = jinja2.FileSystemLoader('./')
94 env = jinja2.Environment(loader=template_loader)
95 template = env.get_template(TEMPL_FILE)
96 output = template.render(title=config.title,
97 author=config.author,
98 header=config.header,
99 footer=config.footer,
100 body=html_text)
101 return output
102
103
104def markdown_render(filename):
105 html_text = markdown_path(PAGES_PATH + filename)
106 return html_text
107
108
109def html_gen():
110 for page in os.listdir(PAGES_PATH):
111 html_text = markdown_render(page)
112 html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0]
113 if not os.path.exists(html_file):
114 os.mkdir(html_file)
115 output = jinja_render(html_text, TEMPL_FILE)
116 with open(os.path.join(html_file, 'index.html'), 'w') as f:
117 f.write(output)
118 print(run('Rendered %s.' % (page)))
119
120
121def server():
122 handler = http.server.SimpleHTTPRequestHandler
123 os.chdir(os.path.join(os.getcwd(), BUILD_PATH))
124 try:
125 with socketserver.TCPServer(('', PORT), handler) as httpd:
126 print(run(f'Serving the {italic("build")} directory at http://localhost:{PORT}'))
127 print(white('Ctrl+C') + ' to stop.')
128 httpd.serve_forever()
129 except KeyboardInterrupt:
130 print(info('Stopping server.'))
131 httpd.socket.close()
132 sys.exit(1)
133
134def builder():
135 path = os.getcwd()
136 start = time.process_time()
137 if not os.listdir(os.path.join(path, PAGES_PATH)):
138 print(info(italic('pages') + ' directory is empty. Nothing to build.'))
139 sys.exit(1)
140 else:
141 try:
142 html_gen()
143 if not os.path.exists(os.path.join(path, BUILD_PATH, 'static')):
144 os.mkdir(os.path.join(path, BUILD_PATH, 'static'))
145 copy_tree('static', os.path.join(path, BUILD_PATH, 'static'))
146 print(good('Done in %0.5fs.' % (time.process_time() - start)))
147 except jinja2.exceptions.TemplateNotFound:
148 print(bad('Error: specified template not found: %s' % TEMPL_FILE))
149