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