all repos — py-vite @ b3dc231750531c45f5b3398d90b62fa6b01a7bd1

the original vite, written in python

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
14usage = lightblue('vite.py') + ' new [PATH]' 
15desc = green('A simple and minimal static site generator.')
16parser = argparse.ArgumentParser(description=desc, usage=usage)
17parser.add_argument('new', nargs='*', help='Create new Vite project.')
18
19if len(sys.argv) == 1:
20    parser.print_help()
21    sys.exit(1)
22
23try:
24    args = parser.parse_args()
25    project_path = args.new[1]
26except IndexError:
27    parser.print_help()
28    sys.exit(1)
29
30
31def create_project(path):
32    try:
33        abs_path = pathlib.Path(path).resolve()
34        cur_path = pathlib.Path('.').resolve()
35        os.makedirs(os.path.join(path, 'build'))
36        os.mkdir(os.path.join(path, 'pages'))
37        os.mkdir(os.path.join(path, 'templates'))
38        os.mkdir(os.path.join(path, 'static'))
39        create_config(path)
40        os.symlink(os.path.join(cur_path, 'make.py'),
41                   os.path.join(abs_path, 'make.py'))
42        create_template(path)
43        print(good('Created project directory at %s.' % (abs_path)))
44    except FileExistsError:
45        print(bad('Error: specified path exists.'))
46
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
84
85def main():
86    if args.new:
87        create_project(project_path)
88
89if __name__ == "__main__":
90    main()