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 argparse
9import pathlib
10import os
11import importlib
12
13from hue import *
14
15parser = argparse.ArgumentParser(description="""
16 A simple and minimal static site generator.
17 """)
18parser.add_argument('action', choices=['new', 'build'])
19# TODO: add help for each action
20parser.add_argument('path', nargs='*')
21
22if len(sys.argv) == 1:
23 parser.print_help()
24 sys.exit(1)
25
26try:
27 args = parser.parse_args()
28 project_path = args.path[0]
29except IndexError:
30 parser.print_help()
31 sys.exit(1)
32
33
34def create_project(path):
35 try:
36 abs_path = pathlib.Path(path).resolve()
37 cur_path = pathlib.Path('.').resolve()
38 os.makedirs(os.path.join(path, 'build'))
39 os.mkdir(os.path.join(path, 'pages'))
40 os.mkdir(os.path.join(path, 'templates'))
41 create_config(path)
42 os.symlink(os.path.join(cur_path, 'make.py'),
43 os.path.join(abs_path, '.make.py'))
44 print(good('Created project directory at %s.' % (abs_path)))
45 except FileExistsError:
46 print(bad('Error: specified path exists.'))
47
48
49def create_config(path):
50 with open(path + '/config.py', 'w') as f:
51 f.write("""# config.py - Vite's configuration script
52
53title = ''
54author = ''
55header = ''
56footer = ''
57template = 'index.html' # default is index.html
58 """)
59
60
61def create_template(path):
62 with open(os.path.join(path, 'templates', 'index.html'), 'w') as f:
63 f.write("""
64<!DOCTYPE html>
65<html>
66<header>
67 {{ header }}
68 <title>
69 {{ title }}
70 </title>
71</header>
72
73<body>
74 {{ body }}
75</body>
76
77<footer>
78 {{ footer }}
79 <p> © {{ author }} </p>
80<footer>
81 """)
82
83
84def build_project(path):
85 try:
86 os.chdir(path)
87 importlib.import_module('.make')
88 except FileNotFoundError as e:
89 print(bad('Error: no such file or directory: %s' % (path)))
90
91
92def main():
93 if args.action == 'new':
94 create_project(project_path)
95 elif args.action == 'build':
96 build_project(project_path)
97
98
99if __name__ == "__main__":
100 main()