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 create_template(path)
45 print(good('Created project directory at %s.' % (abs_path)))
46 except FileExistsError:
47 print(bad('Error: specified path exists.'))
48
49
50
51def create_config(path):
52 with open(path + '/config.py', 'w') as f:
53 f.write("""# config.py - Vite's configuration script
54
55title = ''
56author = ''
57header = ''
58footer = ''
59template = 'index.html' # default is index.html
60 """)
61
62
63def create_template(path):
64 with open(os.path.join(path, 'templates', 'index.html'), 'w') as f:
65 f.write("""
66<!DOCTYPE html>
67<html>
68<header>
69 {{ header }}
70 <title>
71 {{ title }}
72 </title>
73</header>
74
75<body>
76 {{ body }}
77</body>
78
79<footer>
80 {{ footer }}
81 <p> © {{ author }} </p>
82<footer>
83
84 """)
85
86
87def build_project(path):
88 try:
89 os.chdir(path)
90 importlib.import_module('.make')
91 except FileNotFoundError as e:
92 print(bad('Error: no such file or directory: %s' % (path)))
93
94
95def main():
96 if args.action == 'new':
97 create_project(project_path)
98 elif args.action == 'build':
99 build_project(project_path)
100
101
102if __name__ == "__main__":
103 main()