all repos — py-vite @ 792d013be0aab7846a57284cb91a075d774b4b19

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
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
#!/usr/bin/env python3

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

import sys
import pathlib
import os
import jinja2
import time
import http.server
import socketserver

from markdown2 import markdown_path
from huepy import *
from distutils.dir_util import copy_tree
from vite import vite


# constants
PAGES_PATH = 'pages/'
BUILD_PATH = 'build/'
TEMPL_PATH = 'templates/'
TEMPL_FILE = ''
PORT = 1911


def import_config():
    try:
        sys.path.append(os.getcwd())
        globals()['config'] = __import__('config') 
        global TEMPL_FILE
        TEMPL_FILE = os.path.join(TEMPL_PATH, config.template)
    except ImportError:
        print(bad('Error: config.py not found.'))
        print(que('Are you sure you\'re in a project directory?'))
        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'))
        os.mkdir(os.path.join(path, 'static'))
        create_config(path)
        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(os.path.join(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>

                """)

# jinja2
def jinja_render(html_text, TEMPL_FILE):
    template_loader = jinja2.FileSystemLoader('./')
    env = jinja2.Environment(loader=template_loader)
    template = env.get_template(TEMPL_FILE)
    output = template.render(title=config.title,
                             author=config.author,
                             header=config.header,
                             footer=config.footer,
                             body=html_text)
    return output


def markdown_render(filename):
    html_text = markdown_path(PAGES_PATH + filename)
    return html_text


def html_gen():
    for page in os.listdir(PAGES_PATH):
        print(page)
        if page == '_index.md':
            index_html = markdown_render(page)
            output = jinja_render(index_html, TEMPL_FILE)
            with open(os.path.join(BUILD_PATH, 'index.html'), 'w') as f:
                f.write(output)
                print(run('Rendered index.html'))
        else:
            html_text = markdown_render(page)
            html_file = os.path.splitext(os.path.join(BUILD_PATH, page))[0]
            if not os.path.exists(html_file):
                os.mkdir(html_file)
            output = jinja_render(html_text, TEMPL_FILE)
            with open(os.path.join(html_file, 'index.html'), 'w') as f:
                f.write(output)
                print(run('Rendered %s.' % (page)))


def server():
    handler = http.server.SimpleHTTPRequestHandler
    os.chdir(os.path.join(os.getcwd(), BUILD_PATH))
    try:
        with socketserver.TCPServer(('', PORT), handler) as httpd:
            print(run(f'Serving the {italic("build")} directory at http://localhost:{PORT}'))
            print(white('Ctrl+C') + ' to stop.')
            httpd.serve_forever()
    except KeyboardInterrupt:
        print(info('Stopping server.'))
        httpd.socket.close()
        sys.exit(1)

def builder():
    path = os.getcwd()
    start = time.process_time()
    if not os.listdir(os.path.join(path, PAGES_PATH)):
        print(info(italic('pages') + ' directory is empty. Nothing to build.'))
        sys.exit(1)
    else:
        try:
            html_gen()
            if not os.path.exists(os.path.join(path, BUILD_PATH, 'static')):
                os.mkdir(os.path.join(path, BUILD_PATH, 'static'))
            copy_tree('static', os.path.join(path, BUILD_PATH, 'static'))
            print(good('Done in %0.5fs.' % (time.process_time() - start)))
        except jinja2.exceptions.TemplateNotFound:
            print(bad('Error: specified template not found: %s' % TEMPL_FILE))