all repos — py-vite @ 915a86e13b2259f60a1632fbf9e9d1ec8f40ef20

the original vite, written in python

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 pathlib
  9import os
 10import jinja2
 11import time
 12import http.server
 13import socketserver
 14
 15from markdown2 import markdown_path
 16from huepy import *
 17from distutils.dir_util import copy_tree
 18from vite import vite
 19
 20try:
 21    sys.path.append(os.getcwd())
 22    import config
 23except FileNotFoundError:
 24    print(bad('Error: config.py not found'))
 25
 26# constants
 27PAGES_PATH = 'pages/'
 28BUILD_PATH = 'build/'
 29TEMPL_PATH = 'templates/'
 30TEMPL_FILE = TEMPL_PATH + config.template
 31PORT = 1911
 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        os.mkdir(os.path.join(path, 'static'))
 42        create_config(path)
 43        os.symlink(os.path.join(cur_path, 'make.py'),
 44                   os.path.join(abs_path, 'make.py'))
 45        create_template(path)
 46        print(good('Created project directory at %s.' % (abs_path)))
 47    except FileExistsError:
 48        print(bad('Error: specified path exists.'))
 49
 50
 51def create_config(path):
 52    with open(os.path.join(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# jinja2
 87def jinja_render(html_text, TEMPL_FILE):
 88    template_loader = jinja2.FileSystemLoader('./')
 89    env = jinja2.Environment(loader=template_loader)
 90    template = env.get_template(TEMPL_FILE)
 91    output = template.render(title=config.title,
 92                             author=config.author,
 93                             header=config.header,
 94                             footer=config.footer,
 95                             body=html_text)
 96    return output
 97
 98
 99def markdown_render(filename):
100    html_text = markdown_path(PAGES_PATH + filename)
101    return html_text
102
103
104def html_gen():
105    for page in os.listdir(PAGES_PATH):
106        html_text = markdown_render(page)
107        html_file= os.path.splitext(os.path.join(BUILD_PATH, page))[0]
108        if not os.path.exists(html_file):
109            os.mkdir(html_file)
110        output = jinja_render(html_text, TEMPL_FILE)
111        with open(os.path.join(html_file, 'index.html'), 'w') as f:
112            f.write(output)
113            print(run('Rendered %s.' % (page)))
114
115
116def server():
117    handler = http.server.SimpleHTTPRequestHandler
118    os.chdir(os.path.join(os.getcwd(), BUILD_PATH))
119    try:
120        with socketserver.TCPServer(('', PORT), handler) as httpd:
121            print(run(f'Serving the {italic("build")} directory at http://localhost:{PORT}'))
122            print(white('Ctrl+C') + ' to stop.')
123            httpd.serve_forever()
124    except KeyboardInterrupt:
125        print(info('Stopping server.'))
126        httpd.socket.close()
127        sys.exit(1)
128
129def builder():
130    path = os.getcwd()
131    start = time.process_time()
132    if not os.listdir(os.path.join(path, PAGES_PATH)):
133        print(info(italic('pages') + ' directory is empty. Nothing to build.'))
134        sys.exit(1)
135    else:
136        try:
137            html_gen()
138            if not os.path.exists(os.path.join(path, BUILD_PATH, 'static')):
139                os.mkdir(os.path.join(path, BUILD_PATH, 'static'))
140            copy_tree('static', os.path.join(path, BUILD_PATH, 'static'))
141            print(good('Done in %0.5fs.' % (time.process_time() - start)))
142        except jinja2.exceptions.TemplateNotFound:
143            print(bad('Error: specified template not found: %s' % TEMPL_FILE))
144 
145
146#if __name__ == "__main__":
147#    usage = lightblue('vite.py') + ' new [PATH]' 
148#    desc = green('A simple and minimal static site generator.')
149#    parser = argparse.ArgumentParser(description=desc, usage=usage)
150#    parser.add_argument('new', nargs='*', help='Create new Vite project.')
151#    
152#    if len(sys.argv) == 1:
153#        parser.print_help()
154#        sys.exit(1)
155#    
156#    try:
157#        args = parser.parse_args()
158#        project_path = args.new[1]
159#    except IndexError:
160#        parser.print_help()
161#        sys.exit(1)
162#
163#    if args.new:
164#        create_project(project_path)
165