all repos — py-vite @ f2f9eb9e25dfaf46f23f5d5e9d622b58d5f68c6a

the original vite, written in python

vite/vite.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
#!/usr/bin/env python3

"""
Vite - A simple and minimal static site generator.
"""

import sys
import argparse
import pathlib
import os
import importlib

from hue import *

parser = argparse.ArgumentParser(description="""
        A simple and minimal static site generator.
        """)
parser.add_argument('action', choices=['new', 'build'])
# TODO: add help for each action
parser.add_argument('path', nargs='*')

if len(sys.argv) == 1:
    parser.print_help()
    sys.exit(1)

try:
    args = parser.parse_args()
    project_path = args.path[0]
except IndexError:
    parser.print_help()
    sys.exit(1)


def create_project(path):
    try:
        abs_path = pathlib.Path(path).resolve()
        cur_path = pathlib.Path('.').resolve()
        os.makedirs(os.path.join(path, 'build'))
        os.mkdir(os.path.join(path, 'pages'))
        os.mkdir(os.path.join(path, 'templates'))
        create_config(path)
        os.symlink(os.path.join(cur_path, 'make.py'),
                   os.path.join(abs_path, '.make.py'))
        create_template(path)
        print(good('Created project directory at %s.' % (abs_path)))
    except FileExistsError:
        print(bad('Error: specified path exists.'))



def create_config(path):
    with open(path + '/config.py', 'w') as f:
        f.write("""# config.py - Vite's configuration script

title = ''
author = ''
header = ''
footer = '' 
template = 'index.html'  # default is index.html
               """)


def create_template(path):
    with open(os.path.join(path, 'templates', 'index.html'), 'w') as f:
        f.write("""
<!DOCTYPE html>
<html>
<header>
	{{ header }}
	<title>
		{{ title }}	
	</title>
</header>

<body>
	{{ body }}
</body>

<footer>
	{{ footer }}
	<p> © {{ author }} </p>
<footer>

                """)


def build_project(path):
    try:
        os.chdir(path)
        importlib.import_module('.make')
    except FileNotFoundError as e:
        print(bad('Error: no such file or directory: %s' % (path)))


def main():
    if args.action == 'new':
        create_project(project_path)
    elif args.action == 'build':
        build_project(project_path)


if __name__ == "__main__":
    main()